Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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