support for connecting to SOCKS5 servers

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2021-07-28 14:47:08 +01:00
parent 80df1be3a6
commit 22b01cf9c2
2 changed files with 19 additions and 0 deletions

2
go.mod
View File

@ -3,6 +3,7 @@ module git.0cd.xyz/michael/gtools
go 1.15
retract v1.1.9 // retracting previous version.
retract v1.1.8 // Published accidentally.
require (
@ -11,6 +12,7 @@ require (
github.com/gorilla/csrf v1.7.0
github.com/gorilla/mux v1.8.0
github.com/sirupsen/logrus v1.8.1
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
google.golang.org/protobuf v1.27.1
)

View File

@ -7,8 +7,12 @@ import (
"math/big"
"net"
"net/http"
"time"
"golang.org/x/net/proxy"
)
// BasicAuth encodes BasicAuth header
func BasicAuth(username, password string) map[string]string {
return map[string]string{
"Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password)),
@ -63,3 +67,16 @@ func Hostname(ip net.IP) (hostname string) {
}
return names[0]
}
// Proxy connect to proxy server using SOCKS5
func Proxy(network, address string, auth *proxy.Auth) (client *http.Client, err error) {
dialer, err := proxy.SOCKS5(network, address, auth, proxy.Direct)
if err != nil {
return nil, err
}
tr := &http.Transport{Dial: dialer.Dial}
return &http.Client{
Transport: tr,
Timeout: time.Second * 10,
}, nil
}