Add support for SOCKS5 proxies.

This commit is contained in:
Martin Dosch 2024-01-13 14:05:35 +01:00
parent 3f0cbac307
commit 7bfa331758
3 changed files with 32 additions and 13 deletions

5
go.mod
View File

@ -2,4 +2,7 @@ module github.com/xmppo/go-xmpp
go 1.21.5 go 1.21.5
require golang.org/x/crypto v0.18.0 require (
golang.org/x/crypto v0.18.0
golang.org/x/net v0.10.0
)

2
go.sum
View File

@ -1,2 +1,4 @@
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=

34
xmpp.go
View File

@ -41,6 +41,7 @@ import (
"time" "time"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
"golang.org/x/net/proxy"
) )
const ( const (
@ -103,12 +104,12 @@ func connect(host, user, passwd string, timeout time.Duration) (net.Conn, error)
addr += ":5222" addr += ":5222"
} }
proxy := os.Getenv("HTTP_PROXY") http_proxy := os.Getenv("HTTP_PROXY")
if proxy == "" { if http_proxy == "" {
proxy = os.Getenv("http_proxy") http_proxy = os.Getenv("http_proxy")
} }
// test for no proxy, takes a comma separated list with substrings to match // test for no proxy, takes a comma separated list with substrings to match
if proxy != "" { if http_proxy != "" {
noproxy := os.Getenv("NO_PROXY") noproxy := os.Getenv("NO_PROXY")
if noproxy == "" { if noproxy == "" {
noproxy = os.Getenv("no_proxy") noproxy = os.Getenv("no_proxy")
@ -117,25 +118,38 @@ func connect(host, user, passwd string, timeout time.Duration) (net.Conn, error)
nplist := strings.Split(noproxy, ",") nplist := strings.Split(noproxy, ",")
for _, s := range nplist { for _, s := range nplist {
if containsIgnoreCase(addr, s) { if containsIgnoreCase(addr, s) {
proxy = "" http_proxy = ""
break break
} }
} }
} }
} }
if proxy != "" { socks5Target, socks5 := strings.CutPrefix(http_proxy, "socks5://")
url, err := url.Parse(proxy) if http_proxy != "" && !socks5 {
url, err := url.Parse(http_proxy)
if err == nil { if err == nil {
addr = url.Host addr = url.Host
} }
} }
var c net.Conn
c, err := net.DialTimeout("tcp", addr, timeout) var err error
if socks5 {
dialer, err := proxy.SOCKS5("tcp", socks5Target, nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c, err = dialer.Dial("tcp", addr)
if err != nil {
return nil, err
}
} else {
c, err = net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}
}
if proxy != "" { if http_proxy != "" && !socks5 {
fmt.Fprintf(c, "CONNECT %s HTTP/1.1\r\n", host) fmt.Fprintf(c, "CONNECT %s HTTP/1.1\r\n", host)
fmt.Fprintf(c, "Host: %s\r\n", host) fmt.Fprintf(c, "Host: %s\r\n", host)
fmt.Fprintf(c, "\r\n") fmt.Fprintf(c, "\r\n")