-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathhandle.go
More file actions
595 lines (533 loc) · 19.1 KB
/
handle.go
File metadata and controls
595 lines (533 loc) · 19.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package federation
import (
"crypto/ed25519"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/matrix-org/util"
)
// EXPERIMENTAL
// MakeJoinRequestsHandler is the http.Handler implementation for the make_join part of
// HandleMakeSendJoinRequests.
func MakeJoinRequestsHandler(s *Server, w http.ResponseWriter, req *http.Request) {
// Check federation signature
fedReq, errResp := fclient.VerifyHTTPRequest(
req, time.Now(), spec.ServerName(s.serverName), nil, s.keyRing,
)
if fedReq == nil {
w.WriteHeader(errResp.Code)
b, _ := json.Marshal(errResp.JSON)
w.Write(b)
return
}
vars := mux.Vars(req)
userID := vars["userID"]
roomID := vars["roomID"]
room, ok := s.rooms[roomID]
if !ok {
w.WriteHeader(404)
w.Write([]byte("complement: HandleMakeSendJoinRequests make_join unexpected room ID: " + roomID))
return
}
makeJoinResp, err := MakeRespMakeJoin(s, room, userID)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("complement: HandleMakeSendJoinRequests %s", err)))
return
}
// Send it
w.WriteHeader(200)
b, _ := json.Marshal(makeJoinResp)
w.Write(b)
}
// EXPERIMENTAL
// MakeRespMakeJoin makes the response for a /make_join request, without verifying any signatures
// or dealing with HTTP responses itself.
func MakeRespMakeJoin(s *Server, room *ServerRoom, userID string) (resp fclient.RespMakeJoin, err error) {
// Generate a join event
proto, err := room.ProtoEventCreator(room, Event{
Type: "m.room.member",
StateKey: &userID,
Content: map[string]interface{}{
"membership": spec.Join,
},
Sender: userID,
})
if err != nil {
err = fmt.Errorf("make_join cannot set create proto event: %w", err)
return
}
resp = fclient.RespMakeJoin{
RoomVersion: room.Version,
JoinEvent: *proto,
}
return
}
// EXPERIMENTAL
// MakeRespMakeKnock makes the response for a /make_knock request, without verifying any signatures
// or dealing with HTTP responses itself.
func MakeRespMakeKnock(s *Server, room *ServerRoom, userID string) (resp fclient.RespMakeKnock, err error) {
// Generate a knock event
proto, err := room.ProtoEventCreator(room, Event{
Type: "m.room.member",
StateKey: &userID,
Content: map[string]interface{}{
"membership": spec.Join, // XXX this feels wrong?
},
Sender: userID,
})
if err != nil {
err = fmt.Errorf("make_knock cannot set create proto event: %w", err)
return
}
resp = fclient.RespMakeKnock{
RoomVersion: room.Version,
KnockEvent: *proto,
}
return
}
// EXPERIMENTAL
// SendJoinRequestsHandler is the http.Handler implementation for the send_join part of
// HandleMakeSendJoinRequests.
//
// expectPartialState should be true if we should expect the incoming send_join
// request to use the partial_state flag, per MSC3706. In that case, we reply
// with only the critical subset of the room state.
//
// omitServersInRoom should be false to respond to partial_state joins with the complete list of
// servers in the room. When omitServersInRoom is true, a misbehaving server is simulated and only
// the current server is returned to the joining server.
func SendJoinRequestsHandler(s *Server, w http.ResponseWriter, req *http.Request, expectPartialState bool, omitServersInRoom bool) {
fedReq, errResp := fclient.VerifyHTTPRequest(
req, time.Now(), s.serverName, nil, s.keyRing,
)
if fedReq == nil {
w.WriteHeader(errResp.Code)
b, _ := json.Marshal(errResp.JSON)
w.Write(b)
return
}
// if we expect a partial-state join, the request should have a "partial_state" flag
queryParams := req.URL.Query()
partialState := queryParams.Get("omit_members")
if expectPartialState && partialState != "true" {
log.Printf("Not a partial-state request: got %v, want %s",
partialState, "true")
w.WriteHeader(500)
w.Write([]byte("complement: Incoming send_join was not partial_state"))
return
}
vars := mux.Vars(req)
roomID := vars["roomID"]
room, ok := s.rooms[roomID]
if !ok {
w.WriteHeader(404)
w.Write([]byte("complement: HandleMakeSendJoinRequests send_join unexpected room ID: " + roomID))
return
}
verImpl, err := gomatrixserverlib.GetRoomVersion(room.Version)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("complement: HandleMakeSendJoinRequests send_join unexpected room version: " + err.Error()))
return
}
event, err := verImpl.NewEventFromUntrustedJSON(fedReq.Content())
if err != nil {
w.WriteHeader(500)
w.Write([]byte("complement: HandleMakeSendJoinRequests send_join cannot parse event JSON: " + err.Error()))
return
}
resp := room.GenerateSendJoinResponse(room, s, event, expectPartialState, omitServersInRoom)
b, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("complement: HandleMakeSendJoinRequests send_join cannot marshal RespSendJoin: " + err.Error()))
return
}
w.WriteHeader(200)
w.Write(b)
}
// EXPERIMENTAL
// HandleMakeSendJoinRequests is an option which will process make_join and send_join requests for rooms which are present
// in this server. To add a room to this server, see Server.MustMakeRoom. No checks are done to see whether join requests
// are allowed or not. If you wish to test that, write your own test.
func HandleMakeSendJoinRequests() func(*Server) {
return func(s *Server) {
s.mux.Handle("/_matrix/federation/v1/make_join/{roomID}/{userID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
MakeJoinRequestsHandler(s, w, req)
})).Methods("GET")
s.mux.Handle("/_matrix/federation/v2/send_join/{roomID}/{eventID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
SendJoinRequestsHandler(s, w, req, false, false)
})).Methods("PUT")
}
}
// HandlePartialStateMakeSendJoinRequests is similar to HandleMakeSendJoinRequests, but expects a partial-state join.
func HandlePartialStateMakeSendJoinRequests() func(*Server) {
return func(s *Server) {
s.mux.Handle("/_matrix/federation/v1/make_join/{roomID}/{userID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
MakeJoinRequestsHandler(s, w, req)
})).Methods("GET")
s.mux.Handle("/_matrix/federation/v2/send_join/{roomID}/{eventID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
SendJoinRequestsHandler(s, w, req, true, false)
})).Methods("PUT")
}
}
// EXPERIMENTAL
// HandleInviteRequests is an option which makes the server process invite requests.
//
// inviteCallback is a callback function that if non-nil will be called and passed the incoming invite event
func HandleInviteRequests(inviteCallback func(gomatrixserverlib.PDU)) func(*Server) {
return func(s *Server) {
// https://matrix.org/docs/spec/server_server/r0.1.4#put-matrix-federation-v2-invite-roomid-eventid
s.mux.Handle("/_matrix/federation/v2/invite/{roomID}/{eventID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fedReq, errResp := fclient.VerifyHTTPRequest(
req, time.Now(), s.serverName, nil, s.keyRing,
)
if fedReq == nil {
w.WriteHeader(errResp.Code)
b, _ := json.Marshal(errResp.JSON)
w.Write(b)
return
}
var inviteRequest fclient.InviteV2Request
if err := json.Unmarshal(fedReq.Content(), &inviteRequest); err != nil {
log.Printf(
"complement: Unable to unmarshal incoming /invite request: %s",
err.Error(),
)
errResp := util.MessageResponse(400, err.Error())
w.WriteHeader(errResp.Code)
b, _ := json.Marshal(errResp.JSON)
w.Write(b)
return
}
if inviteCallback != nil {
inviteCallback(inviteRequest.Event())
}
// Sign the event before we send it back
signedEvent := inviteRequest.Event().Sign(string(s.serverName), s.KeyID, s.Priv)
// Send the response
res := map[string]interface{}{
"event": signedEvent,
}
w.WriteHeader(200)
b, _ := json.Marshal(res)
w.Write(b)
})).Methods("PUT")
}
}
// EXPERIMENTAL
// HandleDirectoryLookups will automatically return room IDs for any aliases present on this server.
func HandleDirectoryLookups() func(*Server) {
return func(s *Server) {
if s.directoryHandlerSetup {
return
}
s.directoryHandlerSetup = true
s.mux.Handle("/_matrix/federation/v1/query/directory", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
alias := req.URL.Query().Get("room_alias")
if roomID, ok := s.aliases[alias]; ok {
b, err := json.Marshal(fclient.RespDirectory{
RoomID: roomID,
Servers: []spec.ServerName{
s.serverName,
},
})
if err != nil {
w.WriteHeader(500)
w.Write([]byte("complement: HandleDirectoryLookups failed to marshal JSON: " + err.Error()))
return
}
w.WriteHeader(200)
w.Write(b)
return
}
w.WriteHeader(404)
w.Write([]byte(`{
"errcode": "M_NOT_FOUND",
"error": "Room alias not found."
}`))
})).Methods("GET")
}
}
// EXPERIMENTAL
// HandleEventRequests is an option which will process GET /_matrix/federation/v1/event/{eventId} requests universally when requested.
func HandleEventRequests() func(*Server) {
return func(srv *Server) {
srv.mux.Handle("/_matrix/federation/v1/event/{eventID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
eventID := vars["eventID"]
var event gomatrixserverlib.PDU
// find the event
RoomLoop:
for _, room := range srv.rooms {
for _, ev := range room.Timeline {
if ev.EventID() == eventID {
event = ev
break RoomLoop
}
}
}
if event == nil {
w.WriteHeader(404)
w.Write([]byte(fmt.Sprintf(`complement: failed to find event: %s`, eventID)))
return
}
txn := gomatrixserverlib.Transaction{
Origin: spec.ServerName(srv.serverName),
OriginServerTS: spec.AsTimestamp(time.Now()),
PDUs: []json.RawMessage{
event.JSON(),
},
}
resp, err := json.Marshal(txn)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf(`complement: failed to marshal JSON response: %s`, err)))
return
}
w.WriteHeader(200)
w.Write(resp)
}))
}
}
// EXPERIMENTAL
// HandleEventAuthRequests is an option which will process GET /_matrix/federation/v1/event_auth/{roomId}/{eventId}
// requests universally when requested.
func HandleEventAuthRequests() func(*Server) {
return func(srv *Server) {
srv.mux.Handle("/_matrix/federation/v1/event_auth/{roomID}/{eventID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
roomID := vars["roomID"]
eventID := vars["eventID"]
room, ok := srv.rooms[roomID]
if !ok {
srv.t.Logf("/event_auth request for unknown room ID %s", roomID)
w.WriteHeader(404)
w.Write([]byte("complement: HandleEventAuthRequests event_auth unknown room ID: " + roomID))
return
}
// find the event
var event gomatrixserverlib.PDU
for _, ev := range room.Timeline {
if ev.EventID() == eventID {
event = ev
break
}
}
if event == nil {
srv.t.Logf("/event_auth request for unknown event ID %s in room %s", eventID, roomID)
w.WriteHeader(404)
w.Write([]byte("complement: HandleEventAuthRequests event_auth unknown event ID: " + eventID))
return
}
authEvents := room.AuthChainForEvents([]gomatrixserverlib.PDU{event})
resp := fclient.RespEventAuth{
AuthEvents: gomatrixserverlib.NewEventJSONsFromEvents(authEvents),
}
respJSON, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf(`complement: failed to marshal JSON response: %s`, err)))
return
}
w.WriteHeader(200)
w.Write(respJSON)
}))
}
}
// EXPERIMENTAL
// HandleKeyRequests is an option which will process GET /_matrix/key/v2/server requests universally when requested.
func HandleKeyRequests() func(*Server) {
return func(srv *Server) {
keymux := srv.mux.PathPrefix("/_matrix/key/v2").Subrouter()
keyFn := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
k := gomatrixserverlib.ServerKeys{}
k.ServerName = spec.ServerName(srv.serverName)
publicKey := srv.Priv.Public().(ed25519.PublicKey)
k.VerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.VerifyKey{
srv.KeyID: {
Key: spec.Base64Bytes(publicKey),
},
}
k.OldVerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.OldVerifyKey{}
k.ValidUntilTS = spec.AsTimestamp(time.Now().Add(24 * time.Hour))
toSign, err := json.Marshal(k.ServerKeyFields)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("complement: HandleKeyRequests cannot marshal serverkeyfields: " + err.Error()))
return
}
k.Raw, err = gomatrixserverlib.SignJSON(
string(srv.serverName), srv.KeyID, srv.Priv, toSign,
)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("complement: HandleKeyRequests cannot sign json: " + err.Error()))
return
}
w.WriteHeader(200)
w.Write(k.Raw)
})
keymux.Handle("/server", keyFn).Methods("GET")
keymux.Handle("/server/", keyFn).Methods("GET")
keymux.Handle("/server/{keyID}", keyFn).Methods("GET")
}
}
// EXPERIMENTAL
// HandleMediaRequests is an option which will process /_matrix/media/v1/download/* using the provided map
// as a way to do so. The key of the map is the media ID to be handled.
func HandleMediaRequests(mediaIds map[string]func(w http.ResponseWriter)) func(*Server) {
return func(srv *Server) {
mediamux := srv.mux.PathPrefix("/_matrix/media").Subrouter()
mediamuxAuthenticated := srv.mux.PathPrefix("/_matrix/federation/v1/media").Subrouter()
downloadFn := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
origin := vars["origin"]
mediaId := vars["mediaId"]
if origin != string(srv.serverName) {
w.WriteHeader(400)
w.Write([]byte("complement: Invalid Origin; Expected " + srv.serverName))
return
}
if f, ok := mediaIds[mediaId]; ok {
f(w)
} else {
w.WriteHeader(404)
w.Write([]byte("complement: Unknown predefined media ID: " + mediaId))
return
}
})
// Note: The spec says to use /v3, but implementations rely on /v1 and /r0 working for federation requests as a legacy
// route.
mediamux.Handle("/r0/download/{origin}/{mediaId}", downloadFn).Methods("GET")
mediamux.Handle("/v1/download/{origin}/{mediaId}", downloadFn).Methods("GET")
mediamux.Handle("/v3/download/{origin}/{mediaId}", downloadFn).Methods("GET")
// Also handle authenticated media requests
mediamuxAuthenticated.Handle("/download/{mediaId}", downloadFn).Methods("GET")
}
}
// EXPERIMENTAL
// HandleTransactionRequests is an option which will process GET /_matrix/federation/v1/send/{transactionID} requests universally when requested.
// pduCallback and eduCallback are functions that if non-nil will be called and passed each PDU or EDU event received in the transaction.
// Callbacks will be fired AFTER the event has been stored onto the respective ServerRoom.
func HandleTransactionRequests(pduCallback func(gomatrixserverlib.PDU), eduCallback func(gomatrixserverlib.EDU)) func(*Server) {
return func(srv *Server) {
srv.mux.Handle("/_matrix/federation/v1/send/{transactionID}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Extract the transaction ID from the request vars
vars := mux.Vars(req)
transactionID := vars["transactionID"]
// Check federation signature
fedReq, errResp := fclient.VerifyHTTPRequest(
req, time.Now(), srv.serverName, nil, srv.keyRing,
)
if fedReq == nil {
log.Printf(
"complement: Transaction '%s': HTTP Code %d. Invalid http request: %s",
transactionID, errResp.Code, errResp.JSON,
)
w.WriteHeader(errResp.Code)
b, _ := json.Marshal(errResp.JSON)
w.Write(b)
return
}
// Unmarshal the request body into a transaction object
var transaction gomatrixserverlib.Transaction
err := json.Unmarshal(fedReq.Content(), &transaction)
if err != nil {
log.Printf(
"complement: Transaction '%s': Unable to unmarshal transaction body bytes into Transaction object: %s",
transaction.TransactionID, err.Error(),
)
errResp := util.MessageResponse(400, err.Error())
w.WriteHeader(errResp.Code)
b, _ := json.Marshal(errResp.JSON)
w.Write(b)
return
}
transaction.TransactionID = gomatrixserverlib.TransactionID(transactionID)
// Transactions are limited in size; they can have at most 50 PDUs and 100 EDUs.
// https://matrix.org/docs/spec/server_server/latest#transactions
if len(transaction.PDUs) > 50 || len(transaction.EDUs) > 100 {
log.Printf(
"complement: Transaction '%s': Transaction too large. PDUs: %d/50, EDUs: %d/100",
transaction.TransactionID, len(transaction.PDUs), len(transaction.EDUs),
)
errResp := util.MessageResponse(400, "Transactions are limited to 50 PDUs and 100 EDUs")
w.WriteHeader(errResp.Code)
b, _ := json.Marshal(errResp.JSON)
w.Write(b)
return
}
// Construct a response and fill as we process each PDU
response := fclient.RespSend{}
response.PDUs = make(map[string]fclient.PDUResult)
for _, pdu := range transaction.PDUs {
var header struct {
RoomID string `json:"room_id"`
}
if err = json.Unmarshal(pdu, &header); err != nil {
log.Printf("complement: Transaction '%s': Failed to extract room ID from event: %s", transaction.TransactionID, err.Error())
// We don't know the event ID at this point so we can't return the
// failure in the PDU results
continue
}
// Retrieve the room version from the server
room := srv.rooms[header.RoomID]
if room == nil {
// An invalid room ID may have been provided
log.Printf("complement: Transaction '%s': Failed to find local room: %s", transaction.TransactionID, header.RoomID)
continue
}
var event gomatrixserverlib.PDU
verImpl, err := gomatrixserverlib.GetRoomVersion(room.Version)
if err != nil {
log.Printf(
"complement: Transaction '%s': Failed to get room version: %s",
transaction.TransactionID, err.Error(),
)
continue
}
event, err = verImpl.NewEventFromUntrustedJSON(pdu)
if err != nil {
// We were unable to verify or process this event.
log.Printf(
"complement: Transaction '%s': Unable to process event '%s': %s",
transaction.TransactionID, event.EventID(), err.Error(),
)
// We still don't know the event ID, and cannot add the failure to the PDU results
continue
}
// Store this PDU in the room's timeline
room.AddEvent(event)
// Add this PDU as a success to the response
response.PDUs[event.EventID()] = fclient.PDUResult{}
// Run the PDU callback function with this event
if pduCallback != nil {
pduCallback(event)
}
}
for _, edu := range transaction.EDUs {
// Run the EDU callback function with this EDU
if eduCallback != nil {
eduCallback(edu)
}
}
resp, err := json.Marshal(response)
if err != nil {
log.Printf("complement: Transaction '%s': Failed to marshal JSON response: %s", transaction.TransactionID, err.Error())
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf(`complement: failed to marshal JSON response: %s`, err)))
return
}
w.WriteHeader(200)
w.Write(resp)
})).Methods("PUT")
}
}