|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+package main
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+ "io"
|
|
|
6
|
+ "io/ioutil"
|
|
|
7
|
+ "net/http"
|
|
|
8
|
+ "net/url"
|
|
|
9
|
+ "os"
|
|
|
10
|
+ "regexp"
|
|
|
11
|
+ "strconv"
|
|
|
12
|
+ "strings"
|
|
|
13
|
+
|
|
|
14
|
+ kingpin "gopkg.in/alecthomas/kingpin.v2"
|
|
|
15
|
+)
|
|
|
16
|
+
|
|
|
17
|
+var (
|
|
|
18
|
+ app = kingpin.New("mtg", "Simple MTPROTO proxy.")
|
|
|
19
|
+
|
|
|
20
|
+ debug = app.Flag("debug", "Run in debug mode.").
|
|
|
21
|
+ Short('d').
|
|
|
22
|
+ Envar("MTG_DEBUG").
|
|
|
23
|
+ Bool()
|
|
|
24
|
+ bindIP = app.Flag("bind-ip", "Which IP to bind to.").
|
|
|
25
|
+ Short('i').
|
|
|
26
|
+ Envar("MTG_IP").
|
|
|
27
|
+ Default("127.0.0.1").
|
|
|
28
|
+ IP()
|
|
|
29
|
+ bindPort = app.Flag("bind-port", "Which port to bind to.").
|
|
|
30
|
+ Short('p').
|
|
|
31
|
+ Envar("MTG_PORT").
|
|
|
32
|
+ Default("3128").
|
|
|
33
|
+ Uint16()
|
|
|
34
|
+ serverName = app.Flag("server-name",
|
|
|
35
|
+ "Which server name to use. Default is IP address resolved by ipify.").
|
|
|
36
|
+ Short('s').
|
|
|
37
|
+ Envar("MTG_SERVER").
|
|
|
38
|
+ String()
|
|
|
39
|
+
|
|
|
40
|
+ secret = app.Arg("secret", "Secret of this proxy.").String()
|
|
|
41
|
+)
|
|
|
42
|
+
|
|
|
43
|
+func main() {
|
|
|
44
|
+ kingpin.MustParse(app.Parse(os.Args[1:]))
|
|
|
45
|
+
|
|
|
46
|
+ if matched, err := regexp.MatchString("[a-fA-F0-9]+", *secret); !matched || err != nil {
|
|
|
47
|
+ usage("Secret has to be hexadecimal string.")
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ if *serverName == "" {
|
|
|
51
|
+ resp, err := http.Get("https://api.ipify.org")
|
|
|
52
|
+ if err != nil || resp.StatusCode != http.StatusOK {
|
|
|
53
|
+ usage("Cannot get local IP address.")
|
|
|
54
|
+ }
|
|
|
55
|
+ myIPBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
56
|
+ resp.Body.Close()
|
|
|
57
|
+
|
|
|
58
|
+ if err != nil {
|
|
|
59
|
+ usage("Cannot get local IP address.")
|
|
|
60
|
+ }
|
|
|
61
|
+ *serverName = strings.TrimSpace(string(myIPBytes))
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
|
64
|
+ printURLs()
|
|
|
65
|
+ serve()
|
|
|
66
|
+}
|
|
|
67
|
+
|
|
|
68
|
+func usage(msg string) {
|
|
|
69
|
+ io.WriteString(os.Stderr, msg+"\n")
|
|
|
70
|
+ os.Exit(1)
|
|
|
71
|
+}
|
|
|
72
|
+
|
|
|
73
|
+func printURLs() {
|
|
|
74
|
+ values := url.Values{}
|
|
|
75
|
+ values.Set("server", *serverName)
|
|
|
76
|
+ values.Set("port", strconv.Itoa(int(*bindPort)))
|
|
|
77
|
+ values.Set("secret", *secret)
|
|
|
78
|
+
|
|
|
79
|
+ tgURL := url.URL{
|
|
|
80
|
+ Scheme: "tg",
|
|
|
81
|
+ Host: "proxy",
|
|
|
82
|
+ RawQuery: values.Encode(),
|
|
|
83
|
+ }
|
|
|
84
|
+ fmt.Println(tgURL.String())
|
|
|
85
|
+
|
|
|
86
|
+ tgURL.Scheme = "https"
|
|
|
87
|
+ tgURL.Host = "t.me"
|
|
|
88
|
+ tgURL.Path = "proxy"
|
|
|
89
|
+ fmt.Println(tgURL.String())
|
|
|
90
|
+}
|
|
|
91
|
+
|
|
|
92
|
+func serve() {
|
|
|
93
|
+
|
|
|
94
|
+}
|