2022-04-09 14:30:36 +00:00
|
|
|
package sniffer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-05-02 00:46:24 +00:00
|
|
|
"github.com/Dreamacro/clash/constant/sniffer"
|
2022-04-09 14:30:36 +00:00
|
|
|
"net"
|
2022-05-02 14:24:14 +00:00
|
|
|
"net/netip"
|
2022-04-21 14:06:08 +00:00
|
|
|
"strconv"
|
2022-04-21 15:08:37 +00:00
|
|
|
"time"
|
2022-04-21 14:06:08 +00:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2022-04-09 14:30:36 +00:00
|
|
|
|
|
|
|
CN "github.com/Dreamacro/clash/common/net"
|
2022-04-21 14:06:08 +00:00
|
|
|
"github.com/Dreamacro/clash/common/utils"
|
2022-04-09 14:30:36 +00: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 00:21:31 +00:00
|
|
|
ErrorSniffFailed = errors.New("all sniffer failed")
|
2022-05-01 21:17:13 +00:00
|
|
|
ErrNoClue = errors.New("not enough information for making a decision")
|
2022-04-09 14:30:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Dispatcher SnifferDispatcher
|
|
|
|
|
2022-04-27 07:22:42 +00:00
|
|
|
type (
|
|
|
|
SnifferDispatcher struct {
|
|
|
|
enable bool
|
2022-04-16 00:21:31 +00:00
|
|
|
|
2022-05-02 00:46:24 +00:00
|
|
|
sniffers []sniffer.Sniffer
|
2022-04-16 00:21:31 +00:00
|
|
|
|
2022-04-27 07:22:42 +00:00
|
|
|
foreDomain *trie.DomainTrie[bool]
|
|
|
|
skipSNI *trie.DomainTrie[bool]
|
|
|
|
portRanges *[]utils.Range[uint16]
|
|
|
|
}
|
|
|
|
)
|
2022-04-16 00:21:31 +00:00
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) TCPSniff(conn net.Conn, metadata *C.Metadata) {
|
2022-04-09 14:30:36 +00:00
|
|
|
bufConn, ok := conn.(*CN.BufferedConn)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-17 12:02:13 +00:00
|
|
|
if metadata.Host == "" || sd.foreDomain.Search(metadata.Host) != nil {
|
2022-04-21 14:06:08 +00:00
|
|
|
port, err := strconv.ParseUint(metadata.DstPort, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugln("[Sniffer] Dst port is error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:52:23 +00:00
|
|
|
inWhitelist := false
|
2022-04-21 14:06:08 +00:00
|
|
|
for _, portRange := range *sd.portRanges {
|
2022-04-23 01:52:23 +00:00
|
|
|
if portRange.Contains(uint16(port)) {
|
|
|
|
inWhitelist = true
|
2022-04-23 01:36:11 +00:00
|
|
|
break
|
2022-04-21 14:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:52:23 +00:00
|
|
|
if !inWhitelist {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-17 12:02:13 +00: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 00:21:31 +00:00
|
|
|
|
2022-04-17 12:02:13 +00:00
|
|
|
sd.replaceDomain(metadata, host)
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 00:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) replaceDomain(metadata *C.Metadata, host string) {
|
2022-04-16 01:04:43 +00:00
|
|
|
log.Debugln("[Sniffer] Sniff TCP [%s:%s]-->[%s:%s] success, replace domain [%s]-->[%s]",
|
2022-04-16 00:21:31 +00:00
|
|
|
metadata.SrcIP, metadata.SrcPort,
|
|
|
|
metadata.DstIP, metadata.DstPort,
|
|
|
|
metadata.Host, host)
|
|
|
|
|
|
|
|
metadata.AddrType = C.AtypDomainName
|
|
|
|
metadata.Host = host
|
2022-04-16 01:51:31 +00:00
|
|
|
metadata.DNSMode = C.DNSMapping
|
2022-04-16 00:21:31 +00:00
|
|
|
resolver.InsertHostByIP(metadata.DstIP, host)
|
2022-04-09 14:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *SnifferDispatcher) Enable() bool {
|
|
|
|
return sd.enable
|
|
|
|
}
|
|
|
|
|
2022-04-16 00:21:31 +00:00
|
|
|
func (sd *SnifferDispatcher) sniffDomain(conn *CN.BufferedConn, metadata *C.Metadata) (string, error) {
|
2022-04-09 14:30:36 +00:00
|
|
|
for _, sniffer := range sd.sniffers {
|
|
|
|
if sniffer.SupportNetwork() == C.TCP {
|
2022-05-02 14:24:14 +00:00
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(3 * time.Second))
|
2022-04-10 12:01:35 +00:00
|
|
|
_, err := conn.Peek(1)
|
2022-05-02 14:24:14 +00:00
|
|
|
_ = conn.SetReadDeadline(time.Time{})
|
2022-04-10 12:01:35 +00:00
|
|
|
if err != nil {
|
2022-04-21 15:08:37 +00:00
|
|
|
_, ok := err.(*net.OpError)
|
2022-05-08 01:09:39 +00:00
|
|
|
if ok {
|
2022-05-07 04:44:28 +00:00
|
|
|
log.Errorln("[Sniffer] [%s] may not have any sent data, Consider adding skip", metadata.DstIP.String())
|
2022-05-02 14:24:14 +00:00
|
|
|
_ = conn.Close()
|
2022-04-21 15:08:37 +00:00
|
|
|
}
|
2022-05-07 04:44:28 +00:00
|
|
|
|
2022-05-08 01:09:39 +00:00
|
|
|
return "", err
|
2022-04-10 12:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bufferedLen := conn.Buffered()
|
|
|
|
bytes, err := conn.Peek(bufferedLen)
|
2022-04-09 14:30:36 +00:00
|
|
|
if err != nil {
|
2022-04-27 10:04:02 +00:00
|
|
|
log.Debugln("[Sniffer] the data length not enough")
|
2022-04-09 14:30:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := sniffer.SniffTCP(bytes)
|
|
|
|
if err != nil {
|
2022-04-16 01:04:43 +00:00
|
|
|
log.Debugln("[Sniffer] [%s] Sniff data failed %s", sniffer.Protocol(), metadata.DstIP)
|
2022-04-09 14:30:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-04-10 12:01:35 +00:00
|
|
|
|
2022-05-02 14:24:14 +00:00
|
|
|
_, err = netip.ParseAddr(host)
|
|
|
|
if err == nil {
|
|
|
|
log.Debugln("[Sniffer] [%s] Sniff data failed %s", sniffer.Protocol(), metadata.DstIP)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-16 00:21:31 +00:00
|
|
|
return host, nil
|
2022-04-09 14:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-16 00:21:31 +00:00
|
|
|
|
|
|
|
return "", ErrorSniffFailed
|
2022-04-09 14:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 00:21:31 +00:00
|
|
|
func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) {
|
2022-04-09 14:30:36 +00:00
|
|
|
dispatcher := SnifferDispatcher{
|
2022-04-16 00:21:31 +00:00
|
|
|
enable: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dispatcher, nil
|
|
|
|
}
|
|
|
|
|
2022-05-02 00:46:24 +00:00
|
|
|
func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTrie[bool],
|
2022-04-21 14:06:08 +00:00
|
|
|
skipSNI *trie.DomainTrie[bool], ports *[]utils.Range[uint16]) (*SnifferDispatcher, error) {
|
2022-04-16 00:21:31 +00:00
|
|
|
dispatcher := SnifferDispatcher{
|
2022-04-17 12:02:13 +00:00
|
|
|
enable: true,
|
|
|
|
foreDomain: forceDomain,
|
|
|
|
skipSNI: skipSNI,
|
2022-04-21 14:06:08 +00:00
|
|
|
portRanges: ports,
|
2022-04-09 14:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, snifferName := range needSniffer {
|
|
|
|
sniffer, err := NewSniffer(snifferName)
|
|
|
|
if err != nil {
|
2022-04-27 10:04:02 +00:00
|
|
|
log.Errorln("Sniffer name[%s] is error", snifferName)
|
2022-04-16 00:21:31 +00:00
|
|
|
return &SnifferDispatcher{enable: false}, err
|
2022-04-09 14:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatcher.sniffers = append(dispatcher.sniffers, sniffer)
|
|
|
|
}
|
|
|
|
|
2022-04-16 00:21:31 +00:00
|
|
|
return &dispatcher, nil
|
2022-04-09 14:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 00:46:24 +00:00
|
|
|
func NewSniffer(name sniffer.Type) (sniffer.Sniffer, error) {
|
2022-04-09 14:30:36 +00:00
|
|
|
switch name {
|
2022-05-02 00:46:24 +00:00
|
|
|
case sniffer.TLS:
|
2022-04-09 14:30:36 +00:00
|
|
|
return &TLSSniffer{}, nil
|
2022-05-02 00:46:24 +00:00
|
|
|
case sniffer.HTTP:
|
|
|
|
return &HTTPSniffer{}, nil
|
2022-04-09 14:30:36 +00:00
|
|
|
default:
|
|
|
|
return nil, ErrorUnsupportedSniffer
|
|
|
|
}
|
|
|
|
}
|