Skip to content

Commit 8ea68ea

Browse files
Added ports ignoring
1 parent afe5bef commit 8ea68ea

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ You can download the binary for your platform from [Releases](https://github.com
107107
Example:
108108

109109
```shell
110-
GOHPTS_RELEASE=v1.10.5; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$GOHPTS_RELEASE/gohpts-$GOHPTS_RELEASE-linux-amd64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$GOHPTS_RELEASE-linux-amd64 gohpts && ./gohpts -h
110+
GOHPTS_RELEASE=v1.10.6; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$GOHPTS_RELEASE/gohpts-$GOHPTS_RELEASE-linux-amd64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$GOHPTS_RELEASE-linux-amd64 gohpts && ./gohpts -h
111111
```
112112

113113
Alternatively, you can install it using `go install` command (requires Go [1.24](https://go.dev/doc/install) or later):
@@ -180,6 +180,7 @@ Options:
180180
-auto Automatically setup iptables for transparent proxy (requires elevated privileges)
181181
-arpspoof Enable ARP spoof proxy for selected targets (Example: "targets 10.0.0.1,10.0.0.5-10,192.168.1.*,192.168.10.0/24;fullduplex false;debug true")
182182
-mark Set mark for each packet sent through transparent proxy (Default: redirect 0, tproxy 100)
183+
-P Comma separated list of ports to ignore when proxying traffic (Example: "22,80,443,9092")
183184
```
184185
185186
### Configuration via CLI flags

cmd/gohpts/cli.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const usageTproxy string = `
6767
-auto Automatically setup iptables for transparent proxy (requires elevated privileges)
6868
-arpspoof Enable ARP spoof proxy for selected targets (Example: "targets 10.0.0.1,10.0.0.5-10,192.168.1.*,192.168.10.0/24;fullduplex false;debug true")
6969
-mark Set mark for each packet sent through transparent proxy (Default: redirect 0, tproxy 100)
70+
-P Comma separated list of ports to ignore when proxying traffic (Example: "22,80,443,9092")
7071
`
7172

7273
func root(args []string) error {
@@ -134,6 +135,12 @@ func root(args []string) error {
134135
"",
135136
"Enable ARP spoof proxy for selected targets",
136137
)
138+
flags.StringVar(
139+
&conf.IgnoredPorts,
140+
"P",
141+
"",
142+
`Comma separated list of ports to ignore when proxying traffic (Example: "22,80,443,9092")`,
143+
)
137144
}
138145
flags.StringVar(&conf.LogFilePath, "logfile", "", "Log file path (Default: stdout)")
139146
flags.BoolVar(&conf.Debug, "d", false, "Show logs in DEBUG mode")
@@ -202,6 +209,11 @@ func root(args []string) error {
202209
return fmt.Errorf("-mark requires -t, -T or -Tu flag")
203210
}
204211
}
212+
if seen["P"] {
213+
if !seen["auto"] {
214+
return fmt.Errorf("-P requires -auto flag")
215+
}
216+
}
205217
if seen["f"] {
206218
for _, da := range []string{"s", "u", "U", "c", "k", "l", "i"} {
207219
if seen[da] {

colorize.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ var (
3030
credsPattern = regexp.MustCompile(
3131
`(?i)(?:"|')?(username|user|login|email|password|pass|pwd)(?:"|')?\s*[:=]\s*(?:"|')?([^\s"'&]+)`,
3232
)
33-
macPattern = regexp.MustCompile(`(?i)([a-z0-9_]+_[0-9a-f]{2}(?::[0-9a-f]{2}){2}|(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})`)
33+
macPattern = regexp.MustCompile(`(?i)([a-z0-9_]+_[0-9a-f]{2}(?::[0-9a-f]{2}){2}|(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})`)
34+
portsPattern = regexp.MustCompile(
35+
`^\s*(?:6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{0,4}|[1-9]\d{0,3})\s*(?:,\s*(?:6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{0,4}|[1-9]\d{0,3})\s*)*$`,
36+
)
3437
)
3538

3639
var rColors = []func(string) *colors.Color{

gohpts.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type Config struct {
7575
Auto bool
7676
Mark uint
7777
ARPSpoof string
78+
IgnoredPorts string
7879
LogFilePath string
7980
Debug bool
8081
JSON bool
@@ -147,6 +148,7 @@ type proxyapp struct {
147148
auto bool
148149
mark uint
149150
arpspoofer *arpspoof.ARPSpoofer
151+
ignoredPorts string
150152
user string
151153
pass string
152154
proxychain chain
@@ -315,6 +317,15 @@ func New(conf *Config) *proxyapp {
315317
if p.mark == 0 && p.tproxyMode == "tproxy" {
316318
p.mark = 100
317319
}
320+
if conf.IgnoredPorts != "" {
321+
if !p.auto {
322+
p.logger.Fatal().Msg("Ignoring ports is only possible in auto configuration")
323+
}
324+
if !portsPattern.MatchString(conf.IgnoredPorts) {
325+
p.logger.Fatal().Msg("Ignored ports must be a comma separated list of port numbers")
326+
}
327+
p.ignoredPorts = conf.IgnoredPorts
328+
}
318329
var addrHTTP, addrSOCKS, certFile, keyFile string
319330
if conf.ServerConfPath != "" {
320331
var sconf serverConfig

tproxy_linux.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ func (ts *tproxyServer) ApplyRedirectRules(opts map[string]string) {
276276
if err := cmdInit.Run(); err != nil {
277277
ts.p.logger.Fatal().Err(err).Msgf("[tcp %s] Failed while configuring iptables. Are you root?", ts.p.tproxyMode)
278278
}
279+
if ts.p.ignoredPorts != "" {
280+
cmdInit1 := exec.Command("bash", "-c", fmt.Sprintf(`
281+
%s
282+
iptables -t nat -A GOHPTS -p tcp -m multiport --dports %s -j RETURN
283+
iptables -t nat -A GOHPTS -p tcp -m multiport --sports %s -j RETURN
284+
`, setex, ts.p.ignoredPorts, ts.p.ignoredPorts))
285+
cmdInit1.Stdout = os.Stdout
286+
cmdInit1.Stderr = os.Stderr
287+
if err := cmdInit1.Run(); err != nil {
288+
ts.p.logger.Fatal().Err(err).Msgf("[tcp %s] Failed while configuring iptables. Are you root?", ts.p.tproxyMode)
289+
}
290+
}
279291
if ts.p.httpServerAddr != "" {
280292
_, httpPort, _ := net.SplitHostPort(ts.p.httpServerAddr)
281293
cmdHTTP := exec.Command("bash", "-c", fmt.Sprintf(`
@@ -375,6 +387,18 @@ func (ts *tproxyServer) ApplyRedirectRules(opts map[string]string) {
375387
if err := cmdInit0.Run(); err != nil {
376388
ts.p.logger.Fatal().Err(err).Msgf("[tcp %s] Failed while configuring iptables. Are you root?", ts.p.tproxyMode)
377389
}
390+
if ts.p.ignoredPorts != "" {
391+
cmdInit1 := exec.Command("bash", "-c", fmt.Sprintf(`
392+
%s
393+
iptables -t mangle -A GOHPTS -p tcp -m multiport --dports %s -j RETURN
394+
iptables -t mangle -A GOHPTS -p tcp -m multiport --sports %s -j RETURN
395+
`, setex, ts.p.ignoredPorts, ts.p.ignoredPorts))
396+
cmdInit1.Stdout = os.Stdout
397+
cmdInit1.Stderr = os.Stderr
398+
if err := cmdInit1.Run(); err != nil {
399+
ts.p.logger.Fatal().Err(err).Msgf("[tcp %s] Failed while configuring iptables. Are you root?", ts.p.tproxyMode)
400+
}
401+
}
378402
cmdDocker := exec.Command("bash", "-c", fmt.Sprintf(`
379403
%s
380404
if command -v docker >/dev/null 2>&1

tproxy_udp_linux.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,18 @@ func (tsu *tproxyServerUDP) ApplyRedirectRules(opts map[string]string) {
693693
if err := cmdInit0.Run(); err != nil {
694694
tsu.p.logger.Fatal().Err(err).Msgf("[udp %s] Failed while configuring iptables. Are you root?", tsu.p.tproxyMode)
695695
}
696+
if tsu.p.ignoredPorts != "" {
697+
cmdInit1 := exec.Command("bash", "-c", fmt.Sprintf(`
698+
%s
699+
iptables -t mangle -A GOHPTS_UDP -p udp -m multiport --dports %s -j RETURN
700+
iptables -t mangle -A GOHPTS_UDP -p udp -m multiport --sports %s -j RETURN
701+
`, setex, tsu.p.ignoredPorts, tsu.p.ignoredPorts))
702+
cmdInit1.Stdout = os.Stdout
703+
cmdInit1.Stderr = os.Stderr
704+
if err := cmdInit1.Run(); err != nil {
705+
tsu.p.logger.Fatal().Err(err).Msgf("[tcp %s] Failed while configuring iptables. Are you root?", tsu.p.tproxyMode)
706+
}
707+
}
696708
cmdDocker := exec.Command("bash", "-c", fmt.Sprintf(`
697709
%s
698710
if command -v docker >/dev/null 2>&1

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package gohpts
22

3-
const Version string = "gohpts v1.10.5"
3+
const Version string = "gohpts v1.10.6"

0 commit comments

Comments
 (0)