2022-04-09 22:30:36 +08:00
|
|
|
package sniffer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2022-04-20 01:52:51 +08:00
|
|
|
"net/netip"
|
2022-04-21 22:06:08 +08:00
|
|
|
"strconv"
|
2022-04-21 23:08:37 +08:00
|
|
|
"time"
|
2022-04-21 22:06:08 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2022-04-09 22:30:36 +08:00
|
|
|
|
|
|
|
CN "github.com/Dreamacro/clash/common/net"
|
2022-04-21 22:06:08 +08:00
|
|
|
"github.com/Dreamacro/clash/common/utils"
|
2022-04-09 22:30:36 +08:00
|
|
|
"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-17 20:02:13 +08:00
|
|
|
enable bool
|
2022-04-16 08:21:31 +08:00
|
|
|
|
2022-04-17 20:02:13 +08:00
|
|
|
sniffers []C.Sniffer
|
2022-04-16 08:21:31 +08:00
|
|
|
|
2022-04-17 20:02:13 +08:00
|
|
|
foreDomain *trie.DomainTrie[bool]
|
|
|
|
skipSNI *trie.DomainTrie[bool]
|
2022-04-21 22:06:08 +08:00
|
|
|
portRanges *[]utils.Range[uint16]
|
2022-04-16 08:21:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-17 20:02:13 +08:00
|
|
|
if metadata.Host == "" || sd.foreDomain.Search(metadata.Host) != nil {
|
2022-04-21 22:06:08 +08:00
|
|
|
port, err := strconv.ParseUint(metadata.DstPort, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugln("[Sniffer] Dst port is error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, portRange := range *sd.portRanges {
|
|
|
|
if !portRange.Contains(uint16(port)) {
|
|
|
|
return
|
2022-04-23 09:36:11 +08:00
|
|
|
} else {
|
|
|
|
break
|
2022-04-21 22:06:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 20:02:13 +08:00
|
|
|
if host, err := sd.sniffDomain(bufConn, metadata); err != nil {
|
|
|
|
log.Debugln("[Sniffer] All sniffing sniff failed with from [%s:%s] to [%s:%s]", metadata.SrcIP, metadata.SrcPort, metadata.String(), metadata.DstPort)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if sd.skipSNI.Search(host) != nil {
|
|
|
|
log.Debugln("[Sniffer] Skip sni[%s]", host)
|
|
|
|
return
|
|
|
|
}
|
2022-04-16 08:21:31 +08:00
|
|
|
|
2022-04-17 20:02:13 +08:00
|
|
|
sd.replaceDomain(metadata, host)
|
|
|
|
}
|
|
|
|
}
|
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)
|
2022-04-20 01:52:51 +08:00
|
|
|
metadata.DstIP = netip.Addr{}
|
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-21 23:08:37 +08:00
|
|
|
conn.SetReadDeadline(time.Now().Add(3 * time.Second))
|
2022-04-10 20:01:35 +08:00
|
|
|
_, err := conn.Peek(1)
|
2022-04-22 17:00:39 +08:00
|
|
|
conn.SetReadDeadline(time.Time{})
|
2022-04-10 20:01:35 +08:00
|
|
|
if err != nil {
|
2022-04-21 23:08:37 +08:00
|
|
|
_, ok := err.(*net.OpError)
|
|
|
|
if ok {
|
|
|
|
log.Errorln("[Sniffer] [%s] Maybe read timeout, Consider adding skip", metadata.DstIP.String())
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorln("[Sniffer] %v", err)
|
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-21 22:06:08 +08:00
|
|
|
func NewSnifferDispatcher(needSniffer []C.SnifferType, forceDomain *trie.DomainTrie[bool],
|
|
|
|
skipSNI *trie.DomainTrie[bool], ports *[]utils.Range[uint16]) (*SnifferDispatcher, error) {
|
2022-04-16 08:21:31 +08:00
|
|
|
dispatcher := SnifferDispatcher{
|
2022-04-17 20:02:13 +08:00
|
|
|
enable: true,
|
|
|
|
foreDomain: forceDomain,
|
|
|
|
skipSNI: skipSNI,
|
2022-04-21 22:06:08 +08:00
|
|
|
portRanges: ports,
|
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
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|