MetaCubeX / mihomo

A simple Python Pydantic model for Honkai: Star Rail parsed data from the Mihomo API.
https://wiki.metacubex.one
MIT License
17.04k stars 2.69k forks source link

[Feature] don't track outbound type dns #1691

Open nunu6689 opened 1 day ago

nunu6689 commented 1 day ago

Verification Steps

Description

When using tproxy with outbound type DNS please don't put it in the connections list. It flooded the connections list and stay there for a while. XD dashboard was lagging a lot, only yacd can handle it. Thanks.

proxies:
  - name: 'dns-hijack'
    type: dns

rules:
  - DST-PORT,53,dns-hijack

Screenshot_20241202-234915_Kiwi Browser

nunu6689 commented 20 hours ago

How about this?

diff --git a/constant/metadata.go b/constant/metadata.go
index 7b73c21a..e0aafdb9 100644
--- a/constant/metadata.go
+++ b/constant/metadata.go
@@ -157,6 +157,8 @@ type Metadata struct {
    RawDstAddr net.Addr `json:"-"`
    // Only domain rule
    SniffHost string `json:"sniffHost"`
+
+   ProxyType AdapterType
 }

 func (m *Metadata) RemoteAddress() string {
diff --git a/tunnel/statistic/manager.go b/tunnel/statistic/manager.go
index 0b309299..3a33ba8f 100644
--- a/tunnel/statistic/manager.go
+++ b/tunnel/statistic/manager.go
@@ -5,6 +5,7 @@ import (
    "time"

    "github.com/metacubex/mihomo/common/atomic"
+   C "github.com/metacubex/mihomo/constant"

    "github.com/puzpuzpuz/xsync/v3"
    "github.com/shirou/gopsutil/v3/process"
@@ -82,7 +83,9 @@ func (m *Manager) Memory() uint64 {
 func (m *Manager) Snapshot() *Snapshot {
    var connections []*TrackerInfo
    m.Range(func(c Tracker) bool {
-       connections = append(connections, c.Info())
+       if c.Info().Metadata.ProxyType != C.Dns {
+           connections = append(connections, c.Info())
+       }
        return true
    })
    return &Snapshot{
diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go
index c379846b..9591ef8a 100644
--- a/tunnel/tunnel.go
+++ b/tunnel/tunnel.go
@@ -409,6 +409,7 @@ func handleUDPConn(packet C.PacketAdapter) {
            }
            logMetadata(metadata, rule, rawPc)

+           metadata.ProxyType = proxy.Type()
            pc := statistic.NewUDPTracker(rawPc, statistic.DefaultManager, metadata, rule, 0, 0, true)

            if rawPc.Chains().Last() == "REJECT-DROP" {
@@ -547,6 +548,7 @@ func handleTCPConn(connCtx C.ConnContext) {
    }
    logMetadata(metadata, rule, remoteConn)

+   metadata.ProxyType = proxy.Type()
    remoteConn = statistic.NewTCPTracker(remoteConn, statistic.DefaultManager, metadata, rule, 0, int64(peekLen), true)
    defer func(remoteConn C.Conn) {
        _ = remoteConn.Close()