diff --git a/.gitignore b/.gitignore index f1c181e..81ff681 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,10 @@ -# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib - -# Test binary, build with `go test -c` *.test - -# Output of the go coverage tool, specifically when used with LiteIDE *.out +mtg +vendor/ +version.go diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..6088f9f --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,30 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "github.com/alecthomas/template" + packages = [ + ".", + "parse" + ] + revision = "a0175ee3bccc567396460bf5acd36800cb10c49c" + +[[projects]] + branch = "master" + name = "github.com/alecthomas/units" + packages = ["."] + revision = "2efee857e7cfd4f3d0138cc3cbb1b4966962b93a" + +[[projects]] + name = "gopkg.in/alecthomas/kingpin.v2" + packages = ["."] + revision = "947dcec5ba9c011838740e680966fd7087a71d0d" + version = "v2.2.6" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "9b3ace12d2b928915476e7d30a29e7e37e0a9d569b37ad070fb0b2d0e06fc088" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..20a0428 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,34 @@ +# Gopkg.toml example +# +# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[prune] + go-tests = true + unused-packages = true + +[[constraint]] + name = "gopkg.in/alecthomas/kingpin.v2" + version = "2.2.6" diff --git a/main.go b/main.go new file mode 100644 index 0000000..6ddbaed --- /dev/null +++ b/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "fmt" + "io" + "io/ioutil" + "net/http" + "net/url" + "os" + "regexp" + "strconv" + "strings" + + kingpin "gopkg.in/alecthomas/kingpin.v2" +) + +var ( + app = kingpin.New("mtg", "Simple MTPROTO proxy.") + + debug = app.Flag("debug", "Run in debug mode."). + Short('d'). + Envar("MTG_DEBUG"). + Bool() + bindIP = app.Flag("bind-ip", "Which IP to bind to."). + Short('i'). + Envar("MTG_IP"). + Default("127.0.0.1"). + IP() + bindPort = app.Flag("bind-port", "Which port to bind to."). + Short('p'). + Envar("MTG_PORT"). + Default("3128"). + Uint16() + serverName = app.Flag("server-name", + "Which server name to use. Default is IP address resolved by ipify."). + Short('s'). + Envar("MTG_SERVER"). + String() + + secret = app.Arg("secret", "Secret of this proxy.").String() +) + +func main() { + kingpin.MustParse(app.Parse(os.Args[1:])) + + if matched, err := regexp.MatchString("[a-fA-F0-9]+", *secret); !matched || err != nil { + usage("Secret has to be hexadecimal string.") + } + + if *serverName == "" { + resp, err := http.Get("https://api.ipify.org") + if err != nil || resp.StatusCode != http.StatusOK { + usage("Cannot get local IP address.") + } + myIPBytes, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + + if err != nil { + usage("Cannot get local IP address.") + } + *serverName = strings.TrimSpace(string(myIPBytes)) + } + + printURLs() + serve() +} + +func usage(msg string) { + io.WriteString(os.Stderr, msg+"\n") + os.Exit(1) +} + +func printURLs() { + values := url.Values{} + values.Set("server", *serverName) + values.Set("port", strconv.Itoa(int(*bindPort))) + values.Set("secret", *secret) + + tgURL := url.URL{ + Scheme: "tg", + Host: "proxy", + RawQuery: values.Encode(), + } + fmt.Println(tgURL.String()) + + tgURL.Scheme = "https" + tgURL.Host = "t.me" + tgURL.Path = "proxy" + fmt.Println(tgURL.String()) +} + +func serve() { + +}