2022-08-12 01:12:24 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2022-08-15 13:12:00 +03:00
|
|
|
"bytes"
|
2022-08-12 01:12:24 +03:00
|
|
|
"fmt"
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
2023-05-14 13:55:57 +03:00
|
|
|
"os/exec"
|
2022-08-14 13:32:35 +03:00
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
2022-08-15 13:12:00 +03:00
|
|
|
"strings"
|
|
|
|
|
"time"
|
2022-08-12 01:12:24 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var uptime = time.Now().Unix()
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
type Amount struct {
|
|
|
|
|
value int64
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
func (a Amount) Value() int64 {
|
|
|
|
|
return a.value
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
func (a *Amount) UnmarshalJSON(b []byte) error {
|
|
|
|
|
str := string(b)
|
|
|
|
|
s := 0
|
|
|
|
|
e := len(b)
|
|
|
|
|
if len(str) > 2 && str[0] == '"' && str[len(b)-1] == '"' {
|
|
|
|
|
s = 1
|
|
|
|
|
e = len(b) - 1
|
|
|
|
|
}
|
|
|
|
|
var err error
|
|
|
|
|
a.value, err = strconv.ParseInt(str[s:e], 10, 64)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ServerResponse struct {
|
|
|
|
|
SubscriptionKey string `json:"subscriptionKey,omitempty"`
|
|
|
|
|
|
|
|
|
|
Params struct {
|
|
|
|
|
Model struct {
|
|
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
|
} `json:"model,omitempty"`
|
|
|
|
|
User struct {
|
|
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
|
} `json:"user,omitempty"`
|
|
|
|
|
ClientId string `json:"clientId,omitempty"`
|
|
|
|
|
Message struct {
|
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
|
Userdata struct {
|
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
|
} `json:"userdata,omitempty"`
|
|
|
|
|
Details struct {
|
|
|
|
|
Amount Amount `json:"amount,omitempty"`
|
|
|
|
|
LovenseDetails struct {
|
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
|
Detail struct {
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
|
Amount Amount `json:"amount,omitempty"`
|
|
|
|
|
} `json:"detail,omitempty"`
|
|
|
|
|
} `json:"lovenseDetails"`
|
|
|
|
|
} `json:"details,omitempty"`
|
|
|
|
|
} `json:"message,omitempty"`
|
|
|
|
|
} `json:"params,omitempty"`
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapRooms() {
|
|
|
|
|
|
|
|
|
|
data := make(map[string]*Info)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case m := <-rooms.Add:
|
2022-08-15 13:12:00 +03:00
|
|
|
data[m.room] = &Info{Id: m.Id, Rid: m.Rid, Server: m.Server, Proxy: m.Proxy, Start: m.Start, Last: m.Last, Online: m.Online, Income: m.Income, Dons: m.Dons, Tips: m.Tips, ch: m.ch}
|
2022-08-12 01:12:24 +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:55:57 +03:00
|
|
|
Chanel string `json:"chanel"`
|
|
|
|
|
Count int `json:"count"`
|
2023-05-11 14:30:47 +03:00
|
|
|
}{
|
2023-05-14 13:55:57 +03:00
|
|
|
Chanel: "stripchat",
|
|
|
|
|
Count: l,
|
2023-05-11 14:30:47 +03:00
|
|
|
})
|
2022-08-12 01:12:24 +03:00
|
|
|
if err == nil {
|
2023-05-11 14:30:47 +03:00
|
|
|
socketServer <- msg
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-15 12:27:38 +03:00
|
|
|
func getToken(room string) string {
|
2023-05-10 18:33:45 +03:00
|
|
|
|
2023-05-14 13:55:57 +03:00
|
|
|
cmd := exec.Command("/home/stat/python/test.py", "https://stripchat.com/api/front/v2/config/data?requestPath="+room)
|
|
|
|
|
stdout, err := cmd.Output()
|
2023-05-10 18:33:45 +03:00
|
|
|
|
2023-05-14 13:55:57 +03:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
|
return "cant exec py"
|
|
|
|
|
}
|
2023-05-10 18:33:45 +03:00
|
|
|
|
2023-05-14 13:55:57 +03:00
|
|
|
//fmt.Println(string(stdout))
|
2023-05-10 18:33:45 +03:00
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
re := regexp.MustCompile(`"websocketUrl":"*(.*?)\s*"`)
|
2023-05-10 18:33:45 +03:00
|
|
|
m := re.FindSubmatch(stdout)
|
2022-08-14 13:32:35 +03:00
|
|
|
if len(m) != 2 {
|
2022-08-15 12:27:38 +03:00
|
|
|
return "cant get ws"
|
2022-08-14 13:32:35 +03:00
|
|
|
}
|
2022-08-15 12:27:38 +03:00
|
|
|
return string(bytes.ReplaceAll(m[1], []byte(`\u002F`), []byte(`/`)))
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-15 21:06:39 +03:00
|
|
|
func reconnectRoom(workerData Info) {
|
|
|
|
|
n := randInt(10, 30)
|
|
|
|
|
fmt.Printf("Sleeping %d seconds...\n", n)
|
|
|
|
|
time.Sleep(time.Duration(n) * time.Second)
|
|
|
|
|
fmt.Println("reconnect:", workerData.room, workerData.Id, workerData.Proxy)
|
|
|
|
|
workerData.Last = time.Now().Unix()
|
|
|
|
|
startRoom(workerData)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
func xWorker(workerData Info) {
|
2022-08-15 12:27:38 +03:00
|
|
|
fmt.Println("Start", workerData.room, "id", workerData.Id, "proxy", workerData.Proxy)
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
rooms.Add <- workerData
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
rooms.Del <- workerData.room
|
|
|
|
|
}()
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-15 21:06:39 +03:00
|
|
|
if workerData.Server == "" {
|
|
|
|
|
workerData.Server = getToken(workerData.room)
|
|
|
|
|
}
|
2023-05-14 13:55:57 +03:00
|
|
|
|
2022-08-15 21:06:39 +03:00
|
|
|
if len(workerData.Server) < 50 {
|
|
|
|
|
fmt.Println(workerData.Server, workerData.room)
|
|
|
|
|
return
|
2022-08-15 12:27:38 +03:00
|
|
|
}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-15 21:06:39 +03:00
|
|
|
u, err := url.Parse(workerData.Server)
|
2022-08-15 12:27:38 +03:00
|
|
|
if err != nil {
|
2022-08-15 21:06:39 +03:00
|
|
|
fmt.Println(err, workerData.room)
|
2022-08-12 01:12:24 +03:00
|
|
|
return
|
|
|
|
|
}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
Dialer := *websocket.DefaultDialer
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
proxyMap := make(map[string]string)
|
|
|
|
|
proxyMap["us"] = "5.161.128.20:3128"
|
|
|
|
|
proxyMap["fi"] = "65.21.180.188:3128"
|
|
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
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 {
|
2022-08-15 12:27:38 +03:00
|
|
|
fmt.Println(err.Error(), u.String(), workerData.room)
|
2022-08-12 01:12:24 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
defer c.Close()
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
dons := make(map[string]struct{})
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
for {
|
|
|
|
|
c.SetReadDeadline(time.Now().Add(30 * time.Minute))
|
|
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
|
if err != nil {
|
2022-08-14 13:32:35 +03:00
|
|
|
fmt.Println(err.Error(), workerData.room)
|
2022-08-15 21:06:39 +03:00
|
|
|
if workerData.Income > 1 && websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
2022-08-15 21:45:28 +03:00
|
|
|
go reconnectRoom(workerData)
|
2022-08-15 21:06:39 +03:00
|
|
|
}
|
2022-08-12 01:12:24 +03:00
|
|
|
return
|
|
|
|
|
}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
now := time.Now().Unix()
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
slog <- saveLog{workerData.Rid, time.Now().Unix(), string(message)}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
m := &ServerResponse{}
|
|
|
|
|
|
|
|
|
|
if err = json.Unmarshal(message, m); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-12 01:12:24 +03:00
|
|
|
workerData.Last = now
|
|
|
|
|
rooms.Add <- workerData
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
if m.SubscriptionKey == "connected" {
|
2022-08-15 12:27:38 +03:00
|
|
|
id := workerData.Id
|
2022-08-14 13:32:35 +03:00
|
|
|
messages := [][]byte{}
|
2022-08-15 22:32:17 +03:00
|
|
|
mTime := strconv.FormatInt(time.Now().UnixMilli(), 10)
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-lotteryChanged","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/lotteryChanged"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-userBanned:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/userBanned:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-goalChanged:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/goalChanged:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-modelStatusChanged:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/modelStatusChanged:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-broadcastSettingsChanged:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/broadcastSettingsChanged:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-tipMenuUpdated:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/tipMenuUpdated:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-topicChanged:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/topicChanged:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-userUpdated:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/userUpdated:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-interactiveToyStatusChanged:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/interactiveToyStatusChanged:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-groupShow:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/groupShow:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-deleteChatMessages:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/deleteChatMessages:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-tipLeaderboardSettingsUpdated:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/tipLeaderboardSettingsUpdated:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-modelAppUpdated:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/modelAppUpdated:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-newKing:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/newKing:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-privateMessageSettingsChanged:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/privateMessageSettingsChanged:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-newChatMessage:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/newChatMessage:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-fanClubUpdated:`+id+`","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/fanClubUpdated:`+id+`"}`))
|
|
|
|
|
messages = append(messages, []byte(`{"id":"`+mTime+`-sub-viewServerChanged:hls-07","method":"PUT","url":"/front/clients/`+m.Params.ClientId+`/subscriptions/viewServerChanged:hls-07"}`))
|
2022-08-14 13:32:35 +03:00
|
|
|
for _, msg := range messages {
|
|
|
|
|
if err = c.WriteMessage(websocket.TextMessage, msg); err != nil {
|
|
|
|
|
fmt.Println(err.Error(), workerData.room)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
messages = nil
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
if strings.Contains(m.SubscriptionKey, "userUpdated") && m.Params.User.Status == "off" {
|
2023-05-10 18:33:45 +03:00
|
|
|
fmt.Println("userUpdated exiting", workerData.room)
|
2022-08-14 13:32:35 +03:00
|
|
|
return
|
2022-08-15 13:12:00 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
if strings.Contains(m.SubscriptionKey, "modelStatusChanged") && m.Params.Model.Status == "off" {
|
2023-05-10 18:33:45 +03:00
|
|
|
fmt.Println("modelStatusChanged exiting", workerData.room)
|
2022-08-12 01:12:24 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
if m.Params.Message.Type == "tip" {
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
if len(m.Params.Message.Userdata.Username) < 3 {
|
2023-05-10 18:33:45 +03:00
|
|
|
m.Params.Message.Userdata.Username = "anon_tips"
|
2022-08-14 13:32:35 +03:00
|
|
|
}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
fmt.Println(m.Params.Message.Userdata.Username, "send", m.Params.Message.Details.Amount.Value(), "tokens")
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
workerData.Tips++
|
|
|
|
|
if _, ok := dons[m.Params.Message.Userdata.Username]; !ok {
|
|
|
|
|
dons[m.Params.Message.Userdata.Username] = struct{}{}
|
|
|
|
|
workerData.Dons++
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
save <- saveData{workerData.room, strings.ToLower(m.Params.Message.Userdata.Username), workerData.Rid, m.Params.Message.Details.Amount.Value(), now}
|
2022-08-15 13:12:00 +03:00
|
|
|
|
2022-08-14 13:32:35 +03:00
|
|
|
workerData.Income += m.Params.Message.Details.Amount.Value()
|
|
|
|
|
rooms.Add <- workerData
|
2022-08-12 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|