| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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() {
-
- }
|