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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 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. rand.Shuffle(len(ips), func(i, j int) {
  37. ips[i], ips[j] = ips[j], ips[i]
  38. })
  39. var conn net.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) MakeHTTPClient(dialFunc func(ctx context.Context,
  49. network, address string) (net.Conn, error)) *http.Client {
  50. if dialFunc == nil {
  51. dialFunc = n.DialContext
  52. }
  53. return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
  54. }
  55. func (n *network) dnsResolve(protocol, address string) ([]string, error) {
  56. if net.ParseIP(address) != nil {
  57. return []string{address}, nil
  58. }
  59. ips := []string{}
  60. wg := &sync.WaitGroup{}
  61. mutex := &sync.Mutex{}
  62. switch protocol {
  63. case "tcp", "tcp4":
  64. wg.Add(1)
  65. go func() {
  66. defer wg.Done()
  67. if recs, _, err := n.dns.LookupA(address); err == nil {
  68. mutex.Lock()
  69. defer mutex.Unlock()
  70. for _, v := range recs {
  71. ips = append(ips, v.IP4)
  72. }
  73. }
  74. }()
  75. }
  76. switch protocol {
  77. case "tcp", "tcp6":
  78. wg.Add(1)
  79. go func() {
  80. defer wg.Done()
  81. if recs, _, err := n.dns.LookupAAAA(address); err == nil {
  82. mutex.Lock()
  83. defer mutex.Unlock()
  84. for _, v := range recs {
  85. ips = append(ips, v.IP6)
  86. }
  87. }
  88. }()
  89. }
  90. wg.Wait()
  91. if len(ips) == 0 {
  92. return nil, fmt.Errorf("cannot find any ips for %s:%s", protocol, address)
  93. }
  94. return ips, nil
  95. }
  96. func NewNetwork(dialer Dialer,
  97. userAgent, dohHostname string,
  98. httpTimeout time.Duration) (mtglib.Network, error) {
  99. switch {
  100. case httpTimeout < 0:
  101. return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
  102. case httpTimeout == 0:
  103. httpTimeout = DefaultHTTPTimeout
  104. }
  105. if net.ParseIP(dohHostname) == nil {
  106. return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
  107. }
  108. return &network{
  109. dialer: dialer,
  110. httpTimeout: httpTimeout,
  111. userAgent: userAgent,
  112. dns: doh.Resolver{
  113. Host: dohHostname,
  114. Class: doh.IN,
  115. HTTPClient: makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext),
  116. },
  117. }, nil
  118. }
  119. func makeHTTPClient(userAgent string,
  120. timeout time.Duration,
  121. dialFunc func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client {
  122. return &http.Client{
  123. Timeout: timeout,
  124. Transport: networkHTTPTransport{
  125. userAgent: userAgent,
  126. next: &http.Transport{
  127. DialContext: dialFunc,
  128. },
  129. },
  130. }
  131. }