|
| 1 | +package certificate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "net" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/quic-go/quic-go" |
| 11 | +) |
| 12 | + |
| 13 | +type HTTPVersionSupport struct { |
| 14 | + Version string |
| 15 | + Supported bool |
| 16 | +} |
| 17 | + |
| 18 | +var probedHTTPVersions = []struct { |
| 19 | + alpn string |
| 20 | + name string |
| 21 | + quic bool |
| 22 | +}{ |
| 23 | + {"http/1.1", "HTTP/1.1", false}, |
| 24 | + {"h2", "HTTP/2", false}, |
| 25 | + {"h3", "HTTP/3", true}, |
| 26 | +} |
| 27 | + |
| 28 | +// Shared so quic-go's UDP buffer-size check logs at most once at process startup |
| 29 | +// (per quic-go/wiki/UDP-Buffer-Sizes), and so probes reuse a single UDP socket. |
| 30 | +var ( |
| 31 | + h3TransportOnce sync.Once |
| 32 | + h3Transport *quic.Transport |
| 33 | +) |
| 34 | + |
| 35 | +func sharedH3Transport() *quic.Transport { |
| 36 | + h3TransportOnce.Do(func() { |
| 37 | + udp, err := net.ListenUDP("udp", &net.UDPAddr{}) |
| 38 | + if err != nil { |
| 39 | + return |
| 40 | + } |
| 41 | + h3Transport = &quic.Transport{Conn: udp} |
| 42 | + }) |
| 43 | + return h3Transport |
| 44 | +} |
| 45 | + |
| 46 | +func probeHTTPVersions(hostPort string, timeout time.Duration) []HTTPVersionSupport { |
| 47 | + results := make([]HTTPVersionSupport, len(probedHTTPVersions)) |
| 48 | + var wg sync.WaitGroup |
| 49 | + dialer := &net.Dialer{Timeout: timeout} |
| 50 | + for i, pv := range probedHTTPVersions { |
| 51 | + wg.Add(1) |
| 52 | + go func(i int, alpn, name string, isQUIC bool) { |
| 53 | + defer wg.Done() |
| 54 | + results[i].Version = name |
| 55 | + if isQUIC { |
| 56 | + results[i].Supported = probeH3(hostPort, alpn, timeout) |
| 57 | + return |
| 58 | + } |
| 59 | + results[i].Supported = probeALPN(dialer, hostPort, alpn) |
| 60 | + }(i, pv.alpn, pv.name, pv.quic) |
| 61 | + } |
| 62 | + wg.Wait() |
| 63 | + return results |
| 64 | +} |
| 65 | + |
| 66 | +func probeALPN(dialer *net.Dialer, hostPort, alpn string) bool { |
| 67 | + conn, err := tls.DialWithDialer(dialer, "tcp", hostPort, &tls.Config{ |
| 68 | + InsecureSkipVerify: true, |
| 69 | + NextProtos: []string{alpn}, |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + return false |
| 73 | + } |
| 74 | + defer conn.Close() |
| 75 | + return conn.ConnectionState().NegotiatedProtocol == alpn |
| 76 | +} |
| 77 | + |
| 78 | +func probeH3(hostPort, alpn string, timeout time.Duration) bool { |
| 79 | + tr := sharedH3Transport() |
| 80 | + if tr == nil { |
| 81 | + return false |
| 82 | + } |
| 83 | + addr, err := net.ResolveUDPAddr("udp", hostPort) |
| 84 | + if err != nil { |
| 85 | + return false |
| 86 | + } |
| 87 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 88 | + defer cancel() |
| 89 | + conn, err := tr.Dial(ctx, addr, &tls.Config{ |
| 90 | + InsecureSkipVerify: true, |
| 91 | + NextProtos: []string{alpn}, |
| 92 | + }, nil) |
| 93 | + if err != nil { |
| 94 | + return false |
| 95 | + } |
| 96 | + _ = conn.CloseWithError(0, "") |
| 97 | + return true |
| 98 | +} |
0 commit comments