forked from 42wim/matterbridge
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbridge.go
More file actions
406 lines (339 loc) · 11.3 KB
/
bridge.go
File metadata and controls
406 lines (339 loc) · 11.3 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
package bridge
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/matterbridge-org/matterbridge/bridge/config"
"github.com/sirupsen/logrus"
)
type Bridger interface {
Send(msg config.Message) (string, error)
Connect() error
JoinChannel(channel config.ChannelInfo) error
Disconnect() error
NewHttpRequest(method, uri string, body io.Reader) (*http.Request, error)
NewHttpClient(proxy string) (*http.Client, error)
}
type Bridge struct {
Bridger
*sync.RWMutex
Name string
Account string
Protocol string
Channels map[string]config.ChannelInfo
Joined map[string]bool
ChannelMembers *config.ChannelMembers
Log *logrus.Entry
Config config.Config
General *config.Protocol
HttpClient *http.Client // Unique HTTP settings per bridge
}
type Config struct {
*Bridge
Remote chan config.Message
}
// Factory is the factory function to create a bridge
type Factory func(*Config) Bridger
// New is a basic constructor. More important fields are populated
// in gateway/gateway.go (AddBridge method).
func New(bridge *config.Bridge) *Bridge {
accInfo := strings.Split(bridge.Account, ".")
if len(accInfo) != 2 {
log.Fatalf("config failure, account incorrect: %s", bridge.Account)
}
protocol := accInfo[0]
name := accInfo[1]
return &Bridge{
RWMutex: new(sync.RWMutex),
Channels: make(map[string]config.ChannelInfo),
Name: name,
Protocol: protocol,
Account: bridge.Account,
Joined: make(map[string]bool),
}
}
func (b *Bridge) JoinChannels() error {
return b.joinChannels(b.Channels, b.Joined)
}
// SetChannelMembers sets the newMembers to the bridge ChannelMembers
func (b *Bridge) SetChannelMembers(newMembers *config.ChannelMembers) {
b.Lock()
b.ChannelMembers = newMembers
b.Unlock()
}
func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map[string]bool) error {
for ID, channel := range channels {
if !exists[ID] {
b.Log.Infof("%s: joining %s (ID: %s)", b.Account, channel.Name, ID)
time.Sleep(time.Duration(b.GetInt("JoinDelay")) * time.Millisecond)
err := b.JoinChannel(channel)
if err != nil {
return err
}
exists[ID] = true
}
}
return nil
}
func (b *Bridge) GetConfigKey(key string) string {
return b.Account + "." + key
}
func (b *Bridge) IsKeySet(key string) bool {
return b.Config.IsKeySet(b.GetConfigKey(key)) || b.Config.IsKeySet("general."+key)
}
func (b *Bridge) GetBool(key string) bool {
val, ok := b.Config.GetBool(b.GetConfigKey(key))
if !ok {
val, _ = b.Config.GetBool("general." + key)
}
return val
}
func (b *Bridge) GetInt(key string) int {
val, ok := b.Config.GetInt(b.GetConfigKey(key))
if !ok {
val, _ = b.Config.GetInt("general." + key)
}
return val
}
func (b *Bridge) GetString(key string) string {
val, ok := b.Config.GetString(b.GetConfigKey(key))
if !ok {
val, _ = b.Config.GetString("general." + key)
}
return val
}
func (b *Bridge) GetStringSlice(key string) []string {
val, ok := b.Config.GetStringSlice(b.GetConfigKey(key))
if !ok {
val, _ = b.Config.GetStringSlice("general." + key)
}
return val
}
func (b *Bridge) GetStringSlice2D(key string) [][]string {
val, ok := b.Config.GetStringSlice2D(b.GetConfigKey(key))
if !ok {
val, _ = b.Config.GetStringSlice2D("general." + key)
}
return val
}
// NewHttpClient produces a single unified http.Client per bridge.
//
// This allows to have project-wide defaults (timeout) as well as
// bridge-configurable values (`http_proxy`).
//
// This method is left public so that if that's needed, a bridge can
// override this constructor.
//
// TODO: maybe protocols without HTTP downloads at all could override
// this method and return nil? Or the other way around?
func (b *Bridge) NewHttpClient(http_proxy string) (*http.Client, error) {
if http_proxy != "" {
proxyUrl, err := url.Parse(b.GetString("http_proxy"))
if err != nil {
return nil, err
}
b.Log.Debugf("%s using HTTP proxy %s", b.Protocol, proxyUrl)
return &http.Client{
Timeout: time.Second * 15,
Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)},
}, nil
}
b.Log.Debugf("%s not using HTTP proxy", b.Protocol)
return &http.Client{
Timeout: time.Second * 5,
}, nil
}
var errHttpGetNotOk = errors.New("HTTP server responded non-OK code")
func HttpGetNotOkError(uri string, code int) error {
return fmt.Errorf("%w: %s returned code %d", errHttpGetNotOk, uri, code)
}
// HttpGetBytes returns bytes from a given URI, if the request
// succeeds and HTTP response status is 200 (OK).
func (b *Bridge) HttpGetBytes(uri string) (*[]byte, error) {
req, err := b.Bridger.NewHttpRequest("GET", uri, nil)
if err != nil {
return nil, err
}
b.Log.Debugf("Getting HTTP bytes with request: %#v", req)
resp, err := b.HttpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, HttpGetNotOkError(uri, resp.StatusCode)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
data := buf.Bytes()
return &data, nil
}
// HttpUpload uploads data to a URI, and validates the response status code.
//
// Params:
//
// - method: specific HTTP verb
// - uri: remote URL
// - headers: map of headers to insert in the request
// - data: raw bytes to upload in the body
// - ok_status: list of HTTP status codes considered successful (default: 200, 201)
//
// The response body is always discarded.
func (b *Bridge) HttpUpload(method string, uri string, headers map[string]string, data *[]byte, ok_status []int) error {
req, err := b.Bridger.NewHttpRequest(method, uri, bytes.NewReader(*data))
if err != nil {
return err
}
for header_name, header_value := range headers {
req.Header.Set(header_name, header_value)
}
b.Log.Debugf("HTTP upload request: %v", req)
resp, err := b.HttpClient.Do(req)
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
return err
}
// By default, allow codes 200 (OK) and 201 (CREATED)
if len(ok_status) == 0 {
ok_status = []int{200, 201}
}
for _, expected_code := range ok_status {
b.Log.Debugf("Successful file upload with code %d", expected_code)
return nil
}
return HttpGetNotOkError(uri, resp.StatusCode)
}
// AddAttachmentFromURL adds a file from a public URL.
//
// The data will be downloaded, and both the URL and the raw bytes will be
// passed to other bridges. When media server is enabled, the content URL
// will also be replaced by our own URL.
func (b *Bridge) AddAttachmentFromURL(msg *config.Message, filename string, id string, comment string, uri string) error {
return b.addAttachment(msg, filename, id, comment, uri, nil, false)
}
// AddAttachmentFromProtectedURL adds a file from a private, protected URL.
//
// The data will be downloaded by the bridge, but only the raw bytes will be
// passed to other bridges so that they don't relay broken URLs, such as
// with Matrix authenticated media. When media server is URL, our new URL
// will be inserted instead.
func (b *Bridge) AddAttachmentFromProtectedURL(msg *config.Message, filename string, id string, comment string, uri string) error {
return b.addAttachmentNoURL(msg, filename, id, comment, uri, nil, false)
}
// AddAttachmentFromBytes adds a file from raw bytes.
//
// The data bytes will be passed to other bridges. If those bridges only
// support URLs and not raw bytes upload:
//
// - if media server is enabled, matterbridge will produce a new URL
// - otherwise, the message will be discarded by the remote bridge
func (b *Bridge) AddAttachmentFromBytes(msg *config.Message, filename string, id string, comment string, data *[]byte) error {
return b.addAttachment(msg, filename, id, comment, "", data, false)
}
func (b *Bridge) AddAvatarFromURL(msg *config.Message, filename string, id string, comment string, uri string) error {
return b.addAttachment(msg, filename, id, comment, uri, nil, true)
}
func (b *Bridge) AddAvatarFromBytes(msg *config.Message, filename string, id string, comment string, data *[]byte) error {
return b.addAttachment(msg, filename, id, comment, "", data, true)
}
// NewHttpRequest produces a new http.Request instance with bridge-specific settings.
//
// This is used by bridges where HTTP downloads require a cookie/token, by overriding
// this method in the bridge struct.
func (b *Bridge) NewHttpRequest(method, uri string, body io.Reader) (*http.Request, error) {
return http.NewRequest(method, uri, body)
}
// Internal method including common parts to attachment/avatar handling methods.
//
// This method will process received bytes. If bytes are not set, they will be downloaded from the given URL.
// If neither data bytes nor uri is provided, this will be a hard error because there's a logic error somewhere.
func (b *Bridge) addAttachment(msg *config.Message, filename string, id string, comment string, uri string, data *[]byte, avatar bool) error {
if data != nil {
return b.addAttachmentProcess(msg, filename, id, comment, uri, data, avatar)
}
if uri == "" {
// This should never happen
b.Log.Fatalf("Logic error in bridge %s: attachment should have either URL or data set, neither was provided", b.Protocol)
}
data, err := b.HttpGetBytes(uri)
if err != nil {
return err
}
return b.addAttachmentProcess(msg, filename, id, comment, uri, data, avatar)
}
// Internal method similar to addAttachment, but will not keep the URL.
//
// This is useful so protected URLs requiring specific headers (such as matrix authenticated media)
// can be downloaded, and then omitted so other bridges don't spread along broken URLs.
func (b *Bridge) addAttachmentNoURL(msg *config.Message, filename string, id string, comment string, uri string, data *[]byte, avatar bool) error {
if data != nil {
return b.addAttachmentProcess(msg, filename, id, comment, "", data, avatar)
}
if uri == "" {
// This should never happen
b.Log.Fatalf("Logic error in bridge %s: attachment should have either URL or data set, neither was provided", b.Protocol)
}
data, err := b.HttpGetBytes(uri)
if err != nil {
return err
}
return b.addAttachmentProcess(msg, filename, id, comment, "", data, avatar)
}
type errFileTooLarge struct {
FileName string
Size int
MaxSize int
}
func (e *errFileTooLarge) Error() string {
return fmt.Sprintf("File %#v to large to download (%#v). MediaDownloadSize is %#v", e.FileName, e.Size, e.MaxSize)
}
type errFileBlacklisted struct {
FileName string
}
func (e *errFileBlacklisted) Error() string {
return fmt.Sprintf("File %#v matches the backlist, not downloading it", e.FileName)
}
func (b *Bridge) addAttachmentProcess(msg *config.Message, filename string, id string, comment string, uri string, data *[]byte, avatar bool) error {
size := len(*data)
if size > b.General.MediaDownloadSize {
return &errFileTooLarge{
FileName: filename,
Size: size,
MaxSize: b.General.MediaDownloadSize,
}
}
// Apply `MediaDownloadBlackList` regexes
if b.Config.IsFilenameBlacklisted(filename) {
return &errFileBlacklisted{
FileName: filename,
}
}
b.Log.Debugf("Download OK %#v %#v", filename, size)
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{
Name: filename,
Data: data,
URL: uri,
Comment: comment,
Avatar: avatar,
// TODO: if id is not set, maybe use hash of bytes?
NativeID: id,
Size: int64(len(*data)),
})
return nil
}