Skip to content

Commit

Permalink
support more proxy protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Apr 5, 2022
1 parent 36af0e5 commit 6b9d2a8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
go.uber.org/zap v1.21.0
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
h12.io/socks v1.0.3
)

require (
Expand All @@ -42,6 +43,7 @@ require (
golang.org/x/mod v0.4.2 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.6 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364 h1:5XxdakFhqd9dnXoAZy1Mb2R/DZ6D1e+0bGC/JhucGYI=
github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364/go.mod h1:eDJQioIyy4Yn3MVivT7rv/39gAJTrA7lgmYr8EW950c=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -192,6 +194,8 @@ github.com/ngdinhtoan/glide-cleanup v0.2.0/go.mod h1:UQzsmiDOb8YV3nOsCxK/c9zPpCZ
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -562,6 +566,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
h12.io/socks v1.0.3 h1:Ka3qaQewws4j4/eDQnOdpr4wXsC//dXtWvftlIcCQUo=
h12.io/socks v1.0.3/go.mod h1:AIhxy1jOId/XCz9BO+EIgNL2rQiPTBNnOfnVnQ+3Eck=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
6 changes: 4 additions & 2 deletions src/core/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func NewClient(ctx context.Context, clientConfig ClientConfig, logger *zap.Logge
tlsConfig = clientConfig.TLSClientConfig
}

proxyFunc := utils.GetProxyFunc(templates.ParseAndExecute(logger, clientConfig.ProxyURLs, ctx), timeout, true)

if clientConfig.StaticHost != nil {
return &fasthttp.HostClient{
Addr: clientConfig.StaticHost.Addr,
Expand All @@ -118,7 +120,7 @@ func NewClient(ctx context.Context, clientConfig ClientConfig, logger *zap.Logge
DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
DisablePathNormalizing: true,
TLSConfig: tlsConfig,
Dial: dialViaProxyFunc(utils.GetProxyFunc(templates.ParseAndExecute(logger, clientConfig.ProxyURLs, ctx), timeout), "tcp"),
Dial: dialViaProxyFunc(proxyFunc, "tcp"),
}
}

Expand All @@ -132,7 +134,7 @@ func NewClient(ctx context.Context, clientConfig ClientConfig, logger *zap.Logge
DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
DisablePathNormalizing: true,
TLSConfig: tlsConfig,
Dial: dialViaProxyFunc(utils.GetProxyFunc(templates.ParseAndExecute(logger, clientConfig.ProxyURLs, ctx), timeout), "tcp"),
Dial: dialViaProxyFunc(proxyFunc, "tcp"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/packetgen/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type netConn struct {
}

func openNetConn(c netConnConfig) (*netConn, error) {
conn, err := utils.GetProxyFunc(c.ProxyURLs, c.Timeout)(c.Protocol, c.Address)
conn, err := utils.GetProxyFunc(c.ProxyURLs, c.Timeout, false)(c.Protocol, c.Address)

if c.TLSClientConfig != nil {
tlsConn := tls.Client(conn, c.TLSClientConfig)
Expand Down
2 changes: 1 addition & 1 deletion src/core/slowloris/slowloris.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s SlowLoris) activeConnectionsCounter(ch <-chan int) {
}

func (s SlowLoris) dialVictim(logger *zap.Logger, config *Config, hostPort string, isTLS bool) io.ReadWriteCloser {
conn, err := utils.GetProxyFunc(config.ProxyURLs, config.Timeout)("tcp", hostPort)
conn, err := utils.GetProxyFunc(config.ProxyURLs, config.Timeout, false)("tcp", hostPort)
if err != nil {
metrics.IncSlowLoris(hostPort, "tcp", metrics.StatusFail)
logger.Debug("couldn't establish connection", zap.String("addr", hostPort), zap.Error(err))
Expand Down
25 changes: 19 additions & 6 deletions src/utils/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"strings"
"time"

"github.com/valyala/fasthttp/fasthttpproxy"
"golang.org/x/net/proxy"
"h12.io/socks"
)

type ProxyFunc func(network, addr string) (net.Conn, error)

func GetProxyFunc(proxyURLs string, timeout time.Duration) ProxyFunc {
func GetProxyFunc(proxyURLs string, timeout time.Duration, httpEnabled bool) ProxyFunc {
direct := &net.Dialer{Timeout: timeout}
if proxyURLs == "" {
return proxy.FromEnvironmentUsing(direct).Dial
Expand All @@ -28,11 +30,22 @@ func GetProxyFunc(proxyURLs string, timeout time.Duration) ProxyFunc {
return nil, fmt.Errorf("error building proxy %v: %w", u.String(), err)
}

client, err := proxy.FromURL(u, direct)
if err != nil {
return nil, fmt.Errorf("error building proxy %v: %w", u.String(), err)
switch u.Scheme {
case "socks5", "socks5h":
client, err := proxy.FromURL(u, direct)
if err != nil {
return nil, fmt.Errorf("error building proxy %v: %w", u.String(), err)
}

return client.Dial(network, addr)
case "socks4", "socks4a":
return socks.Dial(u.String())(network, addr)
default:
if httpEnabled {
return fasthttpproxy.FasthttpHTTPDialerTimeout(u.Host, timeout)(addr)
}

return nil, fmt.Errorf("unsupported proxy scheme %v", u.Scheme)
}

return client.Dial(network, addr)
}
}

0 comments on commit 6b9d2a8

Please sign in to comment.