2022-07-27 11:34:26 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2023-05-10 18:31:09 +03:00
|
|
|
"net/url"
|
2023-05-14 13:57:18 +03:00
|
|
|
"time"
|
2022-07-27 11:34:26 +03:00
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2023-05-10 18:31:09 +03:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2022-07-27 11:34:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var uptime = time.Now().Unix()
|
|
|
|
|
|
|
|
|
|
func mapRooms() {
|
|
|
|
|
|
|
|
|
|
data := make(map[string]*Info)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case m := <-rooms.Add:
|
2023-05-10 18:31:09 +03:00
|
|
|
data[m.room] = &Info{Id: m.Id, Auth: m.Auth, Proxy: m.Proxy, Rid: m.Rid, Start: m.Start, Last: m.Last, Online: m.Online, Income: m.Income, Dons: m.Dons, Tips: m.Tips, ch: m.ch}
|
2022-07-27 11:34:26 +03:00
|
|
|
|
|
|
|
|
case s := <-rooms.Json:
|
|
|
|
|
j, err := json.Marshal(data)
|
|
|
|
|
if err == nil {
|
|
|
|
|
s = string(j)
|
|
|
|
|
}
|
|
|
|
|
rooms.Json <- s
|
|
|
|
|
|
|
|
|
|
case <-rooms.Count:
|
|
|
|
|
rooms.Count <- len(data)
|
|
|
|
|
|
|
|
|
|
case key := <-rooms.Del:
|
|
|
|
|
delete(data, key)
|
|
|
|
|
|
|
|
|
|
case room := <-rooms.Check:
|
|
|
|
|
if _, ok := data[room]; !ok {
|
|
|
|
|
room = ""
|
|
|
|
|
}
|
|
|
|
|
rooms.Check <- room
|
|
|
|
|
|
|
|
|
|
case room := <-rooms.Stop:
|
|
|
|
|
if _, ok := data[room]; ok {
|
|
|
|
|
close(data[room].ch)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func announceCount() {
|
|
|
|
|
for {
|
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
|
rooms.Count <- 0
|
|
|
|
|
l := <-rooms.Count
|
|
|
|
|
msg, err := json.Marshal(struct {
|
2023-05-14 13:57:18 +03:00
|
|
|
Chanel string `json:"chanel"`
|
|
|
|
|
Count int `json:"count"`
|
2023-05-11 13:52:11 +03:00
|
|
|
}{
|
2023-05-14 13:57:18 +03:00
|
|
|
Chanel: "chaturbate",
|
|
|
|
|
Count: l,
|
2023-05-11 13:52:11 +03:00
|
|
|
})
|
2022-07-27 11:34:26 +03:00
|
|
|
if err == nil {
|
2023-05-11 13:52:11 +03:00
|
|
|
socketServer <- msg
|
2022-07-27 11:34:26 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-21 12:17:54 +03:00
|
|
|
func reconnectRoom(workerData Info) {
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
fmt.Println("reconnect:", workerData.room, workerData.Id, workerData.Auth, workerData.Proxy)
|
|
|
|
|
workerData.Last = time.Now().Unix()
|
|
|
|
|
startRoom(workerData)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 11:34:26 +03:00
|
|
|
func xWorker(workerData Info, u url.URL) {
|
|
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
fmt.Println("Start", workerData.room, "room_id", workerData.Id, "auth", workerData.Auth, "proxy", workerData.Proxy)
|
2022-07-27 11:34:26 +03:00
|
|
|
|
|
|
|
|
rooms.Add <- workerData
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
rooms.Del <- workerData.room
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
Dialer := *websocket.DefaultDialer
|
|
|
|
|
|
|
|
|
|
if _, ok := conf.Proxy[workerData.Proxy]; ok {
|
|
|
|
|
Dialer = websocket.Dialer{
|
|
|
|
|
Proxy: http.ProxyURL(&url.URL{
|
|
|
|
|
Scheme: "http", // or "https" depending on your proxy
|
|
|
|
|
Host: conf.Proxy[workerData.Proxy],
|
|
|
|
|
Path: "/",
|
|
|
|
|
}),
|
|
|
|
|
HandshakeTimeout: 45 * time.Second, // https://pkg.go.dev/github.com/gorilla/websocket
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c, _, err := Dialer.Dial(u.String(), nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
|
|
dons := make(map[string]struct{})
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
initMessages := []string{
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:tip_alert:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:purchase:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:fanclub:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:message:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"global:push_service","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room_anon:presence:` + workerData.Id + `:0","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:quality_update:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:notice:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:enter_leave:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:password_protected:` + workerData.Id + `:13","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:mod_promoted:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:mod_revoked:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:status:` + workerData.Id + `:13","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:title_change:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:silence:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:kick:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:update:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
`{"action":10,"flags":327680,"channel":"room:settings:` + workerData.Id + `","params":{}}`,
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
c.SetReadDeadline(time.Now().Add(1 * time.Minute))
|
|
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
slog <- saveLog{Rid: workerData.Rid, Now: time.Now().Unix(), Mes: string(message)}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
input := struct {
|
2023-05-14 13:57:18 +03:00
|
|
|
Action int `json:"action"`
|
|
|
|
|
Key string `json:"connectionkey"`
|
2023-05-10 18:31:09 +03:00
|
|
|
Error jsoniter.RawMessage `json:"error"`
|
2023-05-14 13:57:18 +03:00
|
|
|
Channel string `json:"channel"`
|
2023-05-10 18:31:09 +03:00
|
|
|
Messages jsoniter.RawMessage `json:"messages"`
|
|
|
|
|
}{}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if err := json.Unmarshal(message, &input); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if err = c.WriteMessage(websocket.TextMessage, []byte(`{"action":17, "auth":{"accessToken":"`+workerData.Auth+`"}}`)); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
_, message, err = c.ReadMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
for _, im := range initMessages {
|
|
|
|
|
if err = c.WriteMessage(websocket.TextMessage, []byte(im)); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-21 16:08:29 +03:00
|
|
|
ticker := time.NewTicker(60 * 60 * 8 * time.Second)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
leave := time.NewTicker(60 * 60 * 8 * time.Second)
|
|
|
|
|
defer leave.Stop()
|
|
|
|
|
|
2022-07-27 11:34:26 +03:00
|
|
|
for {
|
|
|
|
|
|
|
|
|
|
select {
|
2023-05-21 16:08:29 +03:00
|
|
|
case <-ticker.C:
|
|
|
|
|
fmt.Println("too_long exit:", workerData.room)
|
|
|
|
|
return
|
|
|
|
|
case <-leave.C:
|
|
|
|
|
fmt.Println("leave exit:", workerData.room)
|
|
|
|
|
return
|
2022-07-27 11:34:26 +03:00
|
|
|
case <-workerData.ch:
|
|
|
|
|
fmt.Println("Exit room:", workerData.room)
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.SetReadDeadline(time.Now().Add(30 * time.Minute))
|
|
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
2023-05-21 12:17:54 +03:00
|
|
|
if workerData.Income > 1 {
|
|
|
|
|
go reconnectRoom(workerData)
|
|
|
|
|
}
|
2022-07-27 11:34:26 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
|
|
|
|
m := string(message)
|
|
|
|
|
slog <- saveLog{Rid: workerData.Rid, Now: now, Mes: m}
|
|
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
//fmt.Println(m)
|
2022-07-27 11:34:26 +03:00
|
|
|
|
|
|
|
|
if now > workerData.Last+60*20 {
|
|
|
|
|
fmt.Println("no_mes exit:", workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if err := json.Unmarshal(message, &input); err != nil {
|
2022-07-27 11:34:26 +03:00
|
|
|
fmt.Println(err.Error(), workerData.room)
|
2023-05-10 18:31:09 +03:00
|
|
|
break
|
2022-07-27 11:34:26 +03:00
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if input.Action == 15 {
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2022-07-27 11:34:26 +03:00
|
|
|
workerData.Last = now
|
|
|
|
|
rooms.Add <- workerData
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-21 16:08:29 +03:00
|
|
|
if input.Channel == "room:enter_leave:"+workerData.Id {
|
|
|
|
|
type Message struct {
|
|
|
|
|
Messages []struct {
|
|
|
|
|
Data string `json:"data"`
|
|
|
|
|
} `json:"messages,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
type Data struct {
|
|
|
|
|
User struct {
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
Gender string `json:"gender"`
|
|
|
|
|
IsBroadcast bool `json:"is_broadcast"`
|
|
|
|
|
} `json:"user"`
|
|
|
|
|
Action string `json:"action"`
|
|
|
|
|
}
|
|
|
|
|
topmsg := &Message{}
|
|
|
|
|
if err := json.Unmarshal(input.Messages, &topmsg); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, msg := range topmsg.Messages {
|
|
|
|
|
inmsg := &Data{}
|
|
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(msg.Data), inmsg); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// inmsg.User.Username, inmsg.User.Gender, inmsg.User.IsBroadcast, inmsg.Action
|
|
|
|
|
|
|
|
|
|
if inmsg.User.Username == workerData.room && inmsg.User.IsBroadcast && inmsg.Action == "leave" {
|
|
|
|
|
fmt.Println("leave, start ticker", workerData.room)
|
|
|
|
|
leave.Reset(60 * 5 * time.Second)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inmsg.User.Username == workerData.room && inmsg.User.IsBroadcast && inmsg.Action == "enter" {
|
|
|
|
|
fmt.Println("enter, stop ticker", workerData.room)
|
|
|
|
|
leave.Reset(60 * 60 * 8 * time.Second)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if input.Channel == "room:tip_alert:"+workerData.Id {
|
|
|
|
|
tips := []struct {
|
|
|
|
|
Data string `json:"data"`
|
|
|
|
|
}{}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if err := json.Unmarshal(input.Messages, &tips); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
continue
|
2022-07-27 11:34:26 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
donate := struct {
|
|
|
|
|
Name string `json:"to_username"`
|
|
|
|
|
From string `json:"from_username"`
|
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
|
}{}
|
|
|
|
|
|
|
|
|
|
for _, tip := range tips {
|
|
|
|
|
if err := json.Unmarshal([]byte(tip.Data), &donate); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if donate.Amount < 1 {
|
|
|
|
|
fmt.Println("empty amount", workerData.room)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
if len(donate.From) < 4 {
|
|
|
|
|
donate.From = "anon_tips"
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
workerData.Tips++
|
|
|
|
|
if _, ok := dons[donate.From]; !ok {
|
|
|
|
|
dons[donate.From] = struct{}{}
|
|
|
|
|
workerData.Dons++
|
|
|
|
|
}
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-10 18:31:09 +03:00
|
|
|
save <- saveData{Room: workerData.room, From: donate.From, Rid: workerData.Rid, Amount: donate.Amount, Now: now}
|
|
|
|
|
workerData.Income += donate.Amount
|
|
|
|
|
rooms.Add <- workerData
|
2023-05-14 13:57:18 +03:00
|
|
|
|
2023-05-21 16:08:29 +03:00
|
|
|
fmt.Println(donate.From, "send", donate.Amount, "tokens to", workerData.room)
|
2022-07-27 11:34:26 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|