Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SOCKS5 proxy when fetching URL previews #616

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/config/conf_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func NewDefaultDomainConfig() DomainRepoConfig {
DefaultLanguage: "en-US,en",
UserAgent: "matrix-media-repo",
OEmbed: false,
ProxyURL: "",
},
Thumbnails: ThumbnailsConfig{
MaxSourceBytes: 10485760, // 10mb
Expand Down
1 change: 1 addition & 0 deletions common/config/conf_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func NewDefaultMainConfig() MainRepoConfig {
DefaultLanguage: "en-US,en",
UserAgent: "matrix-media-repo",
OEmbed: false,
ProxyURL: "",
},
NumWorkers: 10,
ExpireDays: 0,
Expand Down
1 change: 1 addition & 0 deletions common/config/models_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type UrlPreviewsConfig struct {
DefaultLanguage string `yaml:"defaultLanguage"`
UserAgent string `yaml:"userAgent"`
OEmbed bool `yaml:"oEmbed"`
ProxyURL string `yaml:"proxyUrl"`
}

type IdenticonsConfig struct {
Expand Down
7 changes: 7 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,13 @@ urlPreviews:
# Defaults to disabled.
oEmbed: false

# When provided, preview webpage requests will be routed through the specified proxy.
# This can be useful to separate preview traffic from Matrix traffic in your network
# for more granular control. Currently only SOCKS5 proxies are supported. The URL
# should be in the format "socks5://127.0.0.1:1080"
# Defaults to a blank string.
proxyUrl: ""

# The thumbnail configuration for the media repository.
thumbnails:
# The maximum number of bytes an image can be before the thumbnailer refuses.
Expand Down
39 changes: 36 additions & 3 deletions url_previewing/u/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"mime"
"net"
"net/http"
"net/url"
"strconv"
"time"

Expand All @@ -17,8 +19,17 @@ import (
"github.com/t2bot/matrix-media-repo/url_previewing/m"
"github.com/t2bot/matrix-media-repo/util"
"github.com/t2bot/matrix-media-repo/util/readers"
"golang.org/x/net/proxy"
)

func getProxy(dialer *net.Dialer, ctx rcontext.RequestContext) (proxy.Dialer, error) {
url, err := url.Parse(ctx.Config.UrlPreviews.ProxyURL)
if err != nil {
return nil, fmt.Errorf("error parsing proxy url: %w", err)
}
return proxy.FromURL(url, dialer)
}

func doHttpGet(urlPayload *m.UrlPayload, languageHeader string, ctx rcontext.RequestContext) (*http.Response, error) {
var client *http.Client

Expand All @@ -37,6 +48,18 @@ func doHttpGet(urlPayload *m.UrlPayload, languageHeader string, ctx rcontext.Req
return nil, err
}

if ctx.Config.UrlPreviews.ProxyURL != "" {
proxyDialer, err := getProxy(dialer, ctx)
if err != nil {
return nil, fmt.Errorf("error creating proxy: %w", err)
}
if contextDialer, ok := proxyDialer.(proxy.ContextDialer); ok {
return contextDialer.DialContext(ctx2, network, net.JoinHostPort(safeIp.String(), safePort))
} else {
return nil, errors.New("failed proxy type assertion to ContextDialer")
}
}

return dialer.DialContext(ctx2, network, net.JoinHostPort(safeIp.String(), safePort))
}

Expand All @@ -48,9 +71,19 @@ func doHttpGet(urlPayload *m.UrlPayload, languageHeader string, ctx rcontext.Req
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// Based on https://github.com/matrix-org/gomatrixserverlib/blob/51152a681e69a832efcd934b60080b92bc98b286/client.go#L74-L90
DialTLSContext: func(ctx2 context.Context, network, addr string) (net.Conn, error) {
rawconn, err := net.Dial(network, addr)
if err != nil {
return nil, err
var rawconn net.Conn
var connErr error
if ctx.Config.UrlPreviews.ProxyURL != "" {
proxyDialer, err := getProxy(dialer, ctx)
if err != nil {
return nil, fmt.Errorf("error creating proxy: %w", err)
}
rawconn, connErr = proxyDialer.Dial(network, addr)
} else {
rawconn, connErr = net.Dial(network, addr)
}
if connErr != nil {
return nil, connErr
}
// Wrap a raw connection ourselves since tls.Dial defaults the SNI
conn := tls.Client(rawconn, &tls.Config{
Expand Down