2023-05-11 00:43:02 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type enterChanel struct {
|
|
|
|
|
Conn *websocket.Conn
|
|
|
|
|
Chanel string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type wsMessage struct {
|
|
|
|
|
Message []byte
|
|
|
|
|
Decode bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
|
if r.Header.Get("origin") == "https://statbate.com" {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
wsClients = make(map[*websocket.Conn]string)
|
|
|
|
|
|
|
|
|
|
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
|
|
|
|
|
|
|
ws = struct {
|
2023-06-09 14:57:43 +03:00
|
|
|
//Count chan int
|
2023-05-11 00:43:02 +03:00
|
|
|
Add chan *websocket.Conn
|
|
|
|
|
Del chan *websocket.Conn
|
|
|
|
|
Send chan wsMessage
|
|
|
|
|
Enter chan enterChanel
|
|
|
|
|
}{
|
2023-06-09 14:57:43 +03:00
|
|
|
//Count: make(chan int, 100),
|
2023-05-11 00:43:02 +03:00
|
|
|
Add: make(chan *websocket.Conn, 100),
|
|
|
|
|
Del: make(chan *websocket.Conn, 100),
|
|
|
|
|
Send: make(chan wsMessage, 100),
|
|
|
|
|
Enter: make(chan enterChanel, 100),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func broadcast() {
|
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case conn := <-ws.Add:
|
|
|
|
|
wsClients[conn] = ""
|
|
|
|
|
|
|
|
|
|
case conn := <-ws.Del:
|
|
|
|
|
delete(wsClients, conn)
|
|
|
|
|
|
2023-06-09 14:57:43 +03:00
|
|
|
//case <-ws.Count:
|
|
|
|
|
// ws.Count <- len(wsClients)
|
2023-05-11 00:43:02 +03:00
|
|
|
|
|
|
|
|
case r := <-ws.Enter:
|
|
|
|
|
wsClients[r.Conn] = r.Chanel
|
|
|
|
|
|
|
|
|
|
case r := <-ws.Send:
|
|
|
|
|
sendMessage(r.Message, r.Decode)
|
|
|
|
|
|
|
|
|
|
case <-ticker.C:
|
2023-06-09 14:57:43 +03:00
|
|
|
fmt.Println("WebSocket:", len(wsClients))
|
2023-05-11 00:43:02 +03:00
|
|
|
sendMessage([]byte("ping"), false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendMessage(message []byte, decode bool) {
|
|
|
|
|
chanel := ""
|
|
|
|
|
if decode {
|
2023-05-11 13:42:01 +03:00
|
|
|
input := struct {
|
|
|
|
|
Chanel string `json:"chanel"`
|
|
|
|
|
}{}
|
2023-05-11 00:43:02 +03:00
|
|
|
if err := json.Unmarshal(message, &input); err != nil {
|
|
|
|
|
fmt.Println("json error: ", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-11 13:42:01 +03:00
|
|
|
if input.Chanel != "" {
|
|
|
|
|
chanel = input.Chanel
|
2023-05-11 00:43:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 14:57:43 +03:00
|
|
|
//fmt.Println(string(message), decode, chanel)
|
2023-05-11 00:43:02 +03:00
|
|
|
|
|
|
|
|
for conn, ch := range wsClients {
|
|
|
|
|
if decode && ch != chanel {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err := conn.WriteMessage(1, message); err != nil {
|
|
|
|
|
conn.Close()
|
|
|
|
|
delete(wsClients, conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go readWS(conn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readWS(conn *websocket.Conn) {
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
|
|
ws.Add <- conn
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
ws.Del <- conn
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
chanels := map[string]bool{
|
|
|
|
|
"chaturbate": true,
|
|
|
|
|
"bongacams": true,
|
|
|
|
|
"stripchat": true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("readWS", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := string(message)
|
|
|
|
|
|
|
|
|
|
if len(input) < 32 && chanels[input] {
|
|
|
|
|
ws.Enter <- enterChanel{Conn: conn, Chanel: input}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|