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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

network.go 2.8KB

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