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 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "net"
  7. "strconv"
  8. "github.com/juju/errors"
  9. statsd "gopkg.in/alexcesaro/statsd.v2"
  10. )
  11. // Config represents common configuration of mtg.
  12. type Config struct {
  13. Debug bool
  14. Verbose bool
  15. SecureMode bool
  16. SecureOnly bool
  17. ReadBufferSize int
  18. WriteBufferSize int
  19. BindPort uint16
  20. PublicIPv4Port uint16
  21. PublicIPv6Port uint16
  22. StatsPort uint16
  23. BindIP net.IP
  24. PublicIPv4 net.IP
  25. PublicIPv6 net.IP
  26. StatsIP net.IP
  27. StatsD struct {
  28. Addr net.Addr
  29. Prefix string
  30. Tags map[string]string
  31. TagsFormat statsd.TagFormat
  32. Enabled bool
  33. }
  34. Secret []byte
  35. AdTag []byte
  36. }
  37. // URLs contains links to the proxy (tg://, t.me) and their QR codes.
  38. type URLs struct {
  39. TG string `json:"tg_url"`
  40. TMe string `json:"tme_url"`
  41. TGQRCode string `json:"tg_qrcode"`
  42. TMeQRCode string `json:"tme_qrcode"`
  43. }
  44. // IPURLs contains links to both ipv4 and ipv6 of the proxy.
  45. type IPURLs struct {
  46. IPv4 URLs `json:"ipv4"`
  47. IPv6 URLs `json:"ipv6"`
  48. BotSecret string `json:"secret_for_mtproxybot"`
  49. }
  50. // BindAddr returns connection for this server to bind to.
  51. func (c *Config) BindAddr() string {
  52. return getAddr(c.BindIP, c.BindPort)
  53. }
  54. // StatAddr returns connection string to the stats API.
  55. func (c *Config) StatAddr() string {
  56. return getAddr(c.StatsIP, c.StatsPort)
  57. }
  58. // UseMiddleProxy defines if this proxy has to connect middle proxies
  59. // which supports promoted channels or directly access Telegram.
  60. func (c *Config) UseMiddleProxy() bool {
  61. return len(c.AdTag) > 0
  62. }
  63. // BotSecretString returns secret string which should work with MTProxybot.
  64. func (c *Config) BotSecretString() string {
  65. return hex.EncodeToString(c.Secret)
  66. }
  67. // SecretString returns a secret in a form entered on the start of the
  68. // application.
  69. func (c *Config) SecretString() string {
  70. secret := c.BotSecretString()
  71. if c.SecureMode {
  72. return "dd" + secret
  73. }
  74. return secret
  75. }
  76. // GetURLs returns configured IPURLs instance with links to this server.
  77. func (c *Config) GetURLs() IPURLs {
  78. urls := IPURLs{}
  79. secret := c.SecretString()
  80. if c.PublicIPv4 != nil {
  81. urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, secret)
  82. }
  83. if c.PublicIPv6 != nil {
  84. urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, secret)
  85. }
  86. urls.BotSecret = c.BotSecretString()
  87. return urls
  88. }
  89. func getAddr(host fmt.Stringer, port uint16) string {
  90. return net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
  91. }
  92. // NewConfig returns new configuration. If required, it manages and
  93. // fetches data from external sources. Parameters passed to this
  94. // function, should come from command line arguments.
  95. func NewConfig(debug, verbose bool, // nolint: gocyclo
  96. writeBufferSize, readBufferSize uint32,
  97. bindIP, publicIPv4, publicIPv6, statsIP net.IP,
  98. bindPort, publicIPv4Port, publicIPv6Port, statsPort, statsdPort uint16,
  99. statsdIP, statsdNetwork, statsdPrefix, statsdTagsFormat string,
  100. statsdTags map[string]string,
  101. secureOnly bool,
  102. secret, adtag []byte) (*Config, error) {
  103. secureMode := secureOnly
  104. if bytes.HasPrefix(secret, []byte{0xdd}) && len(secret) == 17 {
  105. secureMode = true
  106. secret = bytes.TrimPrefix(secret, []byte{0xdd})
  107. } else if len(secret) != 16 {
  108. return nil, errors.New("Telegram demands secret of length 32")
  109. }
  110. var err error
  111. if publicIPv4 == nil {
  112. publicIPv4, err = getGlobalIPv4()
  113. if err != nil {
  114. publicIPv4 = nil
  115. } else if publicIPv4.To4() == nil {
  116. return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
  117. }
  118. }
  119. if publicIPv4Port == 0 {
  120. publicIPv4Port = bindPort
  121. }
  122. if publicIPv6 == nil {
  123. publicIPv6, err = getGlobalIPv6()
  124. if err != nil {
  125. publicIPv6 = nil
  126. } else if publicIPv6.To4() != nil {
  127. return nil, errors.Errorf("IP %s is not IPv6", publicIPv6.String())
  128. }
  129. }
  130. if publicIPv6Port == 0 {
  131. publicIPv6Port = bindPort
  132. }
  133. if statsIP == nil {
  134. statsIP = publicIPv4
  135. }
  136. conf := &Config{
  137. Debug: debug,
  138. Verbose: verbose,
  139. SecureOnly: secureOnly,
  140. BindIP: bindIP,
  141. BindPort: bindPort,
  142. PublicIPv4: publicIPv4,
  143. PublicIPv4Port: publicIPv4Port,
  144. PublicIPv6: publicIPv6,
  145. PublicIPv6Port: publicIPv6Port,
  146. StatsIP: statsIP,
  147. StatsPort: statsPort,
  148. Secret: secret,
  149. AdTag: adtag,
  150. SecureMode: secureMode,
  151. ReadBufferSize: int(readBufferSize),
  152. WriteBufferSize: int(writeBufferSize),
  153. }
  154. if statsdIP != "" {
  155. conf.StatsD.Enabled = true
  156. conf.StatsD.Prefix = statsdPrefix
  157. conf.StatsD.Tags = statsdTags
  158. var (
  159. addr net.Addr
  160. err error
  161. )
  162. hostPort := net.JoinHostPort(statsdIP, strconv.Itoa(int(statsdPort)))
  163. switch statsdNetwork {
  164. case "tcp":
  165. addr, err = net.ResolveTCPAddr("tcp", hostPort)
  166. case "udp":
  167. addr, err = net.ResolveUDPAddr("udp", hostPort)
  168. default:
  169. err = errors.Errorf("Unknown network %s", statsdNetwork)
  170. }
  171. if err != nil {
  172. return nil, errors.Annotate(err, "Cannot resolve statsd address")
  173. }
  174. conf.StatsD.Addr = addr
  175. switch statsdTagsFormat {
  176. case "datadog":
  177. conf.StatsD.TagsFormat = statsd.Datadog
  178. case "influxdb":
  179. conf.StatsD.TagsFormat = statsd.InfluxDB
  180. case "":
  181. default:
  182. return nil, errors.Errorf("Unknown tags format %s", statsdTagsFormat)
  183. }
  184. }
  185. return conf, nil
  186. }