animated-sniffle/app/cmd.go

117 lines
2.4 KiB
Go
Raw Normal View History

2022-07-27 11:28:07 +03:00
package main
import (
"fmt"
"net/http"
"net/url"
2022-08-07 14:18:21 +03:00
"os"
2022-07-27 11:28:07 +03:00
"runtime"
"strings"
2022-08-07 14:18:21 +03:00
"time"
2022-07-27 11:28:07 +03:00
)
2022-08-07 14:18:21 +03:00
var memInfo runtime.MemStats
2022-07-27 11:28:07 +03:00
type Info struct {
2022-08-07 14:18:21 +03:00
ch chan struct{}
2022-07-27 11:28:07 +03:00
room string
Server string `json:"server"`
2023-06-09 14:58:41 +03:00
Params string `json:"params"`
2022-07-27 11:28:07 +03:00
Proxy string `json:"proxy"`
2022-08-07 14:18:21 +03:00
Online string `json:"online"`
Rid int64 `json:"rid"`
2022-07-27 11:28:07 +03:00
Start int64 `json:"start"`
Last int64 `json:"last"`
Income int64 `json:"income"`
2022-08-07 14:18:21 +03:00
Dons int64 `json:"dons"`
Tips int64 `json:"tips"`
2022-07-27 11:28:07 +03:00
}
2022-08-07 14:18:21 +03:00
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)
2022-07-27 11:28:07 +03:00
}
}
func listHandler(w http.ResponseWriter, _ *http.Request) {
2022-08-07 14:18:21 +03:00
dat, err := os.ReadFile(conf.Conn["start"])
if err != nil {
fmt.Println(err)
return
}
fmt.Fprint(w, string(dat))
2022-07-27 11:28:07 +03:00
}
func debugHandler(w http.ResponseWriter, _ *http.Request) {
runtime.ReadMemStats(&memInfo)
2022-08-07 14:18:21 +03:00
j, err := json.Marshal(struct {
Goroutines int
Uptime int64
Alloc uint64
HeapSys uint64
}{
Goroutines: runtime.NumGoroutine(),
Alloc: memInfo.Alloc,
HeapSys: memInfo.HeapSys,
Uptime: uptime,
})
2022-07-27 11:28:07 +03:00
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()
2023-06-09 14:58:41 +03:00
if len(params["room"]) > 0 && len(params["server"]) > 0 && len(params["proxy"]) > 0 && len(params["params"]) > 0 {
2022-08-07 14:18:21 +03:00
now := time.Now().Unix()
workerData := Info{
room: params["room"][0],
Server: params["server"][0],
2023-06-09 14:58:41 +03:00
Params: params["params"][0],
2022-08-07 14:18:21 +03:00
Proxy: params["proxy"][0],
Online: "0",
Start: now,
Last: now,
Rid: 0,
Income: 0,
Dons: 0,
Tips: 0,
2022-07-27 11:28:07 +03:00
}
2022-08-07 14:18:21 +03:00
startRoom(workerData)
}
if len(params["exit"]) > 0 {
rooms.Stop <- strings.Join(params["exit"], "")
}
fmt.Fprint(w, string("ok"))
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
func startRoom(workerData Info) {
rooms.Check <- workerData.room
testRoom := <-rooms.Check
if testRoom == workerData.room {
fmt.Println("Already track:", workerData.room)
return
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
rid, ok := getRoomInfo(workerData.room)
if !ok {
fmt.Println("No room in MySQL:", workerData.room)
return
}
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
workerData.Rid = rid
workerData.ch = make(chan struct{})
2022-07-27 11:28:07 +03:00
2022-08-07 14:18:21 +03:00
go xWorker(workerData, url.URL{Scheme: "wss", Host: workerData.Server + ".bcccdn.com", Path: "/websocket"})
2022-07-27 11:28:07 +03:00
}