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

doctor_test.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package cli
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "crypto/tls"
  8. "crypto/x509"
  9. "crypto/x509/pkix"
  10. "math/big"
  11. "net"
  12. "strings"
  13. "testing"
  14. "time"
  15. )
  16. // makeCert builds a self-signed leaf certificate valid for the supplied DNS
  17. // name (and IP, so dialing 127.0.0.1 still reaches the listener) plus a
  18. // matching tls.Config and an x509 pool that trusts it.
  19. func makeCert(t *testing.T, dnsName string, notAfter time.Time) (tls.Certificate, *x509.CertPool) {
  20. t.Helper()
  21. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  22. if err != nil {
  23. t.Fatalf("generate key: %v", err)
  24. }
  25. tmpl := &x509.Certificate{
  26. SerialNumber: big.NewInt(1),
  27. Subject: pkix.Name{CommonName: dnsName},
  28. NotBefore: time.Now().Add(-time.Hour),
  29. NotAfter: notAfter,
  30. KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
  31. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  32. BasicConstraintsValid: true,
  33. IsCA: true,
  34. DNSNames: []string{dnsName},
  35. IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
  36. }
  37. der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
  38. if err != nil {
  39. t.Fatalf("create certificate: %v", err)
  40. }
  41. leaf, err := x509.ParseCertificate(der)
  42. if err != nil {
  43. t.Fatalf("parse certificate: %v", err)
  44. }
  45. pool := x509.NewCertPool()
  46. pool.AddCert(leaf)
  47. return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: leaf}, pool
  48. }
  49. // startTLSServer spins up a TLS listener that completes handshakes using cert
  50. // and returns its address. It is closed when the test finishes.
  51. func startTLSServer(t *testing.T, cert tls.Certificate) string {
  52. t.Helper()
  53. ln, err := tls.Listen("tcp", "127.0.0.1:0", &tls.Config{
  54. Certificates: []tls.Certificate{cert},
  55. MinVersion: tls.VersionTLS12,
  56. })
  57. if err != nil {
  58. t.Fatalf("listen: %v", err)
  59. }
  60. t.Cleanup(func() { _ = ln.Close() })
  61. go func() {
  62. for {
  63. conn, err := ln.Accept()
  64. if err != nil {
  65. return
  66. }
  67. go func() {
  68. // Drive the handshake so the client side completes, then drop.
  69. if tc, ok := conn.(*tls.Conn); ok {
  70. _ = tc.Handshake()
  71. }
  72. _ = conn.Close()
  73. }()
  74. }
  75. }()
  76. return ln.Addr().String()
  77. }
  78. func TestProbeFrontingTLS_ValidCert(t *testing.T) {
  79. cert, pool := makeCert(t, "front.example.org", time.Now().Add(24*time.Hour))
  80. addr := startTLSServer(t, cert)
  81. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  82. defer cancel()
  83. err := probeFrontingTLS(ctx, &net.Dialer{}, addr, "front.example.org", pool)
  84. if err != nil {
  85. t.Fatalf("expected success, got error: %v", err)
  86. }
  87. }
  88. func TestProbeFrontingTLS_WrongHost(t *testing.T) {
  89. // Cert is for front.example.org, but we verify against other.example.org.
  90. cert, pool := makeCert(t, "front.example.org", time.Now().Add(24*time.Hour))
  91. addr := startTLSServer(t, cert)
  92. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  93. defer cancel()
  94. err := probeFrontingTLS(ctx, &net.Dialer{}, addr, "other.example.org", pool)
  95. if err == nil {
  96. t.Fatal("expected SAN-mismatch failure, got success")
  97. }
  98. if !strings.Contains(err.Error(), "x509") {
  99. t.Fatalf("expected x509 verification error, got: %v", err)
  100. }
  101. }
  102. func TestProbeFrontingTLS_UntrustedCA(t *testing.T) {
  103. // Server cert is self-signed; we hand the client an empty pool that does
  104. // not trust it. Default verification must reject.
  105. cert, _ := makeCert(t, "front.example.org", time.Now().Add(24*time.Hour))
  106. addr := startTLSServer(t, cert)
  107. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  108. defer cancel()
  109. err := probeFrontingTLS(ctx, &net.Dialer{}, addr, "front.example.org", x509.NewCertPool())
  110. if err == nil {
  111. t.Fatal("expected untrusted-CA failure, got success")
  112. }
  113. if !strings.Contains(err.Error(), "x509") {
  114. t.Fatalf("expected x509 verification error, got: %v", err)
  115. }
  116. }
  117. func TestProbeFrontingTLS_ExpiredCert(t *testing.T) {
  118. // Same trust anchor, but the cert is already expired.
  119. cert, pool := makeCert(t, "front.example.org", time.Now().Add(-time.Hour))
  120. addr := startTLSServer(t, cert)
  121. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  122. defer cancel()
  123. err := probeFrontingTLS(ctx, &net.Dialer{}, addr, "front.example.org", pool)
  124. if err == nil {
  125. t.Fatal("expected expiry failure, got success")
  126. }
  127. if !strings.Contains(err.Error(), "expired") {
  128. t.Fatalf("expected expiry error, got: %v", err)
  129. }
  130. }
  131. func TestProbeFrontingTLS_OverrideDialDifferentFromSNI(t *testing.T) {
  132. // Domain-fronting override: dial one address (the listener bound to
  133. // 127.0.0.1), but verify the cert against the secret host name. The cert
  134. // is issued for the secret host, so verification must pass even though the
  135. // dial target is a bare IP:port.
  136. cert, pool := makeCert(t, "secret.example.org", time.Now().Add(24*time.Hour))
  137. addr := startTLSServer(t, cert) // e.g. 127.0.0.1:NNNNN
  138. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  139. defer cancel()
  140. // dial-target (addr, an IP:port) != SNI (secret.example.org)
  141. err := probeFrontingTLS(ctx, &net.Dialer{}, addr, "secret.example.org", pool)
  142. if err != nil {
  143. t.Fatalf("expected success when dialing override addr with secret-host SNI, got: %v", err)
  144. }
  145. }