config: add support for using a proxy

This commit is contained in:
Tulir Asokan 2024-11-29 19:26:47 +02:00
parent b330c5836e
commit 8b61dc5352
6 changed files with 25 additions and 6 deletions

View file

@ -29,7 +29,7 @@ import (
"go.mau.fi/mautrix-discord/database"
)
func downloadDiscordAttachment(url string, maxSize int64) ([]byte, error) {
func downloadDiscordAttachment(cli *http.Client, url string, maxSize int64) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
@ -38,7 +38,7 @@ func downloadDiscordAttachment(url string, maxSize int64) ([]byte, error) {
req.Header.Set(key, value)
}
resp, err := http.DefaultClient.Do(req)
resp, err := cli.Do(req)
if err != nil {
return nil, err
}
@ -65,7 +65,7 @@ func downloadDiscordAttachment(url string, maxSize int64) ([]byte, error) {
}
}
func uploadDiscordAttachment(url string, data []byte) error {
func uploadDiscordAttachment(cli *http.Client, url string, data []byte) error {
req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data))
if err != nil {
return err
@ -80,7 +80,7 @@ func uploadDiscordAttachment(url string, data []byte) error {
req.Header.Del("X-Discord-Timezone")
req.Header.Del("X-Super-Properties")
resp, err := http.DefaultClient.Do(req)
resp, err := cli.Do(req)
if err != nil {
return err
}
@ -301,7 +301,7 @@ func (br *DiscordBridge) copyAttachmentToMatrix(intent *appservice.IntentAPI, ur
}()
var data []byte
data, onceErr = downloadDiscordAttachment(url, br.MediaConfig.UploadSize)
data, onceErr = downloadDiscordAttachment(http.DefaultClient, url, br.MediaConfig.UploadSize)
if onceErr != nil {
return
}

View file

@ -57,6 +57,8 @@ type BridgeConfig struct {
EnableWebhookAvatars bool `yaml:"enable_webhook_avatars"`
UseDiscordCDNUpload bool `yaml:"use_discord_cdn_upload"`
Proxy string `yaml:"proxy"`
CacheMedia string `yaml:"cache_media"`
DirectMedia DirectMedia `yaml:"direct_media"`

View file

@ -63,6 +63,7 @@ func DoUpgrade(helper *up.Helper) {
helper.Copy(up.Bool, "bridge", "prefix_webhook_messages")
helper.Copy(up.Bool, "bridge", "enable_webhook_avatars")
helper.Copy(up.Bool, "bridge", "use_discord_cdn_upload")
helper.Copy(up.Str|up.Null, "bridge", "proxy")
helper.Copy(up.Str, "bridge", "cache_media")
helper.Copy(up.Bool, "bridge", "direct_media", "enabled")
helper.Copy(up.Str, "bridge", "direct_media", "server_name")

View file

@ -168,6 +168,8 @@ bridge:
# like the official client does? The other option is sending the media in the message send request as a form part
# (which is always used by bots and webhooks).
use_discord_cdn_upload: true
# Proxy for Discord connections
proxy:
# Should mxc uris copied from Discord be cached?
# This can be `never` to never cache, `unencrypted` to only cache unencrypted mxc uris, or `always` to cache everything.
# If you have a media repo that generates non-unique mxc uris, you should set this to never.

View file

@ -1647,7 +1647,7 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event) {
}
prepared := prep.Attachments[0]
att.UploadedFilename = prepared.UploadFilename
err = uploadDiscordAttachment(prepared.UploadURL, data)
err = uploadDiscordAttachment(sender.Session.Client, prepared.UploadURL, data)
if err != nil {
go portal.sendMessageMetrics(evt, err, "Error reuploading media in")
return

14
user.go
View file

@ -2,10 +2,12 @@ package main
import (
"context"
"crypto/tls"
"errors"
"fmt"
"math/rand"
"net/http"
"net/url"
"os"
"runtime/debug"
"sort"
@ -547,6 +549,18 @@ func (user *User) Connect() error {
if err != nil {
return err
}
if user.bridge.Config.Bridge.Proxy != "" {
u, _ := url.Parse(user.bridge.Config.Bridge.Proxy)
tlsConf := &tls.Config{
InsecureSkipVerify: os.Getenv("DISCORD_SKIP_TLS_VERIFICATION") == "true",
}
session.Client.Transport = &http.Transport{
Proxy: http.ProxyURL(u),
TLSClientConfig: tlsConf,
}
session.Dialer.Proxy = http.ProxyURL(u)
session.Dialer.TLSClientConfig = tlsConf
}
// TODO move to config
if os.Getenv("DISCORD_DEBUG") == "1" {
session.LogLevel = discordgo.LogDebug