Skip to content

Commit 9e8f4ad

Browse files
committed
chore: better addr parsing
1 parent 09c7ee0 commit 9e8f4ad

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

listener/sing_shadowsocks/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (l *Listener) HandleConn(conn net.Conn, tunnel C.Tunnel, additions ...inbou
196196
ctx := sing.WithAdditions(context.TODO(), additions...)
197197
err := l.service.NewConnection(ctx, conn, M.Metadata{
198198
Protocol: "shadowsocks",
199-
Source: M.ParseSocksaddr(conn.RemoteAddr().String()),
199+
Source: M.SocksaddrFromNet(conn.RemoteAddr()),
200200
})
201201
if err != nil {
202202
_ = conn.Close()

listener/sing_vless/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (l *Listener) HandleConn(conn net.Conn, tunnel C.Tunnel, additions ...inbou
201201
ctx := sing.WithAdditions(context.TODO(), additions...)
202202
err := l.service.NewConnection(ctx, conn, metadata.Metadata{
203203
Protocol: "vless",
204-
Source: metadata.ParseSocksaddr(conn.RemoteAddr().String()),
204+
Source: metadata.SocksaddrFromNet(conn.RemoteAddr()),
205205
})
206206
if err != nil {
207207
_ = conn.Close()

listener/sing_vmess/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (l *Listener) HandleConn(conn net.Conn, tunnel C.Tunnel, additions ...inbou
187187
ctx := sing.WithAdditions(context.TODO(), additions...)
188188
err := l.service.NewConnection(ctx, conn, metadata.Metadata{
189189
Protocol: "vmess",
190-
Source: metadata.ParseSocksaddr(conn.RemoteAddr().String()),
190+
Source: metadata.SocksaddrFromNet(conn.RemoteAddr()),
191191
})
192192
if err != nil {
193193
_ = conn.Close()

transport/socks5/socks5.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func ServerHandshake(rw net.Conn, authenticator auth.Authenticator) (addr Addr,
189189
switch command {
190190
case CmdConnect, CmdUDPAssociate:
191191
// Acquire server listened address info
192-
localAddr := ParseAddr(rw.LocalAddr().String())
192+
localAddr := ParseAddrToSocksAddr(rw.LocalAddr())
193193
if localAddr == nil {
194194
err = ErrAddressNotSupported
195195
} else {
@@ -414,12 +414,15 @@ func ParseAddr(s string) Addr {
414414
func ParseAddrToSocksAddr(addr net.Addr) Addr {
415415
var hostip net.IP
416416
var port int
417-
if udpaddr, ok := addr.(*net.UDPAddr); ok {
418-
hostip = udpaddr.IP
419-
port = udpaddr.Port
420-
} else if tcpaddr, ok := addr.(*net.TCPAddr); ok {
421-
hostip = tcpaddr.IP
422-
port = tcpaddr.Port
417+
switch addr := addr.(type) {
418+
case *net.UDPAddr:
419+
hostip = addr.IP
420+
port = addr.Port
421+
case *net.TCPAddr:
422+
hostip = addr.IP
423+
port = addr.Port
424+
case nil:
425+
return nil
423426
}
424427

425428
// fallback parse

0 commit comments

Comments
 (0)