Skip to content

Commit fa64775

Browse files
authored
Tunnel/Dokodemo: Fix stats conn unwrap (#5440)
Fixes #5439
1 parent a6792dd commit fa64775

File tree

8 files changed

+18
-26
lines changed

8 files changed

+18
-26
lines changed

proxy/dokodemo/dokodemo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st
111111
destinationOverridden = true
112112
}
113113
}
114-
if tlsConn, ok := conn.(tls.Interface); ok && !destinationOverridden {
114+
iConn := stat.TryUnwrapStatsConn(conn)
115+
if tlsConn, ok := iConn.(tls.Interface); ok && !destinationOverridden {
115116
if serverName := tlsConn.HandshakeContextServerName(ctx); serverName != "" {
116117
dest.Address = net.DomainAddress(serverName)
117118
destinationOverridden = true

proxy/http/client.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,7 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u
296296
return nil, err
297297
}
298298

299-
iConn := rawConn
300-
if statConn, ok := iConn.(*stat.CounterConnection); ok {
301-
iConn = statConn.Connection
302-
}
299+
iConn := stat.TryUnwrapStatsConn(rawConn)
303300

304301
nextProto := ""
305302
if tlsConn, ok := iConn.(*tls.Conn); ok {

proxy/proxy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,7 @@ func readV(ctx context.Context, reader buf.Reader, writer buf.Writer, timer sign
787787
}
788788

789789
func IsRAWTransportWithoutSecurity(conn stat.Connection) bool {
790-
iConn := conn
791-
if statConn, ok := iConn.(*stat.CounterConnection); ok {
792-
iConn = statConn.Connection
793-
}
790+
iConn := stat.TryUnwrapStatsConn(conn)
794791
_, ok1 := iConn.(*proxyproto.Conn)
795792
_, ok2 := iConn.(*net.TCPConn)
796793
_, ok3 := iConn.(*internet.UnixConnWrapper)

proxy/trojan/server.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,7 @@ func (s *Server) Network() []net.Network {
147147

148148
// Process implements proxy.Inbound.Process().
149149
func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
150-
iConn := conn
151-
statConn, ok := iConn.(*stat.CounterConnection)
152-
if ok {
153-
iConn = statConn.Connection
154-
}
150+
iConn := stat.TryUnwrapStatsConn(conn)
155151

156152
sessionPolicy := s.policyManager.ForLevel(0)
157153
if err := conn.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)); err != nil {

proxy/vless/inbound/inbound.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ func (*Handler) Network() []net.Network {
265265

266266
// Process implements proxy.Inbound.Process().
267267
func (h *Handler) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
268-
iConn := connection
269-
if statConn, ok := iConn.(*stat.CounterConnection); ok {
270-
iConn = statConn.Connection
271-
}
268+
iConn := stat.TryUnwrapStatsConn(connection)
272269

273270
if h.decryption != nil {
274271
var err error

proxy/vless/outbound/outbound.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
192192

193193
ob.Conn = conn // for Vision's pre-connect
194194

195-
iConn := conn
196-
if statConn, ok := iConn.(*stat.CounterConnection); ok {
197-
iConn = statConn.Connection
198-
}
195+
iConn := stat.TryUnwrapStatsConn(conn)
199196
target := ob.Target
200197
errors.LogInfo(ctx, "tunneling request to ", target, " via ", rec.Destination.NetAddr())
201198

proxy/vmess/inbound/inbound.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
229229
return errors.New("unable to set read deadline").Base(err).AtWarning()
230230
}
231231

232-
iConn := connection
233-
if statConn, ok := iConn.(*stat.CounterConnection); ok {
234-
iConn = statConn.Connection
235-
}
232+
iConn := stat.TryUnwrapStatsConn(connection)
236233
_, isDrain := iConn.(*net.TCPConn)
237234
if !isDrain {
238235
_, isDrain = iConn.(*net.UnixConn)

transport/internet/stat/connection.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ func (c *CounterConnection) Write(b []byte) (int, error) {
3232
}
3333
return nBytes, err
3434
}
35+
36+
func TryUnwrapStatsConn(conn net.Conn) net.Conn {
37+
if conn == nil {
38+
return conn
39+
}
40+
if conn, ok := conn.(*CounterConnection); ok {
41+
return conn.Connection
42+
}
43+
return conn
44+
}

0 commit comments

Comments
 (0)