Skip to content

Commit 0a9a877

Browse files
added handling of plain ports in config
1 parent 62dbe7d commit 0a9a877

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

cmd/gohpts/cli.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import (
55
"fmt"
66
"os"
77
"strconv"
8+
"strings"
89

910
gohpts "github.com/shadowy-pycoder/go-http-proxy-to-socks"
1011
"golang.org/x/term"
1112
)
1213

1314
const (
1415
app string = "gohpts"
15-
addrSOCKS = ":1080"
16-
addrHTTP = ":8080"
16+
addrSOCKS = "127.0.0.1:1080"
17+
addrHTTP = "127.0.0.1:8080"
1718
)
1819
const usagePrefix string = `
1920
_____ _ _ _____ _______ _____
@@ -35,12 +36,16 @@ func root(args []string) error {
3536
conf := gohpts.Config{AddrSOCKS: addrSOCKS, AddrHTTP: addrHTTP}
3637
flags := flag.NewFlagSet(app, flag.ExitOnError)
3738
flags.Func("s", "Address of SOCKS5 proxy server (Default: localhost:1080)", func(flagValue string) error {
39+
var addr string
3840
i, err := strconv.Atoi(flagValue)
3941
if err == nil {
40-
conf.AddrSOCKS = fmt.Sprintf(":%d", i)
42+
addr = fmt.Sprintf("127.0.0.1:%d", i)
43+
} else if strings.HasPrefix(flagValue, ":") {
44+
addr = fmt.Sprintf("127.0.0.1%s", flagValue)
4145
} else {
42-
conf.AddrSOCKS = flagValue
46+
addr = flagValue
4347
}
48+
conf.AddrSOCKS = addr
4449
return nil
4550
})
4651
flags.StringVar(&conf.User, "u", "", "User for SOCKS5 proxy")
@@ -55,12 +60,16 @@ func root(args []string) error {
5560
return nil
5661
})
5762
flags.Func("l", "Address of HTTP proxy server (Default: localhost:8080)", func(flagValue string) error {
63+
var addr string
5864
i, err := strconv.Atoi(flagValue)
5965
if err == nil {
60-
conf.AddrHTTP = fmt.Sprintf(":%d", i)
66+
addr = fmt.Sprintf("127.0.0.1:%d", i)
67+
} else if strings.HasPrefix(flagValue, ":") {
68+
addr = fmt.Sprintf("127.0.0.1%s", flagValue)
6169
} else {
62-
conf.AddrHTTP = flagValue
70+
addr = flagValue
6371
}
72+
conf.AddrHTTP = addr
6473
return nil
6574
})
6675
flags.StringVar(&conf.CertFile, "c", "", "Path to certificate PEM encoded file ")

gohpts.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os/signal"
1515
"path/filepath"
1616
"slices"
17+
"strconv"
1718
"strings"
1819
"sync"
1920
"sync/atomic"
@@ -275,7 +276,7 @@ func (p *proxyApp) getSocks() (proxy.Dialer, *http.Client, error) {
275276
return http.ErrUseLastResponse
276277
},
277278
}
278-
p.logger.Debug().Msgf("[%s] Current chain: %s", chainType, p.printProxyChain(copyProxyList))
279+
p.logger.Debug().Msgf("[%s] Request chain: %s", chainType, p.printProxyChain(copyProxyList))
279280
return dialer, socks, nil
280281
}
281282

@@ -635,15 +636,19 @@ func New(conf *Config) *proxyApp {
635636
p.logger.Fatal().Msg("[proxychain config] Proxy list is empty")
636637
}
637638
seen := make(map[string]struct{})
638-
for _, pr := range p.proxychain.ProxyList {
639+
for idx, pr := range p.proxychain.ProxyList {
639640
var addr string
640-
if strings.HasPrefix(pr.Address, ":") {
641+
i, err := strconv.Atoi(pr.Address)
642+
if err == nil {
643+
addr = fmt.Sprintf("127.0.0.1:%d", i)
644+
} else if strings.HasPrefix(pr.Address, ":") {
641645
addr = fmt.Sprintf("127.0.0.1%s", pr.Address)
642646
} else {
643647
addr = pr.Address
644648
}
645649
if _, ok := seen[addr]; !ok {
646650
seen[addr] = struct{}{}
651+
p.proxychain.ProxyList[idx].Address = addr
647652
} else {
648653
p.logger.Fatal().Msgf("[proxychain config] Duplicate entry `%s`", addr)
649654
}
@@ -694,9 +699,6 @@ func New(conf *Config) *proxyApp {
694699
hs.Protocols.SetHTTP1(true)
695700
p.httpServer = hs
696701
p.httpServerAddr = conf.AddrHTTP
697-
if strings.HasPrefix(p.httpServerAddr, ":") {
698-
p.httpServerAddr = fmt.Sprintf("127.0.0.1%s", p.httpServerAddr)
699-
}
700702
hc := &http.Client{
701703
Transport: &http.Transport{
702704
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -707,7 +709,7 @@ func New(conf *Config) *proxyApp {
707709
}
708710
p.httpClient = hc
709711
if p.proxychain != nil {
710-
p.logger.Info().Msgf("SOCKS5 proxy chain [%s]: %s", p.proxychain.Chain.Type, p.printProxyChain(p.proxychain.ProxyList))
712+
p.logger.Info().Msgf("SOCKS5 Proxy [%s] chain: %s", p.proxychain.Chain.Type, p.printProxyChain(p.proxychain.ProxyList))
711713
} else {
712714
p.logger.Info().Msgf("SOCKS5 Proxy: %s", conf.AddrSOCKS)
713715
}

0 commit comments

Comments
 (0)