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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

network.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  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.Add(1)
  66. go func() {
  67. defer wg.Done()
  68. resolved := n.dns.LookupA(address)
  69. mutex.Lock()
  70. ips = append(ips, resolved...)
  71. mutex.Unlock()
  72. }()
  73. }
  74. switch protocol {
  75. case "tcp", "tcp6":
  76. wg.Add(1)
  77. go func() {
  78. defer wg.Done()
  79. resolved := n.dns.LookupAAAA(address)
  80. mutex.Lock()
  81. ips = append(ips, resolved...)
  82. mutex.Unlock()
  83. }()
  84. }
  85. wg.Wait()
  86. if len(ips) == 0 {
  87. return nil, fmt.Errorf("cannot find any ips for %s:%s", protocol, address)
  88. }
  89. return ips, nil
  90. }
  91. // NewNetwork assembles an mtglib.Network compatible structure based on a
  92. // dialer and given params.
  93. //
  94. // It brings simple DNS cache and DNS-Over-HTTPS when necessary.
  95. func NewNetwork(dialer Dialer,
  96. userAgent, dohHostname string,
  97. httpTimeout time.Duration,
  98. ) (mtglib.Network, error) {
  99. switch {
  100. case httpTimeout < 0:
  101. return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
  102. case httpTimeout == 0:
  103. httpTimeout = DefaultHTTPTimeout
  104. }
  105. if net.ParseIP(dohHostname) == nil {
  106. return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
  107. }
  108. return &network{
  109. dialer: dialer,
  110. httpTimeout: httpTimeout,
  111. userAgent: userAgent,
  112. dns: newDNSResolver(dohHostname,
  113. makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext)),
  114. }, nil
  115. }
  116. func makeHTTPClient(userAgent string,
  117. timeout time.Duration,
  118. dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error),
  119. ) *http.Client {
  120. return &http.Client{
  121. Timeout: timeout,
  122. Transport: networkHTTPTransport{
  123. userAgent: userAgent,
  124. next: &http.Transport{
  125. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  126. return dialFunc(ctx, network, address)
  127. },
  128. },
  129. },
  130. }
  131. }