Browse Source

Add boilerplate

tags/0.9
9seconds 8 years ago
parent
commit
62f2ef8a14
4 changed files with 161 additions and 5 deletions
  1. 3
    5
      .gitignore
  2. 30
    0
      Gopkg.lock
  3. 34
    0
      Gopkg.toml
  4. 94
    0
      main.go

+ 3
- 5
.gitignore View File

@@ -1,12 +1,10 @@
1
-# Binaries for programs and plugins
2 1
 *.exe
3 2
 *.exe~
4 3
 *.dll
5 4
 *.so
6 5
 *.dylib
7
-
8
-# Test binary, build with `go test -c`
9 6
 *.test
10
-
11
-# Output of the go coverage tool, specifically when used with LiteIDE
12 7
 *.out
8
+mtg
9
+vendor/
10
+version.go

+ 30
- 0
Gopkg.lock View File

@@ -0,0 +1,30 @@
1
+# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
2
+
3
+
4
+[[projects]]
5
+  branch = "master"
6
+  name = "github.com/alecthomas/template"
7
+  packages = [
8
+    ".",
9
+    "parse"
10
+  ]
11
+  revision = "a0175ee3bccc567396460bf5acd36800cb10c49c"
12
+
13
+[[projects]]
14
+  branch = "master"
15
+  name = "github.com/alecthomas/units"
16
+  packages = ["."]
17
+  revision = "2efee857e7cfd4f3d0138cc3cbb1b4966962b93a"
18
+
19
+[[projects]]
20
+  name = "gopkg.in/alecthomas/kingpin.v2"
21
+  packages = ["."]
22
+  revision = "947dcec5ba9c011838740e680966fd7087a71d0d"
23
+  version = "v2.2.6"
24
+
25
+[solve-meta]
26
+  analyzer-name = "dep"
27
+  analyzer-version = 1
28
+  inputs-digest = "9b3ace12d2b928915476e7d30a29e7e37e0a9d569b37ad070fb0b2d0e06fc088"
29
+  solver-name = "gps-cdcl"
30
+  solver-version = 1

+ 34
- 0
Gopkg.toml View File

@@ -0,0 +1,34 @@
1
+# Gopkg.toml example
2
+#
3
+# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4
+# for detailed Gopkg.toml documentation.
5
+#
6
+# required = ["github.com/user/thing/cmd/thing"]
7
+# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8
+#
9
+# [[constraint]]
10
+#   name = "github.com/user/project"
11
+#   version = "1.0.0"
12
+#
13
+# [[constraint]]
14
+#   name = "github.com/user/project2"
15
+#   branch = "dev"
16
+#   source = "github.com/myfork/project2"
17
+#
18
+# [[override]]
19
+#   name = "github.com/x/y"
20
+#   version = "2.4.0"
21
+#
22
+# [prune]
23
+#   non-go = false
24
+#   go-tests = true
25
+#   unused-packages = true
26
+
27
+
28
+[prune]
29
+  go-tests = true
30
+  unused-packages = true
31
+
32
+[[constraint]]
33
+  name = "gopkg.in/alecthomas/kingpin.v2"
34
+  version = "2.2.6"

+ 94
- 0
main.go View File

@@ -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
+}

Loading…
Cancel
Save