Merge pull request #179 from mdosch/socks

Add support for SOCKS5 proxies.
This commit is contained in:
Martin 2024-01-13 14:58:10 +01:00 committed by GitHub
commit 685570cbd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
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/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"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/net/proxy"
)
const (
@ -103,12 +104,12 @@ func connect(host, user, passwd string, timeout time.Duration) (net.Conn, error)
addr += ":5222"
}
proxy := os.Getenv("HTTP_PROXY")
if proxy == "" {
proxy = os.Getenv("http_proxy")
http_proxy := os.Getenv("HTTP_PROXY")
if http_proxy == "" {
http_proxy = os.Getenv("http_proxy")
}
// test for no proxy, takes a comma separated list with substrings to match
if proxy != "" {
if http_proxy != "" {
noproxy := os.Getenv("NO_PROXY")
if noproxy == "" {
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, ",")
for _, s := range nplist {
if containsIgnoreCase(addr, s) {
proxy = ""
http_proxy = ""
break
}
}
}
}
if proxy != "" {
url, err := url.Parse(proxy)
socks5Target, socks5 := strings.CutPrefix(http_proxy, "socks5://")
if http_proxy != "" && !socks5 {
url, err := url.Parse(http_proxy)
if err == nil {
addr = url.Host
}
}
c, err := net.DialTimeout("tcp", addr, timeout)
var c net.Conn
var err error
if socks5 {
dialer, err := proxy.SOCKS5("tcp", socks5Target, nil, nil)
if err != nil {
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, "Host: %s\r\n", host)
fmt.Fprintf(c, "\r\n")