chore:merge & adjust code

This commit is contained in:
gVisor bot 2022-04-12 20:20:04 +08:00
parent 950e638222
commit 895d93b10b
4 changed files with 22 additions and 18 deletions

View file

@ -53,13 +53,13 @@ func (sd *SnifferDispatcher) cover(conn *CN.BufferedConn, metadata *C.Metadata)
bufferedLen := conn.Buffered() bufferedLen := conn.Buffered()
bytes, err := conn.Peek(bufferedLen) bytes, err := conn.Peek(bufferedLen)
if err != nil { if err != nil {
log.Debugln("[Sniffer] the data lenght not enough") log.Warnln("[Sniffer] the data lenght not enough")
continue continue
} }
host, err := sniffer.SniffTCP(bytes) host, err := sniffer.SniffTCP(bytes)
if err != nil { if err != nil {
log.Debugln("[Sniffer][%s] Sniff data failed", sniffer.Protocol()) log.Warnln("[Sniffer][%s] Sniff data failed", sniffer.Protocol())
continue continue
} }
metadata.Host = host metadata.Host = host

View file

@ -3,6 +3,7 @@ package executor
import ( import (
"fmt" "fmt"
"net" "net"
"net/netip"
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
@ -89,16 +90,6 @@ func ApplyConfig(cfg *config.Config, force bool) {
log.SetLevel(cfg.General.LogLevel) log.SetLevel(cfg.General.LogLevel)
} }
func updateSniffer(sniffer *config.Sniffer) {
if sniffer.Enable {
var err error
SNI.Dispatcher, err = SNI.NewSnifferDispatcher(sniffer.Sniffers, sniffer.Force)
if err != nil {
log.Errorln("Init Sniffer failed, err:%v", err)
}
}
}
func GetGeneral() *config.General { func GetGeneral() *config.General {
ports := P.GetPorts() ports := P.GetPorts()
var authenticator []string var authenticator []string
@ -181,7 +172,7 @@ func updateDNS(c *config.DNS, t *config.Tun) {
} }
} }
func updateHosts(tree *trie.DomainTrie) { func updateHosts(tree *trie.DomainTrie[netip.Addr]) {
resolver.DefaultHosts = tree resolver.DefaultHosts = tree
} }
@ -229,6 +220,17 @@ func updateTun(tun *config.Tun, dns *config.DNS) {
P.ReCreateTun(tun, dns, tunnel.TCPIn(), tunnel.UDPIn()) P.ReCreateTun(tun, dns, tunnel.TCPIn(), tunnel.UDPIn())
} }
func updateSniffer(sniffer *config.Sniffer) {
if sniffer.Enable {
var err error
SNI.Dispatcher, err = SNI.NewSnifferDispatcher(sniffer.Sniffers, sniffer.Force)
if err != nil {
log.Warnln("initial sniffer failed, err:%v", err)
}
log.Infoln("Sniffer is loaded and working")
}
}
func updateGeneral(general *config.General, force bool) { func updateGeneral(general *config.General, force bool) {
log.SetLevel(general.LogLevel) log.SetLevel(general.LogLevel)
tunnel.SetMode(general.Mode) tunnel.SetMode(general.Mode)

View file

@ -10,7 +10,7 @@ import (
type domainStrategy struct { type domainStrategy struct {
shouldResolveIP bool shouldResolveIP bool
count int count int
domainRules *trie.DomainTrie domainRules *trie.DomainTrie[bool]
} }
func (d *domainStrategy) Match(metadata *C.Metadata) bool { func (d *domainStrategy) Match(metadata *C.Metadata) bool {
@ -26,9 +26,9 @@ func (d *domainStrategy) ShouldResolveIP() bool {
} }
func (d *domainStrategy) OnUpdate(rules []string) { func (d *domainStrategy) OnUpdate(rules []string) {
domainTrie := trie.New() domainTrie := trie.New[bool]()
for _, rule := range rules { for _, rule := range rules {
err := domainTrie.Insert(rule, "") err := domainTrie.Insert(rule, true)
if err != nil { if err != nil {
log.Warnln("invalid domain:[%s]", rule) log.Warnln("invalid domain:[%s]", rule)
} else { } else {

View file

@ -247,7 +247,7 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout) ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout)
defer cancel() defer cancel()
rawPc, err := proxy.ListenPacketContext(ctx, metadata.Pure()) rawPc, err := proxy.ListenPacketContext(ctx, metadata)
if err != nil { if err != nil {
if rule == nil { if rule == nil {
log.Warnln("[UDP] dial %s to %s error: %s", proxy.Name(), metadata.RemoteAddress(), err.Error()) log.Warnln("[UDP] dial %s to %s error: %s", proxy.Name(), metadata.RemoteAddress(), err.Error())
@ -309,7 +309,9 @@ func handleTCPConn(connCtx C.ConnContext) {
return return
} }
remoteConn, err := proxy.DialContext(ctx, metadata.Pure()) ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout)
defer cancel()
remoteConn, err := proxy.DialContext(ctx, metadata)
if err != nil { if err != nil {
if rule == nil { if rule == nil {
log.Warnln("[TCP] dial %s to %s error: %s", proxy.Name(), metadata.RemoteAddress(), err.Error()) log.Warnln("[TCP] dial %s to %s error: %s", proxy.Name(), metadata.RemoteAddress(), err.Error())