/
federationclient.go
422 lines (378 loc) · 13.2 KB
/
federationclient.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
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
package gomatrixserverlib
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"golang.org/x/crypto/ed25519"
)
// A FederationClient is a matrix federation client that adds
// "Authorization: X-Matrix" headers to requests that need ed25519 signatures
type FederationClient struct {
Client
serverName ServerName
serverKeyID KeyID
serverPrivateKey ed25519.PrivateKey
proto string
}
// NewFederationClient makes a new FederationClient
func NewFederationClient(
serverName ServerName, keyID KeyID, privateKey ed25519.PrivateKey, rootCA, certPem, keyPem string,
) *FederationClient {
if rootCA == "" {
return &FederationClient{
Client: *NewClient(),
serverName: serverName,
serverKeyID: keyID,
serverPrivateKey: privateKey,
proto: "http",
}
}
return &FederationClient{
Client: *NewHttpsClient(rootCA, certPem, keyPem),
serverName: serverName,
serverKeyID: keyID,
serverPrivateKey: privateKey,
proto: "https",
}
}
func (ac *FederationClient) doRequest(ctx context.Context, r FederationRequest, resBody interface{}) error {
/*if err := r.Sign(ac.serverName, ac.serverKeyID, ac.serverPrivateKey); err != nil {
return err
}*/
// do request without sign
if r.fields.Origin != "" && r.fields.Origin != ac.serverName {
return fmt.Errorf("gomatrixserverlib: the request is already signed by a different server")
}
r.fields.Origin = ac.serverName
req, err := r.HTTPRequest(ac.proto)
if err != nil {
return err
}
return ac.Client.DoRequestAndParseResponse(ctx, req, resBody)
}
var federationPathPrefix = "/_matrix/federation/v1"
func (ac *FederationClient) SendEvent(
ctx context.Context, destination ServerName, e interface{},
) (res RespSend, err error) {
path := federationPathPrefix + "/send/event/"
req := NewFederationRequest("PUT", destination, path)
if err = req.SetContent(e); err != nil {
return
}
err = ac.doRequest(ctx, req, &res)
return
}
// SendTransaction sends a transaction
func (ac *FederationClient) SendTransaction(
ctx context.Context, t Transaction,
) (res RespSend, err error) {
path := federationPathPrefix + "/send/" + string(t.TransactionID) + "/"
req := NewFederationRequest("PUT", t.Destination, path)
if err = req.SetContent(t); err != nil {
return
}
err = ac.doRequest(ctx, req, &res)
return
}
// MakeJoin makes a join m.room.member event for a room on a remote matrix server.
// This is used to join a room the local server isn't a member of.
// We need to query a remote server because if we aren't in the room we don't
// know what to use for the "prev_events" in the join event.
// The remote server should return us a m.room.member event for our local user
// with the "prev_events" filled out.
// If this successfully returns an acceptable event we will sign it with our
// server's key and pass it to SendJoin.
// See https://matrix.org/docs/spec/server_server/unstable.html#joining-rooms
func (ac *FederationClient) MakeJoin(
ctx context.Context, s ServerName, roomID, userID string, ver []string,
) (res RespMakeJoin, err error) {
path := federationPathPrefix + "/make_join/" +
url.PathEscape(roomID) + "/" +
url.PathEscape(userID)
for i, v := range ver {
if i == 0 {
path += "?v=" + url.PathEscape(v)
} else {
path += "&v=" + url.PathEscape(v)
}
}
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
// SendJoin sends a join m.room.member event obtained using MakeJoin via a
// remote matrix server.
// This is used to join a room the local server isn't a member of.
// See https://matrix.org/docs/spec/server_server/unstable.html#joining-rooms
func (ac *FederationClient) SendJoin(
ctx context.Context, s ServerName, roomID, eventID string, event Event,
) (res RespSendJoin, err error) {
path := federationPathPrefix + "/send_join/" +
url.PathEscape(roomID) + "/" +
url.PathEscape(eventID)
req := NewFederationRequest("PUT", s, path)
if err = req.SetContent(event); err != nil {
return
}
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) MakeLeave(
ctx context.Context, s ServerName, roomID, userID string,
) (res RespMakeLeave, err error) {
path := federationPathPrefix + "/make_leave/" +
url.PathEscape(roomID) + "/" +
url.PathEscape(userID)
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) SendLeave(
ctx context.Context, s ServerName, roomID, eventID string, event Event,
) (res RespSendLeave, err error) {
path := federationPathPrefix + "/send_leave/" +
url.PathEscape(roomID) + "/" +
url.PathEscape(eventID)
req := NewFederationRequest("PUT", s, path)
if err = req.SetContent(event); err != nil {
return
}
err = ac.doRequest(ctx, req, &res)
return
}
// SendInvite sends an invite m.room.member event to an invited server to be
// signed by it. This is used to invite a user that is not on the local server.
func (ac *FederationClient) SendInvite(
ctx context.Context, s ServerName, event Event,
) (res RespInvite, err error) {
path := federationPathPrefix + "/invite/" +
url.PathEscape(event.RoomID()) + "/" +
url.PathEscape(event.EventID())
req := NewFederationRequest("PUT", s, path)
if err = req.SetContent(event); err != nil {
return
}
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) SendInviteRaw(
ctx context.Context, s ServerName, roomID, eventID string, event interface{},
) (res RespInvite, err error) {
path := federationPathPrefix + "/invite/" +
url.PathEscape(roomID) + "/" +
url.PathEscape(eventID)
req := NewFederationRequest("PUT", s, path)
if err = req.SetContent(event); err != nil {
return
}
err = ac.doRequest(ctx, req, &res)
return
}
// ExchangeThirdPartyInvite sends the builder of a m.room.member event of
// "invite" membership derived from a response from invites sent by an identity
// server.
// This is used to exchange a m.room.third_party_invite event for a m.room.member
// one in a room the local server isn't a member of.
func (ac *FederationClient) ExchangeThirdPartyInvite(
ctx context.Context, s ServerName, builder EventBuilder,
) (err error) {
path := federationPathPrefix + "/exchange_third_party_invite/" +
url.PathEscape(builder.RoomID)
req := NewFederationRequest("PUT", s, path)
if err = req.SetContent(builder); err != nil {
return
}
err = ac.doRequest(ctx, req, nil)
return
}
// LookupState retrieves the room state for a room at an event from a
// remote matrix server as full matrix events.
func (ac *FederationClient) LookupState(
ctx context.Context, s ServerName, roomID, eventID string,
) (res RespState, err error) {
path := federationPathPrefix + "/state/" +
url.PathEscape(roomID) +
"/?event_id=" +
url.QueryEscape(eventID)
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
// LookupStateIDs retrieves the room state for a room at an event from a
// remote matrix server as lists of matrix event IDs.
func (ac *FederationClient) LookupStateIDs(
ctx context.Context, s ServerName, roomID, eventID string,
) (res RespStateIDs, err error) {
path := federationPathPrefix + "/state_ids/" +
url.PathEscape(roomID) +
"/?event_id=" +
url.QueryEscape(eventID)
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
// LookupRoomAlias looks up a room alias hosted on the remote server.
// The domain part of the roomAlias must match the name of the server it is
// being looked up on.
// If the room alias doesn't exist on the remote server then a 404 gomatrix.HTTPError
// is returned.
func (ac *FederationClient) LookupRoomAlias(
ctx context.Context, s ServerName, roomAlias string,
) (res RespDirectory, err error) {
path := federationPathPrefix + "/query/directory?room_alias=" +
url.QueryEscape(roomAlias)
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
type BackfillRequest struct {
EventID string `json:"event_id"`
Limit int `json:"limit"`
RoomID string `json:"room_id"`
Dir string `json:"dir"`
Domain string `json:"domain"`
Origin string `json:"origin"`
}
type BackfillResponse struct {
Error string `json:"error"`
Origin string `json:"origin"`
OriginServerTs Timestamp `json:"origin_server_ts"`
PDUs []Event `json:"pdus"`
}
// Backfill asks a homeserver for events early enough for them to not be in the
// local database.
// See https://matrix.org/docs/spec/server_server/unstable.html#get-matrix-federation-v1-backfill-roomid
func (ac *FederationClient) Backfill(
ctx context.Context, s ServerName, domain, roomID string, limit int, eventIDs []string, dir string,
) (res BackfillResponse, err error) {
// Encode the room ID so it won't interfer with the path.
roomID = url.PathEscape(roomID)
// Parse the limit into a string so that we can include it in the URL's query.
limitStr := strconv.Itoa(limit)
// Define the URL's query.
query := url.Values{}
query["v"] = eventIDs
query.Set("domain", domain)
query.Set("limit", limitStr)
query.Set("dir", dir)
// Use the url.URL structure to easily generate the request's URI (path?query).
u := url.URL{
Path: "/_matrix/federation/v1/backfill/" + roomID + "/",
RawQuery: query.Encode(),
}
path := u.RequestURI()
// Send the request.
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) LookupDisplayname(
ctx context.Context, s ServerName, userID string,
) (res RespDisplayname, err error) {
path := federationPathPrefix + "/query/profile?user_id=" +
url.PathEscape(userID) + "&field=displayname"
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) LookupAvatarURL(
ctx context.Context, s ServerName, userID string,
) (res RespAvatarURL, err error) {
path := federationPathPrefix + "/query/profile?user_id=" +
url.PathEscape(userID) + "&field=avatar_url"
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) LookupProfile(
ctx context.Context, s ServerName, userID string,
) (res RespProfile, err error) {
path := federationPathPrefix + "/query/profile?user_id=" +
url.PathEscape(userID) + "&field="
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) GetMissingEvents(
ctx context.Context, s ServerName, roomID string, content *ReqGetMissingEventContent,
) (res RespGetMissingEvents, err error) {
path := federationPathPrefix + "/get_missing_events/" + url.PathEscape(roomID)
req := NewFederationRequest("POST", s, path)
req.SetContent(*content)
err = ac.doRequest(ctx, req, &res)
return
}
// LookupDeviceKeys looks up a batch of keys which are the counterpart of request body
// It gives a key per device in request body
// If the device is in wrong val it won't appear in response body
func (ac *FederationClient) LookupDeviceKeys(
ctx context.Context, s ServerName, content *QueryRequest,
) (res QueryResponse, err error) {
path := federationPathPrefix + "/user/keys/query"
req := NewFederationRequest("POST", "python:9999", path)
req.SetContent(*content)
err = ac.doRequest(ctx, req, &res)
return
}
// LookupOneTimeKeys lookup a key for certain device
// which is used to encryption chatting session set up
func (ac *FederationClient) LookupOneTimeKeys(
ctx context.Context, s ServerName, roomID string, content *ClaimRequest,
) (res ClaimResponse, err error) {
path := federationPathPrefix + "/keys/claim"
req := NewFederationRequest("GET", s, path)
req.SetContent(*content)
err = ac.doRequest(ctx, req, &res)
return
}
// Download download media
// which is used to encryption chatting session set up
func (ac *FederationClient) Download(
ctx context.Context, s ServerName, domain, mediaID, width, method, fileType string, cb func(response *http.Response) error,
) (err error) {
path := federationPathPrefix + "/media/download/" + domain + "/" + mediaID + "/" + fileType
req := NewFederationRequest("GET", s, path)
/*if err := r.Sign(ac.serverName, ac.serverKeyID, ac.serverPrivateKey); err != nil {
return err
}*/
// do request without sign
if req.fields.Origin != "" && req.fields.Origin != ac.serverName {
return fmt.Errorf("gomatrixserverlib: the request is already signed by a different server")
}
req.fields.Origin = ac.serverName
r, err := req.HTTPRequest(ac.proto)
if err != nil {
return err
}
if r.Form == nil {
r.Form = make(url.Values)
}
r.Form.Set("width", width)
r.Form.Set("method", method)
response, err := ac.Client.DoHTTPRequest(ctx, r, true)
if response != nil {
defer response.Body.Close() // nolint: errcheck
}
if cb != nil {
err = cb(response)
}
return
}
func (ac *FederationClient) LookupMediaInfo(
ctx context.Context, s ServerName, mediaID, userID string,
) (res RespMediaInfo, err error) {
path := federationPathPrefix + "/media/info/" + mediaID + "/" + userID
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}
func (ac *FederationClient) LookupUserInfo(
ctx context.Context, s ServerName, userID string,
) (res RespUserInfo, err error) {
path := federationPathPrefix + "/query/user_info?user_id=" + userID
req := NewFederationRequest("GET", s, path)
err = ac.doRequest(ctx, req, &res)
return
}