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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package cli
  2. import (
  3. "context"
  4. "net"
  5. "github.com/9seconds/mtg/v2/internal/config"
  6. "github.com/9seconds/mtg/v2/mtglib"
  7. )
  8. // sniCheckResult holds the data gathered while comparing the secret
  9. // hostname's DNS records against this server's public IP addresses.
  10. //
  11. // IPv4Match / IPv6Match report whether a resolved record actually equals the
  12. // corresponding public IP. They are false when that family's public IP could
  13. // not be determined — there is nothing to compare against. Callers decide
  14. // what counts as a clean result from these fields: `mtg doctor` and the
  15. // startup warning apply different rules.
  16. type sniCheckResult struct {
  17. Resolved []net.IP
  18. OurIPv4 net.IP
  19. OurIPv6 net.IP
  20. IPv4Match bool
  21. IPv6Match bool
  22. ResolveErr error
  23. }
  24. // PublicIPKnown reports whether at least one public IP family was detected.
  25. func (r sniCheckResult) PublicIPKnown() bool {
  26. return r.OurIPv4 != nil || r.OurIPv6 != nil
  27. }
  28. // runSNICheck resolves conf.Secret.Host and compares the records with this
  29. // server's public IPv4 and IPv6. Public IPs come from config first and fall
  30. // back to on-the-fly detection via ntw. It gathers data only — it does not
  31. // decide success; see sniCheckResult.
  32. func runSNICheck(
  33. ctx context.Context,
  34. resolver *net.Resolver,
  35. conf *config.Config,
  36. ntw mtglib.Network,
  37. ) sniCheckResult {
  38. res := sniCheckResult{}
  39. addrs, err := resolver.LookupIPAddr(ctx, conf.Secret.Host)
  40. if err != nil {
  41. res.ResolveErr = err
  42. return res
  43. }
  44. res.Resolved = make([]net.IP, 0, len(addrs))
  45. for _, a := range addrs {
  46. res.Resolved = append(res.Resolved, a.IP)
  47. }
  48. res.OurIPv4 = conf.PublicIPv4.Get(nil)
  49. if res.OurIPv4 == nil {
  50. res.OurIPv4 = getIP(ntw, "tcp4")
  51. }
  52. res.OurIPv6 = conf.PublicIPv6.Get(nil)
  53. if res.OurIPv6 == nil {
  54. res.OurIPv6 = getIP(ntw, "tcp6")
  55. }
  56. for _, ip := range res.Resolved {
  57. if res.OurIPv4 != nil && ip.String() == res.OurIPv4.String() {
  58. res.IPv4Match = true
  59. }
  60. if res.OurIPv6 != nil && ip.String() == res.OurIPv6.String() {
  61. res.IPv6Match = true
  62. }
  63. }
  64. return res
  65. }