Skip to content

Commit 88419cb

Browse files
committed
chore: better parse remote dst
1 parent 4ed8303 commit 88419cb

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

adapter/outbound/base.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"net"
77
"runtime"
8-
"strings"
98
"sync"
109
"syscall"
1110

@@ -203,12 +202,21 @@ func NewBase(opt BaseOption) *Base {
203202

204203
type conn struct {
205204
N.ExtendedConn
206-
chain C.Chain
207-
actualRemoteDestination string
205+
chain C.Chain
206+
adapterAddr string
208207
}
209208

210209
func (c *conn) RemoteDestination() string {
211-
return c.actualRemoteDestination
210+
if remoteAddr := c.RemoteAddr(); remoteAddr != nil {
211+
m := C.Metadata{}
212+
if err := m.SetRemoteAddr(remoteAddr); err != nil {
213+
if m.Valid() {
214+
return m.String()
215+
}
216+
}
217+
}
218+
host, _, _ := net.SplitHostPort(c.adapterAddr)
219+
return host
212220
}
213221

214222
// Chains implements C.Connection
@@ -241,19 +249,20 @@ func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
241249
if _, ok := c.(syscall.Conn); !ok { // exclusion system conn like *net.TCPConn
242250
c = N.NewDeadlineConn(c) // most conn from outbound can't handle readDeadline correctly
243251
}
244-
return &conn{N.NewExtendedConn(c), []string{a.Name()}, parseRemoteDestination(a.Addr())}
252+
return &conn{N.NewExtendedConn(c), []string{a.Name()}, a.Addr()}
245253
}
246254

247255
type packetConn struct {
248256
N.EnhancePacketConn
249-
chain C.Chain
250-
adapterName string
251-
connID string
252-
actualRemoteDestination string
257+
chain C.Chain
258+
adapterName string
259+
connID string
260+
adapterAddr string
253261
}
254262

255263
func (c *packetConn) RemoteDestination() string {
256-
return c.actualRemoteDestination
264+
host, _, _ := net.SplitHostPort(c.adapterAddr)
265+
return host
257266
}
258267

259268
// Chains implements C.Connection
@@ -292,19 +301,7 @@ func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn {
292301
if _, ok := pc.(syscall.Conn); !ok { // exclusion system conn like *net.UDPConn
293302
epc = N.NewDeadlineEnhancePacketConn(epc) // most conn from outbound can't handle readDeadline correctly
294303
}
295-
return &packetConn{epc, []string{a.Name()}, a.Name(), utils.NewUUIDV4().String(), parseRemoteDestination(a.Addr())}
296-
}
297-
298-
func parseRemoteDestination(addr string) string {
299-
if dst, _, err := net.SplitHostPort(addr); err == nil {
300-
return dst
301-
} else {
302-
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
303-
return dst
304-
} else {
305-
return ""
306-
}
307-
}
304+
return &packetConn{epc, []string{a.Name()}, a.Name(), utils.NewUUIDV4().String(), a.Addr()}
308305
}
309306

310307
type AddRef interface {

tunnel/statistic/tracker.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package statistic
33
import (
44
"io"
55
"net"
6-
"net/netip"
76
"time"
87

98
"github.com/metacubex/mihomo/common/atomic"
@@ -116,20 +115,8 @@ func (tt *tcpTracker) Upstream() any {
116115
return tt.Conn
117116
}
118117

119-
func parseRemoteDestination(addr net.Addr, conn C.Connection) string {
120-
if addr != nil {
121-
if addrPort, err := netip.ParseAddrPort(addr.String()); err == nil && addrPort.Addr().IsValid() {
122-
return addrPort.Addr().String()
123-
}
124-
}
125-
if conn != nil {
126-
return conn.RemoteDestination()
127-
}
128-
return ""
129-
}
130-
131118
func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *tcpTracker {
132-
metadata.RemoteDst = parseRemoteDestination(conn.RemoteAddr(), conn)
119+
metadata.RemoteDst = conn.RemoteDestination()
133120

134121
t := &tcpTracker{
135122
Conn: conn,
@@ -220,7 +207,7 @@ func (ut *udpTracker) Upstream() any {
220207
}
221208

222209
func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *udpTracker {
223-
metadata.RemoteDst = parseRemoteDestination(nil, conn)
210+
metadata.RemoteDst = conn.RemoteDestination()
224211

225212
ut := &udpTracker{
226213
PacketConn: conn,

0 commit comments

Comments
 (0)