Skip to content

Commit ec6563b

Browse files
small refactoring for future basic auth support
1 parent 0a9a877 commit ec6563b

File tree

5 files changed

+172
-122
lines changed

5 files changed

+172
-122
lines changed

README.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Specify http server in proxy configuration of Postman
4949
- **SOCKS5 Authentication Support**
5050
Supports username/password authentication for SOCKS5 proxies.
5151

52+
- **HTTP Authentication Support**
53+
Supports username/password authentication for HTTP proxy server.
54+
5255
- **Lightweight and Fast**
5356
Designed with minimal overhead and efficient request handling.
5457

@@ -93,32 +96,35 @@ gohpts -h
9396
| |__| | (_) | | | | | | | ____) |
9497
\_____|\___/|_| |_|_| |_| |_____/
9598

96-
GoHPTS (HTTP Proxy to SOCKS5) by shadowy-pycoder
99+
GoHPTS (HTTP(S) Proxy to SOCKS5 proxy) by shadowy-pycoder
97100
GitHub: https://github.com/shadowy-pycoder/go-http-proxy-to-socks
98101

99102
Usage: gohpts [OPTIONS]
100103
Options:
101104
-h Show this help message and exit.
105+
-U string
106+
User for HTTP proxy (basic auth). This flag invokes prompt for password (not echoed to terminal)
102107
-c string
103108
Path to certificate PEM encoded file
104109
-d Show logs in DEBUG mode
105110
-f string
106-
Path to proxychain YAML configuration file
111+
Path to server configuration file in YAML format
107112
-j Show logs in JSON format
108113
-k string
109114
Path to private key PEM encoded file
110-
-l value
111-
Address of HTTP proxy server (Default: localhost:8080)
112-
-p Password for SOCKS5 proxy (not echoed to terminal)
113-
-s value
114-
Address of SOCKS5 proxy server (Default: localhost:1080)
115+
-l string
116+
Address of HTTP proxy server (default "127.0.0.1:8080")
117+
-s string
118+
Address of SOCKS5 proxy server (default "127.0.0.1:1080")
115119
-u string
116-
User for SOCKS5 proxy
120+
User for SOCKS5 proxy authentication. This flag invokes prompt for password (not echoed to terminal)
117121
-v print version
118122
```
119123

120124
## Example
121125

126+
### Configuration via CLI flags
127+
122128
```shell
123129
gohpts -s 1080 -l 8080 -d -j
124130
```
@@ -131,23 +137,34 @@ Output:
131137
{"level":"debug","time":"2025-05-28T06:15:22+00:00","message":"HTTP/1.1 - CONNECT - www.google.com:443"}
132138
```
133139

134-
Specify username and password fo SOCKS5 proxy server:
140+
Specify username and password for SOCKS5 proxy server:
135141

136142
```shell
137-
gohpts -s 1080 -l 8080 -d -j -u user -p
143+
gohpts -s 1080 -l 8080 -d -j -u user
138144
SOCKS5 Password: #you will be prompted for password input here
139145
```
140146

147+
Specify username and password for HTTP proxy server:
148+
149+
```shell
150+
gohpts -s 1080 -l 8080 -d -j -U user
151+
HTTP Password: #you will be prompted for password input here
152+
```
153+
154+
When both `-u` and `-U` are present, you will be prompted twice
155+
141156
Run http proxy over TLS connection
142157

143158
```shell
144159
gohpts -s 1080 -l 8080 -c "path/to/certificate" -k "path/to/private/key"
145160
```
146161

147-
Run http proxy in SOCKS5 proxy chain mode
162+
### Configuration via YAML file
163+
164+
Run http proxy in SOCKS5 proxy chain mode (specify server settings via YAML configuration file)
148165

149166
```shell
150-
gohpts -f "path/to/proxychain/config" -d
167+
gohpts -f "path/to/proxychain/config" -d -j
151168
```
152169

153170
Config example:
@@ -188,6 +205,14 @@ proxy_list:
188205
password: password
189206
- address: 127.0.0.1:1081
190207
- address: :1082 # empty host means localhost
208+
server:
209+
address: 127.0.0.1:8080 # the only required field in this section
210+
# these are for adding basic authentication
211+
username: username
212+
password: password
213+
# comment out these to use HTTP instead of HTTPS
214+
cert_file: ~/local.crt
215+
key_file: ~/local.key
191216
```
192217
193218
To learn more about proxy chains visit [Proxychains Github](https://github.com/rofl0r/proxychains-ng)

cmd/gohpts/cli.go

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"flag"
55
"fmt"
66
"os"
7-
"strconv"
8-
"strings"
97

108
gohpts "github.com/shadowy-pycoder/go-http-proxy-to-socks"
119
"golang.org/x/term"
@@ -24,7 +22,7 @@ const usagePrefix string = `
2422
| |__| | (_) | | | | | | | ____) |
2523
\_____|\___/|_| |_|_| |_| |_____/
2624
27-
GoHPTS (HTTP Proxy to SOCKS5) by shadowy-pycoder
25+
GoHPTS (HTTP(S) Proxy to SOCKS5 proxy) by shadowy-pycoder
2826
GitHub: https://github.com/shadowy-pycoder/go-http-proxy-to-socks
2927
3028
Usage: gohpts [OPTIONS]
@@ -33,48 +31,15 @@ Options:
3331
`
3432

3533
func root(args []string) error {
36-
conf := gohpts.Config{AddrSOCKS: addrSOCKS, AddrHTTP: addrHTTP}
34+
conf := gohpts.Config{}
3735
flags := flag.NewFlagSet(app, flag.ExitOnError)
38-
flags.Func("s", "Address of SOCKS5 proxy server (Default: localhost:1080)", func(flagValue string) error {
39-
var addr string
40-
i, err := strconv.Atoi(flagValue)
41-
if err == nil {
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)
45-
} else {
46-
addr = flagValue
47-
}
48-
conf.AddrSOCKS = addr
49-
return nil
50-
})
51-
flags.StringVar(&conf.User, "u", "", "User for SOCKS5 proxy")
52-
flags.BoolFunc("p", "Password for SOCKS5 proxy (not echoed to terminal)", func(flagValue string) error {
53-
fmt.Print("SOCKS5 Password: ")
54-
bytepw, err := term.ReadPassword(int(os.Stdin.Fd()))
55-
if err != nil {
56-
os.Exit(1)
57-
}
58-
conf.Pass = string(bytepw)
59-
fmt.Print("\033[2K\r")
60-
return nil
61-
})
62-
flags.Func("l", "Address of HTTP proxy server (Default: localhost:8080)", func(flagValue string) error {
63-
var addr string
64-
i, err := strconv.Atoi(flagValue)
65-
if err == nil {
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)
69-
} else {
70-
addr = flagValue
71-
}
72-
conf.AddrHTTP = addr
73-
return nil
74-
})
75-
flags.StringVar(&conf.CertFile, "c", "", "Path to certificate PEM encoded file ")
76-
flags.StringVar(&conf.KeyFile, "k", "", "Path to private key PEM encoded file ")
77-
flags.StringVar(&conf.ProxyChainPath, "f", "", "Path to proxychain YAML configuration file")
36+
flags.StringVar(&conf.AddrSOCKS, "s", addrSOCKS, "Address of SOCKS5 proxy server")
37+
flags.StringVar(&conf.User, "u", "", "User for SOCKS5 proxy authentication. This flag invokes prompt for password (not echoed to terminal)")
38+
flags.StringVar(&conf.AddrHTTP, "l", addrHTTP, "Address of HTTP proxy server")
39+
flags.StringVar(&conf.ServerUser, "U", "", "User for HTTP proxy (basic auth). This flag invokes prompt for password (not echoed to terminal)")
40+
flags.StringVar(&conf.CertFile, "c", "", "Path to certificate PEM encoded file")
41+
flags.StringVar(&conf.KeyFile, "k", "", "Path to private key PEM encoded file")
42+
flags.StringVar(&conf.ServerConfPath, "f", "", "Path to server configuration file in YAML format")
7843
flags.BoolFunc("d", "Show logs in DEBUG mode", func(flagValue string) error {
7944
conf.Debug = true
8045
return nil
@@ -100,12 +65,30 @@ func root(args []string) error {
10065
seen := make(map[string]bool)
10166
flags.Visit(func(f *flag.Flag) { seen[f.Name] = true })
10267
if seen["f"] {
103-
for _, da := range []string{"s", "u", "p"} {
68+
for _, da := range []string{"s", "u", "U", "c", "k", "l"} {
10469
if seen[da] {
105-
return fmt.Errorf("specify either -f or -s -u -p flags")
70+
return fmt.Errorf("-f flag only works with -d and -j flags")
10671
}
10772
}
10873
}
74+
if seen["u"] {
75+
fmt.Print("SOCKS5 Password: ")
76+
bytepw, err := term.ReadPassword(int(os.Stdin.Fd()))
77+
if err != nil {
78+
return err
79+
}
80+
conf.Pass = string(bytepw)
81+
fmt.Print("\033[2K\r")
82+
}
83+
if seen["U"] {
84+
fmt.Print("HTTP Password: ")
85+
bytepw, err := term.ReadPassword(int(os.Stdin.Fd()))
86+
if err != nil {
87+
return err
88+
}
89+
conf.ServerPass = string(bytepw)
90+
fmt.Print("\033[2K\r")
91+
}
10992
p := gohpts.New(&conf)
11093
p.Run()
11194
return nil

0 commit comments

Comments
 (0)