Skip to content

Commit 22c50a7

Browse files
FanglidingRPRX
andauthored
UDS: Make all remote addr 0.0.0.0 (#4390)
#4389 (comment) --------- Co-authored-by: RPRX <[email protected]>
1 parent 94c7970 commit 22c50a7

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

app/proxyman/inbound/worker.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package inbound
22

33
import (
44
"context"
5-
"strings"
65
"sync"
76
"sync/atomic"
87
"time"
@@ -464,19 +463,8 @@ func (w *dsWorker) callback(conn stat.Connection) {
464463
WriteCounter: w.downlinkCounter,
465464
}
466465
}
467-
// For most of time, unix obviously have no source addr. But if we leave it empty, it will cause panic.
468-
// So we use gateway as source for log.
469-
// However, there are some special situations where a valid source address might be available.
470-
// Such as the source address parsed from X-Forwarded-For in websocket.
471-
// In that case, we keep it.
472-
var source net.Destination
473-
if !strings.Contains(conn.RemoteAddr().String(), "unix") {
474-
source = net.DestinationFromAddr(conn.RemoteAddr())
475-
} else {
476-
source = net.UnixDestination(w.address)
477-
}
478466
ctx = session.ContextWithInbound(ctx, &session.Inbound{
479-
Source: source,
467+
Source: net.DestinationFromAddr(conn.RemoteAddr()),
480468
Gateway: net.UnixDestination(w.address),
481469
Tag: w.tag,
482470
Conn: conn,

common/net/destination.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,10 @@ func UnixDestination(address Address) Destination {
8989
// NetAddr returns the network address in this Destination in string form.
9090
func (d Destination) NetAddr() string {
9191
addr := ""
92-
if d.Address != nil {
93-
if d.Network == Network_TCP || d.Network == Network_UDP {
94-
addr = d.Address.String() + ":" + d.Port.String()
95-
} else if d.Network == Network_UNIX {
96-
addr = d.Address.String()
97-
}
92+
if d.Network == Network_TCP || d.Network == Network_UDP {
93+
addr = d.Address.String() + ":" + d.Port.String()
94+
} else if d.Network == Network_UNIX {
95+
addr = d.Address.String()
9896
}
9997
return addr
10098
}

transport/internet/system_listener.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,6 @@ type DefaultListener struct {
2121
controllers []control.Func
2222
}
2323

24-
type combinedListener struct {
25-
net.Listener
26-
locker *FileLocker // for unix domain socket
27-
}
28-
29-
func (cl *combinedListener) Close() error {
30-
if cl.locker != nil {
31-
cl.locker.Release()
32-
cl.locker = nil
33-
}
34-
return cl.Listener.Close()
35-
}
36-
3724
func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []control.Func) func(network, address string, c syscall.RawConn) error {
3825
return func(network, address string, c syscall.RawConn) error {
3926
return c.Control(func(fd uintptr) {
@@ -54,6 +41,40 @@ func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []co
5441
}
5542
}
5643

44+
// For some reason, other component of ray will assume the listener is a TCP listener and have valid remote address.
45+
// But in fact it doesn't. So we need to wrap the listener to make it return 0.0.0.0(unspecified) as remote address.
46+
// If other issues encountered, we should able to fix it here.
47+
type listenUDSWrapper struct {
48+
net.Listener
49+
locker *FileLocker
50+
}
51+
52+
func (l *listenUDSWrapper) Accept() (net.Conn, error) {
53+
conn, err := l.Listener.Accept()
54+
if err != nil {
55+
return nil, err
56+
}
57+
return &listenUDSWrapperConn{Conn: conn}, nil
58+
}
59+
60+
func (l *listenUDSWrapper) Close() error {
61+
if l.locker != nil {
62+
l.locker.Release()
63+
l.locker = nil
64+
}
65+
return l.Listener.Close()
66+
}
67+
68+
type listenUDSWrapperConn struct {
69+
net.Conn
70+
}
71+
72+
func (conn *listenUDSWrapperConn) RemoteAddr() net.Addr {
73+
return &net.TCPAddr{
74+
IP: []byte{0, 0, 0, 0},
75+
}
76+
}
77+
5778
func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (l net.Listener, err error) {
5879
var lc net.ListenConfig
5980
var network, address string
@@ -113,9 +134,9 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
113134
callback = func(l net.Listener, err error) (net.Listener, error) {
114135
if err != nil {
115136
locker.Release()
116-
return l, err
137+
return nil, err
117138
}
118-
l = &combinedListener{Listener: l, locker: locker}
139+
l = &listenUDSWrapper{Listener: l, locker: locker}
119140
if filePerm == nil {
120141
return l, nil
121142
}
@@ -129,9 +150,8 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
129150
}
130151
}
131152

132-
l, err = lc.Listen(ctx, network, address)
133-
l, err = callback(l, err)
134-
if sockopt != nil && sockopt.AcceptProxyProtocol {
153+
l, err = callback(lc.Listen(ctx, network, address))
154+
if err == nil && sockopt != nil && sockopt.AcceptProxyProtocol {
135155
policyFunc := func(upstream net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil }
136156
l = &proxyproto.Listener{Listener: l, Policy: policyFunc}
137157
}

0 commit comments

Comments
 (0)