This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket.go
133 lines (101 loc) · 3.02 KB
/
websocket.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"codeberg.org/Yonle/bostr2/pingpong"
"codeberg.org/Yonle/bostr2/relayHandler"
)
func Accept_Websocket(w http.ResponseWriter, r *http.Request, ip string, ua string) {
defer ConnPerIPRateLimit_OnDisconnect(ip)
var AcceptOptions = &websocket.AcceptOptions{
InsecureSkipVerify: true,
CompressionMode: websocket.CompressionContextTakeover,
}
isApple := (strings.Contains(ua, "CFNetwork") || strings.Contains(ua, "Safari")) && !strings.Contains(ua, "Chrome") && !strings.Contains(ua, "Firefox")
// compression is fucked on apple & nobody knows why.
if isApple {
AcceptOptions.CompressionMode = websocket.CompressionDisabled
}
conn, err := websocket.Accept(w, r, AcceptOptions)
if err != nil {
return
}
defer conn.CloseNow()
log.Printf("%s connected (%s)", ip, ua)
conn.SetReadLimit(-1)
// we are not expecting normal users would remain connected for more than 30 minutes.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
go pingpong.Stare(ctx, cancel, conn)
var relaySession = relayHandler.NewSession(ctx)
var s = Session{
ClientIP: ip,
ClientREQ: make(MessageChan),
ClientCLOSE: make(MessageChan),
ClientEVENT: make(MessageChan),
events: make(SessionEvents),
pendingEOSE: make(SessionEOSEs),
subscriptions: make(SessionSubs),
destroyed: make(chan struct{}),
relay: &relaySession,
conn: conn,
ctx: ctx,
}
s.StartListening()
defer log.Printf("%s disconnect (%s)", ip, ua)
listenForMessages(ctx, conn, &s)
}
func listenForMessages(ctx context.Context, conn *websocket.Conn, s *Session) {
listener:
for {
_, msg, err := conn.Read(ctx)
if err != nil {
log.Printf("%s: %v", s.ClientIP, err)
break listener
}
var data []json.RawMessage
if err := json.Unmarshal(msg, &data); err != nil {
// doesn't looks right
wsjson.Write(ctx, conn, [2]string{"NOTICE", "error: your json doesn't looks right."})
continue listener
}
if len(data) < 1 {
wsjson.Write(ctx, conn, [2]string{"NOTICE", "error: does not looks like there's something in your message."})
continue listener
}
var cmd string
if err := json.Unmarshal(data[0], &cmd); err != nil {
wsjson.Write(ctx, conn, [2]string{"NOTICE", "error: please check your command."})
continue listener
}
switch cmd {
case "REQ":
if len(data) < 3 {
wsjson.Write(ctx, conn, [2]string{"NOTICE", "error: invalid REQ"})
continue listener
}
s.ClientREQ <- data
case "CLOSE":
if len(data) < 2 {
wsjson.Write(ctx, conn, [2]string{"NOTICE", "error: invalid CLOSE"})
continue listener
}
s.ClientCLOSE <- data
case "EVENT":
if len(data) < 2 {
wsjson.Write(ctx, conn, [2]string{"NOTICE", "error: invalid EVENT"})
continue listener
}
s.ClientEVENT <- data
default:
wsjson.Write(ctx, conn, [2]string{"NOTICE", fmt.Sprintf("error: unknown command %s", cmd)})
}
}
}