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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

network.go 3.3KB

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