-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
327 lines (289 loc) · 7.3 KB
/
main.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"bufio"
"flag"
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"os/exec"
"strings"
"sync"
"github.com/gorilla/websocket"
)
var (
namespace = "chesshook-intermediary"
version = "1"
addr = flag.String("addr", "localhost:8080", "http service address")
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return r.Header.Get("Origin") == "https://www.chess.com"
},
}
passKey = randomPassKey()
needsAuthForWrite = flag.Bool("authwrite", true, "whether to require authentication for writing to the engine")
needsAuthForRead = flag.Bool("authread", false, "whether to require authentication for reading from the engine")
localhostBypass = flag.Bool("localhost", true, "whether to bypass authentication for localhost")
enginePath = flag.String("engine", "./stockfish", "path to the engine binary")
uciArgsFlag = flag.String("uciargs", "", "arguments to pass to the engine on startup, split with \";\"")
engineInputChannel = make(chan string)
engineOutputChannel = make(chan string)
engineName = ""
)
func randomPassKey() string {
length := 10
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
passKey := make([]rune, length)
for i := range passKey {
passKey[i] = chars[rand.Intn(len(chars))]
}
return string(passKey)
}
func spawnEngine() {
engineArgs := strings.Split(*enginePath, " ")
engine := exec.Command(engineArgs[0], engineArgs[1:]...)
stdin, err := engine.StdinPipe()
if err != nil {
panic(err)
}
stdout, err := engine.StdoutPipe()
if err != nil {
panic(err)
}
err = engine.Start()
if err != nil {
panic(err)
}
go func() {
for {
uciInput := <-engineInputChannel
_, err = fmt.Fprintf(stdin, "%s\n", uciInput)
if err != nil {
panic(err)
}
}
}()
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
// Send the output to a channel for further processing
output := scanner.Text()
fmt.Println("engine: " + output)
if strings.HasPrefix(output, "id name ") {
engineName = output[8:]
}
if strings.HasPrefix(output, "bestmove") || strings.HasPrefix(output, "info") {
engineOutputChannel <- output
}
}
}()
engineInputChannel <- "uci"
engineInputChannel <- "isready"
uciArgs := strings.Split(*uciArgsFlag, ";")
for _, arg := range uciArgs {
engineInputChannel <- arg
}
}
var isEngineLocked = false
type User struct {
connection *websocket.Conn
mu sync.Mutex
isSubscribed bool
hasLock bool
isAuthenticated bool
incorrectAttempts int
}
var users = make(map[*websocket.Conn]*User)
func writePump() {
for {
message := <-engineOutputChannel
for _, user := range users {
if user.isSubscribed {
user.send(message)
}
}
}
}
func (user *User) send(message string) bool {
user.mu.Lock()
defer user.mu.Unlock()
err := user.connection.WriteMessage(websocket.TextMessage, []byte(message))
if err != nil {
log.Println("write:", err)
return false
}
return true
}
func wsHandler(writer http.ResponseWriter, request *http.Request) {
connection, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
connection.SetReadLimit(64 * 1024) // 64 KB max message size
log.Print("New ws opened: ", connection.RemoteAddr())
user := User{
connection: connection,
isSubscribed: false,
hasLock: false,
isAuthenticated: false,
incorrectAttempts: 0,
}
if *localhostBypass && strings.HasPrefix(connection.RemoteAddr().String(), "127.0.0.1:") {
user.isAuthenticated = true
}
users[connection] = &user
defer func() {
log.Print("ws closed: ", connection.RemoteAddr())
if user.hasLock {
isEngineLocked = false
}
connection.Close()
delete(users, connection)
}()
for {
_, message, err := connection.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
parts := strings.Split(string(message), " ")
if parts[0] == "whoareyou" {
if !user.send(fmt.Sprintf("iam %sv%s", namespace, version)) {
break
}
} else if parts[0] == "whatengine" {
if !user.isAuthenticated && *needsAuthForRead {
if !user.send("autherr") {
break
}
continue
}
if !user.send(fmt.Sprintf("engine %s", engineName)) {
break
}
} else if parts[0] == "auth" {
// Check if the passkey is correct, and if the user has not exceeded the number of incorrect attempts
if parts[1] == passKey && user.incorrectAttempts < 3 {
if !user.send("authok") {
break
}
user.isAuthenticated = true
} else {
if !user.send("autherr") {
break
}
user.incorrectAttempts++
}
} else if parts[0] == "sub" {
if !user.isAuthenticated && *needsAuthForRead {
if !user.send("autherr") {
break
}
continue
}
if !user.isSubscribed {
user.isSubscribed = true
if !user.send("subok") {
break
}
} else {
if !user.send("suberr") {
break
}
}
} else if parts[0] == "unsub" {
if !user.isAuthenticated && *needsAuthForRead {
if !user.send("autherr") {
break
}
continue
}
if user.isSubscribed {
user.isSubscribed = false
if !user.send("unsubok") {
break
}
} else {
if !user.send("unsuberr") {
break
}
}
} else if parts[0] == "lock" {
if !user.isAuthenticated && *needsAuthForWrite {
if !user.send("autherr") {
break
}
continue
}
if !user.hasLock && !isEngineLocked {
user.hasLock = true
isEngineLocked = true
if !user.send("lockok") {
break
}
} else {
if !user.send("lockerr") {
break
}
}
} else if parts[0] == "unlock" {
if !user.isAuthenticated && *needsAuthForWrite {
if !user.send("autherr") {
break
}
continue
}
if user.hasLock {
user.hasLock = false
isEngineLocked = false
if !user.send("unlockok") {
break
}
} else {
if !user.send("unlockerr") {
break
}
}
} else {
if !user.isAuthenticated && *needsAuthForWrite {
if !user.send("autherr") {
break
}
continue
}
engineInputChannel <- string(message)
}
}
}
func home(w http.ResponseWriter, r *http.Request) {
homeTemplate.Execute(w, *addr)
}
func main() {
flag.Parse()
log.SetFlags(0)
http.HandleFunc("/ws", wsHandler)
http.HandleFunc("/", home)
go spawnEngine()
go writePump()
log.Print("Server started, passkey: ", passKey)
log.Print("Server is requesting authentication for read operations: ", *needsAuthForRead)
log.Print("Server is requesting authentication for write operations: ", *needsAuthForWrite)
log.Print("Server is bypassing authentication for localhost connections: ", *localhostBypass)
panic(http.ListenAndServe(*addr, nil))
}
var homeTemplate = template.Must(template.New("").Parse(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chesshook External Engine server</title>
</head>
<body>
<p>This server is running a websocket server for the Chesshook userscript external engine protocol. You can find the chesshook userscript <a href="https://github.com/0mlml/chesshook">here.</a> You can find the source for this server <a href="https://github.com/0mlml/chesshook-intermediary">here.</a></p>
<p>To use this server with the userscript, set the engine url to <code>ws://{{.}}/ws</code></p>
</body>
</html>
`))