Skip to content

Commit

Permalink
restructure proxy related code
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed May 8, 2022
1 parent 060ae19 commit 02664fe
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 54 deletions.
11 changes: 2 additions & 9 deletions src/core/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ type ClientConfig struct {
WriteTimeout *time.Duration
IdleTimeout *time.Duration
MaxIdleConns *int
ProxyURLs string
LocalAddr string
Interface string
Proxy *utils.ProxyParams
}

// NewClient creates a fasthttp client based on the config.
Expand All @@ -115,12 +113,7 @@ func NewClient(ctx context.Context, clientConfig ClientConfig, logger *zap.Logge
tlsConfig := utils.NonNilOrDefault(clientConfig.TLSClientConfig, tls.Config{
InsecureSkipVerify: true, //nolint:gosec // This is intentional
})
proxyFunc := utils.GetProxyFunc(utils.ProxyParams{
URLs: clientConfig.ProxyURLs,
LocalAddr: utils.ResolveAddr("tcp", clientConfig.LocalAddr),
Timeout: timeout,
Interface: clientConfig.Interface,
}, true)
proxyFunc := utils.GetProxyFunc(*clientConfig.Proxy, "http")

if clientConfig.StaticHost != nil {
makeHostClient := func(tls bool) *fasthttp.HostClient {
Expand Down
21 changes: 7 additions & 14 deletions src/core/packetgen/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import (

// ConnectionConfig describes which network to use when sending packets
type ConnectionConfig struct {
Type string
Args map[string]any
Type string
Args map[string]any
Proxy *utils.ProxyParams
}

func OpenConnection(ctx context.Context, c ConnectionConfig) (Connection, error) {
Expand All @@ -56,7 +57,7 @@ func OpenConnection(ctx context.Context, c ConnectionConfig) (Connection, error)
return nil, fmt.Errorf("error decoding connection config: %w", err)
}

return openNetConn(ctx, cfg)
return openNetConn(ctx, cfg, c.Proxy)
default:
return nil, fmt.Errorf("unknown connection type: %v", c.Type)
}
Expand Down Expand Up @@ -114,9 +115,7 @@ type netConnConfig struct {
Protocol string
Address string
Timeout time.Duration
ProxyURLs string
LocalAddr string
Interface string
Proxy utils.ProxyParams
Reader *netReaderConfig
TLSClientConfig *tls.Config
}
Expand Down Expand Up @@ -150,14 +149,8 @@ func readStub(ctx context.Context, conn net.Conn, c *netReaderConfig) {
}
}

func openNetConn(ctx context.Context, c netConnConfig) (*netConn, error) {
proxyParams := utils.ProxyParams{
URLs: c.ProxyURLs,
LocalAddr: utils.ResolveAddr(c.Protocol, c.LocalAddr),
Interface: c.Interface,
Timeout: c.Timeout,
}
conn, err := utils.GetProxyFunc(proxyParams, false)(c.Protocol, c.Address)
func openNetConn(ctx context.Context, c netConnConfig, proxyParams *utils.ProxyParams) (*netConn, error) {
conn, err := utils.GetProxyFunc(utils.NonNilOrDefault(proxyParams, utils.ProxyParams{}), c.Protocol)(c.Protocol, c.Address)

switch {
case err != nil:
Expand Down
2 changes: 1 addition & 1 deletion src/job/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewGlobalConfigWithFlags() *GlobalConfig {
func (g GlobalConfig) GetProxyParams(logger *zap.Logger, data any) utils.ProxyParams {
return utils.ProxyParams{
URLs: templates.ParseAndExecute(logger, g.ProxyURLs, data),
LocalAddr: utils.ResolveAddr("tcp", templates.ParseAndExecute(logger, g.LocalAddr, data)),
LocalAddr: templates.ParseAndExecute(logger, g.LocalAddr, data),
Interface: templates.ParseAndExecute(logger, g.Interface, data),
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/job/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,8 @@ func getHTTPJobConfigs(ctx context.Context, args config.Args, global GlobalConfi
return nil, nil, nil, fmt.Errorf("error parsing client config: %w", err)
}

if global.ProxyURLs != "" {
clientConfig.ProxyURLs = templates.ParseAndExecute(logger, global.ProxyURLs, ctx)
}

if global.LocalAddr != "" {
clientConfig.LocalAddr = templates.ParseAndExecute(logger, global.LocalAddr, ctx)
}

if global.Interface != "" {
clientConfig.Interface = templates.ParseAndExecute(logger, global.Interface, ctx)
}
proxyCfg := utils.NonNilOrDefault(clientConfig.Proxy, global.GetProxyParams(logger, ctx))
clientConfig.Proxy = &proxyCfg

requestTpl, err = templates.ParseMapStruct(jobConfig.Request)
if err != nil {
Expand Down
13 changes: 2 additions & 11 deletions src/job/packetgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,8 @@ func parsePacketgenArgs(ctx context.Context, args config.Args, globalConfig *Glo
return nil, err
}

if globalConfig.ProxyURLs != "" && jobConfig.Connection.Args["protocol"] == "tcp" {
jobConfig.Connection.Args["proxy_urls"] = templates.ParseAndExecute(logger, globalConfig.ProxyURLs, ctx)
}

if globalConfig.LocalAddr != "" {
jobConfig.Connection.Args["local_addr"] = templates.ParseAndExecute(logger, globalConfig.LocalAddr, ctx)
}

if globalConfig.Interface != "" {
jobConfig.Connection.Args["interface"] = templates.ParseAndExecute(logger, globalConfig.Interface, ctx)
}
proxyCfg := utils.NonNilOrDefault(jobConfig.Connection.Proxy, globalConfig.GetProxyParams(logger, ctx))
jobConfig.Connection.Proxy = &proxyCfg

return &packetgenJobConfig{
BasicJobConfig: jobConfig.BasicJobConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/countrychecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func fetchLocationInfo(logger *zap.Logger, proxyParams ProxyParams) (country, ip
requestTimeout = 3 * time.Second
)

proxyFunc := GetProxyFunc(proxyParams, true)
proxyFunc := GetProxyFunc(proxyParams, "http")

client := &fasthttp.Client{
MaxConnDuration: requestTimeout,
Expand Down
14 changes: 7 additions & 7 deletions src/utils/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type ProxyFunc func(network, addr string) (net.Conn, error)

type ProxyParams struct {
URLs string
LocalAddr net.Addr
LocalAddr string
Interface string
Timeout time.Duration
}

func GetProxyFunc(params ProxyParams, httpEnabled bool) ProxyFunc {
direct := &net.Dialer{Timeout: params.Timeout, LocalAddr: params.LocalAddr, Control: BindToInterface(params.Interface)}
func GetProxyFunc(params ProxyParams, protocol string) ProxyFunc {
direct := &net.Dialer{Timeout: params.Timeout, LocalAddr: resolveAddr(protocol, params.LocalAddr), Control: BindToInterface(params.Interface)}
if params.URLs == "" {
return proxy.FromEnvironmentUsing(direct).Dial
}
Expand All @@ -48,7 +48,7 @@ func GetProxyFunc(params ProxyParams, httpEnabled bool) ProxyFunc {
case "socks4", "socks4a":
return socks.Dial(u.String())(network, addr)
default:
if httpEnabled {
if protocol == "http" {
return fasthttpproxy.FasthttpHTTPDialerTimeout(u.Host, params.Timeout)(addr)
}

Expand All @@ -57,7 +57,7 @@ func GetProxyFunc(params ProxyParams, httpEnabled bool) ProxyFunc {
}
}

func ResolveAddr(network, addr string) net.Addr {
func resolveAddr(protocol, addr string) net.Addr {
if addr == "" {
return nil
}
Expand All @@ -72,8 +72,8 @@ func ResolveAddr(network, addr string) net.Addr {

ip := net.ParseIP(addr)

switch network {
case "tcp", "tcp4", "tcp6":
switch protocol {
case "tcp", "tcp4", "tcp6", "http":
return &net.TCPAddr{IP: ip, Zone: zone}
case "udp", "udp4", "udp6":
return &net.UDPAddr{IP: ip, Zone: zone}
Expand Down

0 comments on commit 02664fe

Please sign in to comment.