From a20e1a48d8bec9351c3caa3284ad509b1ee792ea Mon Sep 17 00:00:00 2001
From: mzz2017 <2017@duck.com>
Date: Wed, 8 Feb 2023 21:32:20 +0800
Subject: [PATCH] optimize: only print routing log for new connection to avoid
 the log exploded (Quic and BT).

---
 control/udp.go          | 43 ++++++++++++++++++++++-------------------
 control/udp_endpoint.go |  1 +
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/control/udp.go b/control/udp.go
index effd038..bc48ad6 100644
--- a/control/udp.go
+++ b/control/udp.go
@@ -212,26 +212,29 @@ getNew:
 	// This is real dialer.
 	d := ue.Dialer
 
-	if isDns && c.log.IsLevelEnabled(logrus.DebugLevel) && len(dnsMessage.Questions) > 0 {
-		q := dnsMessage.Questions[0]
-		c.log.WithFields(logrus.Fields{
-			"l4proto":  "UDP(DNS)",
-			"outbound": outbound.Name,
-			"dialer":   d.Name(),
-			"qname":    strings.ToLower(q.Name.String()),
-			"qtype":    q.Type,
-		}).Infof("%v <-> %v",
-			RefineSourceToShow(src, destToSend.Addr()), RefineAddrPortToShow(destToSend),
-		)
-	} else {
-		// TODO: Set-up ip to domain mapping and show domain if possible.
-		c.log.WithFields(logrus.Fields{
-			"l4proto":  "UDP",
-			"outbound": outbound.Name,
-			"dialer":   d.Name(),
-		}).Infof("%v <-> %v",
-			RefineSourceToShow(src, destToSend.Addr()), RefineAddrPortToShow(destToSend),
-		)
+	if isNew {
+		// Only print routing for new connection to avoid the log exploded (Quic and BT).
+		if isDns && c.log.IsLevelEnabled(logrus.DebugLevel) && len(dnsMessage.Questions) > 0 {
+			q := dnsMessage.Questions[0]
+			c.log.WithFields(logrus.Fields{
+				"l4proto":  "UDP(DNS)",
+				"outbound": outbound.Name,
+				"dialer":   d.Name(),
+				"qname":    strings.ToLower(q.Name.String()),
+				"qtype":    q.Type,
+			}).Infof("%v <-> %v",
+				RefineSourceToShow(src, destToSend.Addr()), RefineAddrPortToShow(destToSend),
+			)
+		} else {
+			// TODO: Set-up ip to domain mapping and show domain if possible.
+			c.log.WithFields(logrus.Fields{
+				"l4proto":  "UDP",
+				"outbound": outbound.Name,
+				"dialer":   d.Name(),
+			}).Infof("%v <-> %v",
+				RefineSourceToShow(src, destToSend.Addr()), RefineAddrPortToShow(destToSend),
+			)
+		}
 	}
 	//log.Printf("WriteToUDPAddrPort->%v", destToSend)
 	_, err = ue.WriteToUDPAddrPort(data, destToSend)
diff --git a/control/udp_endpoint.go b/control/udp_endpoint.go
index 009581a..f8d8dea 100644
--- a/control/udp_endpoint.go
+++ b/control/udp_endpoint.go
@@ -98,6 +98,7 @@ func (p *UdpEndpointPool) Remove(lAddr netip.AddrPort, udpEndpoint *UdpEndpoint)
 	}
 	return nil
 }
+
 func (p *UdpEndpointPool) GetOrCreate(lAddr netip.AddrPort, createOption *UdpEndpointOptions) (udpEndpoint *UdpEndpoint, isNew bool, err error) {
 	p.mu.Lock()
 	defer p.mu.Unlock()