|
|
@@ -11,8 +11,6 @@ import (
|
|
11
|
11
|
"os"
|
|
12
|
12
|
"strconv"
|
|
13
|
13
|
"strings"
|
|
14
|
|
-
|
|
15
|
|
- "github.com/9seconds/mtg/v2/mtglib/network"
|
|
16
|
14
|
)
|
|
17
|
15
|
|
|
18
|
16
|
type runAccessResponse struct {
|
|
|
@@ -33,45 +31,34 @@ type runAccessResponseURLs struct {
|
|
33
|
31
|
}
|
|
34
|
32
|
|
|
35
|
33
|
type cliCommandAccess struct {
|
|
|
34
|
+ cli
|
|
|
35
|
+
|
|
36
|
36
|
ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
|
|
37
|
37
|
Hex bool `help:"Print secret in hex encoding."`
|
|
38
|
38
|
}
|
|
39
|
39
|
|
|
40
|
40
|
func (c *cliCommandAccess) Run(cli *CLI) error {
|
|
41
|
|
- filefp, err := os.Open(cli.Access.ConfigPath)
|
|
42
|
|
- if err != nil {
|
|
43
|
|
- return fmt.Errorf("cannot open config file: %w", err)
|
|
44
|
|
- }
|
|
45
|
|
-
|
|
46
|
|
- defer filefp.Close()
|
|
47
|
|
-
|
|
48
|
|
- conf, err := parseConfig(filefp)
|
|
49
|
|
- if err != nil {
|
|
50
|
|
- return fmt.Errorf("cannot parse config: %w", err)
|
|
51
|
|
- }
|
|
52
|
|
-
|
|
53
|
|
- ntw, err := makeNetwork(conf)
|
|
54
|
|
- if err != nil {
|
|
55
|
|
- return fmt.Errorf("cannot build a network: %w", err)
|
|
|
41
|
+ if err := c.ReadConfig(cli.Access.ConfigPath); err != nil {
|
|
|
42
|
+ return fmt.Errorf("cannot init config: %w", err)
|
|
56
|
43
|
}
|
|
57
|
44
|
|
|
58
|
|
- ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
|
|
59
|
|
- ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
|
|
|
45
|
+ ipv4 := c.conf.Network.PublicIP.IPv4.Value(nil)
|
|
|
46
|
+ ipv6 := c.conf.Network.PublicIP.IPv6.Value(nil)
|
|
60
|
47
|
|
|
61
|
48
|
if ipv4 == nil {
|
|
62
|
|
- ipv4 = c.getIP(ntw, "tcp4")
|
|
|
49
|
+ ipv4 = c.getIP("tcp4")
|
|
63
|
50
|
}
|
|
64
|
51
|
|
|
65
|
52
|
if ipv6 == nil {
|
|
66
|
|
- ipv6 = c.getIP(ntw, "tcp6")
|
|
|
53
|
+ ipv6 = c.getIP("tcp6")
|
|
67
|
54
|
}
|
|
68
|
55
|
|
|
69
|
56
|
resp := runAccessResponse{
|
|
70
|
|
- IPv4: c.makeResponseURLs(ipv4, conf, cli),
|
|
71
|
|
- IPv6: c.makeResponseURLs(ipv6, conf, cli),
|
|
|
57
|
+ IPv4: c.makeResponseURLs(ipv4, cli),
|
|
|
58
|
+ IPv6: c.makeResponseURLs(ipv6, cli),
|
|
72
|
59
|
}
|
|
73
|
|
- resp.Secret.Base64 = conf.Secret.Base64()
|
|
74
|
|
- resp.Secret.Hex = conf.Secret.Hex()
|
|
|
60
|
+ resp.Secret.Base64 = c.conf.Secret.Base64()
|
|
|
61
|
+ resp.Secret.Hex = c.conf.Secret.Hex()
|
|
75
|
62
|
|
|
76
|
63
|
encoder := json.NewEncoder(os.Stdout)
|
|
77
|
64
|
|
|
|
@@ -85,12 +72,12 @@ func (c *cliCommandAccess) Run(cli *CLI) error {
|
|
85
|
72
|
return nil
|
|
86
|
73
|
}
|
|
87
|
74
|
|
|
88
|
|
-func (c *cliCommandAccess) getIP(ntw *network.Network, protocol string) net.IP {
|
|
|
75
|
+func (c *cliCommandAccess) getIP(protocol string) net.IP {
|
|
89
|
76
|
client := &http.Client{
|
|
90
|
|
- Timeout: ntw.HTTP.Timeout,
|
|
|
77
|
+ Timeout: c.network.HTTP.Timeout,
|
|
91
|
78
|
Transport: &http.Transport{
|
|
92
|
79
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
93
|
|
- return ntw.DialContext(ctx, protocol, address)
|
|
|
80
|
+ return c.network.DialContext(ctx, protocol, address)
|
|
94
|
81
|
},
|
|
95
|
82
|
},
|
|
96
|
83
|
}
|
|
|
@@ -114,7 +101,7 @@ func (c *cliCommandAccess) getIP(ntw *network.Network, protocol string) net.IP {
|
|
114
|
101
|
return net.ParseIP(strings.TrimSpace(string(data)))
|
|
115
|
102
|
}
|
|
116
|
103
|
|
|
117
|
|
-func (c *cliCommandAccess) makeResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
|
|
|
104
|
+func (c *cliCommandAccess) makeResponseURLs(ip net.IP, cli *CLI) *runAccessResponseURLs {
|
|
118
|
105
|
if ip == nil {
|
|
119
|
106
|
return nil
|
|
120
|
107
|
}
|
|
|
@@ -122,12 +109,12 @@ func (c *cliCommandAccess) makeResponseURLs(ip net.IP, conf *config, cli *CLI) *
|
|
122
|
109
|
values := url.Values{}
|
|
123
|
110
|
|
|
124
|
111
|
values.Set("server", ip.String())
|
|
125
|
|
- values.Set("port", strconv.Itoa(int(conf.BindTo.port.Value(0))))
|
|
|
112
|
+ values.Set("port", strconv.Itoa(int(c.conf.BindTo.port.Value(0))))
|
|
126
|
113
|
|
|
127
|
114
|
if cli.Access.Hex {
|
|
128
|
|
- values.Set("secret", conf.Secret.Hex())
|
|
|
115
|
+ values.Set("secret", c.conf.Secret.Hex())
|
|
129
|
116
|
} else {
|
|
130
|
|
- values.Set("secret", conf.Secret.Base64())
|
|
|
117
|
+ values.Set("secret", c.conf.Secret.Base64())
|
|
131
|
118
|
}
|
|
132
|
119
|
|
|
133
|
120
|
urlQuery := values.Encode()
|