Browse Source

More reasonable cli for access

tags/v2.0.0-rc1
9seconds 5 years ago
parent
commit
37a78bd1c3
6 changed files with 16 additions and 35 deletions
  1. 8
    5
      cli/access.go
  2. 5
    12
      cli/access_test.go
  3. 1
    1
      cli/cli.go
  4. 1
    1
      cli/generate_secret.go
  5. 0
    8
      config/config.go
  6. 1
    8
      example.config.toml

+ 8
- 5
cli/access.go View File

36
 type Access struct {
36
 type Access struct {
37
 	base `kong:"-"`
37
 	base `kong:"-"`
38
 
38
 
39
-	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"`       // nolint: lll
40
-	Port       uint   `kong:"help='Port number. Default port is taken from configuration file, bind-to parameter',type:'uint'"` // nolint: lll
41
-	Hex        bool   `kong:"help='Print secret in hex encoding.'"`
39
+	PublicIPv4 net.IP `kong:"help='Public IPv4 address for proxy. By default it is resolved via remote website',name='ipv4',short='i'"`   // nolint: lll
40
+	PublicIPv6 net.IP `kong:"help='Public IPv6 address for proxy. By default it is resolved via remote website',name='ipv6',short='I'"`   // nolint: lll
41
+	Port       uint   `kong:"help='Port number. Default port is taken from configuration file, bind-to parameter',type:'uint',short='p'"` // nolint: lll
42
+	Hex        bool   `kong:"help='Print secret in hex encoding.',short='x'"`
43
+
44
+	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` // nolint: lll
42
 }
45
 }
43
 
46
 
44
 func (c *Access) Run(cli *CLI, version string) error {
47
 func (c *Access) Run(cli *CLI, version string) error {
60
 	go func() {
63
 	go func() {
61
 		defer wg.Done()
64
 		defer wg.Done()
62
 
65
 
63
-		ip := c.Config.Network.PublicIP.IPv4.Value(nil)
66
+		ip := cli.Access.PublicIPv4
64
 		if ip == nil {
67
 		if ip == nil {
65
 			ip = c.getIP("tcp4")
68
 			ip = c.getIP("tcp4")
66
 		}
69
 		}
75
 	go func() {
78
 	go func() {
76
 		defer wg.Done()
79
 		defer wg.Done()
77
 
80
 
78
-		ip := c.Config.Network.PublicIP.IPv6.Value(nil)
81
+		ip := cli.Access.PublicIPv6
79
 		if ip == nil {
82
 		if ip == nil {
80
 			ip = c.getIP("tcp6")
83
 			ip = c.getIP("tcp6")
81
 		}
84
 		}

+ 5
- 12
cli/access_test.go View File

1
 package cli_test
1
 package cli_test
2
 
2
 
3
 import (
3
 import (
4
+	"net"
4
 	"net/http"
5
 	"net/http"
5
 	"testing"
6
 	"testing"
6
 
7
 
122
 }
123
 }
123
 
124
 
124
 func (suite *AccessTestSuite) TestGenerateNoCalls() {
125
 func (suite *AccessTestSuite) TestGenerateNoCalls() {
125
-	suite.NoError(
126
-		suite.cli.Access.Config.Network.PublicIP.IPv4.UnmarshalText(
127
-			[]byte("10.0.0.10")))
128
-	suite.NoError(
129
-		suite.cli.Access.Config.Network.PublicIP.IPv6.UnmarshalText(
130
-			[]byte("2001:0db8:85a3:0000:0000:8a2e:0370:7334")))
126
+	suite.cli.Access.PublicIPv4 = net.ParseIP("10.0.0.10")
127
+	suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
131
 
128
 
132
 	output := suite.CaptureStdout(func() {
129
 	output := suite.CaptureStdout(func() {
133
 		suite.NoError(suite.cli.Access.Execute(suite.cli))
130
 		suite.NoError(suite.cli.Access.Execute(suite.cli))
148
 }
145
 }
149
 
146
 
150
 func (suite *AccessTestSuite) TestGenerateIPv4Call() {
147
 func (suite *AccessTestSuite) TestGenerateIPv4Call() {
151
-	suite.NoError(
152
-		suite.cli.Access.Config.Network.PublicIP.IPv6.UnmarshalText(
153
-			[]byte("2001:0db8:85a3:0000:0000:8a2e:0370:7334")))
148
+	suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
154
 
149
 
155
 	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
150
 	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
156
 		httpmock.NewStringResponder(http.StatusOK, "10.11.12.13"))
151
 		httpmock.NewStringResponder(http.StatusOK, "10.11.12.13"))
174
 }
169
 }
175
 
170
 
176
 func (suite *AccessTestSuite) TestIPv4CallFail() {
171
 func (suite *AccessTestSuite) TestIPv4CallFail() {
177
-	suite.NoError(
178
-		suite.cli.Access.Config.Network.PublicIP.IPv6.UnmarshalText(
179
-			[]byte("2001:0db8:85a3:0000:0000:8a2e:0370:7334")))
172
+	suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
180
 
173
 
181
 	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
174
 	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
182
 		httpmock.NewStringResponder(http.StatusForbidden, ""))
175
 		httpmock.NewStringResponder(http.StatusForbidden, ""))

+ 1
- 1
cli/cli.go View File

5
 type CLI struct {
5
 type CLI struct {
6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
7
 	Access         Access           `kong:"cmd,help='Print access information.'"`
7
 	Access         Access           `kong:"cmd,help='Print access information.'"`
8
-	Version        kong.VersionFlag `kong:"help='Print version.'"`
8
+	Version        kong.VersionFlag `kong:"help='Print version.',short='v'"`
9
 }
9
 }

+ 1
- 1
cli/generate_secret.go View File

10
 	base `kong:"-"`
10
 	base `kong:"-"`
11
 
11
 
12
 	HostName string `kong:"arg,required,help='Hostname to use for domain fronting.',name='hostname'"`
12
 	HostName string `kong:"arg,required,help='Hostname to use for domain fronting.',name='hostname'"`
13
-	Hex      bool   `kong:"help='Print secret in hex encoding.'"`
13
+	Hex      bool   `kong:"help='Print secret in hex encoding.',short='x'"`
14
 }
14
 }
15
 
15
 
16
 func (c *GenerateSecret) Run(cli *CLI, _ string) error {
16
 func (c *GenerateSecret) Run(cli *CLI, _ string) error {

+ 0
- 8
config/config.go View File

35
 		} `json:"blocklist"`
35
 		} `json:"blocklist"`
36
 	} `json:"defense"`
36
 	} `json:"defense"`
37
 	Network struct {
37
 	Network struct {
38
-		PublicIP struct {
39
-			IPv4 TypeIP `json:"ipv4"`
40
-			IPv6 TypeIP `json:"ipv6"`
41
-		} `json:"public-ip"`
42
 		Timeout struct {
38
 		Timeout struct {
43
 			TCP  TypeDuration `json:"tcp"`
39
 			TCP  TypeDuration `json:"tcp"`
44
 			HTTP TypeDuration `json:"http"`
40
 			HTTP TypeDuration `json:"http"`
113
 		} `toml:"blocklist" json:"blocklist,omitempty"`
109
 		} `toml:"blocklist" json:"blocklist,omitempty"`
114
 	} `toml:"defense" json:"defense,omitempty"`
110
 	} `toml:"defense" json:"defense,omitempty"`
115
 	Network struct {
111
 	Network struct {
116
-		PublicIP struct {
117
-			IPv4 string `toml:"ipv4" json:"ipv4,omitempty"`
118
-			IPv6 string `toml:"ipv6" json:"ipv6,omitempty"`
119
-		} `toml:"public-ip" json:"public-ip,omitempty"`
120
 		Timeout struct {
112
 		Timeout struct {
121
 			TCP  string `toml:"tcp" json:"tcp,omitempty"`
113
 			TCP  string `toml:"tcp" json:"tcp,omitempty"`
122
 			HTTP string `toml:"http" json:"http,omitempty"`
114
 			HTTP string `toml:"http" json:"http,omitempty"`

+ 1
- 8
example.config.toml View File

98
     # "socks5://user:password@host:port?open_threshold=5&half_open_timeout=1m&reset_failures_timeout=10s"
98
     # "socks5://user:password@host:port?open_threshold=5&half_open_timeout=1m&reset_failures_timeout=10s"
99
 ]
99
 ]
100
 
100
 
101
-# public ip addresses of the server. Actually, it is required only to
102
-# generate a correct access file. if you use default values here, mtg
103
-# will try to resolve these IPs on its own.
104
-[network.public-ip]
105
-ipv4 = ""
106
-ipv6 = ""
107
-
108
 # network timeouts define different settings for timeouts. tcp timeout
101
 # network timeouts define different settings for timeouts. tcp timeout
109
 # define a global timeout on establishing of network connections. idle
102
 # define a global timeout on establishing of network connections. idle
110
 # means a timeout on pumping data between sockset when nothing is
103
 # means a timeout on pumping data between sockset when nothing is
171
     # "/local.file"
164
     # "/local.file"
172
 ]
165
 ]
173
 # How often do we need to update a blocklist set.
166
 # How often do we need to update a blocklist set.
174
-update-each = "1d"
167
+update-each = "24h"
175
 
168
 
176
 # statsd statistics integration.
169
 # statsd statistics integration.
177
 [stats.statsd]
170
 [stats.statsd]

Loading…
Cancel
Save