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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 captures the outcome of comparing the secret hostname's DNS
  9. // records with this server's public IP addresses.
  10. //
  11. // IPv4Match/IPv6Match are true when either a matching record was found, or
  12. // when the corresponding public IP could not be detected — in which case
  13. // there is nothing to compare against.
  14. type sniCheckResult struct {
  15. Host string
  16. Resolved []net.IP
  17. OurIPv4 net.IP
  18. OurIPv6 net.IP
  19. IPv4Match bool
  20. IPv6Match bool
  21. ResolveErr error
  22. }
  23. // Known reports whether at least one public IP family was detected.
  24. func (r sniCheckResult) Known() bool {
  25. return r.OurIPv4 != nil || r.OurIPv6 != nil
  26. }
  27. // OK reports whether every detected public IP family matches a resolved
  28. // record. A partial match (one family matches, another does not) is not OK.
  29. func (r sniCheckResult) OK() bool {
  30. return r.ResolveErr == nil && r.IPv4Match && r.IPv6Match
  31. }
  32. // runSNICheck resolves conf.Secret.Host and compares the result with the
  33. // server's public IPv4 and IPv6. Public IPs come from config first and fall
  34. // back to on-the-fly detection via ntw.
  35. func runSNICheck(ctx context.Context,
  36. resolver *net.Resolver,
  37. conf *config.Config,
  38. ntw mtglib.Network,
  39. ) sniCheckResult {
  40. res := sniCheckResult{Host: conf.Secret.Host}
  41. if res.Host == "" {
  42. res.IPv4Match = true
  43. res.IPv6Match = true
  44. return res
  45. }
  46. addrs, err := resolver.LookupIPAddr(ctx, res.Host)
  47. if err != nil {
  48. res.ResolveErr = err
  49. return res
  50. }
  51. res.Resolved = make([]net.IP, 0, len(addrs))
  52. for _, a := range addrs {
  53. res.Resolved = append(res.Resolved, a.IP)
  54. }
  55. res.OurIPv4 = conf.PublicIPv4.Get(nil)
  56. if res.OurIPv4 == nil {
  57. res.OurIPv4 = getIP(ntw, "tcp4")
  58. }
  59. res.OurIPv6 = conf.PublicIPv6.Get(nil)
  60. if res.OurIPv6 == nil {
  61. res.OurIPv6 = getIP(ntw, "tcp6")
  62. }
  63. res.IPv4Match = res.OurIPv4 == nil
  64. res.IPv6Match = res.OurIPv6 == nil
  65. for _, ip := range res.Resolved {
  66. if res.OurIPv4 != nil && ip.String() == res.OurIPv4.String() {
  67. res.IPv4Match = true
  68. }
  69. if res.OurIPv6 != nil && ip.String() == res.OurIPv6.String() {
  70. res.IPv6Match = true
  71. }
  72. }
  73. return res
  74. }