chore: adjust sniffer constant

This commit is contained in:
MetaCubeX 2022-05-02 08:46:24 +08:00
parent ebbce4d061
commit 26a38bd8de
3 changed files with 19 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package sniffer
import ( import (
"errors" "errors"
"github.com/Dreamacro/clash/constant/sniffer"
"net" "net"
"net/netip" "net/netip"
"strconv" "strconv"
@ -28,7 +29,7 @@ type (
SnifferDispatcher struct { SnifferDispatcher struct {
enable bool enable bool
sniffers []C.Sniffer sniffers []sniffer.Sniffer
foreDomain *trie.DomainTrie[bool] foreDomain *trie.DomainTrie[bool]
skipSNI *trie.DomainTrie[bool] skipSNI *trie.DomainTrie[bool]
@ -136,7 +137,7 @@ func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) {
return &dispatcher, nil return &dispatcher, nil
} }
func NewSnifferDispatcher(needSniffer []C.SnifferType, forceDomain *trie.DomainTrie[bool], func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTrie[bool],
skipSNI *trie.DomainTrie[bool], ports *[]utils.Range[uint16]) (*SnifferDispatcher, error) { skipSNI *trie.DomainTrie[bool], ports *[]utils.Range[uint16]) (*SnifferDispatcher, error) {
dispatcher := SnifferDispatcher{ dispatcher := SnifferDispatcher{
enable: true, enable: true,
@ -158,9 +159,9 @@ func NewSnifferDispatcher(needSniffer []C.SnifferType, forceDomain *trie.DomainT
return &dispatcher, nil return &dispatcher, nil
} }
func NewSniffer(name C.SnifferType) (C.Sniffer, error) { func NewSniffer(name sniffer.Type) (sniffer.Sniffer, error) {
switch name { switch name {
case C.TLS: case sniffer.TLS:
return &TLSSniffer{}, nil return &TLSSniffer{}, nil
default: default:
return nil, ErrorUnsupportedSniffer return nil, ErrorUnsupportedSniffer

View File

@ -4,6 +4,7 @@ import (
"container/list" "container/list"
"errors" "errors"
"fmt" "fmt"
"github.com/Dreamacro/clash/constant/sniffer"
"github.com/Dreamacro/clash/listener/tun/ipstack/commons" "github.com/Dreamacro/clash/listener/tun/ipstack/commons"
"net" "net"
"net/netip" "net/netip"
@ -30,6 +31,7 @@ import (
"github.com/Dreamacro/clash/component/trie" "github.com/Dreamacro/clash/component/trie"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
providerTypes "github.com/Dreamacro/clash/constant/provider" providerTypes "github.com/Dreamacro/clash/constant/provider"
snifferTypes "github.com/Dreamacro/clash/constant/sniffer"
"github.com/Dreamacro/clash/dns" "github.com/Dreamacro/clash/dns"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
T "github.com/Dreamacro/clash/tunnel" T "github.com/Dreamacro/clash/tunnel"
@ -128,7 +130,7 @@ type IPTables struct {
type Sniffer struct { type Sniffer struct {
Enable bool Enable bool
Force bool Force bool
Sniffers []C.SnifferType Sniffers []sniffer.Type
Reverses *trie.DomainTrie[bool] Reverses *trie.DomainTrie[bool]
ForceDomain *trie.DomainTrie[bool] ForceDomain *trie.DomainTrie[bool]
SkipSNI *trie.DomainTrie[bool] SkipSNI *trie.DomainTrie[bool]
@ -954,11 +956,11 @@ func parseSniffer(snifferRaw SnifferRaw) (*Sniffer, error) {
sniffer.Ports = &ports sniffer.Ports = &ports
loadSniffer := make(map[C.SnifferType]struct{}) loadSniffer := make(map[snifferTypes.Type]struct{})
for _, snifferName := range snifferRaw.Sniffing { for _, snifferName := range snifferRaw.Sniffing {
find := false find := false
for _, snifferType := range C.SnifferList { for _, snifferType := range snifferTypes.List {
if snifferType.String() == strings.ToUpper(snifferName) { if snifferType.String() == strings.ToUpper(snifferName) {
find = true find = true
loadSniffer[snifferType] = struct{}{} loadSniffer[snifferType] = struct{}{}

View File

@ -1,23 +1,25 @@
package constant package sniffer
import "github.com/Dreamacro/clash/constant"
type Sniffer interface { type Sniffer interface {
SupportNetwork() NetWork SupportNetwork() constant.NetWork
SniffTCP(bytes []byte) (string, error) SniffTCP(bytes []byte) (string, error)
Protocol() string Protocol() string
} }
const ( const (
TLS SnifferType = iota TLS Type = iota
HTTP SnifferType HTTP
) )
var ( var (
SnifferList = []SnifferType{TLS, HTTP} List = []Type{TLS, HTTP}
) )
type SnifferType int type Type int
func (rt SnifferType) String() string { func (rt Type) String() string {
switch rt { switch rt {
case TLS: case TLS:
return "TLS" return "TLS"