From 560862d4e252ee85a5e14d22b928f92c1e9fe0bd Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Fri, 14 Oct 2022 08:42:28 +0800 Subject: [PATCH] chore: add parse-pure-ip in sniffer --- component/sniffer/dispatcher.go | 23 +++++++++++++++++------ config/config.go | 4 ++++ hub/executor/executor.go | 5 ++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/component/sniffer/dispatcher.go b/component/sniffer/dispatcher.go index 6f41db12..6a5e632a 100644 --- a/component/sniffer/dispatcher.go +++ b/component/sniffer/dispatcher.go @@ -38,6 +38,7 @@ type SnifferDispatcher struct { rwMux sync.RWMutex forceDnsMapping bool + parsePureIp bool } func (sd *SnifferDispatcher) TCPSniff(conn net.Conn, metadata *C.Metadata) { @@ -46,7 +47,7 @@ func (sd *SnifferDispatcher) TCPSniff(conn net.Conn, metadata *C.Metadata) { return } - if metadata.Host == "" || sd.forceDomain.Search(metadata.Host) != nil || (metadata.DNSMode == C.DNSMapping && sd.forceDnsMapping) { + if (metadata.Host == "" && sd.parsePureIp) || sd.forceDomain.Search(metadata.Host) != nil || (metadata.DNSMode == C.DNSMapping && sd.forceDnsMapping) { port, err := strconv.ParseUint(metadata.DstPort, 10, 16) if err != nil { log.Debugln("[Sniffer] Dst port is error") @@ -98,10 +99,18 @@ func (sd *SnifferDispatcher) replaceDomain(metadata *C.Metadata, host string) { if metadata.DstIP.IsValid() { dstIP = metadata.DstIP.String() } - log.Debugln("[Sniffer] Sniff TCP [%s:%s]-->[%s:%s] success, replace domain [%s]-->[%s]", - metadata.SrcIP, metadata.SrcPort, - dstIP, metadata.DstPort, - metadata.Host, host) + originHost := metadata.Host + if originHost != host { + log.Infoln("[Sniffer] Sniff TCP [%s:%s]-->[%s:%s] success, replace domain [%s]-->[%s]", + metadata.SrcIP, metadata.SrcPort, + dstIP, metadata.DstPort, + metadata.Host, host) + } else { + log.Debugln("[Sniffer] Sniff TCP [%s:%s]-->[%s:%s] success, replace domain [%s]-->[%s]", + metadata.SrcIP, metadata.SrcPort, + dstIP, metadata.DstPort, + metadata.Host, host) + } metadata.AddrType = C.AtypDomainName metadata.Host = host @@ -175,7 +184,8 @@ func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) { } func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTrie[bool], - skipSNI *trie.DomainTrie[bool], ports *[]utils.Range[uint16], forceDnsMapping bool) (*SnifferDispatcher, error) { + skipSNI *trie.DomainTrie[bool], ports *[]utils.Range[uint16], + forceDnsMapping bool, parsePureIp bool) (*SnifferDispatcher, error) { dispatcher := SnifferDispatcher{ enable: true, forceDomain: forceDomain, @@ -183,6 +193,7 @@ func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTr portRanges: ports, skipList: cache.NewLRUCache[string, uint8](cache.WithSize[string, uint8](128), cache.WithAge[string, uint8](600)), forceDnsMapping: forceDnsMapping, + parsePureIp: parsePureIp, } for _, snifferName := range needSniffer { diff --git a/config/config.go b/config/config.go index b9c3d863..ee4fbb16 100644 --- a/config/config.go +++ b/config/config.go @@ -202,6 +202,7 @@ type Sniffer struct { SkipDomain *trie.DomainTrie[bool] Ports *[]utils.Range[uint16] ForceDnsMapping bool + ParsePureIp bool } // Experimental config @@ -332,6 +333,7 @@ type RawSniffer struct { SkipDomain []string `yaml:"skip-domain" json:"skip-domain"` Ports []string `yaml:"port-whitelist" json:"port-whitelist"` ForceDnsMapping bool `yaml:"force-dns-mapping" json:"force-dns-mapping"` + ParsePureIp bool `yaml:"parse-pure-ip" json:"parse-pure-ip"` } // EBpf config @@ -427,6 +429,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { SkipDomain: []string{}, Ports: []string{}, ForceDnsMapping: true, + ParsePureIp: true, }, Profile: Profile{ StoreSelected: true, @@ -1178,6 +1181,7 @@ func parseSniffer(snifferRaw RawSniffer) (*Sniffer, error) { sniffer := &Sniffer{ Enable: snifferRaw.Enable, ForceDnsMapping: snifferRaw.ForceDnsMapping, + ParsePureIp: snifferRaw.ParsePureIp, } var ports []utils.Range[uint16] diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 6efb3654..e5bb1ad7 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -265,7 +265,10 @@ func updateTun(tun *config.Tun) { func updateSniffer(sniffer *config.Sniffer) { if sniffer.Enable { - dispatcher, err := SNI.NewSnifferDispatcher(sniffer.Sniffers, sniffer.ForceDomain, sniffer.SkipDomain, sniffer.Ports, sniffer.ForceDnsMapping) + dispatcher, err := SNI.NewSnifferDispatcher( + sniffer.Sniffers, sniffer.ForceDomain, sniffer.SkipDomain, sniffer.Ports, + sniffer.ForceDnsMapping, sniffer.ParsePureIp, + ) if err != nil { log.Warnln("initial sniffer failed, err:%v", err) }