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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package config
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "github.com/juju/errors"
  8. )
  9. // Buffer sizes define internal socket buffer sizes.
  10. const (
  11. BufferWriteSize = 32 * 1024
  12. BufferReadSize = 32 * 1024
  13. )
  14. // Config represents common configuration of mtg.
  15. type Config struct {
  16. Debug bool
  17. Verbose bool
  18. BindPort uint16
  19. PublicIPv4Port uint16
  20. PublicIPv6Port uint16
  21. StatsPort uint16
  22. BindIP net.IP
  23. PublicIPv4 net.IP
  24. PublicIPv6 net.IP
  25. StatsIP net.IP
  26. Secret []byte
  27. AdTag []byte
  28. }
  29. // URLs contains links to the proxy (tg://, t.me) and their QR codes.
  30. type URLs struct {
  31. TG string `json:"tg_url"`
  32. TMe string `json:"tme_url"`
  33. TGQRCode string `json:"tg_qrcode"`
  34. TMeQRCode string `json:"tme_qrcode"`
  35. }
  36. // IPURLs contains links to both ipv4 and ipv6 of the proxy.
  37. type IPURLs struct {
  38. IPv4 URLs `json:"ipv4"`
  39. IPv6 URLs `json:"ipv6"`
  40. }
  41. // BindAddr returns connection for this server to bind to.
  42. func (c *Config) BindAddr() string {
  43. return getAddr(c.BindIP, c.BindPort)
  44. }
  45. // StatAddr returns connection string to the stats API.
  46. func (c *Config) StatAddr() string {
  47. return getAddr(c.StatsIP, c.StatsPort)
  48. }
  49. // UseMiddleProxy defines if this proxy has to connect middle proxies
  50. // which supports promoted channels or directly access Telegram.
  51. func (c *Config) UseMiddleProxy() bool {
  52. return len(c.AdTag) > 0
  53. }
  54. // GetURLs returns configured IPURLs instance with links to this server.
  55. func (c *Config) GetURLs() IPURLs {
  56. urls := IPURLs{}
  57. if c.PublicIPv4 != nil {
  58. urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret)
  59. }
  60. if c.PublicIPv6 != nil {
  61. urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret)
  62. }
  63. return urls
  64. }
  65. func getAddr(host fmt.Stringer, port uint16) string {
  66. return net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
  67. }
  68. // NewConfig returns new configuration. If required, it manages and
  69. // fetches data from external sources. Parameters passed to this
  70. // function, should come from command line arguments.
  71. func NewConfig(debug, verbose bool, // nolint: gocyclo
  72. bindIP net.IP, bindPort uint16,
  73. publicIPv4 net.IP, PublicIPv4Port uint16,
  74. publicIPv6 net.IP, publicIPv6Port uint16,
  75. statsIP net.IP, statsPort uint16,
  76. secret, adtag string) (*Config, error) {
  77. if len(secret) != 32 {
  78. return nil, errors.New("Telegram demands secret of length 32")
  79. }
  80. secretBytes, err := hex.DecodeString(secret)
  81. if err != nil {
  82. return nil, errors.Annotate(err, "Cannot create config")
  83. }
  84. var adTagBytes []byte
  85. if len(adtag) != 0 {
  86. adTagBytes, err = hex.DecodeString(adtag)
  87. if err != nil {
  88. return nil, errors.Annotate(err, "Cannot create config")
  89. }
  90. }
  91. if publicIPv4 == nil {
  92. publicIPv4, err = getGlobalIPv4()
  93. if err != nil {
  94. publicIPv4 = nil
  95. } else if publicIPv4.To4() == nil {
  96. return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
  97. }
  98. }
  99. if PublicIPv4Port == 0 {
  100. PublicIPv4Port = bindPort
  101. }
  102. if publicIPv6 == nil {
  103. publicIPv6, err = getGlobalIPv6()
  104. if err != nil {
  105. publicIPv6 = nil
  106. } else if publicIPv6.To4() != nil {
  107. return nil, errors.Errorf("IP %s is not IPv6", publicIPv6.String())
  108. }
  109. }
  110. if publicIPv6Port == 0 {
  111. publicIPv6Port = bindPort
  112. }
  113. if statsIP == nil {
  114. statsIP = publicIPv4
  115. }
  116. conf := &Config{
  117. Debug: debug,
  118. Verbose: verbose,
  119. BindIP: bindIP,
  120. BindPort: bindPort,
  121. PublicIPv4: publicIPv4,
  122. PublicIPv4Port: PublicIPv4Port,
  123. PublicIPv6: publicIPv6,
  124. PublicIPv6Port: publicIPv6Port,
  125. StatsIP: statsIP,
  126. StatsPort: statsPort,
  127. Secret: secretBytes,
  128. AdTag: adTagBytes,
  129. }
  130. return conf, nil
  131. }