This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
websocket.go
227 lines (191 loc) · 5.9 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package aternos_api
import (
"context"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"github.com/sleeyax/aternos-api/internal/tlsadapter"
httpx "github.com/useflyent/fhttp"
"log"
"net"
"net/http"
"time"
)
const (
websocketUrl = "wss://aternos.org/hermes/"
)
type Websocket struct {
// Whether we are connected.
isConnected bool
// receiverDone indicates whether the receiver goroutine is done processing incoming messages.
// It's considered done when the channel is closed.
receiverDone chan interface{}
// Received messages channel.
Message chan WebsocketMessage
// The current websocket connection.
conn *websocket.Conn
}
func (w *Websocket) init() {
w.receiverDone = make(chan interface{})
w.Message = make(chan WebsocketMessage)
go w.startReceiver()
}
// IsConnected returns whether we are connected.
func (w *Websocket) IsConnected() bool {
return w.isConnected
}
// Close closes the websocket connection.
func (w *Websocket) Close() error {
// Try to tell the server that we want to close the connection.
if err := w.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")); err != nil {
return fmt.Errorf("failed send close message: %e", err)
}
w.isConnected = false
select {
case <-w.receiverDone:
log.Println("Receiver Channel Closed! Exiting....")
return nil
case <-time.After(time.Duration(3) * time.Second):
log.Println("Timeout in closing receiving channel. Exiting by force....")
return w.conn.Close()
}
}
// Send sends a message over the websocket connection.
func (w *Websocket) Send(message WebsocketMessage) error {
return w.conn.WriteJSON(message)
}
// StartConsoleLogStream starts fetching the server start logs (console).
// This function should only be called once the server has been started over HTTP.
func (w *Websocket) StartConsoleLogStream() error {
return w.Send(WebsocketMessage{
Stream: "console",
Type: "start",
})
}
// StopConsoleLogStream starts fetching the server stop logs (console).
// This function should only be called once the server has been stopped over HTTP.
func (w *Websocket) StopConsoleLogStream() error {
return w.Send(WebsocketMessage{
Stream: "console",
Type: "stop",
})
}
// StartHeapInfoStream starts fetching information about the server heap.
// See https://www.javatpoint.com/java-heap for more information about heaps.
func (w *Websocket) StartHeapInfoStream() error {
return w.Send(WebsocketMessage{
Stream: "heap",
Type: "start",
})
}
// StopHeapInfoStream stops fetching information about the server heap.
// See https://www.javatpoint.com/java-heap for more information about heaps.
func (w *Websocket) StopHeapInfoStream() error {
return w.Send(WebsocketMessage{
Stream: "heap",
Type: "stop",
})
}
// StartTickStream starts streaming the current server tick count.
// See https://minecraft.fandom.com/wiki/Tick for more information about ticks.
func (w *Websocket) StartTickStream() error {
return w.Send(WebsocketMessage{
Stream: "tick",
Type: "start",
})
}
// StopTickStream stops streaming the current server tick count.
// See https://minecraft.fandom.com/wiki/Tick for more information about ticks.
func (w *Websocket) StopTickStream() error {
return w.Send(WebsocketMessage{
Stream: "tick",
Type: "stop",
})
}
// SendHeartBeat sends a single keep-alive request.
// The server doesn't respond to this request, but a heartbeat should be regularly sent to keep the connection alive.
func (w *Websocket) SendHeartBeat() error {
return w.Send(WebsocketMessage{
Type: "\xE2\x9D\xA4", // hearth emoji ❤
})
}
// SendHearthBeats keeps sending keep-alive requests at a specified interval.
// If no interval is specified, a default is used.
// It's recommended to use the default value unless you have a good reason not to do so.
//
// See Websocket.SendHeartBeat for more information.
func (w *Websocket) SendHearthBeats(ctx context.Context, duration ...time.Duration) {
d := time.Millisecond * 49000
if len(duration) > 0 {
d = duration[0]
}
ticker := time.NewTicker(d)
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
w.SendHeartBeat()
}
}
}
// startReceiver starts listening for incoming messages.
func (w *Websocket) startReceiver() {
defer close(w.receiverDone)
defer close(w.Message)
for {
msgType, rawMsg, err := w.conn.ReadMessage()
if err != nil {
closeErr, ok := err.(*websocket.CloseError)
if !ok || closeErr.Code != websocket.CloseNormalClosure {
log.Println("Unknown error in receiver: ", err)
}
w.isConnected = !ok
return
}
switch msgType {
case websocket.TextMessage:
var msg WebsocketMessage
if err = json.Unmarshal(rawMsg, &msg); err != nil {
log.Println("Receiver failed to parse msg: ", err)
break
}
if msg.Message != "" {
msg.MessageBytes = []byte(msg.Message)
}
w.Message <- msg
case websocket.CloseMessage:
w.isConnected = false
return
default:
log.Printf("Unknown message received: %s\n", rawMsg)
}
}
}
// ConnectWebSocket connects to the Aternos websockets server.
func (api *Api) ConnectWebSocket() (*Websocket, error) {
headers := api.client.Options.Headers.Clone()
headers.Set("accept", "*/*")
headers.Set("cache-control", "no-cache")
headers.Set("host", "aternos.org")
headers.Set("origin", api.client.Options.PrefixURL)
headers.Del(httpx.HeaderOrderKey)
dialer := websocket.Dialer{
Proxy: http.ProxyURL(api.Options.Proxy),
HandshakeTimeout: 30 * time.Second,
EnableCompression: true,
ProxyTLSConnection: func(ctx context.Context, proxyConn net.Conn) (net.Conn, error) {
adapter := (api.client.Options.Adapter).(*tlsadapter.TLSAdapter)
return adapter.ConnectTLSContext(ctx, proxyConn)
},
Jar: api.client.Options.CookieJar,
}
conn, _, err := dialer.Dial(websocketUrl, headers)
if err != nil {
return nil, err
}
wss := &Websocket{conn: conn, isConnected: true}
wss.init()
return wss, nil
}