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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 5.3KB

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