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 3.1KB

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