mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 19:56:51 +08:00
chore: add parse-pure-ip in sniffer
This commit is contained in:
parent
846c2978fd
commit
560862d4e2
|
@ -38,6 +38,7 @@ type SnifferDispatcher struct {
|
||||||
rwMux sync.RWMutex
|
rwMux sync.RWMutex
|
||||||
|
|
||||||
forceDnsMapping bool
|
forceDnsMapping bool
|
||||||
|
parsePureIp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sd *SnifferDispatcher) TCPSniff(conn net.Conn, metadata *C.Metadata) {
|
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
|
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)
|
port, err := strconv.ParseUint(metadata.DstPort, 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugln("[Sniffer] Dst port is error")
|
log.Debugln("[Sniffer] Dst port is error")
|
||||||
|
@ -98,10 +99,18 @@ func (sd *SnifferDispatcher) replaceDomain(metadata *C.Metadata, host string) {
|
||||||
if metadata.DstIP.IsValid() {
|
if metadata.DstIP.IsValid() {
|
||||||
dstIP = metadata.DstIP.String()
|
dstIP = metadata.DstIP.String()
|
||||||
}
|
}
|
||||||
log.Debugln("[Sniffer] Sniff TCP [%s:%s]-->[%s:%s] success, replace domain [%s]-->[%s]",
|
originHost := metadata.Host
|
||||||
metadata.SrcIP, metadata.SrcPort,
|
if originHost != host {
|
||||||
dstIP, metadata.DstPort,
|
log.Infoln("[Sniffer] Sniff TCP [%s:%s]-->[%s:%s] success, replace domain [%s]-->[%s]",
|
||||||
metadata.Host, host)
|
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.AddrType = C.AtypDomainName
|
||||||
metadata.Host = host
|
metadata.Host = host
|
||||||
|
@ -175,7 +184,8 @@ func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTrie[bool],
|
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{
|
dispatcher := SnifferDispatcher{
|
||||||
enable: true,
|
enable: true,
|
||||||
forceDomain: forceDomain,
|
forceDomain: forceDomain,
|
||||||
|
@ -183,6 +193,7 @@ func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTr
|
||||||
portRanges: ports,
|
portRanges: ports,
|
||||||
skipList: cache.NewLRUCache[string, uint8](cache.WithSize[string, uint8](128), cache.WithAge[string, uint8](600)),
|
skipList: cache.NewLRUCache[string, uint8](cache.WithSize[string, uint8](128), cache.WithAge[string, uint8](600)),
|
||||||
forceDnsMapping: forceDnsMapping,
|
forceDnsMapping: forceDnsMapping,
|
||||||
|
parsePureIp: parsePureIp,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, snifferName := range needSniffer {
|
for _, snifferName := range needSniffer {
|
||||||
|
|
|
@ -202,6 +202,7 @@ type Sniffer struct {
|
||||||
SkipDomain *trie.DomainTrie[bool]
|
SkipDomain *trie.DomainTrie[bool]
|
||||||
Ports *[]utils.Range[uint16]
|
Ports *[]utils.Range[uint16]
|
||||||
ForceDnsMapping bool
|
ForceDnsMapping bool
|
||||||
|
ParsePureIp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Experimental config
|
// Experimental config
|
||||||
|
@ -332,6 +333,7 @@ type RawSniffer struct {
|
||||||
SkipDomain []string `yaml:"skip-domain" json:"skip-domain"`
|
SkipDomain []string `yaml:"skip-domain" json:"skip-domain"`
|
||||||
Ports []string `yaml:"port-whitelist" json:"port-whitelist"`
|
Ports []string `yaml:"port-whitelist" json:"port-whitelist"`
|
||||||
ForceDnsMapping bool `yaml:"force-dns-mapping" json:"force-dns-mapping"`
|
ForceDnsMapping bool `yaml:"force-dns-mapping" json:"force-dns-mapping"`
|
||||||
|
ParsePureIp bool `yaml:"parse-pure-ip" json:"parse-pure-ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// EBpf config
|
// EBpf config
|
||||||
|
@ -427,6 +429,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
|
||||||
SkipDomain: []string{},
|
SkipDomain: []string{},
|
||||||
Ports: []string{},
|
Ports: []string{},
|
||||||
ForceDnsMapping: true,
|
ForceDnsMapping: true,
|
||||||
|
ParsePureIp: true,
|
||||||
},
|
},
|
||||||
Profile: Profile{
|
Profile: Profile{
|
||||||
StoreSelected: true,
|
StoreSelected: true,
|
||||||
|
@ -1178,6 +1181,7 @@ func parseSniffer(snifferRaw RawSniffer) (*Sniffer, error) {
|
||||||
sniffer := &Sniffer{
|
sniffer := &Sniffer{
|
||||||
Enable: snifferRaw.Enable,
|
Enable: snifferRaw.Enable,
|
||||||
ForceDnsMapping: snifferRaw.ForceDnsMapping,
|
ForceDnsMapping: snifferRaw.ForceDnsMapping,
|
||||||
|
ParsePureIp: snifferRaw.ParsePureIp,
|
||||||
}
|
}
|
||||||
|
|
||||||
var ports []utils.Range[uint16]
|
var ports []utils.Range[uint16]
|
||||||
|
|
|
@ -265,7 +265,10 @@ func updateTun(tun *config.Tun) {
|
||||||
|
|
||||||
func updateSniffer(sniffer *config.Sniffer) {
|
func updateSniffer(sniffer *config.Sniffer) {
|
||||||
if sniffer.Enable {
|
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 {
|
if err != nil {
|
||||||
log.Warnln("initial sniffer failed, err:%v", err)
|
log.Warnln("initial sniffer failed, err:%v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user