2022-04-09 22:30:36 +08:00
|
|
|
package sniffer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-04-16 08:21:31 +08:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2022-04-09 22:30:36 +08:00
|
|
|
"net"
|
|
|
|
|
|
|
|
CN "github.com/Dreamacro/clash/common/net"
|
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrorUnsupportedSniffer = errors.New("unsupported sniffer")
|
2022-04-16 08:21:31 +08:00
|
|
|
ErrorSniffFailed = errors.New("all sniffer failed")
|
2022-04-09 22:30:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var Dispatcher SnifferDispatcher
|
|
|
|
|
|
|
|
type SnifferDispatcher struct {
|
2022-04-16 11:55:49 +08:00
|
|
|
enable bool
|
|
|
|
force bool
|
|
|
|
sniffers []C.Sniffer
|
|
|
|
domains *trie.DomainTrie[bool]
|
|
|
|
tcpHandler func(conn *CN.BufferedConn, metadata *C.Metadata)
|
2022-04-09 22:30:36 +08:00
|
|
|
}
|
|
|
|
|
2022-04-16 08:21:31 +08:00
|
|
|
func (sd *SnifferDispatcher) forceReplace(conn *CN.BufferedConn, metadata *C.Metadata) {
|
|
|
|
host, err := sd.sniffDomain(conn, metadata)
|
|
|
|
if err != nil {
|
2022-04-16 11:55:49 +08:00
|
|
|
log.Debugln("[Sniffer] All sniffing sniff failed with from [%s:%s] to [%s:%s]", metadata.SrcIP, metadata.SrcPort, metadata.String(), metadata.DstPort)
|
2022-04-16 08:21:31 +08:00
|
|
|
return
|
|
|
|
} else {
|
2022-04-16 08:53:31 +08:00
|
|
|
if sd.inReverse(host) {
|
2022-04-16 09:04:43 +08:00
|
|
|
log.Debugln("[Sniffer] Skip replace host:%s", host)
|
2022-04-16 08:21:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sd.replaceDomain(metadata, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) replace(conn *CN.BufferedConn, metadata *C.Metadata) {
|
2022-04-16 08:53:31 +08:00
|
|
|
if metadata.Host != "" && !sd.inReverse(metadata.Host) {
|
2022-04-16 09:04:43 +08:00
|
|
|
log.Debugln("[Sniffer] Skip Sniff domain:%s", metadata.Host)
|
2022-04-16 08:21:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := sd.sniffDomain(conn, metadata)
|
|
|
|
if err != nil {
|
2022-04-16 11:55:49 +08:00
|
|
|
log.Debugln("[Sniffer] All sniffing sniff failed with from [%s:%s] to [%s:%s]", metadata.SrcIP, metadata.SrcPort, metadata.String(), metadata.DstPort)
|
2022-04-16 08:21:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sd.replaceDomain(metadata, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) TCPSniff(conn net.Conn, metadata *C.Metadata) {
|
2022-04-09 22:30:36 +08:00
|
|
|
bufConn, ok := conn.(*CN.BufferedConn)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-16 08:21:31 +08:00
|
|
|
sd.tcpHandler(bufConn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) inReverse(host string) bool {
|
2022-04-16 11:55:49 +08:00
|
|
|
return sd.domains != nil && sd.domains.Search(host) != nil
|
2022-04-16 08:21:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) replaceDomain(metadata *C.Metadata, host string) {
|
2022-04-16 09:04:43 +08:00
|
|
|
log.Debugln("[Sniffer] Sniff TCP [%s:%s]-->[%s:%s] success, replace domain [%s]-->[%s]",
|
2022-04-16 08:21:31 +08:00
|
|
|
metadata.SrcIP, metadata.SrcPort,
|
|
|
|
metadata.DstIP, metadata.DstPort,
|
|
|
|
metadata.Host, host)
|
|
|
|
|
|
|
|
metadata.AddrType = C.AtypDomainName
|
|
|
|
metadata.Host = host
|
2022-04-16 09:51:31 +08:00
|
|
|
metadata.DNSMode = C.DNSMapping
|
2022-04-16 08:21:31 +08:00
|
|
|
resolver.InsertHostByIP(metadata.DstIP, host)
|
|
|
|
metadata.DstIP = nil
|
2022-04-09 22:30:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) Enable() bool {
|
|
|
|
return sd.enable
|
|
|
|
}
|
|
|
|
|
2022-04-16 08:21:31 +08:00
|
|
|
func (sd *SnifferDispatcher) sniffDomain(conn *CN.BufferedConn, metadata *C.Metadata) (string, error) {
|
2022-04-09 22:30:36 +08:00
|
|
|
for _, sniffer := range sd.sniffers {
|
|
|
|
if sniffer.SupportNetwork() == C.TCP {
|
2022-04-10 20:01:35 +08:00
|
|
|
_, err := conn.Peek(1)
|
|
|
|
if err != nil {
|
2022-04-16 08:21:31 +08:00
|
|
|
return "", err
|
2022-04-10 20:01:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bufferedLen := conn.Buffered()
|
|
|
|
bytes, err := conn.Peek(bufferedLen)
|
2022-04-09 22:30:36 +08:00
|
|
|
if err != nil {
|
2022-04-16 08:21:31 +08:00
|
|
|
log.Debugln("[Sniffer] the data length not enough")
|
2022-04-09 22:30:36 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := sniffer.SniffTCP(bytes)
|
|
|
|
if err != nil {
|
2022-04-16 09:04:43 +08:00
|
|
|
log.Debugln("[Sniffer] [%s] Sniff data failed %s", sniffer.Protocol(), metadata.DstIP)
|
2022-04-09 22:30:36 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-04-10 20:01:35 +08:00
|
|
|
|
2022-04-16 08:21:31 +08:00
|
|
|
return host, nil
|
2022-04-09 22:30:36 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-16 08:21:31 +08:00
|
|
|
|
|
|
|
return "", ErrorSniffFailed
|
2022-04-09 22:30:36 +08:00
|
|
|
}
|
|
|
|
|
2022-04-16 08:21:31 +08:00
|
|
|
func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) {
|
2022-04-09 22:30:36 +08:00
|
|
|
dispatcher := SnifferDispatcher{
|
2022-04-16 08:21:31 +08:00
|
|
|
enable: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dispatcher, nil
|
|
|
|
}
|
|
|
|
|
2022-04-16 11:55:49 +08:00
|
|
|
func NewSnifferDispatcher(needSniffer []C.SnifferType, force bool, reverses *trie.DomainTrie[bool]) (*SnifferDispatcher, error) {
|
2022-04-16 08:21:31 +08:00
|
|
|
dispatcher := SnifferDispatcher{
|
2022-04-16 11:55:49 +08:00
|
|
|
enable: true,
|
|
|
|
force: force,
|
|
|
|
domains: reverses,
|
2022-04-09 22:30:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, snifferName := range needSniffer {
|
|
|
|
sniffer, err := NewSniffer(snifferName)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Sniffer name[%s] is error", snifferName)
|
2022-04-16 08:21:31 +08:00
|
|
|
return &SnifferDispatcher{enable: false}, err
|
2022-04-09 22:30:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatcher.sniffers = append(dispatcher.sniffers, sniffer)
|
|
|
|
}
|
|
|
|
|
2022-04-16 08:21:31 +08:00
|
|
|
if force {
|
|
|
|
dispatcher.tcpHandler = dispatcher.forceReplace
|
|
|
|
} else {
|
|
|
|
dispatcher.tcpHandler = dispatcher.replace
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dispatcher, nil
|
2022-04-09 22:30:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSniffer(name C.SnifferType) (C.Sniffer, error) {
|
|
|
|
switch name {
|
|
|
|
case C.TLS:
|
|
|
|
return &TLSSniffer{}, nil
|
|
|
|
default:
|
|
|
|
return nil, ErrorUnsupportedSniffer
|
|
|
|
}
|
|
|
|
}
|