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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

network.go 3.3KB

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