Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add shouldSniffDomain to not do domain sniff twice #1

Merged
merged 7 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool) (Sni

metaresult, metadataErr := sniffer.SniffMetadata(ctx)

if metadataOnly || metaresult != nil {
if metadataOnly {
return metaresult, metadataErr
}

Expand All @@ -286,7 +286,7 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool) (Sni

cReader.Cache(payload)
if !payload.IsEmpty() {
result, err := sniffer.Sniff(ctx, payload.Bytes())
result, err := sniffer.Sniff(ctx, payload.Bytes(), metadataErr != nil)
if err != common.ErrNoClue {
return result, err
}
Expand Down
2 changes: 1 addition & 1 deletion app/dispatcher/fakednssniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newFakeDNSSniffer(ctx context.Context) (protocolSnifferWithMetadata, error)
errNotInit := newError("FakeDNSEngine is not initialized, but such a sniffer is used").AtError()
return protocolSnifferWithMetadata{}, errNotInit
}
return protocolSnifferWithMetadata{protocolSniffer: func(ctx context.Context, bytes []byte) (SniffResult, error) {
return protocolSnifferWithMetadata{protocolSniffer: func(ctx context.Context, bytes []byte, shouldSniffDomain bool) (SniffResult, error) {
Target := session.OutboundFromContext(ctx).Target
if Target.Network == net.Network_TCP || Target.Network == net.Network_UDP {
domainFromFakeDNS := fakeDNSEngine.GetDomainFromFakeDNS(Target.Address)
Expand Down
14 changes: 7 additions & 7 deletions app/dispatcher/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type SniffResult interface {
Domain() string
}

type protocolSniffer func(context.Context, []byte) (SniffResult, error)
type protocolSniffer func(context.Context, []byte, bool) (SniffResult, error)

type protocolSnifferWithMetadata struct {
protocolSniffer protocolSniffer
Expand All @@ -31,9 +31,9 @@ type Sniffer struct {
func NewSniffer(ctx context.Context) *Sniffer {
ret := &Sniffer{
sniffer: []protocolSnifferWithMetadata{
{func(c context.Context, b []byte) (SniffResult, error) { return http.SniffHTTP(b) }, false},
{func(c context.Context, b []byte) (SniffResult, error) { return tls.SniffTLS(b) }, false},
{func(c context.Context, b []byte) (SniffResult, error) { return bittorrent.SniffBittorrent(b) }, false},
{func(c context.Context, b []byte, s bool) (SniffResult, error) { return http.SniffHTTP(b, s) }, false},
{func(c context.Context, b []byte, s bool) (SniffResult, error) { return tls.SniffTLS(b, s) }, false},
{func(c context.Context, b []byte, s bool) (SniffResult, error) { return bittorrent.SniffBittorrent(b, s) }, false},
},
}
if sniffer, err := newFakeDNSSniffer(ctx); err == nil {
Expand All @@ -44,14 +44,14 @@ func NewSniffer(ctx context.Context) *Sniffer {

var errUnknownContent = newError("unknown content")

func (s *Sniffer) Sniff(c context.Context, payload []byte) (SniffResult, error) {
func (s *Sniffer) Sniff(c context.Context, payload []byte, shouldSniffDomain bool) (SniffResult, error) {
var pendingSniffer []protocolSnifferWithMetadata
for _, si := range s.sniffer {
s := si.protocolSniffer
if si.metadataSniffer {
continue
}
result, err := s(c, payload)
result, err := s(c, payload, shouldSniffDomain)
if err == common.ErrNoClue {
pendingSniffer = append(pendingSniffer, si)
continue
Expand All @@ -78,7 +78,7 @@ func (s *Sniffer) SniffMetadata(c context.Context) (SniffResult, error) {
pendingSniffer = append(pendingSniffer, si)
continue
}
result, err := s(c, nil)
result, err := s(c, nil, true)
if err == common.ErrNoClue {
pendingSniffer = append(pendingSniffer, si)
continue
Expand Down
2 changes: 1 addition & 1 deletion common/protocol/bittorrent/bittorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (h *SniffHeader) Domain() string {

var errNotBittorrent = errors.New("not bittorrent header")

func SniffBittorrent(b []byte) (*SniffHeader, error) {
func SniffBittorrent(b []byte, shouldSniffDomain bool) (*SniffHeader, error) {
if len(b) < 20 {
return nil, common.ErrNoClue
}
Expand Down
40 changes: 21 additions & 19 deletions common/protocol/http/sniff.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func beginWithHTTPMethod(b []byte) error {
return errNotHTTPMethod
}

func SniffHTTP(b []byte) (*SniffHeader, error) {
func SniffHTTP(b []byte, shouldSniffDomain bool) (*SniffHeader, error) {
if err := beginWithHTTPMethod(b); err != nil {
return nil, err
}
Expand All @@ -65,28 +65,30 @@ func SniffHTTP(b []byte) (*SniffHeader, error) {
version: HTTP1,
}

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
if shouldSniffDomain {
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()
}
sh.host = dest.Address.String()
}
}

if len(sh.host) > 0 {
if !shouldSniffDomain || len(sh.host) > 0 {
return sh, nil
}

Expand Down
10 changes: 7 additions & 3 deletions common/protocol/tls/sniff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func IsValidTLSVersion(major, minor byte) bool {

// ReadClientHello returns server name (if any) from TLS client hello message.
// https://github.com/golang/go/blob/master/src/crypto/tls/handshake_messages.go#L300
func ReadClientHello(data []byte, h *SniffHeader) error {
func ReadClientHello(shouldSniffDomain bool, data []byte, h *SniffHeader) error {
if len(data) < 42 {
return common.ErrNoClue
}
Expand Down Expand Up @@ -70,6 +70,10 @@ func ReadClientHello(data []byte, h *SniffHeader) error {
return errNotClientHello
}

if !shouldSniffDomain {
return nil
}

for len(data) != 0 {
if len(data) < 4 {
return errNotClientHello
Expand Down Expand Up @@ -121,7 +125,7 @@ func ReadClientHello(data []byte, h *SniffHeader) error {
return errNotTLS
}

func SniffTLS(b []byte) (*SniffHeader, error) {
func SniffTLS(b []byte, shouldSniffDomain bool) (*SniffHeader, error) {
if len(b) < 5 {
return nil, common.ErrNoClue
}
Expand All @@ -138,7 +142,7 @@ func SniffTLS(b []byte) (*SniffHeader, error) {
}

h := &SniffHeader{}
err := ReadClientHello(b[5:5+headerLen], h)
err := ReadClientHello(shouldSniffDomain, b[5:5+headerLen], h)
if err == nil {
return h, nil
}
Expand Down