| 1 | package caddyhttp |
| 2 | |
| 3 | import ( |
| 4 | "crypto/tls" |
| 5 | "io" |
| 6 | "net" |
| 7 | |
| 8 | "go.uber.org/zap" |
| 9 | "golang.org/x/net/http2" |
| 10 | ) |
| 11 | |
| 12 | type connectionStater interface { |
| 13 | ConnectionState() tls.ConnectionState |
| 14 | } |
| 15 | |
| 16 | // http2Listener wraps the listener to solve the following problems: |
| 17 | // 1. prevent genuine h2c connections from succeeding if h2c is not enabled |
| 18 | // and the connection doesn't implement connectionStater or the resulting NegotiatedProtocol |
| 19 | // isn't http2. |
| 20 | // This does allow a connection to pass as tls enabled even if it's not, listener wrappers |
| 21 | // can do this. |
| 22 | // 2. After wrapping the connection doesn't implement connectionStater, emit a warning so that listener |
| 23 | // wrapper authors will hopefully implement it. |
| 24 | // 3. check if the connection matches a specific http version. h2/h2c has a distinct preface. |
| 25 | type http2Listener struct { |
| 26 | useTLS bool |
| 27 | useH1 bool |
| 28 | useH2 bool |
| 29 | net.Listener |
| 30 | logger *zap.Logger |
| 31 | } |
| 32 | |
| 33 | func (h *http2Listener) Accept() (net.Conn, error) { |
| 34 | conn, err := h.Listener.Accept() |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | |
| 39 | // *tls.Conn doesn't need to be wrapped because we already removed unwanted alpns |
| 40 | // and handshake won't succeed without mutually supported alpns |
| 41 | if tlsConn, ok := conn.(*tls.Conn); ok { |
| 42 | return tlsConn, nil |
| 43 | } |
| 44 | |
| 45 | _, isConnectionStater := conn.(connectionStater) |
| 46 | // emit a warning |
| 47 | if h.useTLS && !isConnectionStater { |
| 48 | h.logger.Warn("tls is enabled, but listener wrapper returns a connection that doesn't implement connectionStater") |
| 49 | } else if !h.useTLS && isConnectionStater { |
| 50 | h.logger.Warn("tls is disabled, but listener wrapper returns a connection that implements connectionStater") |
| 51 | } |
| 52 | |
| 53 | // if both h1 and h2 are enabled, we don't need to check the preface |
| 54 | if h.useH1 && h.useH2 { |
| 55 | if isConnectionStater { |
| 56 | return tlsStateConn{conn}, nil |
| 57 | } |
| 58 | return conn, nil |
| 59 | } |
| 60 | |
| 61 | // impossible both are false, either useH1 or useH2 must be true, |
| 62 | // or else the listener wouldn't be created |
| 63 | h2Conn := &http2Conn{ |
| 64 | h2Expected: h.useH2, |
| 65 | logger: h.logger, |
| 66 | Conn: conn, |
| 67 | } |
| 68 | if isConnectionStater { |
| 69 | return tlsStateConn{http2StateConn{h2Conn}}, nil |
| 70 | } |
| 71 | return h2Conn, nil |
| 72 | } |
| 73 | |
| 74 | // tlsStateConn wraps a net.Conn that implements connectionStater to hide that method |
| 75 | // we can call netConn to get the original net.Conn and get the tls connection state |
| 76 | // golang 1.25 will call that method, and it breaks h2 with connections other than *tls.Conn |
| 77 | type tlsStateConn struct { |
| 78 | net.Conn |
| 79 | } |
| 80 | |
| 81 | func (conn tlsStateConn) tlsNetConn() net.Conn { |
| 82 | return conn.Conn |
| 83 | } |
| 84 | |
| 85 | type http2StateConn struct { |
| 86 | *http2Conn |
| 87 | } |
| 88 | |
| 89 | func (conn http2StateConn) ConnectionState() tls.ConnectionState { |
| 90 | return conn.Conn.(connectionStater).ConnectionState() |
| 91 | } |
| 92 | |
| 93 | type http2Conn struct { |
| 94 | // current index where the preface should match, |
| 95 | // no matching is done if idx is >= len(http2.ClientPreface) |
| 96 | idx int |
| 97 | // whether the connection is expected to be h2/h2c |
| 98 | h2Expected bool |
| 99 | // log if one such connection is detected |
| 100 | logger *zap.Logger |
| 101 | net.Conn |
| 102 | } |
| 103 | |
| 104 | func (c *http2Conn) Read(p []byte) (int, error) { |
| 105 | if c.idx >= len(http2.ClientPreface) { |
| 106 | return c.Conn.Read(p) |
| 107 | } |
| 108 | n, err := c.Conn.Read(p) |
| 109 | for i := range n { |
| 110 | // first mismatch |
| 111 | if p[i] != http2.ClientPreface[c.idx] { |
| 112 | // close the connection if h2 is expected |
| 113 | if c.h2Expected { |
| 114 | c.logger.Debug("h1 connection detected, but h1 is not enabled") |
| 115 | _ = c.Conn.Close() |
| 116 | return 0, io.EOF |
| 117 | } |
| 118 | // no need to continue matching anymore |
| 119 | c.idx = len(http2.ClientPreface) |
| 120 | return n, err |
| 121 | } |
| 122 | c.idx++ |
| 123 | // matching complete |
| 124 | if c.idx == len(http2.ClientPreface) && !c.h2Expected { |
| 125 | c.logger.Debug("h2/h2c connection detected, but h2/h2c is not enabled") |
| 126 | _ = c.Conn.Close() |
| 127 | return 0, io.EOF |
| 128 | } |
| 129 | } |
| 130 | return n, err |
| 131 | } |