Skip to content

Proxy support for Discord#109

Closed
Wohlstand wants to merge 3 commits intomatterbridge-org:masterfrom
Wohlstand:discord-proxy
Closed

Proxy support for Discord#109
Wohlstand wants to merge 3 commits intomatterbridge-org:masterfrom
Wohlstand:discord-proxy

Conversation

@Wohlstand
Copy link
Copy Markdown

Recreation of #48, CI still fails, and tells nothing why it fails. All the stuff refered related to downloads.

@poVoq
Copy link
Copy Markdown
Collaborator

poVoq commented Dec 25, 2025

Sorry, right now I am also out of ideas what the cause might be. Maybe @selfhoster1312 (who set up the CI and is generally more familiar with Github quirks than me) will have time to look at it after the holidays.

@jugendhacker
Copy link
Copy Markdown

The CI fails at the go fmt step, so just running go fmt ./... should fix it I think? At least if I check out your PR locally and run go fmt it changes a few files

@Wohlstand
Copy link
Copy Markdown
Author

Wohlstand commented Dec 27, 2025

I ran, and... it produced unrelated changes that weren't supposed to change. Actually, the error is absolutely unobvious. Maybe inject an echo that tells an advice if such thing fails? VSCode did these changes too, but I reverted them since they are unrelated to the work that I done.

@Wohlstand
Copy link
Copy Markdown
Author

And ye, now I see the thing is fixed now. Actually, the CI error should be obvious. Especially for people who aren't sure for this.

@poVoq
Copy link
Copy Markdown
Collaborator

poVoq commented Dec 28, 2025

From my side I see no issue in merging this if it is a purely optional feature. But I have not tested it and no easy way to do so right now.

@selfhoster1312 any objections from the point of view of your http file handling changes?

@Wohlstand
Copy link
Copy Markdown
Author

Wohlstand commented Dec 28, 2025

From my side I see no issue in merging this if it is a purely optional feature. But I have not tested it and no easy way to do so right now.

Yes, feature is fully optional, by default it's just unused, and everything works as usually, but when used, then Discord connects through HTTP proxy.

@Wohlstand
Copy link
Copy Markdown
Author

From my side I see no issue in merging this if it is a purely optional feature. But I have not tested it and no easy way to do so right now.

Also! If you want to test, I can provide temporary login credentials at my own proxy server, but that's better if I send credentials via email. My email you can find just at any source file at one of my projects, for example https://github.com/Wohlstand/libADLMIDI.

- xmpp
- New and revised advanced authentication settings `UseDirectTLS`, `NoStartTls`, `NoPlain`, and `Mechanism` ([#77](https://github.com/matterbridge-org/matterbridge/pull/77))
- discord
- Added a setting `AlwaysDownloadFiles` that allows to download and re-upload sent files from Discord CDN to other messengers or mediaproxy if configured
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debate about this setting is still ongoing here: #37 (comment)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already added that setting, but I didn't documented at changelog at all, and I done that just here.

- 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))
- audio attachments are now sent as documents, not as a voice messages, otherwise they won't be delivered
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because it was forgotten in another PR? If so, can you link to the relevant PR here please?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because it was forgotten in another PR?

Absolutely!

If so, can you link to the relevant PR here please?

Sure...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's here: #45

req, err := http.NewRequest("GET", fileurl, nil) //nolint:usestdlibvars

req.Header.Add("X-Auth-Token", token)
req.Header.Add("X-User-Id", userID)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably not touch helper.go at all. We now have http helpers which respect the http_proxy global or bridge setting: https://github.com/matterbridge-org/matterbridge/blob/master/docs/development/protocol.md#handling-http-requests

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I touched it because I wanted to implement the file handling that will use the custom proxy, I extended API, because it was totally missing here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new helpers were added in #59 specifically because you needed them here, and because they're useful in other bridges too. No need to add custom logic to discord bridge, except to make sure the discord API client itself uses the proxy of course :)

return
}
data, err = helper.DownloadFile(url)
data, err = helper.DownloadFileProxy(url, b.httpProxy)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this should be b.AddAttachmentFromURL(&rmsg, attach.Filename, attach.Filename, caption , url). If there's an attachment ID somewhere, you can use it instead of the second filename argument.

Also, this method replaces HandleDownloadSize, DownloadFileProxy, and HandleDownloadData making the whole code simpler.

}
b.Log.Info("Connection succeeded")

if b.httpProxy != "" {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be b.GetString("http_proxy") so it's the same across all bridges.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it became such? When I developed the thing initially, it was like I done already.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: THIS and other changes I done a while ago before the matterbridge got been forked, and then I contributing my stuff, so, I already learned some changes and tweaked.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I opened #50 after the discussion in your previous PR #48. This led to #59 being merged so all protocols can enjoy the HTTP proxy settings.

b.nickMemberMap = make(map[string]*discordgo.Member)
b.channelInfoMap = make(map[string]*config.ChannelInfo)

b.httpProxy = b.GetString("HttpProxy")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really useful to store this here. You can just remove it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stored it here for convenience to track all bridge-specific settings at the same place, as it was done before me when I started to tweak this. And that's guarantee that it will be always valid config field requested, and such dupes would lead human factor errors like typo in the field name, and result - it works at one place but fails at another. With a variable it's impossible, compilation will fail.


if b.httpProxy != "" {
b.Log.Info("Using HTTP proxy to connect Discord")
proxyURL, _ := url.Parse(b.httpProxy)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to discard the error here because in bridge.go:NewHttpClient, failing to parse http_proxy produces an error, which is considered fatal in gateway.go:AddBridge. You should still add a comment saying if the URL was invalid, AddBridge would panic and you would never reach this point.

Copy link
Copy Markdown
Author

@Wohlstand Wohlstand Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question: DID that unrelated http_proxy implemented just recently? Actually, why not to just take my thing and refactor it together with everything so the full integrity is guaranteed? I feeling just tired of this bueroracy led by unexpected changes are doing just now, and as result, my not-yet-taken-changes yesterday was actual, and now they are so ancient as the dinosaur skeleton. (Just my expressing, sorry for that).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worry i understand your confusion. It is indeed very recent. The discussion started on your previous PR #48 but since you closed it you probably didn't read everything. I added the new helpers to help you and other bridge maintainers. If the new helpers are not helping, feel free to provide feedback so we can improve them.

AutoWebhooks=true
```

## HttpProxy
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be http_proxy

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this field following format that was already defined by other fields here.

@selfhoster1312
Copy link
Copy Markdown
Collaborator

That looks good, thanks. Just a few changes here and there (especially using the new HTTP helpers) and we'll be good to go! 👍

@Wohlstand
Copy link
Copy Markdown
Author

That looks good, thanks. Just a few changes here and there (especially using the new HTTP helpers) and we'll be good to go! 👍

These new HTTP helpers are just unexpected for me, especially because I am tired of recent CI fighting. I would take a while before I will apply all the new thingies without breaking of everything.

@selfhoster1312
Copy link
Copy Markdown
Collaborator

These new HTTP helpers are just unexpected for me, especially because I am tired of recent CI fighting. I would take a while before I will apply all the new thingies without breaking of everything.

If you feel like this is too much for you just say so. I'm OK to take the time to edit your PR or open a new one with the changes.

@Wohlstand
Copy link
Copy Markdown
Author

If you feel like this is too much for you just say so. I'm OK to take the time to edit your PR or open a new one with the changes.

Ye, you can try just remake my code if you can, I am fine, just it's need to make sure the final thing will just work. For my side that proxy is needed, otherwise Discord just doesn't work at all.

@selfhoster1312
Copy link
Copy Markdown
Collaborator

So apparently i can't push directly to your branch, even if you have clicked "allow edits from maintainers". Github is really stupid. Maybe that's because i'm admin of the matterbridge-org org, and not of the matterbridge repo directly? Maybe you explicitly need to allow me to push to your fork, but i can't find documentation about this behavior.

Anyway i'll just open a new PR, sorry.

@Wohlstand
Copy link
Copy Markdown
Author

I'll just grant access to my copy of repo.

@selfhoster1312 selfhoster1312 mentioned this pull request Dec 31, 2025
@selfhoster1312
Copy link
Copy Markdown
Collaborator

I'll just grant access to my copy of repo.

That will be appreciated for next time :-)

For now i already opened #113 so we can head over there for review. Thanks for your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants