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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. HTTP http.Client
  14. DNS doh.Resolver
  15. dialer Dialer
  16. }
  17. func (d *Network) Dial(network, address string) (net.Conn, error) {
  18. return d.DialContext(context.Background(), network, address)
  19. }
  20. func (d *Network) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
  21. host, port, _ := net.SplitHostPort(address)
  22. ips, err := d.resolveIPs(network, 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. for _, v := range ips {
  32. if conn, err := d.dialer.DialContext(ctx, network, net.JoinHostPort(v, port)); err == nil {
  33. return conn, nil
  34. }
  35. }
  36. return nil, fmt.Errorf("cannot dial to %s:%s", network, address)
  37. }
  38. func (d *Network) resolveIPs(network, address string) ([]string, error) {
  39. if net.ParseIP(address) != nil {
  40. return []string{address}, nil
  41. }
  42. ips := []string{}
  43. wg := &sync.WaitGroup{}
  44. mutex := &sync.Mutex{}
  45. switch network {
  46. case "tcp", "tcp4":
  47. wg.Add(1)
  48. go func() {
  49. defer wg.Done()
  50. if recs, _, err := d.DNS.LookupA(address); err == nil {
  51. mutex.Lock()
  52. defer mutex.Unlock()
  53. for _, v := range recs {
  54. ips = append(ips, v.IP4)
  55. }
  56. }
  57. }()
  58. }
  59. switch network {
  60. case "tcp", "tcp6":
  61. wg.Add(1)
  62. go func() {
  63. defer wg.Done()
  64. if recs, _, err := d.DNS.LookupAAAA(address); err == nil {
  65. mutex.Lock()
  66. defer mutex.Unlock()
  67. for _, v := range recs {
  68. ips = append(ips, v.IP6)
  69. }
  70. }
  71. }()
  72. }
  73. wg.Wait()
  74. if len(ips) == 0 {
  75. return nil, fmt.Errorf("cannot find any ips for %s:%s", network, address)
  76. }
  77. return ips, nil
  78. }
  79. func NewNetwork(dialer Dialer, dohHostname string, httpTimeout time.Duration) (*Network, error) {
  80. switch {
  81. case httpTimeout < 0:
  82. return nil, fmt.Errorf("timeout should be positive number %v", httpTimeout)
  83. case httpTimeout == 0:
  84. httpTimeout = DefaultHTTPTimeout
  85. }
  86. if net.ParseIP(dohHostname) == nil {
  87. return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
  88. }
  89. dohHTTPClient := &http.Client{
  90. Timeout: DefaultDNSTimeout,
  91. Transport: &http.Transport{
  92. DialContext: dialer.DialContext,
  93. },
  94. }
  95. network := &Network{
  96. dialer: dialer,
  97. DNS: doh.Resolver{
  98. Host: dohHostname,
  99. Class: doh.IN,
  100. HTTPClient: dohHTTPClient,
  101. },
  102. }
  103. network.HTTP = http.Client{
  104. Timeout: httpTimeout,
  105. Transport: &http.Transport{
  106. DialContext: network.DialContext,
  107. },
  108. }
  109. return network, nil
  110. }