Skip to content

Commit b29119d

Browse files
Merge pull request #10 from shadowy-pycoder/tproxyauto
added auto configuration for tproxy mode, categorized cli flags
2 parents 1a85b27 + d4f521e commit b29119d

File tree

5 files changed

+338
-172
lines changed

5 files changed

+338
-172
lines changed

README.md

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ You can download the binary for your platform from [Releases](https://github.com
9797
Example:
9898

9999
```shell
100-
HPTS_RELEASE=v1.8.2; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$HPTS_RELEASE/gohpts-$HPTS_RELEASE-linux-amd64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$HPTS_RELEASE-linux-amd64 gohpts && ./gohpts -h
100+
HPTS_RELEASE=v1.8.3; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$HPTS_RELEASE/gohpts-$HPTS_RELEASE-linux-amd64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$HPTS_RELEASE-linux-amd64 gohpts && ./gohpts -h
101101
```
102102

103103
Alternatively, you can install it using `go install` command (requires Go [1.24](https://go.dev/doc/install) or later):
@@ -135,45 +135,36 @@ GitHub: https://github.com/shadowy-pycoder/go-http-proxy-to-socks
135135

136136
Usage: gohpts [OPTIONS]
137137
Options:
138-
-h Show this help message and exit.
139-
-D Run as a daemon (provide -logfile to see logs)
140-
-M value
141-
Transparent proxy mode: [redirect tproxy]
142-
-T string
143-
Address of transparent proxy server (no HTTP)
144-
-U string
145-
User for HTTP proxy (basic auth). This flag invokes prompt for password (not echoed to terminal)
146-
-auto
147-
Automatically setup iptables for transparent proxy (requires elevated privileges)
148-
-body
149-
Collect request and response body for HTTP sniffing
150-
-c string
151-
Path to certificate PEM encoded file
152-
-d Show logs in DEBUG mode
153-
-f string
154-
Path to server configuration file in YAML format
155-
-j Show logs in JSON format
156-
-k string
157-
Path to private key PEM encoded file
158-
-l string
159-
Address of HTTP proxy server (default "127.0.0.1:8080")
160-
-logfile string
161-
Log file path (Default: stdout)
162-
-mark uint
163-
Set the mark for each packet sent through transparent proxy
164-
-nocolor
165-
Disable colored output for logs (no effect if -j flag specified)
166-
-s string
167-
Address of SOCKS5 proxy server (default "127.0.0.1:1080")
168-
-sniff
169-
Enable traffic sniffing for HTTP and TLS
170-
-snifflog string
171-
Sniffed traffic log file path (Default: the same as -logfile)
172-
-t string
173-
Address of transparent proxy server (it starts along with HTTP proxy server)
174-
-u string
175-
User for SOCKS5 proxy authentication. This flag invokes prompt for password (not echoed to terminal)
176-
-v print version
138+
-h Show this help message and exit
139+
-v Show version and build information
140+
-D Run as a daemon (provide -logfile to see logs)
141+
142+
Proxy:
143+
-l Address of HTTP proxy server (default "127.0.0.1:8080")
144+
-s Address of SOCKS5 proxy server (default "127.0.0.1:1080")
145+
-c Path to certificate PEM encoded file
146+
-k Path to private key PEM encoded file
147+
-U User for HTTP proxy (basic auth). This flag invokes prompt for password (not echoed to terminal)
148+
-u User for SOCKS5 proxy authentication. This flag invokes prompt for password (not echoed to terminal)
149+
-f Path to server configuration file in YAML format (overrides other proxy flags)
150+
151+
Logs:
152+
-d Show logs in DEBUG mode
153+
-j Show logs in JSON format
154+
-logfile Log file path (Default: stdout)
155+
-nocolor Disable colored output for logs (no effect if -j flag specified)
156+
157+
Sniffing:
158+
-sniff Enable traffic sniffing for HTTP and TLS
159+
-snifflog Sniffed traffic log file path (Default: the same as -logfile)
160+
-body Collect request and response body for HTTP traffic (credentials, tokens, etc)
161+
162+
TProxy:
163+
-t Address of transparent proxy server (it starts along with HTTP proxy server)
164+
-T Address of transparent proxy server (no HTTP)
165+
-M Transparent proxy mode: (redirect, tproxy)
166+
-auto Automatically setup iptables for transparent proxy (requires elevated privileges)
167+
-mark Set mark for each packet sent through transparent proxy (Default: redirect 0, tproxy 100)
177168
```
178169
179170
### Configuration via CLI flags
@@ -450,6 +441,44 @@ ip netns del ns-client
450441
ip link del veth1
451442
```
452443
444+
### Auto configuration for `tproxy` mode
445+
446+
To configure your system automatically, run the following command (for example, on a separate VM):
447+
448+
```shell
449+
ssh remote -D 1080 -Nf
450+
sudo env PATH=$PATH gohpts -d -T 8888 -M tproxy -auto -mark 100
451+
```
452+
453+
Run the following on your host:
454+
455+
```shell
456+
ip route show default > /tmp/default-route.txt
457+
458+
ip route add 0.0.0.0/1 via 192.168.0.1 # change with ip of your VM
459+
ip route add 128.0.0.0/1 via 192.168.0.1
460+
```
461+
462+
Test connection:
463+
464+
```shell
465+
curl http://example.com #check logs on your VM
466+
```
467+
468+
Undo everything:
469+
470+
```shell
471+
ip route del 0.0.0.0/1 via 192.168.0.1 2>/dev/null || true
472+
ip route del 128.0.0.0/1 via 192.168.0.1 2>/dev/null || true
473+
474+
if [[ -f /tmp/default-route.txt ]]; then
475+
eval $(awk '{print "ip route add "$0}' /tmp/default-route.txt)
476+
rm -f /tmp/default-route.txt
477+
else
478+
echo "Something went wrong"
479+
fi
480+
```
481+
453482
## Traffic sniffing
454483
455484
[[Back]](#table-of-contents)

cmd/gohpts/cli.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,37 @@ GitHub: https://github.com/shadowy-pycoder/go-http-proxy-to-socks
2929
3030
Usage: gohpts [OPTIONS]
3131
Options:
32-
-h Show this help message and exit.
32+
-h Show this help message and exit
33+
-v Show version and build information
34+
-D Run as a daemon (provide -logfile to see logs)
35+
36+
Proxy:
37+
-l Address of HTTP proxy server (default "127.0.0.1:8080")
38+
-s Address of SOCKS5 proxy server (default "127.0.0.1:1080")
39+
-c Path to certificate PEM encoded file
40+
-k Path to private key PEM encoded file
41+
-U User for HTTP proxy (basic auth). This flag invokes prompt for password (not echoed to terminal)
42+
-u User for SOCKS5 proxy authentication. This flag invokes prompt for password (not echoed to terminal)
43+
-f Path to server configuration file in YAML format (overrides other proxy flags)
44+
45+
Logs:
46+
-d Show logs in DEBUG mode
47+
-j Show logs in JSON format
48+
-logfile Log file path (Default: stdout)
49+
-nocolor Disable colored output for logs (no effect if -j flag specified)
50+
51+
Sniffing:
52+
-sniff Enable traffic sniffing for HTTP and TLS
53+
-snifflog Sniffed traffic log file path (Default: the same as -logfile)
54+
-body Collect request and response body for HTTP traffic (credentials, tokens, etc)
55+
`
56+
const usageTproxy string = `
57+
TProxy:
58+
-t Address of transparent proxy server (it starts along with HTTP proxy server)
59+
-T Address of transparent proxy server (no HTTP)
60+
-M Transparent proxy mode: (redirect, tproxy)
61+
-auto Automatically setup iptables for transparent proxy (requires elevated privileges)
62+
-mark Set mark for each packet sent through transparent proxy (Default: redirect 0, tproxy 100)
3363
`
3464

3565
func root(args []string) error {
@@ -41,7 +71,7 @@ func root(args []string) error {
4171
flags.StringVar(&conf.ServerUser, "U", "", "User for HTTP proxy (basic auth). This flag invokes prompt for password (not echoed to terminal)")
4272
flags.StringVar(&conf.CertFile, "c", "", "Path to certificate PEM encoded file")
4373
flags.StringVar(&conf.KeyFile, "k", "", "Path to private key PEM encoded file")
44-
flags.StringVar(&conf.ServerConfPath, "f", "", "Path to server configuration file in YAML format")
74+
flags.StringVar(&conf.ServerConfPath, "f", "", "Path to server configuration file in YAML format (overrides other proxy flags)")
4575
daemon := flags.Bool("D", false, "Run as a daemon (provide -logfile to see logs)")
4676
if runtime.GOOS == tproxyOS {
4777
flags.StringVar(&conf.TProxy, "t", "", "Address of transparent proxy server (it starts along with HTTP proxy server)")
@@ -55,24 +85,26 @@ func root(args []string) error {
5585
return nil
5686
})
5787
flags.BoolVar(&conf.Auto, "auto", false, "Automatically setup iptables for transparent proxy (requires elevated privileges)")
58-
flags.UintVar(&conf.Mark, "mark", 0, "Set the mark for each packet sent through transparent proxy")
88+
flags.UintVar(&conf.Mark, "mark", 0, "Set mark for each packet sent through transparent proxy (Default: redirect 0, tproxy 100)")
5989
}
6090
flags.StringVar(&conf.LogFilePath, "logfile", "", "Log file path (Default: stdout)")
6191
flags.BoolVar(&conf.Debug, "d", false, "Show logs in DEBUG mode")
6292
flags.BoolVar(&conf.Json, "j", false, "Show logs in JSON format")
6393
flags.BoolVar(&conf.Sniff, "sniff", false, "Enable traffic sniffing for HTTP and TLS")
6494
flags.StringVar(&conf.SniffLogFile, "snifflog", "", "Sniffed traffic log file path (Default: the same as -logfile)")
6595
flags.BoolVar(&conf.NoColor, "nocolor", false, "Disable colored output for logs (no effect if -j flag specified)")
66-
flags.BoolVar(&conf.Body, "body", false, "Collect request and response body for HTTP sniffing")
67-
flags.BoolFunc("v", "print version", func(flagValue string) error {
96+
flags.BoolVar(&conf.Body, "body", false, "Collect request and response body for HTTP traffic (credentials, tokens, etc)")
97+
flags.BoolFunc("v", "Show version and build information", func(flagValue string) error {
6898
fmt.Printf("%s (built for %s %s with %s)\n", gohpts.Version, runtime.GOOS, runtime.GOARCH, runtime.Version())
6999
os.Exit(0)
70100
return nil
71101
})
72102

73103
flags.Usage = func() {
74104
fmt.Print(usagePrefix)
75-
flags.PrintDefaults()
105+
if runtime.GOOS == tproxyOS {
106+
fmt.Print(usageTproxy)
107+
}
76108
}
77109

78110
if err := flags.Parse(args); err != nil {
@@ -107,9 +139,6 @@ func root(args []string) error {
107139
if !seen["t"] && !seen["T"] {
108140
return fmt.Errorf("-auto requires -t or -T flag")
109141
}
110-
if conf.TProxyMode != "redirect" {
111-
return fmt.Errorf("-auto is available only for -M redirect")
112-
}
113142
}
114143
if seen["mark"] {
115144
if !seen["t"] && !seen["T"] {

0 commit comments

Comments
 (0)