@@ -42,12 +42,14 @@ var (
42
42
func main () {
43
43
flag .Parse ()
44
44
45
+ blobKey := genKey ()
46
+
45
47
actions := make (chan BotAction )
46
48
47
49
if * bot {
48
50
var callbacks []CallbackHandler
49
51
for _ , g := range games {
50
- callbacks = append (callbacks , handleGame (g , * baseURL ))
52
+ callbacks = append (callbacks , handleGame (g , * baseURL , blobKey ))
51
53
}
52
54
for _ , g := range multigames {
53
55
callbacks = append (callbacks , handleMultiGame (g , * baseURL ))
@@ -56,18 +58,18 @@ func main() {
56
58
}
57
59
58
60
log .Printf ("listening on %s...\n " , * listen )
59
- log .Fatal (http .ListenAndServe (* listen , mux (actions , * static )))
61
+ log .Fatal (http .ListenAndServe (* listen , mux (actions , * static , blobKey )))
60
62
}
61
63
62
- func mux (actions chan <- BotAction , static string ) * httprouter.Router {
64
+ func mux (actions chan <- BotAction , static string , blobKey [ 32 ] byte ) * httprouter.Router {
63
65
r := httprouter .New ()
64
66
if static != "" {
65
67
r .GET ("/" , func (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
66
68
http .ServeFile (w , r , static + "index.html" )
67
69
})
68
70
r .ServeFiles ("/static/*filepath" , http .Dir (static ))
69
71
}
70
- r .GET ("/api/win" , winHandler (actions ))
72
+ r .GET ("/api/win" , winHandler (actions , blobKey ))
71
73
r .GET ("/api/join" , multiHandler (newRooms ()))
72
74
return r
73
75
}
@@ -177,11 +179,7 @@ type Blob struct {
177
179
InlineMessageID string `json:"iid,omitempty"`
178
180
}
179
181
180
- var (
181
- key = [32 ]byte {0x49 , 0xf3 , 0xae , 0x3f , 0x82 , 0x26 , 0x72 , 0x6d , 0xf4 , 0x5c , 0xf4 , 0x3c , 0x36 , 0x66 , 0x12 , 0xdf , 0x8a , 0xc1 , 0x2b , 0xe9 , 0x94 , 0x87 , 0x92 , 0x47 , 0x8e , 0xfa , 0xcf , 0xb9 , 0xcc , 0x77 , 0xf7 , 0x3d }
182
- )
183
-
184
- func encode (b Blob ) string {
182
+ func encode (b Blob , key [32 ]byte ) string {
185
183
js , err := json .Marshal (b )
186
184
if err != nil {
187
185
panic (err )
@@ -197,7 +195,7 @@ func encode(b Blob) string {
197
195
return base64 .RawURLEncoding .EncodeToString (bs )
198
196
}
199
197
200
- func decode (s string ) (Blob , error ) {
198
+ func decode (s string , key [ 32 ] byte ) (Blob , error ) {
201
199
bs , err := base64 .RawURLEncoding .DecodeString (s )
202
200
if err != nil {
203
201
return Blob {}, err
@@ -214,7 +212,16 @@ func decode(s string) (Blob, error) {
214
212
return b , json .Unmarshal (js , & b )
215
213
}
216
214
217
- func handleGame (shortname , u string ) CallbackHandler {
215
+ func genKey () [32 ]byte {
216
+ var key [32 ]byte
217
+ _ , err := rand .Read (key [:])
218
+ if err != nil {
219
+ panic (err )
220
+ }
221
+ return key
222
+ }
223
+
224
+ func handleGame (shortname , u string , key [32 ]byte ) CallbackHandler {
218
225
return func (q * tgbotapi.CallbackQuery ) * tgbotapi.CallbackConfig {
219
226
if g := q .GameShortName ; g != shortname {
220
227
return nil
@@ -231,7 +238,7 @@ func handleGame(shortname, u string) CallbackHandler {
231
238
b .MessageID = msg .MessageID
232
239
b .ChatID = msg .Chat .ID
233
240
}
234
- key := encode (b )
241
+ key := encode (b , key )
235
242
var v = url.Values {}
236
243
v .Add ("game" , shortname )
237
244
v .Add ("scored" , "1" )
@@ -282,7 +289,7 @@ func sendScore(blob Blob, score int) BotAction {
282
289
}
283
290
}
284
291
285
- func winHandler (actions chan <- BotAction ) httprouter.Handle {
292
+ func winHandler (actions chan <- BotAction , blobKey [ 32 ] byte ) httprouter.Handle {
286
293
return func (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
287
294
key := r .FormValue ("key" )
288
295
if key == "" {
@@ -294,7 +301,7 @@ func winHandler(actions chan<- BotAction) httprouter.Handle {
294
301
http .Error (w , "missing/bad parameter `score`" , http .StatusBadRequest )
295
302
return
296
303
}
297
- blob , err := decode (key )
304
+ blob , err := decode (key , blobKey )
298
305
if err != nil {
299
306
log .Printf ("decoding blob %q: %s" , key , err )
300
307
http .Error (w , "bad key" , http .StatusBadRequest )
0 commit comments