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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. doh "github.com/babolivier/go-doh-client"
  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)
  20. }
  21. type network struct {
  22. dialer Dialer
  23. dns doh.Resolver
  24. idleTimeout time.Duration
  25. httpTimeout time.Duration
  26. userAgent string
  27. }
  28. func (n *network) Dial(protocol, address string) (net.Conn, error) {
  29. return n.DialContext(context.Background(), protocol, address)
  30. }
  31. func (n *network) DialContext(ctx context.Context, protocol, address string) (net.Conn, error) {
  32. host, port, _ := net.SplitHostPort(address)
  33. ips, err := n.dnsResolve(protocol, host)
  34. if err != nil {
  35. return nil, fmt.Errorf("cannot resolve dns names: %w", err)
  36. }
  37. if len(ips) > 1 {
  38. rand.Shuffle(len(ips), func(i, j int) {
  39. ips[i], ips[j] = ips[j], ips[i]
  40. })
  41. }
  42. var conn net.Conn
  43. for _, v := range ips {
  44. conn, err = n.dialer.DialContext(ctx, protocol, net.JoinHostPort(v, port))
  45. if err == nil {
  46. return conn, nil
  47. }
  48. }
  49. return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err)
  50. }
  51. func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
  52. network, address string) (net.Conn, error)) *http.Client {
  53. if dialFunc == nil {
  54. dialFunc = n.DialContext
  55. }
  56. return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
  57. }
  58. func (n *network) IdleTimeout() time.Duration {
  59. return n.idleTimeout
  60. }
  61. func (n *network) dnsResolve(protocol, address string) ([]string, error) {
  62. if net.ParseIP(address) != nil {
  63. return []string{address}, nil
  64. }
  65. ips := []string{}
  66. wg := &sync.WaitGroup{}
  67. mutex := &sync.Mutex{}
  68. switch protocol {
  69. case "tcp", "tcp4":
  70. wg.Add(1)
  71. go func() {
  72. defer wg.Done()
  73. if recs, _, err := n.dns.LookupA(address); err == nil {
  74. mutex.Lock()
  75. defer mutex.Unlock()
  76. for _, v := range recs {
  77. ips = append(ips, v.IP4)
  78. }
  79. }
  80. }()
  81. }
  82. switch protocol {
  83. case "tcp", "tcp6":
  84. wg.Add(1)
  85. go func() {
  86. defer wg.Done()
  87. if recs, _, err := n.dns.LookupAAAA(address); err == nil {
  88. mutex.Lock()
  89. defer mutex.Unlock()
  90. for _, v := range recs {
  91. ips = append(ips, v.IP6)
  92. }
  93. }
  94. }()
  95. }
  96. wg.Wait()
  97. if len(ips) == 0 {
  98. return nil, fmt.Errorf("cannot find any ips for %s:%s", protocol, address)
  99. }
  100. return ips, nil
  101. }
  102. func NewNetwork(dialer Dialer,
  103. userAgent, dohHostname string,
  104. httpTimeout, idleTimeout time.Duration) (mtglib.Network, error) {
  105. switch {
  106. case idleTimeout < 0:
  107. return nil, fmt.Errorf("timeout should be positive number %s", idleTimeout)
  108. case idleTimeout == 0:
  109. idleTimeout = DefaultIdleTimeout
  110. }
  111. if net.ParseIP(dohHostname) == nil {
  112. return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
  113. }
  114. return &network{
  115. dialer: dialer,
  116. idleTimeout: idleTimeout,
  117. httpTimeout: httpTimeout,
  118. userAgent: userAgent,
  119. dns: doh.Resolver{
  120. Host: dohHostname,
  121. Class: doh.IN,
  122. HTTPClient: makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext),
  123. },
  124. }, nil
  125. }
  126. func makeHTTPClient(userAgent string,
  127. timeout time.Duration,
  128. dialFunc func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client {
  129. return &http.Client{
  130. Timeout: timeout,
  131. Transport: networkHTTPTransport{
  132. userAgent: userAgent,
  133. next: &http.Transport{
  134. DialContext: dialFunc,
  135. },
  136. },
  137. }
  138. }