From 2209572754078ef791749b41c1911c1ebdf7f365 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Fri, 19 Dec 2025 20:44:46 +0300 Subject: [PATCH 1/2] discord: Support http_proxy to connect to the API --- bridge/discord/discord.go | 15 +++++++++++++++ bridge/discord/handlers.go | 28 +++++++++++----------------- changelog.md | 2 ++ docs/protocols/discord/settings.md | 16 +++++++++++++++- docs/settings.md | 17 +++++++++++++++++ 5 files changed, 60 insertions(+), 18 deletions(-) diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index f3e623e8f..1c402615b 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -3,6 +3,8 @@ package bdiscord import ( "bytes" "fmt" + "net/http" + "net/url" "strings" "sync" @@ -86,6 +88,19 @@ func (b *Bdiscord) Connect() error { return err } b.Log.Info("Connection succeeded") + + http_proxy := b.GetString("http_proxy") + if http_proxy != "" { + b.Log.Infof("Using HTTP proxy %s to connect Discord API", http_proxy) + // This cannot fail because when the URL is invalid, `NewHttpClient` (bridge.go) + // produces an error which is caught by `AddBridge` (gateway.go), and this + // point in code is never reached. + proxyURL, _ := url.Parse(http_proxy) + // Implemented the use of proxy hinted here: https://github.com/bwmarrin/discordgo/issues/852 + b.c.Client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)} + b.c.Dialer.Proxy = http.ProxyURL(proxyURL) + } + // Add privileged intent for guild member tracking. This is needed to track nicks // for display names and @mention translation b.c.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged | diff --git a/bridge/discord/handlers.go b/bridge/discord/handlers.go index dc3480ed4..9252c9ac4 100644 --- a/bridge/discord/handlers.go +++ b/bridge/discord/handlers.go @@ -4,7 +4,6 @@ import ( "github.com/bwmarrin/discordgo" "github.com/davecgh/go-spew/spew" "github.com/matterbridge-org/matterbridge/bridge/config" - "github.com/matterbridge-org/matterbridge/bridge/helper" ) func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { //nolint:unparam @@ -106,32 +105,27 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat first := true for _, attach := range m.Attachments { if b.alwaysDownloadFiles { - var url, name, caption string - var data *[]byte - - url = attach.URL - name = attach.Filename - - err = helper.HandleDownloadSize(b.Log, &rmsg, name, int64(attach.Size), b.General) - if err != nil { - return - } - data, err = helper.DownloadFile(url) - if err != nil { - return - } + var caption string if first { caption = m.Content if caption == "" { - caption = name + caption = attach.Filename } first = false } else { + // There's only one caption for many attachments, see + // https://github.com/matterbridge-org/matterbridge/pull/44#discussion_r2590140958 caption = "" } - helper.HandleDownloadData(b.Log, &rmsg, name, caption, "", data, b.General) + // TODO: does the discord API have a unique ID for the file? + // in the meantime, let's reuse the filename + err = b.AddAttachmentFromURL(&rmsg, attach.Filename, attach.Filename, caption, attach.URL) + if err != nil { + b.Log.WithError(err).Warn("Failed to download one attachment. Skipping to the next.") + continue + } } else { m.Content = m.Content + "\n" + attach.URL } diff --git a/changelog.md b/changelog.md index 03a9239dd..f8d81618e 100644 --- a/changelog.md +++ b/changelog.md @@ -30,6 +30,8 @@ - Supports attachments - xmpp - New and revised advanced authentication settings `UseDirectTLS`, `NoStartTls`, `NoPlain`, and `Mechanism` ([#77](https://github.com/matterbridge-org/matterbridge/pull/77)) +- discord + - `http_proxy` support ([#113](https://github.com/matterbridge-org/matterbridge/pull/113)) ## Bugfixes diff --git a/docs/protocols/discord/settings.md b/docs/protocols/discord/settings.md index 1fec03b93..c4ae86b7c 100644 --- a/docs/protocols/discord/settings.md +++ b/docs/protocols/discord/settings.md @@ -128,7 +128,21 @@ This feature requires the "Manage Webhooks" permission (either globally or as pe Setting: OPTIONAL \ Format: boolean \ Example: - ```toml AutoWebhooks=true ``` + +## http_proxy + +> [!INFO] +> This setting can also be set application-wide, see [../../settings.md#http_proxy]. + +Specify the HTTP proxy to connect to the Discord API, and to download HTTP +attachments received from Discord. + +- Setting: **OPTIONAL** +- Format: *string* +- Example: + ```toml + http_proxy="http://login:password@server.example.org:1234" + ``` diff --git a/docs/settings.md b/docs/settings.md index e5399eff1..d90745f70 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -256,3 +256,20 @@ Example: ## RemoteNickFormat See [RemoteNickFormat](#RemoteNickFormat) + +## http_proxy + +Defines a matterbridge-wide HTTP proxy setting used when making HTTP requests. This is used when: + +- downloading remote HTTP attachments +- connecting to HTTP APIs for protocols that support it: + - discord + +This setting is not used when connecting to the media server to upload attachments. + +- Setting: **OPTIONAL** +- Format: *string* +- Example: + ```toml + http_proxy="http://login:password@server.example.org:1234" + ``` From 8ccaa811444f12ee6ab3016de218f4d9d568bcb4 Mon Sep 17 00:00:00 2001 From: selfhoster1312 Date: Wed, 31 Dec 2025 09:41:28 +0100 Subject: [PATCH 2/2] docs: Minor unrelated changelog updates (#44, #45) --- changelog.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index f8d81618e..a5e944adb 100644 --- a/changelog.md +++ b/changelog.md @@ -32,6 +32,10 @@ - New and revised advanced authentication settings `UseDirectTLS`, `NoStartTls`, `NoPlain`, and `Mechanism` ([#77](https://github.com/matterbridge-org/matterbridge/pull/77)) - discord - `http_proxy` support ([#113](https://github.com/matterbridge-org/matterbridge/pull/113)) + - `AlwaysDownloadFiles` setting enables upload of discord media to 3rd party + media server and protocols, to work around Discord providing short-lived + URLs for attachments ([#44](https://github.com/matterbridge-org/matterbridge/pull/44)) + (this setting may be deprecated and become the default in the future, see [this discussion](https://github.com/matterbridge-org/matterbridge/issues/37#issuecomment-3694667703)) ## Bugfixes @@ -40,7 +44,8 @@ the return code is not 200 to avoid saving trash data ([#20](https://github.com/matterbridge-org/matterbridge/pull/20)) - matrix - attachments received from matrix are working again, with authenticated media (MSC3916) implemented ([#61](https://github.com/matterbridge-org/matterbridge/pull/61)) - - image attachments are now send as images with more metadata ([#61](https://github.com/matterbridge-org/matterbridge/pull/61)) + - image attachments are now sent as images with more metadata ([#61](https://github.com/matterbridge-org/matterbridge/pull/61)) + - audio attachments are now sent as documents, not as a voice messages, otherwise they won't be delivered ([#45](https://github.com/matterbridge-org/matterbridge/pull/45)) - xmpp - various upstream go-xmpp changes fix connection on SASL2 with PLAIN auth - telegram