From 895d93b10bb7df8e72be25a9ddc88608ce7a5c97 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Tue, 12 Apr 2022 20:20:04 +0800 Subject: [PATCH] chore:merge & adjust code --- component/sniffer/dispatcher.go | 4 ++-- hub/executor/executor.go | 24 +++++++++++++----------- rule/provider/domain_strategy.go | 6 +++--- tunnel/tunnel.go | 6 ++++-- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/component/sniffer/dispatcher.go b/component/sniffer/dispatcher.go index 677c6ffa..91bf7f46 100644 --- a/component/sniffer/dispatcher.go +++ b/component/sniffer/dispatcher.go @@ -53,13 +53,13 @@ func (sd *SnifferDispatcher) cover(conn *CN.BufferedConn, metadata *C.Metadata) bufferedLen := conn.Buffered() bytes, err := conn.Peek(bufferedLen) if err != nil { - log.Debugln("[Sniffer] the data lenght not enough") + log.Warnln("[Sniffer] the data lenght not enough") continue } host, err := sniffer.SniffTCP(bytes) if err != nil { - log.Debugln("[Sniffer][%s] Sniff data failed", sniffer.Protocol()) + log.Warnln("[Sniffer][%s] Sniff data failed", sniffer.Protocol()) continue } metadata.Host = host diff --git a/hub/executor/executor.go b/hub/executor/executor.go index f4b57347..b9785c6c 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -3,6 +3,7 @@ package executor import ( "fmt" "net" + "net/netip" "os" "runtime" "strconv" @@ -89,16 +90,6 @@ func ApplyConfig(cfg *config.Config, force bool) { 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 { ports := P.GetPorts() 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 } @@ -229,6 +220,17 @@ func updateTun(tun *config.Tun, dns *config.DNS) { 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) { log.SetLevel(general.LogLevel) tunnel.SetMode(general.Mode) diff --git a/rule/provider/domain_strategy.go b/rule/provider/domain_strategy.go index 31ecb184..0288c8aa 100644 --- a/rule/provider/domain_strategy.go +++ b/rule/provider/domain_strategy.go @@ -10,7 +10,7 @@ import ( type domainStrategy struct { shouldResolveIP bool count int - domainRules *trie.DomainTrie + domainRules *trie.DomainTrie[bool] } func (d *domainStrategy) Match(metadata *C.Metadata) bool { @@ -26,9 +26,9 @@ func (d *domainStrategy) ShouldResolveIP() bool { } func (d *domainStrategy) OnUpdate(rules []string) { - domainTrie := trie.New() + domainTrie := trie.New[bool]() for _, rule := range rules { - err := domainTrie.Insert(rule, "") + err := domainTrie.Insert(rule, true) if err != nil { log.Warnln("invalid domain:[%s]", rule) } else { diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 0072d4f7..b7d8fd69 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -247,7 +247,7 @@ func handleUDPConn(packet *inbound.PacketAdapter) { ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout) defer cancel() - rawPc, err := proxy.ListenPacketContext(ctx, metadata.Pure()) + rawPc, err := proxy.ListenPacketContext(ctx, metadata) if err != nil { if rule == nil { 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 } - 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 rule == nil { log.Warnln("[TCP] dial %s to %s error: %s", proxy.Name(), metadata.RemoteAddress(), err.Error())