From 6a0987c52ae4f108b667ffcf72c280ca8fe9dce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 7 Jul 2022 21:39:44 +0800 Subject: [PATCH] Minor fixes --- common/cond.go | 10 ++++++++++ common/control/bind_linux.go | 4 ++-- common/control/mark_linux.go | 4 ++-- common/control/reuse_linux.go | 6 +++--- protocol/socks/handshake.go | 5 +++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/common/cond.go b/common/cond.go index 4e60848..a655657 100644 --- a/common/cond.go +++ b/common/cond.go @@ -92,6 +92,16 @@ func FilterNotDefault[T comparable](arr []T) []T { }) } +func FilterIndexed[T any](arr []T, block func(index int, it T) bool) []T { + var retArr []T + for i, it := range arr { + if block(i, it) { + retArr = append(retArr, it) + } + } + return retArr +} + func Find[T any](arr []T, block func(it T) bool) T { for _, it := range arr { if block(it) { diff --git a/common/control/bind_linux.go b/common/control/bind_linux.go index ebc5681..883acca 100644 --- a/common/control/bind_linux.go +++ b/common/control/bind_linux.go @@ -3,7 +3,7 @@ package control import ( "syscall" - "github.com/sagernet/sing/common" + E "github.com/sagernet/sing/common/exceptions" ) func BindToInterface(interfaceName string) Func { @@ -12,6 +12,6 @@ func BindToInterface(interfaceName string) Func { err := conn.Control(func(fd uintptr) { innerErr = syscall.BindToDevice(int(fd), interfaceName) }) - return common.AnyError(innerErr, err) + return E.Errors(innerErr, err) } } diff --git a/common/control/mark_linux.go b/common/control/mark_linux.go index d217371..040ebf7 100644 --- a/common/control/mark_linux.go +++ b/common/control/mark_linux.go @@ -3,7 +3,7 @@ package control import ( "syscall" - "github.com/sagernet/sing/common" + E "github.com/sagernet/sing/common/exceptions" ) func RoutingMark(mark int) Func { @@ -12,6 +12,6 @@ func RoutingMark(mark int) Func { err := conn.Control(func(fd uintptr) { innerErr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, mark) }) - return common.AnyError(innerErr, err) + return E.Errors(innerErr, err) } } diff --git a/common/control/reuse_linux.go b/common/control/reuse_linux.go index d7e2281..3d92db1 100644 --- a/common/control/reuse_linux.go +++ b/common/control/reuse_linux.go @@ -3,7 +3,7 @@ package control import ( "syscall" - "github.com/sagernet/sing/common" + E "github.com/sagernet/sing/common/exceptions" ) func ReuseAddr() Func { @@ -11,11 +11,11 @@ func ReuseAddr() Func { var innerErr error err := conn.Control(func(fd uintptr) { const SO_REUSEPORT = 0xf - innerErr = common.AnyError( + innerErr = E.Errors( syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1), syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, SO_REUSEPORT, 1), ) }) - return common.AnyError(innerErr, err) + return E.Errors(innerErr, err) } } diff --git a/protocol/socks/handshake.go b/protocol/socks/handshake.go index 3202bb5..98715a2 100644 --- a/protocol/socks/handshake.go +++ b/protocol/socks/handshake.go @@ -202,7 +202,8 @@ func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authent metadata.Destination = request.Destination return handler.NewConnection(ctx, conn, metadata) case socks5.CommandUDPAssociate: - udpConn, err := net.ListenUDP(M.NetworkFromNetAddr("udp", M.AddrFromNetAddr(conn.LocalAddr())), net.UDPAddrFromAddrPort(netip.AddrPortFrom(M.AddrFromNetAddr(conn.LocalAddr()), 0))) + var udpConn *net.UDPConn + udpConn, err = net.ListenUDP(M.NetworkFromNetAddr("udp", M.AddrFromNetAddr(conn.LocalAddr())), net.UDPAddrFromAddrPort(netip.AddrPortFrom(M.AddrFromNetAddr(conn.LocalAddr()), 0))) if err != nil { return err } @@ -224,7 +225,7 @@ func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authent close(done) }() err = common.Error(io.Copy(io.Discard, conn)) - return E.New(innerError, err) + return E.Errors(innerError, err) default: err = socks5.WriteResponse(conn, socks5.Response{ ReplyCode: socks5.ReplyCodeUnsupported,