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 kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

network.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 httpTimeout < 0:
  107. return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
  108. case httpTimeout == 0:
  109. httpTimeout = DefaultHTTPTimeout
  110. }
  111. switch {
  112. case idleTimeout < 0:
  113. return nil, fmt.Errorf("timeout should be positive number %s", idleTimeout)
  114. case idleTimeout == 0:
  115. idleTimeout = DefaultIdleTimeout
  116. }
  117. if net.ParseIP(dohHostname) == nil {
  118. return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
  119. }
  120. return &network{
  121. dialer: dialer,
  122. idleTimeout: idleTimeout,
  123. httpTimeout: httpTimeout,
  124. userAgent: userAgent,
  125. dns: doh.Resolver{
  126. Host: dohHostname,
  127. Class: doh.IN,
  128. HTTPClient: makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext),
  129. },
  130. }, nil
  131. }
  132. func makeHTTPClient(userAgent string,
  133. timeout time.Duration,
  134. dialFunc func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client {
  135. return &http.Client{
  136. Timeout: timeout,
  137. Transport: networkHTTPTransport{
  138. userAgent: userAgent,
  139. next: &http.Transport{
  140. DialContext: dialFunc,
  141. },
  142. },
  143. }
  144. }