/
cmd.go
113 lines (102 loc) · 2.19 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"net/http"
"os"
"runtime"
"strings"
"time"
)
var memInfo runtime.MemStats
type Info struct {
ch chan struct{}
room string
Key string `json:"key"`
Proxy string `json:"proxy"`
Online string `json:"online"`
Rid int64 `json:"rid"`
Start int64 `json:"start"`
Last int64 `json:"last"`
Income int64 `json:"income"`
Dons int64 `json:"dons"`
Tips int64 `json:"tips"`
}
func updateFileRooms() string {
for {
rooms.Json <- ""
s := <-rooms.Json
err := os.WriteFile(conf.Conn["start"], []byte(s), 0644)
if err != nil {
fmt.Println(err)
}
time.Sleep(10 * time.Second)
}
}
func listHandler(w http.ResponseWriter, _ *http.Request) {
dat, err := os.ReadFile(conf.Conn["start"])
if err != nil {
fmt.Println(err)
return
}
fmt.Fprint(w, string(dat))
}
func debugHandler(w http.ResponseWriter, _ *http.Request) {
runtime.ReadMemStats(&memInfo)
j, err := json.Marshal(struct {
Goroutines int
Uptime int64
Alloc uint64
HeapSys uint64
}{
Goroutines: runtime.NumGoroutine(),
Alloc: memInfo.Alloc,
HeapSys: memInfo.HeapSys,
Uptime: uptime,
})
if err == nil {
fmt.Fprint(w, string(j))
}
}
func cmdHandler(w http.ResponseWriter, r *http.Request) {
if !conf.List[r.Header.Get("X-REAL-IP")] {
fmt.Fprint(w, "403")
return
}
params := r.URL.Query()
if len(params["room"]) > 0 && len(params["key"]) > 0 && len(params["proxy"]) > 0 {
now := time.Now().Unix()
workerData := Info{
room: params["room"][0],
Key: params["key"][0],
Proxy: params["proxy"][0],
Online: "0",
Start: now,
Last: now,
Rid: 0,
Income: 0,
Dons: 0,
Tips: 0,
}
startRoom(workerData)
}
if len(params["exit"]) > 0 {
rooms.Stop <- strings.Join(params["exit"], "")
}
fmt.Fprint(w, string("ok"))
}
func startRoom(workerData Info) {
rooms.Check <- workerData.room
testRoom := <-rooms.Check
if testRoom == workerData.room {
fmt.Println("Already track:", workerData.room)
return
}
rid, ok := getRoomInfo(workerData.room)
if !ok {
fmt.Println("No room in MySQL:", workerData.room)
return
}
workerData.Rid = rid
workerData.ch = make(chan struct{})
go xWorker(workerData)
}