-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.go
More file actions
75 lines (63 loc) · 1.78 KB
/
args.go
File metadata and controls
75 lines (63 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"net"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/ljwun/forward/http"
"github.com/ljwun/forward/socks5"
)
type Args struct {
Global GlobalConfig
HttpProxy http.ServerConfig
Socks5 socks5.ServerConfig
}
type GlobalConfig struct {
Address string
Debug bool
}
func ParseArgs() (*Args, error) {
// global config
pflag.IP("global-server-address", net.IPv4(0, 0, 0, 0), "")
pflag.Bool("global-debug", false, "")
// http proxy config
pflag.Int("http-proxy-port", 8080, "")
pflag.String("http-proxy-auth-username", "anonymous", "")
pflag.String("http-proxy-auth-password", "anonymous", "")
// socks5 proxy config
pflag.Int("socks5-proxy-port", 1080, "")
pflag.String("socks5-proxy-auth-username", "anonymous", "")
pflag.String("socks5-proxy-auth-password", "anonymous", "")
// bind pflag to viper
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
viper.AutomaticEnv()
viper.SetEnvPrefix("FORWARD")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// parse global config
global := GlobalConfig{
Address: viper.GetString("server-address"),
Debug: viper.GetBool("debug"),
}
return &Args{
Global: global,
HttpProxy: http.ServerConfig{
Debug: global.Debug,
Address: global.Address,
Port: viper.GetInt("http-proxy-port"),
AuthConfig: http.ProxyAuthConfig{
Username: viper.GetString("http-proxy-auth-username"),
Password: viper.GetString("http-proxy-auth-password"),
},
},
Socks5: socks5.ServerConfig{
Debug: global.Debug,
Address: global.Address,
Port: viper.GetInt("socks5-proxy-port"),
AuthConfig: socks5.ProxyAuthConfig{
Username: viper.GetString("socks5-proxy-auth-username"),
Password: viper.GetString("socks5-proxy-auth-password"),
},
},
}, nil
}