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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand/v2"
  6. "net"
  7. "net/http"
  8. "sync"
  9. "time"
  10. "github.com/9seconds/mtg/v2/essentials"
  11. "github.com/9seconds/mtg/v2/mtglib"
  12. )
  13. type networkHTTPTransport struct {
  14. userAgent string
  15. next http.RoundTripper
  16. }
  17. func (n networkHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  18. req.Header.Set("User-Agent", n.userAgent)
  19. return n.next.RoundTrip(req) //nolint: wrapcheck
  20. }
  21. type network struct {
  22. dialer Dialer
  23. httpTimeout time.Duration
  24. userAgent string
  25. dns *dnsResolver
  26. }
  27. func (n *network) Dial(protocol, address string) (essentials.Conn, error) {
  28. return n.DialContext(context.Background(), protocol, address)
  29. }
  30. func (n *network) DialContext(ctx context.Context, protocol, address string) (essentials.Conn, error) {
  31. host, port, _ := net.SplitHostPort(address)
  32. ips, err := n.dnsResolve(protocol, host)
  33. if err != nil {
  34. return nil, fmt.Errorf("cannot resolve dns names: %w", err)
  35. }
  36. rand.Shuffle(len(ips), func(i, j int) {
  37. ips[i], ips[j] = ips[j], ips[i]
  38. })
  39. var conn essentials.Conn
  40. for _, v := range ips {
  41. conn, err = n.dialer.DialContext(ctx, protocol, net.JoinHostPort(v, port))
  42. if err == nil {
  43. return conn, nil
  44. }
  45. }
  46. return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err)
  47. }
  48. func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
  49. network, address string) (essentials.Conn, error),
  50. ) *http.Client {
  51. if dialFunc == nil {
  52. dialFunc = n.DialContext
  53. }
  54. return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
  55. }
  56. func (n *network) dnsResolve(protocol, address string) ([]string, error) {
  57. if net.ParseIP(address) != nil {
  58. return []string{address}, nil
  59. }
  60. ips := []string{}
  61. wg := &sync.WaitGroup{}
  62. mutex := &sync.Mutex{}
  63. switch protocol {
  64. case "tcp", "tcp4":
  65. wg.Go(func() {
  66. resolved := n.dns.LookupA(address)
  67. mutex.Lock()
  68. ips = append(ips, resolved...)
  69. mutex.Unlock()
  70. })
  71. }
  72. switch protocol {
  73. case "tcp", "tcp6":
  74. wg.Go(func() {
  75. resolved := n.dns.LookupAAAA(address)
  76. mutex.Lock()
  77. ips = append(ips, resolved...)
  78. mutex.Unlock()
  79. })
  80. }
  81. wg.Wait()
  82. if len(ips) == 0 {
  83. return nil, fmt.Errorf("cannot find any ips for %s:%s", protocol, address)
  84. }
  85. return ips, nil
  86. }
  87. // NewNetwork assembles an mtglib.Network compatible structure based on a
  88. // dialer and given params.
  89. //
  90. // It brings simple DNS cache and DNS-Over-HTTPS when necessary.
  91. func NewNetwork(dialer Dialer,
  92. userAgent, dohHostname string,
  93. httpTimeout time.Duration,
  94. ) (mtglib.Network, error) {
  95. switch {
  96. case httpTimeout < 0:
  97. return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
  98. case httpTimeout == 0:
  99. httpTimeout = DefaultHTTPTimeout
  100. }
  101. if net.ParseIP(dohHostname) == nil {
  102. return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
  103. }
  104. return &network{
  105. dialer: dialer,
  106. httpTimeout: httpTimeout,
  107. userAgent: userAgent,
  108. dns: newDNSResolver(dohHostname,
  109. makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext)),
  110. }, nil
  111. }
  112. func makeHTTPClient(userAgent string,
  113. timeout time.Duration,
  114. dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error),
  115. ) *http.Client {
  116. return &http.Client{
  117. Timeout: timeout,
  118. Transport: networkHTTPTransport{
  119. userAgent: userAgent,
  120. next: &http.Transport{
  121. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  122. return dialFunc(ctx, network, address)
  123. },
  124. },
  125. },
  126. }
  127. }