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文字以内のものにしてください。

cli_access.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "strconv"
  12. "strings"
  13. "github.com/9seconds/mtg/v2/mtglib/network"
  14. )
  15. type runAccessResponse struct {
  16. IPv4 *runAccessResponseURLs `json:"ipv4,omitempty"`
  17. IPv6 *runAccessResponseURLs `json:"ipv6,omitempty"`
  18. Secret struct {
  19. Hex string `json:"hex"`
  20. Base64 string `json:"base64"`
  21. } `json:"secret"`
  22. }
  23. type runAccessResponseURLs struct {
  24. IP net.IP `json:"ip"`
  25. TgURL string `json:"tg_url"`
  26. TgQrCode string `json:"tg_qrcode"`
  27. TmeURL string `json:"tme_url"`
  28. TmeQrCode string `json:"tme_qrcode"`
  29. }
  30. type cliCommandAccess struct {
  31. ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
  32. Hex bool `help:"Print secret in hex encoding."`
  33. }
  34. func (c *cliCommandAccess) Run(cli *CLI) error {
  35. filefp, err := os.Open(cli.Access.ConfigPath)
  36. if err != nil {
  37. return fmt.Errorf("cannot open config file: %w", err)
  38. }
  39. defer filefp.Close()
  40. conf, err := parseConfig(filefp)
  41. if err != nil {
  42. return fmt.Errorf("cannot parse config: %w", err)
  43. }
  44. ntw, err := makeNetwork(conf)
  45. if err != nil {
  46. return fmt.Errorf("cannot build a network: %w", err)
  47. }
  48. ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
  49. ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
  50. if ipv4 == nil {
  51. ipv4 = c.getIP(ntw, "tcp4")
  52. }
  53. if ipv6 == nil {
  54. ipv6 = c.getIP(ntw, "tcp6")
  55. }
  56. resp := runAccessResponse{
  57. IPv4: c.makeResponseURLs(ipv4, conf, cli),
  58. IPv6: c.makeResponseURLs(ipv6, conf, cli),
  59. }
  60. resp.Secret.Base64 = conf.Secret.Base64()
  61. resp.Secret.Hex = conf.Secret.Hex()
  62. encoder := json.NewEncoder(os.Stdout)
  63. encoder.SetEscapeHTML(false)
  64. encoder.SetIndent("", " ")
  65. if err := encoder.Encode(resp); err != nil {
  66. return fmt.Errorf("cannot dump access json: %w", err)
  67. }
  68. return nil
  69. }
  70. func (c *cliCommandAccess) getIP(ntw *network.Network, protocol string) net.IP {
  71. client := &http.Client{
  72. Timeout: ntw.HTTP.Timeout,
  73. Transport: &http.Transport{
  74. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  75. return ntw.DialContext(ctx, protocol, address)
  76. },
  77. },
  78. }
  79. resp, err := client.Get("https://ifconfig.co") // nolint: bodyclose, noctx
  80. if err != nil {
  81. return nil
  82. }
  83. if resp.StatusCode != http.StatusOK {
  84. return nil
  85. }
  86. defer exhaustResponse(resp)
  87. data, err := ioutil.ReadAll(resp.Body)
  88. if err != nil {
  89. return nil
  90. }
  91. return net.ParseIP(strings.TrimSpace(string(data)))
  92. }
  93. func (c *cliCommandAccess) makeResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
  94. if ip == nil {
  95. return nil
  96. }
  97. values := url.Values{}
  98. values.Set("server", ip.String())
  99. values.Set("port", strconv.Itoa(int(conf.BindTo.port.Value(0))))
  100. if cli.Access.Hex {
  101. values.Set("secret", conf.Secret.Hex())
  102. } else {
  103. values.Set("secret", conf.Secret.Base64())
  104. }
  105. urlQuery := values.Encode()
  106. rv := &runAccessResponseURLs{
  107. IP: ip,
  108. TgURL: (&url.URL{
  109. Scheme: "tg",
  110. Host: "proxy",
  111. RawQuery: urlQuery,
  112. }).String(),
  113. TmeURL: (&url.URL{
  114. Scheme: "https",
  115. Host: "t.me",
  116. Path: "proxy",
  117. RawQuery: urlQuery,
  118. }).String(),
  119. }
  120. rv.TgQrCode = c.makeResponseQRCode(rv.TgURL)
  121. rv.TmeQrCode = c.makeResponseQRCode(rv.TmeURL)
  122. return rv
  123. }
  124. func (c *cliCommandAccess) makeResponseQRCode(data string) string {
  125. values := url.Values{}
  126. values.Set("qzone", "4")
  127. values.Set("format", "svg")
  128. values.Set("data", data)
  129. return (&url.URL{
  130. Scheme: "https",
  131. Host: "api.qrserver.com",
  132. Path: "v1/create-qr-code",
  133. RawQuery: values.Encode(),
  134. }).String()
  135. }