animated-sniffle/app/worker.go

287 lines
6.5 KiB
Go
Raw Normal View History

2022-07-27 11:28:07 +03:00
package main
import (
"fmt"
"github.com/gorilla/websocket"
jsoniter "github.com/json-iterator/go"
"net/http"
"net/url"
"strings"
"time"
2023-06-09 14:58:41 +03:00
"encoding/base64"
2022-07-27 11:28:07 +03:00
)
var uptime = time.Now().Unix()
type AuthResponse struct {
Status string `json:"status"`
LocalData struct {
DataKey string `json:"dataKey"`
} `json:"localData"`
UserData struct {
Username string `json:"username"`
DisplayName string `json:"displayName"`
Location string `json:"location"`
Chathost string `json:"chathost"`
IsRu bool `json:"isRu"`
} `json:"userData"`
}
type ServerResponse struct {
TS int64 `json:"ts"`
Type string `json:"type"`
Body jsoniter.RawMessage `json:"body"`
}
type DonateResponse struct {
F struct {
Username string `json:"username"`
} `json:"f"`
A int64 `json:"a"`
}
func mapRooms() {
data := make(map[string]*Info)
for {
select {
case m := <-rooms.Add:
2022-08-15 13:13:21 +03:00
data[m.room] = &Info{Server: m.Server, 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:28:07 +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)
2022-08-07 14:18:21 +03:00
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)
}
2022-07-27 11:28:07 +03:00
}
}
}
func announceCount() {
for {
time.Sleep(30 * time.Second)
rooms.Count <- 0
l := <-rooms.Count
2022-08-07 14:18:21 +03:00
msg, err := json.Marshal(struct {
2023-05-14 13:53:05 +03:00
Chanel string `json:"chanel"`
Count int `json:"count"`
2023-05-11 14:40:18 +03:00
}{
Chanel: "bongacams",
Count: l,
})
2022-07-27 11:28:07 +03:00
if err == nil {
2023-05-14 13:53:05 +03:00
socketServer <- msg
2022-07-27 11:28:07 +03:00
}
}
}
2023-06-09 14:58:41 +03:00
func reconnectRoom(workerData Info) {
time.Sleep(5 * time.Second)
fmt.Println("reconnect:", workerData.room, workerData.Proxy)
startRoom(workerData)
2022-07-27 11:28:07 +03:00
}
2022-08-07 14:18:21 +03:00
func xWorker(workerData Info, u url.URL) {
fmt.Println("Start", workerData.room, "server", workerData.Server, "proxy", workerData.Proxy)
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
rooms.Add <- workerData
defer func() {
rooms.Del <- workerData.room
}()
2023-06-09 14:58:41 +03:00
b64, err := base64.StdEncoding.DecodeString(workerData.Params)
if err != nil {
fmt.Println(err, workerData.room)
return
}
v := struct {
Chathost string `json:"chathost"`
Username string `json:"username"`
DisplayName string `json:"displayName"`
Location string `json:"location"`
IsRu bool `json:"isRu"`
DataKey string `json:"dataKey"`
}{}
if err := json.Unmarshal(b64, &v); err != nil {
fmt.Println("exit: no amf parms", workerData.room, err)
2022-07-27 11:28:07 +03:00
return
}
Dialer := *websocket.DefaultDialer
2022-08-07 14:18:21 +03:00
if _, ok := conf.Proxy[workerData.Proxy]; ok {
2022-07-27 11:28:07 +03:00
Dialer = websocket.Dialer{
Proxy: http.ProxyURL(&url.URL{
Scheme: "http", // or "https" depending on your proxy
2022-08-07 14:18:21 +03:00
Host: conf.Proxy[workerData.Proxy],
2022-07-27 11:28:07 +03:00
Path: "/",
}),
HandshakeTimeout: 45 * time.Second, // https://pkg.go.dev/github.com/gorilla/websocket
}
}
c, _, err := Dialer.Dial(u.String(), nil)
if err != nil {
2022-08-07 14:18:21 +03:00
fmt.Println(err.Error(), workerData.room)
2022-07-27 11:28:07 +03:00
return
}
2022-08-07 14:18:21 +03:00
defer c.Close()
2022-07-27 11:28:07 +03:00
c.SetReadDeadline(time.Now().Add(60 * time.Second))
2023-06-09 14:58:41 +03:00
if err = c.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"id":%d,"name":"joinRoom","args":["%s",{"username":"%s","displayName":"%s","location":"%s","chathost":"%s","isRu":%t,"isPerformer":false,"hasStream":false,"isLogged":false,"isPayable":false,"showType":"public"},"%s"]}`, 1, v.Chathost, v.Username, v.DisplayName, v.Location, v.Chathost, v.IsRu, v.DataKey))); err != nil {
2022-07-27 11:28:07 +03:00
fmt.Println(err.Error())
return
}
_, message, err := c.ReadMessage()
if err != nil {
2022-08-07 14:18:21 +03:00
fmt.Println(err.Error(), workerData.room)
2022-07-27 11:28:07 +03:00
return
}
2022-08-07 14:18:21 +03:00
slog <- saveLog{workerData.Rid, time.Now().Unix(), string(message)}
2022-07-27 11:28:07 +03:00
if string(message) == `{"id":1,"result":{"audioAvailable":false,"freeShow":false},"error":null}` {
2022-08-07 14:18:21 +03:00
fmt.Println("room offline, exit", workerData.room)
2022-07-27 11:28:07 +03:00
return
}
if err = c.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"id":%d,"name":"ChatModule.connect","args":["public-chat"]}`, 2))); err != nil {
2022-08-07 14:18:21 +03:00
fmt.Println(err.Error(), workerData.room)
2022-07-27 11:28:07 +03:00
return
}
_, message, err = c.ReadMessage()
if err != nil {
2022-08-07 14:18:21 +03:00
fmt.Println(err.Error(), workerData.room)
2022-07-27 11:28:07 +03:00
return
}
2022-08-07 14:18:21 +03:00
slog <- saveLog{workerData.Rid, time.Now().Unix(), string(message)}
2022-07-27 11:28:07 +03:00
2022-08-07 14:26:31 +03:00
quit := make(chan struct{})
2022-07-27 11:28:07 +03:00
pid := 3
defer func() {
2022-08-07 14:26:31 +03:00
close(quit)
2022-07-27 11:28:07 +03:00
}()
go func() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-quit:
return
case <-ticker.C:
if err = c.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"id":%d,"name":"ping"}`, pid))); err != nil {
2022-08-07 14:18:21 +03:00
fmt.Println(err.Error(), workerData.room)
rooms.Stop <- workerData.room
2022-07-27 11:28:07 +03:00
return
}
pid++
break
}
}
}()
2022-08-07 14:26:31 +03:00
dons := make(map[string]struct{})
2023-06-09 14:58:41 +03:00
timeout := time.NewTicker(60 * 60 * 8 * time.Second)
defer timeout.Stop()
var income int64
income = 0
2022-08-07 14:26:31 +03:00
2022-07-27 11:28:07 +03:00
for {
select {
2023-06-09 14:58:41 +03:00
case <-timeout.C:
fmt.Println("too_long exit:", workerData.room)
return
2022-08-07 14:18:21 +03:00
case <-workerData.ch:
fmt.Println("Exit room:", workerData.room)
2022-07-27 11:28:07 +03:00
return
default:
2022-08-07 14:18:21 +03:00
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
c.SetReadDeadline(time.Now().Add(30 * time.Minute))
_, message, err := c.ReadMessage()
if err != nil {
2023-06-09 14:58:41 +03:00
if income > 1 && websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
go reconnectRoom(workerData)
}
2022-08-07 14:18:21 +03:00
return
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
now := time.Now().Unix()
slog <- saveLog{workerData.Rid, now, string(message)}
2023-06-09 14:58:41 +03:00
if now > workerData.Last+60*60 {
fmt.Println("no_tips exit:", workerData.room)
return
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
m := &ServerResponse{}
if err = json.Unmarshal(message, m); err != nil {
fmt.Println(err.Error(), workerData.room)
continue
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
if m.Type == "ServerMessageEvent:PERFORMER_STATUS_CHANGE" && string(m.Body) == `"offline"` {
fmt.Println(m.Type, workerData.room)
return
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
if m.Type == "ServerMessageEvent:ROOM_CLOSE" {
fmt.Println(m.Type, workerData.room)
return
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
if m.Type == "ServerMessageEvent:INCOMING_TIP" {
d := &DonateResponse{}
if err = json.Unmarshal(m.Body, d); err == nil {
2022-07-27 11:28:07 +03:00
2022-08-07 14:26:31 +03:00
workerData.Tips++
if _, ok := dons[d.F.Username]; !ok {
dons[d.F.Username] = struct{}{}
workerData.Dons++
}
2022-08-15 13:13:21 +03:00
save <- saveData{workerData.room, strings.ToLower(d.F.Username), workerData.Rid, d.A, now}
2022-08-07 14:18:21 +03:00
2023-06-09 14:58:41 +03:00
income += d.A
workerData.Last = now
2022-08-07 14:18:21 +03:00
workerData.Income += d.A
rooms.Add <- workerData
2023-06-09 14:58:41 +03:00
fmt.Println(d.F.Username, "send", d.A, "tokens to", workerData.room)
2022-07-27 11:28:07 +03:00
}
}
}
}