-
Notifications
You must be signed in to change notification settings - Fork 2
/
websocket.go
206 lines (167 loc) · 4.56 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package raiden
import (
"bytes"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/fasthttp/websocket"
"github.com/valyala/fasthttp"
)
const (
maxReconnectAttempts = 5
pingPeriod = 30 * time.Second
)
var (
closeWsOnce sync.Once
)
func WebSocketHandler(ctx *fasthttp.RequestCtx, u *url.URL) {
proxyWebSocket(ctx, u)
}
func proxyWebSocket(ctx *fasthttp.RequestCtx, u *url.URL) {
upgrader := websocket.FastHTTPUpgrader{
ReadBufferSize: 2048,
WriteBufferSize: 2048,
HandshakeTimeout: 10,
CheckOrigin: func(ctx *fasthttp.RequestCtx) bool { return true },
}
// Upgrade HTTP connection to WebSocket
err := upgrader.Upgrade(ctx, func(connClient *websocket.Conn) {
defer connClient.Close()
scheme := "ws"
if u.Scheme == "https" {
scheme = "wss"
}
targetURL := url.URL{
Scheme: scheme,
Host: u.Host,
Path: "realtime/v1/websocket",
RawQuery: "apikey=" + getConfig().ServiceKey,
}
reconnectAttempts := 0
for {
// Connect to the target WebSocket server
log.Println("Connecting to Server URL", targetURL.String())
connServer, _, err := websocket.DefaultDialer.Dial(targetURL.String(), nil)
if err != nil {
reconnectAttempts++
if reconnectAttempts > maxReconnectAttempts {
log.Printf("Exceeded maximum reconnect attempts (%d). Exiting.\n", maxReconnectAttempts)
return
}
log.Printf("Failed to connect to target WebSocket server: %v\n", err)
time.Sleep(5 * time.Second) // Wait before attempting reconnection
continue
}
// Reset reconnectAttempts counter on successful connection
reconnectAttempts = 0
// Handle ping-pong messages
go pingPongHandler(connClient, connServer)
// Handle WebSocket communication
done := make(chan struct{})
var wg sync.WaitGroup
wg.Add(2)
// Proxy messages from client to server
go proxyMessages(connClient, connServer, done, &wg)
// Proxy messages from server to client
go proxyMessages(connServer, connClient, done, &wg)
wg.Wait()
// Wait until either side closes the connection
select {
case <-done:
// Client closed the connection
log.Println("Client closed WebSocket connection, closing server connection...")
connServer.Close()
return
}
}
})
if err != nil {
log.Println("WebSocket upgrade error:", err)
ctx.Error("WebSocket upgrade error", fasthttp.StatusInternalServerError)
}
}
func proxyMessages(src, dest *websocket.Conn, done chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
for {
mt, msg, err := src.ReadMessage()
if err != nil {
log.Printf("Error reading message: %v\n", err)
break
}
log.Printf("Received message: %s\n", string(msg))
err = dest.WriteMessage(mt, msg)
if err != nil {
log.Printf("Error writing message: %v\n", err)
break
}
log.Printf("Send message: %s", string(msg))
}
// Close the 'done' channel exactly once to signal completion
closeOnce(done)
}
func pingPongHandler(src, dest *websocket.Conn) {
ticker := time.NewTicker(pingPeriod)
defer ticker.Stop()
heartbeat := `{"topic":"phoenix","event":"heartbeat","payload":{},"ref":""}}`
for {
select {
case <-ticker.C:
err := src.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
if err != nil {
log.Printf("Error sending ping to client: %v\n", err)
return
}
err = dest.WriteControl(websocket.PingMessage, []byte(heartbeat), time.Now().Add(time.Second))
if err != nil {
log.Printf("Error sending ping to server: %v\n", err)
return
}
}
}
}
func getConfig() *Config {
currentDir, err := os.Getwd()
if err != nil {
log.Println(err)
return nil
}
configFilePath := strings.Join([]string{currentDir, "app.yaml"}, string(os.PathSeparator))
config, err := LoadConfig(&configFilePath)
if err != nil {
log.Println(err)
return nil
}
return config
}
func closeOnce(ch chan struct{}) {
closeWsOnce.Do(func() {
close(ch)
})
}
func RealtimeBroadcastHandler(ctx *fasthttp.RequestCtx, u *url.URL) {
supabaseUrl := url.URL{
Scheme: u.Scheme,
Host: u.Host,
Path: "realtime/v1/api/broadcast",
}
client := &http.Client{}
req, err := http.NewRequest("POST", supabaseUrl.String(), bytes.NewBuffer(ctx.PostBody()))
if err != nil {
log.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Apikey", string(ctx.Request.Header.Peek("Apikey")))
if len(ctx.Request.Header.Peek("Authorization")) > 0 {
req.Header.Add("Authorization", string(ctx.Request.Header.Peek("Authorization")))
}
_, err = client.Do(req)
if err != nil {
log.Println(err)
return
}
}