| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package vhost |
| 16 |
| 17 | import ( |
| 18 | "crypto/tls" |
| 19 | "io" |
| 20 | "net" |
| 21 | "time" |
| 22 |
| 23 | libnet "github.com/fatedier/golib/net" |
| 24 | ) |
| 25 |
| 26 | type HTTPSMuxer struct { |
| 27 | *Muxer |
| 28 | } |
| 29 |
| 30 | func NewHTTPSMuxer(listener net.Listener, timeout time.Duration) (*HTTPSMuxer, error) { |
| 31 | mux, err := NewMuxer(listener, GetHTTPSHostname, timeout) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | mux.SetFailHookFunc(vhostFailed) |
| 36 | return &HTTPSMuxer{mux}, nil |
| 37 | } |
| 38 |
| 39 | func GetHTTPSHostname(c net.Conn) (_ net.Conn, _ map[string]string, err error) { |
| 40 | reqInfoMap := make(map[string]string, 0) |
| 41 | sc, rd := libnet.NewSharedConn(c) |
| 42 |
| 43 | clientHello, err := readClientHello(rd) |
| 44 | if err != nil { |
| 45 | return nil, reqInfoMap, err |
| 46 | } |
| 47 |
| 48 | reqInfoMap["Host"] = clientHello.ServerName |
| 49 | reqInfoMap["Scheme"] = "https" |
| 50 | return sc, reqInfoMap, nil |
| 51 | } |
| 52 |
| 53 | func readClientHello(reader io.Reader) (*tls.ClientHelloInfo, error) { |
| 54 | var hello *tls.ClientHelloInfo |
| 55 |
| 56 | // Note that Handshake always fails because the readOnlyConn is not a real connection. |
| 57 | // As long as the Client Hello is successfully read, the failure should only happen after GetConfigForClient is called, |
| 58 | // so we only care about the error if hello was never set. |
| 59 | err := tls.Server(readOnlyConn{reader: reader}, &tls.Config{ |
| 60 | GetConfigForClient: func(argHello *tls.ClientHelloInfo) (*tls.Config, error) { |
| 61 | hello = &tls.ClientHelloInfo{} |
| 62 | *hello = *argHello |
| 63 | return nil, nil |
| 64 | }, |
| 65 | }).Handshake() |
| 66 |
| 67 | if hello == nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | return hello, nil |
| 71 | } |
| 72 |
| 73 | func vhostFailed(c net.Conn) { |
| 74 | // Alert with alertUnrecognizedName |
| 75 | _ = tls.Server(c, &tls.Config{}).Handshake() |
| 76 | c.Close() |
| 77 | } |
| 78 |
| 79 | type readOnlyConn struct { |
| 80 | reader io.Reader |
| 81 | } |
| 82 |
| 83 | func (conn readOnlyConn) Read(p []byte) (int, error) { return conn.reader.Read(p) } |
| 84 | func (conn readOnlyConn) Write(_ []byte) (int, error) { return 0, io.ErrClosedPipe } |
| 85 | func (conn readOnlyConn) Close() error { return nil } |
| 86 | func (conn readOnlyConn) LocalAddr() net.Addr { return nil } |
| 87 | func (conn readOnlyConn) RemoteAddr() net.Addr { return nil } |
| 88 | func (conn readOnlyConn) SetDeadline(_ time.Time) error { return nil } |
| 89 | func (conn readOnlyConn) SetReadDeadline(_ time.Time) error { return nil } |
| 90 | func (conn readOnlyConn) SetWriteDeadline(_ time.Time) error { return nil } |