1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-12 15:27:16 -05:00
v2fly/common/protocol/http/sniff.go
dyhkwong 8de2f27043
fix protocol matching in routing (#2540)
* remove invalid http2 sniffer

* do not set metadata protocol for http inbound

http inbound may have transport settings

* fix doh metadata protocol
2023-09-14 08:27:36 +08:00

81 lines
1.5 KiB
Go

package http
import (
"bytes"
"errors"
"strings"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/net"
)
type version byte
type SniffHeader struct {
host string
}
func (h *SniffHeader) Protocol() string {
return "http1"
}
func (h *SniffHeader) Domain() string {
return h.host
}
var (
// refer to https://pkg.go.dev/net/http@master#pkg-constants
methods = [...]string{"get", "post", "head", "put", "delete", "options", "connect", "patch", "trace"}
errNotHTTPMethod = errors.New("not an HTTP method")
)
func beginWithHTTPMethod(b []byte) error {
for _, m := range &methods {
if len(b) >= len(m) && strings.EqualFold(string(b[:len(m)]), m) {
return nil
}
if len(b) < len(m) {
return common.ErrNoClue
}
}
return errNotHTTPMethod
}
func SniffHTTP(b []byte) (*SniffHeader, error) {
if err := beginWithHTTPMethod(b); err != nil {
return nil, err
}
sh := &SniffHeader{}
headers := bytes.Split(b, []byte{'\n'})
for i := 1; i < len(headers); i++ {
header := headers[i]
if len(header) == 0 {
break
}
parts := bytes.SplitN(header, []byte{':'}, 2)
if len(parts) != 2 {
continue
}
key := strings.ToLower(string(parts[0]))
if key == "host" {
rawHost := strings.ToLower(string(bytes.TrimSpace(parts[1])))
dest, err := ParseHost(rawHost, net.Port(80))
if err != nil {
return nil, err
}
sh.host = dest.Address.String()
}
}
if len(sh.host) > 0 {
return sh, nil
}
return nil, common.ErrNoClue
}