|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+package main
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "context"
|
|
|
5
|
+ "encoding/json"
|
|
|
6
|
+ "io/ioutil"
|
|
|
7
|
+ "net"
|
|
|
8
|
+ "net/http"
|
|
|
9
|
+ "net/url"
|
|
|
10
|
+ "os"
|
|
|
11
|
+ "strconv"
|
|
|
12
|
+ "strings"
|
|
|
13
|
+
|
|
|
14
|
+ "github.com/9seconds/mtg/v2/mtglib/network"
|
|
|
15
|
+)
|
|
|
16
|
+
|
|
|
17
|
+type runAccessResponse struct {
|
|
|
18
|
+ IPv4 *runAccessResponseURLs `json:"ipv4,omitempty"`
|
|
|
19
|
+ IPv6 *runAccessResponseURLs `json:"ipv6,omitempty"`
|
|
|
20
|
+ Secret struct {
|
|
|
21
|
+ Hex string `json:"hex"`
|
|
|
22
|
+ Base64 string `json:"base64"`
|
|
|
23
|
+ } `json:"secret"`
|
|
|
24
|
+}
|
|
|
25
|
+
|
|
|
26
|
+type runAccessResponseURLs struct {
|
|
|
27
|
+ IP net.IP `json:"ip"`
|
|
|
28
|
+ TgURL string `json:"tg_url"`
|
|
|
29
|
+ TgQrCode string `json:"tg_qrcode"`
|
|
|
30
|
+ TmeURL string `json:"tme_url"`
|
|
|
31
|
+ TmeQrCode string `json:"tme_qrcode"`
|
|
|
32
|
+}
|
|
|
33
|
+
|
|
|
34
|
+func runAccess(cli *CLI) {
|
|
|
35
|
+ filefp, err := os.Open(cli.Access.ConfigPath)
|
|
|
36
|
+ if err != nil {
|
|
|
37
|
+ exit(err)
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ defer filefp.Close()
|
|
|
41
|
+
|
|
|
42
|
+ conf, err := parseConfig(filefp)
|
|
|
43
|
+ if err != nil {
|
|
|
44
|
+ exit(err)
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ ntw, err := makeNetwork(conf)
|
|
|
48
|
+ if err != nil {
|
|
|
49
|
+ exit(err)
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
|
|
|
53
|
+ ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
|
|
|
54
|
+
|
|
|
55
|
+ if ipv4 == nil {
|
|
|
56
|
+ ipv4 = runAccessGetIP(ntw, "tcp4")
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ if ipv6 == nil {
|
|
|
60
|
+ ipv6 = runAccessGetIP(ntw, "tcp6")
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ resp := runAccessResponse{
|
|
|
64
|
+ IPv4: runMakeAccessResponseURLs(ipv4, conf, cli),
|
|
|
65
|
+ IPv6: runMakeAccessResponseURLs(ipv6, conf, cli),
|
|
|
66
|
+ }
|
|
|
67
|
+ resp.Secret.Base64 = conf.Secret.Base64()
|
|
|
68
|
+ resp.Secret.Hex = conf.Secret.EE()
|
|
|
69
|
+
|
|
|
70
|
+ encoder := json.NewEncoder(os.Stdout)
|
|
|
71
|
+
|
|
|
72
|
+ encoder.SetEscapeHTML(false)
|
|
|
73
|
+ encoder.SetIndent("", " ")
|
|
|
74
|
+
|
|
|
75
|
+ if err := encoder.Encode(resp); err != nil {
|
|
|
76
|
+ exit(err)
|
|
|
77
|
+ }
|
|
|
78
|
+}
|
|
|
79
|
+
|
|
|
80
|
+func runAccessGetIP(ntw *network.Network, protocol string) net.IP {
|
|
|
81
|
+ client := &http.Client{
|
|
|
82
|
+ Timeout: ntw.HTTP.Timeout,
|
|
|
83
|
+ Transport: &http.Transport{
|
|
|
84
|
+ DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
85
|
+ return ntw.DialContext(ctx, protocol, address)
|
|
|
86
|
+ },
|
|
|
87
|
+ },
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ resp, err := client.Get("https://ifconfig.co")
|
|
|
91
|
+ if err != nil {
|
|
|
92
|
+ return nil
|
|
|
93
|
+ }
|
|
|
94
|
+
|
|
|
95
|
+ defer exhaustResponse(resp)
|
|
|
96
|
+
|
|
|
97
|
+ data, err := ioutil.ReadAll(resp.Body)
|
|
|
98
|
+ if err != nil {
|
|
|
99
|
+ return nil
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ return net.ParseIP(strings.TrimSpace(string(data)))
|
|
|
103
|
+}
|
|
|
104
|
+
|
|
|
105
|
+func runMakeAccessResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
|
|
|
106
|
+ if ip == nil {
|
|
|
107
|
+ return nil
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ values := url.Values{}
|
|
|
111
|
+
|
|
|
112
|
+ values.Set("server", ip.String())
|
|
|
113
|
+ values.Set("port", strconv.Itoa(int(conf.BindTo.port.Value(0))))
|
|
|
114
|
+ values.Set("secret", conf.Secret.Base64())
|
|
|
115
|
+
|
|
|
116
|
+ if cli.Access.Hex {
|
|
|
117
|
+ values.Set("secret", conf.Secret.EE())
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ urlQuery := values.Encode()
|
|
|
121
|
+
|
|
|
122
|
+ rv := &runAccessResponseURLs{
|
|
|
123
|
+ IP: ip,
|
|
|
124
|
+ TgURL: (&url.URL{
|
|
|
125
|
+ Scheme: "tg",
|
|
|
126
|
+ Host: "proxy",
|
|
|
127
|
+ RawQuery: urlQuery,
|
|
|
128
|
+ }).String(),
|
|
|
129
|
+ TmeURL: (&url.URL{
|
|
|
130
|
+ Scheme: "https",
|
|
|
131
|
+ Host: "t.me",
|
|
|
132
|
+ Path: "proxy",
|
|
|
133
|
+ RawQuery: urlQuery,
|
|
|
134
|
+ }).String(),
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ rv.TgQrCode = runMakeAccessResponseURLsQRCode(rv.TgURL)
|
|
|
138
|
+ rv.TmeQrCode = runMakeAccessResponseURLsQRCode(rv.TmeURL)
|
|
|
139
|
+
|
|
|
140
|
+ return rv
|
|
|
141
|
+}
|
|
|
142
|
+
|
|
|
143
|
+func runMakeAccessResponseURLsQRCode(data string) string {
|
|
|
144
|
+ values := url.Values{}
|
|
|
145
|
+
|
|
|
146
|
+ values.Set("qzone", "4")
|
|
|
147
|
+ values.Set("format", "svg")
|
|
|
148
|
+ values.Set("data", data)
|
|
|
149
|
+
|
|
|
150
|
+ return (&url.URL{
|
|
|
151
|
+ Scheme: "https",
|
|
|
152
|
+ Host: "api.qrserver.com",
|
|
|
153
|
+ Path: "v1/create-qr-code",
|
|
|
154
|
+ RawQuery: values.Encode(),
|
|
|
155
|
+ }).String()
|
|
|
156
|
+}
|