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.4KB

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