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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

network.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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) NativeDialer() *net.Dialer {
  49. return &net.Dialer{}
  50. }
  51. func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
  52. network, address string) (essentials.Conn, error),
  53. ) *http.Client {
  54. if dialFunc == nil {
  55. dialFunc = n.DialContext
  56. }
  57. return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
  58. }
  59. func (n *network) dnsResolve(protocol, address string) ([]string, error) {
  60. if net.ParseIP(address) != nil {
  61. return []string{address}, nil
  62. }
  63. ips := []string{}
  64. wg := &sync.WaitGroup{}
  65. mutex := &sync.Mutex{}
  66. switch protocol {
  67. case "tcp", "tcp4":
  68. wg.Go(func() {
  69. resolved := n.dns.LookupA(address)
  70. mutex.Lock()
  71. ips = append(ips, resolved...)
  72. mutex.Unlock()
  73. })
  74. }
  75. switch protocol {
  76. case "tcp", "tcp6":
  77. wg.Go(func() {
  78. resolved := n.dns.LookupAAAA(address)
  79. mutex.Lock()
  80. ips = append(ips, resolved...)
  81. mutex.Unlock()
  82. })
  83. }
  84. wg.Wait()
  85. if len(ips) == 0 {
  86. return nil, fmt.Errorf("cannot find any ips for %s:%s", protocol, address)
  87. }
  88. return ips, nil
  89. }
  90. // NewNetwork assembles an mtglib.Network compatible structure based on a
  91. // dialer and given params.
  92. //
  93. // It brings simple DNS cache and DNS-Over-HTTPS when necessary.
  94. func NewNetwork(dialer Dialer,
  95. userAgent, dohHostname string,
  96. httpTimeout time.Duration,
  97. ) (mtglib.Network, error) {
  98. switch {
  99. case httpTimeout < 0:
  100. return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
  101. case httpTimeout == 0:
  102. httpTimeout = DefaultHTTPTimeout
  103. }
  104. if net.ParseIP(dohHostname) == nil {
  105. return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
  106. }
  107. return &network{
  108. dialer: dialer,
  109. httpTimeout: httpTimeout,
  110. userAgent: userAgent,
  111. dns: newDNSResolver(dohHostname,
  112. makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext)),
  113. }, nil
  114. }
  115. func makeHTTPClient(userAgent string,
  116. timeout time.Duration,
  117. dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error),
  118. ) *http.Client {
  119. return &http.Client{
  120. Timeout: timeout,
  121. Transport: networkHTTPTransport{
  122. userAgent: userAgent,
  123. next: &http.Transport{
  124. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  125. return dialFunc(ctx, network, address)
  126. },
  127. },
  128. },
  129. }
  130. }