From 19bb0b655c93fc5d3e6d0648d046c0bc1a15b94c Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Fri, 31 Jan 2020 14:58:54 +0800 Subject: [PATCH] Fix: match log display --- constant/metadata.go | 4 ++++ tunnel/tunnel.go | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/constant/metadata.go b/constant/metadata.go index 8bdbd7bc..78ac9580 100644 --- a/constant/metadata.go +++ b/constant/metadata.go @@ -71,6 +71,10 @@ func (m *Metadata) RemoteAddress() string { return net.JoinHostPort(m.String(), m.DstPort) } +func (m *Metadata) SourceAddress() string { + return net.JoinHostPort(m.SrcIP.String(), m.SrcPort) +} + func (m *Metadata) UDPAddr() *net.UDPAddr { if m.NetWork != UDP || m.DstIP == nil { return nil diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 50ba1cd6..5b388353 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -207,10 +207,15 @@ func (t *Tunnel) handleUDPConn(packet *inbound.PacketAdapter) { } pc = newUDPTracker(rawPc, DefaultManager, metadata, rule) - if rule != nil { - log.Infoln("[UDP] %s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), rawPc.Chains().String()) - } else { - log.Infoln("[UDP] %s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String()) + switch true { + case rule != nil: + log.Infoln("[UDP] %s --> %v match %s using %s", metadata.SourceAddress(), metadata.String(), rule.RuleType().String(), rawPc.Chains().String()) + case t.mode == Global: + log.Infoln("[UDP] %s --> %v using GLOBAL", metadata.SourceAddress(), metadata.String()) + case t.mode == Direct: + log.Infoln("[UDP] %s --> %v using DIRECT", metadata.SourceAddress(), metadata.String()) + default: + log.Infoln("[UDP] %s --> %v doesn't match any rule using DIRECT", metadata.SourceAddress(), metadata.String()) } t.natTable.Set(key, pc) @@ -250,10 +255,15 @@ func (t *Tunnel) handleTCPConn(localConn C.ServerAdapter) { remoteConn = newTCPTracker(remoteConn, DefaultManager, metadata, rule) defer remoteConn.Close() - if rule != nil { - log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), remoteConn.Chains().String()) - } else { - log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String()) + switch true { + case rule != nil: + log.Infoln("[TCP] %s --> %v match %s using %s", metadata.SourceAddress(), metadata.String(), rule.RuleType().String(), remoteConn.Chains().String()) + case t.mode == Global: + log.Infoln("[TCP] %s --> %v using GLOBAL", metadata.SourceAddress(), metadata.String()) + case t.mode == Direct: + log.Infoln("[TCP] %s --> %v using DIRECT", metadata.SourceAddress(), metadata.String()) + default: + log.Infoln("[TCP] %s --> %v doesn't match any rule using DIRECT", metadata.SourceAddress(), metadata.String()) } switch adapter := localConn.(type) {