Skip to content

Commit

Permalink
Add a separate proxy configuration option for Bridge -> HS
Browse files Browse the repository at this point in the history
  • Loading branch information
duo committed Mar 3, 2023
1 parent d3c7c3f commit 9b88db0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ appservice:

# Bridge config
bridge:
# Proxy for homeserver connection.
hs_proxy:
# Localpart template of MXIDs for WeChat users.
username_template: _wechat_{{.}}
# Displayname template for WeChat users.
Expand Down
2 changes: 2 additions & 0 deletions internal/config/bridgeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
)

type BridgeConfig struct {
HomeserverProxy string `yaml:"hs_proxy"`

UsernameTemplate string `yaml:"username_template"`
DisplaynameTemplate string `yaml:"displayname_template"`
ListenAddress string `yaml:"listen_address"`
Expand Down
1 change: 1 addition & 0 deletions internal/config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func DoUpgrade(helper *up.Helper) {
bridgeconfig.Upgrader.DoUpgrade(helper)

helper.Copy(up.Str, "bridge", "hs_proxy")
helper.Copy(up.Str, "bridge", "username_template")
helper.Copy(up.Str, "bridge", "displayname_template")
helper.Copy(up.Str, "bridge", "listen_address")
Expand Down
1 change: 1 addition & 0 deletions internal/wechat/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func (ws *WechatService) getConn(client *WechatClient) *Conn {
if conn, ok := ws.conns[client.getConnKey()]; ok {
return conn
} else {
// a better connection pick?
for k, v := range ws.conns {
client.setConnKey(k)
return v
Expand Down
10 changes: 10 additions & 0 deletions internal/wechatbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"sync"
"time"
Expand Down Expand Up @@ -101,6 +103,14 @@ func (br *WechatBridge) Init() {
)

br.WebsocketHandler = NewWebsocketCommandHandler(br)

if br.Config.Bridge.HomeserverProxy != "" {
if proxyUrl, err := url.Parse(br.Config.Bridge.HomeserverProxy); err != nil {
br.Log.Warnfln("Failed to parse bridge.hs_proxy: %v", err)
} else {
br.AS.HTTPClient.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
}
}
}

func (br *WechatBridge) Start() {
Expand Down

0 comments on commit 9b88db0

Please sign in to comment.