|
|
@@ -3,6 +3,7 @@ package main
|
|
3
|
3
|
import (
|
|
4
|
4
|
"context"
|
|
5
|
5
|
"encoding/json"
|
|
|
6
|
+ "fmt"
|
|
6
|
7
|
"io/ioutil"
|
|
7
|
8
|
"net"
|
|
8
|
9
|
"net/http"
|
|
|
@@ -31,38 +32,43 @@ type runAccessResponseURLs struct {
|
|
31
|
32
|
TmeQrCode string `json:"tme_qrcode"`
|
|
32
|
33
|
}
|
|
33
|
34
|
|
|
34
|
|
-func runAccess(cli *CLI) {
|
|
|
35
|
+type cliCommandAccess struct {
|
|
|
36
|
+ ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
|
|
|
37
|
+ Hex bool `help:"Print secret in hex encoding."`
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+func (c *cliCommandAccess) Run(cli *CLI) error {
|
|
35
|
41
|
filefp, err := os.Open(cli.Access.ConfigPath)
|
|
36
|
42
|
if err != nil {
|
|
37
|
|
- exit(err)
|
|
|
43
|
+ return fmt.Errorf("cannot open config file: %w", err)
|
|
38
|
44
|
}
|
|
39
|
45
|
|
|
40
|
46
|
defer filefp.Close()
|
|
41
|
47
|
|
|
42
|
48
|
conf, err := parseConfig(filefp)
|
|
43
|
49
|
if err != nil {
|
|
44
|
|
- exit(err)
|
|
|
50
|
+ return fmt.Errorf("cannot parse config: %w", err)
|
|
45
|
51
|
}
|
|
46
|
52
|
|
|
47
|
53
|
ntw, err := makeNetwork(conf)
|
|
48
|
54
|
if err != nil {
|
|
49
|
|
- exit(err)
|
|
|
55
|
+ return fmt.Errorf("cannot build a network: %w", err)
|
|
50
|
56
|
}
|
|
51
|
57
|
|
|
52
|
58
|
ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
|
|
53
|
59
|
ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
|
|
54
|
60
|
|
|
55
|
61
|
if ipv4 == nil {
|
|
56
|
|
- ipv4 = runAccessGetIP(ntw, "tcp4")
|
|
|
62
|
+ ipv4 = c.getIP(ntw, "tcp4")
|
|
57
|
63
|
}
|
|
58
|
64
|
|
|
59
|
65
|
if ipv6 == nil {
|
|
60
|
|
- ipv6 = runAccessGetIP(ntw, "tcp6")
|
|
|
66
|
+ ipv6 = c.getIP(ntw, "tcp6")
|
|
61
|
67
|
}
|
|
62
|
68
|
|
|
63
|
69
|
resp := runAccessResponse{
|
|
64
|
|
- IPv4: runMakeAccessResponseURLs(ipv4, conf, cli),
|
|
65
|
|
- IPv6: runMakeAccessResponseURLs(ipv6, conf, cli),
|
|
|
70
|
+ IPv4: c.makeResponseURLs(ipv4, conf, cli),
|
|
|
71
|
+ IPv6: c.makeResponseURLs(ipv6, conf, cli),
|
|
66
|
72
|
}
|
|
67
|
73
|
resp.Secret.Base64 = conf.Secret.Base64()
|
|
68
|
74
|
resp.Secret.Hex = conf.Secret.Hex()
|
|
|
@@ -73,11 +79,13 @@ func runAccess(cli *CLI) {
|
|
73
|
79
|
encoder.SetIndent("", " ")
|
|
74
|
80
|
|
|
75
|
81
|
if err := encoder.Encode(resp); err != nil {
|
|
76
|
|
- exit(err)
|
|
|
82
|
+ return fmt.Errorf("cannot dump access json: %w", err)
|
|
77
|
83
|
}
|
|
|
84
|
+
|
|
|
85
|
+ return nil
|
|
78
|
86
|
}
|
|
79
|
87
|
|
|
80
|
|
-func runAccessGetIP(ntw *network.Network, protocol string) net.IP {
|
|
|
88
|
+func (c *cliCommandAccess) getIP(ntw *network.Network, protocol string) net.IP {
|
|
81
|
89
|
client := &http.Client{
|
|
82
|
90
|
Timeout: ntw.HTTP.Timeout,
|
|
83
|
91
|
Transport: &http.Transport{
|
|
|
@@ -106,7 +114,7 @@ func runAccessGetIP(ntw *network.Network, protocol string) net.IP {
|
|
106
|
114
|
return net.ParseIP(strings.TrimSpace(string(data)))
|
|
107
|
115
|
}
|
|
108
|
116
|
|
|
109
|
|
-func runMakeAccessResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
|
|
|
117
|
+func (c *cliCommandAccess) makeResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
|
|
110
|
118
|
if ip == nil {
|
|
111
|
119
|
return nil
|
|
112
|
120
|
}
|
|
|
@@ -139,13 +147,13 @@ func runMakeAccessResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResp
|
|
139
|
147
|
}).String(),
|
|
140
|
148
|
}
|
|
141
|
149
|
|
|
142
|
|
- rv.TgQrCode = runMakeAccessResponseURLsQRCode(rv.TgURL)
|
|
143
|
|
- rv.TmeQrCode = runMakeAccessResponseURLsQRCode(rv.TmeURL)
|
|
|
150
|
+ rv.TgQrCode = c.makeResponseQRCode(rv.TgURL)
|
|
|
151
|
+ rv.TmeQrCode = c.makeResponseQRCode(rv.TmeURL)
|
|
144
|
152
|
|
|
145
|
153
|
return rv
|
|
146
|
154
|
}
|
|
147
|
155
|
|
|
148
|
|
-func runMakeAccessResponseURLsQRCode(data string) string {
|
|
|
156
|
+func (c *cliCommandAccess) makeResponseQRCode(data string) string {
|
|
149
|
157
|
values := url.Values{}
|
|
150
|
158
|
|
|
151
|
159
|
values.Set("qzone", "4")
|