2016-06-13 22:19:24 +08:00
|
|
|
// Copyright 2016 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package vhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-10-12 20:13:12 +08:00
|
|
|
"net"
|
2016-06-13 22:19:24 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-05-09 00:23:42 +08:00
|
|
|
gnet "github.com/fatedier/golib/net"
|
2018-05-08 02:13:30 +08:00
|
|
|
"github.com/fatedier/golib/pool"
|
2016-06-13 22:19:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeClientHello uint8 = 1 // Type client hello
|
|
|
|
)
|
|
|
|
|
|
|
|
// TLS extension numbers
|
|
|
|
const (
|
|
|
|
extensionServerName uint16 = 0
|
|
|
|
extensionStatusRequest uint16 = 5
|
|
|
|
extensionSupportedCurves uint16 = 10
|
|
|
|
extensionSupportedPoints uint16 = 11
|
|
|
|
extensionSignatureAlgorithms uint16 = 13
|
|
|
|
extensionALPN uint16 = 16
|
|
|
|
extensionSCT uint16 = 18
|
|
|
|
extensionSessionTicket uint16 = 35
|
|
|
|
extensionNextProtoNeg uint16 = 13172 // not IANA assigned
|
|
|
|
extensionRenegotiationInfo uint16 = 0xff01
|
|
|
|
)
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type HTTPSMuxer struct {
|
|
|
|
*Muxer
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewHTTPSMuxer(listener net.Listener, timeout time.Duration) (*HTTPSMuxer, error) {
|
|
|
|
mux, err := NewMuxer(listener, GetHTTPSHostname, nil, nil, nil, timeout)
|
|
|
|
return &HTTPSMuxer{mux}, err
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
2016-06-24 15:43:58 +08:00
|
|
|
func readHandshake(rd io.Reader) (host string, err error) {
|
2016-07-31 03:26:41 +08:00
|
|
|
data := pool.GetBuf(1024)
|
|
|
|
origin := data
|
|
|
|
defer pool.PutBuf(origin)
|
2018-05-06 04:27:30 +08:00
|
|
|
|
|
|
|
_, err = io.ReadFull(rd, data[:47])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
length, err := rd.Read(data[47:])
|
2016-06-13 22:19:24 +08:00
|
|
|
if err != nil {
|
2016-06-24 15:43:58 +08:00
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
length += 47
|
2016-06-13 22:19:24 +08:00
|
|
|
data = data[:length]
|
|
|
|
if uint8(data[5]) != typeClientHello {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: type[%d] is not clientHello", uint16(data[5]))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
2016-06-24 15:43:58 +08:00
|
|
|
// session
|
2020-05-24 17:48:37 +08:00
|
|
|
sessionIDLen := int(data[43])
|
|
|
|
if sessionIDLen > 32 || len(data) < 44+sessionIDLen {
|
|
|
|
err = fmt.Errorf("readHandshake: sessionIdLen[%d] is long", sessionIDLen)
|
2016-06-24 15:43:58 +08:00
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
data = data[44+sessionIDLen:]
|
2016-06-13 22:19:24 +08:00
|
|
|
if len(data) < 2 {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: dataLen[%d] after session is short", len(data))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cipher suite numbers
|
|
|
|
cipherSuiteLen := int(data[0])<<8 | int(data[1])
|
|
|
|
if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: dataLen[%d] after cipher suite is short", len(data))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
data = data[2+cipherSuiteLen:]
|
|
|
|
if len(data) < 1 {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: cipherSuiteLen[%d] is long", cipherSuiteLen)
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
2016-06-24 15:43:58 +08:00
|
|
|
// compression method
|
2016-06-13 22:19:24 +08:00
|
|
|
compressionMethodsLen := int(data[0])
|
|
|
|
if len(data) < 1+compressionMethodsLen {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: compressionMethodsLen[%d] is long", compressionMethodsLen)
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data = data[1+compressionMethodsLen:]
|
|
|
|
if len(data) == 0 {
|
|
|
|
// ClientHello is optionally followed by extension data
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: there is no extension data to get servername")
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
if len(data) < 2 {
|
2018-03-21 18:06:43 +08:00
|
|
|
err = fmt.Errorf("readHandshake: extension dataLen[%d] is too short", len(data))
|
2016-06-24 15:43:58 +08:00
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extensionsLength := int(data[0])<<8 | int(data[1])
|
|
|
|
data = data[2:]
|
|
|
|
if extensionsLength != len(data) {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: extensionsLen[%d] is not equal to dataLen[%d]", extensionsLength, len(data))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
for len(data) != 0 {
|
|
|
|
if len(data) < 4 {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: extensionsDataLen[%d] is too short", len(data))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
extension := uint16(data[0])<<8 | uint16(data[1])
|
|
|
|
length := int(data[2])<<8 | int(data[3])
|
|
|
|
data = data[4:]
|
|
|
|
if len(data) < length {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: extensionLen[%d] is long", length)
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch extension {
|
|
|
|
case extensionRenegotiationInfo:
|
|
|
|
if length != 1 || data[0] != 0 {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: extension reNegotiationInfoLen[%d] is short", length)
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
case extensionNextProtoNeg:
|
|
|
|
case extensionStatusRequest:
|
|
|
|
case extensionServerName:
|
|
|
|
d := data[:length]
|
|
|
|
if len(d) < 2 {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: remiaining dataLen[%d] is short", len(d))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
namesLen := int(d[0])<<8 | int(d[1])
|
|
|
|
d = d[2:]
|
|
|
|
if len(d) != namesLen {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: nameListLen[%d] is not equal to dataLen[%d]", namesLen, len(d))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
for len(d) > 0 {
|
|
|
|
if len(d) < 3 {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: extension serverNameLen[%d] is short", len(d))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
nameType := d[0]
|
|
|
|
nameLen := int(d[1])<<8 | int(d[2])
|
|
|
|
d = d[3:]
|
|
|
|
if len(d) < nameLen {
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("readHandshake: nameLen[%d] is not equal to dataLen[%d]", nameLen, len(d))
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
if nameType == 0 {
|
|
|
|
serverName := string(d[:nameLen])
|
2016-06-24 15:43:58 +08:00
|
|
|
host = strings.TrimSpace(serverName)
|
|
|
|
return host, nil
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
d = d[nameLen:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data = data[length:]
|
|
|
|
}
|
2016-06-24 15:43:58 +08:00
|
|
|
err = fmt.Errorf("Unknow error")
|
|
|
|
return
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func GetHTTPSHostname(c net.Conn) (_ net.Conn, _ map[string]string, err error) {
|
2016-08-29 10:53:02 +08:00
|
|
|
reqInfoMap := make(map[string]string, 0)
|
2018-05-09 00:23:42 +08:00
|
|
|
sc, rd := gnet.NewSharedConn(c)
|
2016-06-13 22:19:24 +08:00
|
|
|
host, err := readHandshake(rd)
|
|
|
|
if err != nil {
|
2018-05-09 00:23:42 +08:00
|
|
|
return nil, reqInfoMap, err
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|
2016-08-29 10:53:02 +08:00
|
|
|
reqInfoMap["Host"] = host
|
2016-12-18 22:40:58 +08:00
|
|
|
reqInfoMap["Scheme"] = "https"
|
2019-10-12 20:13:12 +08:00
|
|
|
return sc, reqInfoMap, nil
|
2016-06-13 22:19:24 +08:00
|
|
|
}
|