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 символов.

sni_check.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package cli
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "sync"
  7. "github.com/9seconds/mtg/v2/internal/config"
  8. "github.com/9seconds/mtg/v2/mtglib"
  9. )
  10. type sniCheckResult struct {
  11. ResolvedIP4 []string
  12. ResolvedIP6 []string
  13. OurIP4 string
  14. OurIP6 string
  15. }
  16. func runSNICheck(
  17. ctx context.Context,
  18. conf *config.Config,
  19. resolver *net.Resolver,
  20. ntw mtglib.Network,
  21. ) (sniCheckResult, error) {
  22. res := sniCheckResult{}
  23. ctx, cancelCause := context.WithCancelCause(ctx)
  24. defer cancelCause(nil)
  25. addrs, err := resolver.LookupIPAddr(ctx, conf.Secret.Host)
  26. if err != nil {
  27. return res, fmt.Errorf("cannot resolve addresses of %s: %w", conf.Secret.Host, err)
  28. }
  29. if len(addrs) == 0 {
  30. return res, fmt.Errorf("no known addresses for %s", conf.Secret.Host)
  31. }
  32. for _, addr := range addrs {
  33. if ip := addr.IP.To4(); ip == nil {
  34. res.ResolvedIP6 = append(res.ResolvedIP6, addr.IP.To16().String())
  35. } else {
  36. res.ResolvedIP4 = append(res.ResolvedIP4, ip.String())
  37. }
  38. }
  39. wg := &sync.WaitGroup{}
  40. if len(res.ResolvedIP4) > 0 {
  41. wg.Go(func() {
  42. var err error
  43. ip := conf.PublicIPv4.Get(nil)
  44. if ip == nil {
  45. ip, err = getIP(ctx, ntw, "tcp4")
  46. if err != nil {
  47. cancelCause(err)
  48. }
  49. }
  50. if ip != nil {
  51. res.OurIP4 = ip.To4().String()
  52. }
  53. })
  54. }
  55. if len(res.ResolvedIP6) > 0 {
  56. wg.Go(func() {
  57. var err error
  58. ip := conf.PublicIPv6.Get(nil)
  59. if ip == nil {
  60. ip, err = getIP(ctx, ntw, "tcp6")
  61. if err != nil {
  62. cancelCause(err)
  63. }
  64. }
  65. if ip != nil {
  66. res.OurIP6 = ip.To16().String()
  67. }
  68. })
  69. }
  70. wg.Wait()
  71. return res, context.Cause(ctx)
  72. }