diff --git a/common/bufio/conn.go b/common/bufio/conn.go index bde9bd5..e58d66a 100644 --- a/common/bufio/conn.go +++ b/common/bufio/conn.go @@ -30,7 +30,7 @@ func (w *ExtendedUDPConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) { return M.Socksaddr{}, err } buffer.Truncate(n) - return M.SocksaddrFromNetIP(addr), nil + return M.SocksaddrFromNetIP(addr).Unwrap(), nil } func (w *ExtendedUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { @@ -58,7 +58,7 @@ func (w *ExtendedPacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) if err != nil { return M.Socksaddr{}, err } - return M.SocksaddrFromNet(addr), err + return M.SocksaddrFromNet(addr).Unwrap(), err } func (w *ExtendedPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { diff --git a/common/metadata/addr.go b/common/metadata/addr.go index 43865ef..3abe98a 100644 --- a/common/metadata/addr.go +++ b/common/metadata/addr.go @@ -6,6 +6,8 @@ import ( "strconv" "strings" "unsafe" + + "github.com/sagernet/sing/common/debug" ) type Socksaddr struct { @@ -19,15 +21,15 @@ func (ap Socksaddr) Network() string { } func (ap Socksaddr) IsIP() bool { - return ap.Addr.IsValid() && ap.Fqdn == "" + return ap.Addr.IsValid() } func (ap Socksaddr) IsIPv4() bool { - return ap.Addr.Is4() || ap.Addr.Is4In6() + return ap.Addr.Is4() } func (ap Socksaddr) IsIPv6() bool { - return ap.Addr.Is6() && !ap.Addr.Is4In6() + return ap.Addr.Is6() } func (ap Socksaddr) Unwrap() Socksaddr { @@ -41,7 +43,7 @@ func (ap Socksaddr) Unwrap() Socksaddr { } func (ap Socksaddr) IsFqdn() bool { - return !ap.Addr.IsValid() && IsDomainName(ap.Fqdn) + return IsDomainName(ap.Fqdn) } func (ap Socksaddr) IsValid() bool { @@ -49,6 +51,9 @@ func (ap Socksaddr) IsValid() bool { } func (ap Socksaddr) AddrString() string { + if debug.Enabled { + ap.CheckBadAddr() + } if ap.Addr.IsValid() { return ap.Addr.String() } else { @@ -57,12 +62,18 @@ func (ap Socksaddr) AddrString() string { } func (ap Socksaddr) IPAddr() *net.IPAddr { + if debug.Enabled { + ap.CheckBadAddr() + } return &net.IPAddr{ IP: ap.Addr.AsSlice(), } } func (ap Socksaddr) TCPAddr() *net.TCPAddr { + if debug.Enabled { + ap.CheckBadAddr() + } return &net.TCPAddr{ IP: ap.Addr.AsSlice(), Port: int(ap.Port), @@ -70,6 +81,9 @@ func (ap Socksaddr) TCPAddr() *net.TCPAddr { } func (ap Socksaddr) UDPAddr() *net.UDPAddr { + if debug.Enabled { + ap.CheckBadAddr() + } return &net.UDPAddr{ IP: ap.Addr.AsSlice(), Port: int(ap.Port), @@ -77,24 +91,22 @@ func (ap Socksaddr) UDPAddr() *net.UDPAddr { } func (ap Socksaddr) AddrPort() netip.AddrPort { + if debug.Enabled { + ap.CheckBadAddr() + } return *(*netip.AddrPort)(unsafe.Pointer(&ap)) } func (ap Socksaddr) String() string { + if debug.Enabled { + ap.CheckBadAddr() + } return net.JoinHostPort(ap.AddrString(), strconv.Itoa(int(ap.Port))) } -func TCPAddr(ap netip.AddrPort) *net.TCPAddr { - return &net.TCPAddr{ - IP: ap.Addr().AsSlice(), - Port: int(ap.Port()), - } -} - -func UDPAddr(ap netip.AddrPort) *net.UDPAddr { - return &net.UDPAddr{ - IP: ap.Addr().AsSlice(), - Port: int(ap.Port()), +func (ap Socksaddr) CheckBadAddr() { + if ap.Addr.Is4In6() || ap.Addr.IsValid() && ap.Fqdn != "" { + panic("bad socksaddr") } } diff --git a/common/metadata/serializer.go b/common/metadata/serializer.go index 6b5321b..98ed304 100644 --- a/common/metadata/serializer.go +++ b/common/metadata/serializer.go @@ -62,7 +62,7 @@ func (s *Serializer) WriteAddress(buffer *buf.Buffer, addr Socksaddr) error { return err } if addr.Addr.IsValid() { - _, err = buffer.Write(addr.Unwrap().Addr.AsSlice()) + _, err = buffer.Write(addr.Addr.AsSlice()) } else { err = WriteSocksString(buffer, addr.Fqdn) } diff --git a/go.mod b/go.mod index fabefb4..45d4d34 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/sagernet/sing go 1.18 -require golang.org/x/sys v0.0.0-20220913120320-3275c407cedc +require golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 diff --git a/go.sum b/go.sum index 6b04132..25341eb 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -golang.org/x/sys v0.0.0-20220913120320-3275c407cedc h1:dpclq5m2YrqPGStKmtw7IcNbKLfbIqKXvNxDJKdIKYc= -golang.org/x/sys v0.0.0-20220913120320-3275c407cedc/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc= +golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/protocol/http/addr.go b/protocol/http/addr.go index 27ad1b2..61b673c 100644 --- a/protocol/http/addr.go +++ b/protocol/http/addr.go @@ -18,5 +18,5 @@ func SourceAddress(request *http.Request) M.Socksaddr { } } } - return address + return address.Unwrap() } diff --git a/protocol/socks/socks4/protocol.go b/protocol/socks/socks4/protocol.go index 619e389..f5c8f7e 100644 --- a/protocol/socks/socks4/protocol.go +++ b/protocol/socks/socks4/protocol.go @@ -96,7 +96,7 @@ func WriteRequest(writer io.Writer, request Request) error { binary.Write(buffer, binary.BigEndian, request.Destination.Port), ) if request.Destination.IsIPv4() { - common.Must1(buffer.Write(request.Destination.Unwrap().Addr.AsSlice())) + common.Must1(buffer.Write(request.Destination.Addr.AsSlice())) } else { // 0.0.0.X common.Must(buffer.WriteZeroN(3)) @@ -153,7 +153,7 @@ func WriteResponse(writer io.Writer, response Response) error { buffer.WriteByte(0), buffer.WriteByte(response.ReplyCode), binary.Write(buffer, binary.BigEndian, response.Destination.Port), - common.Error(buffer.Write(response.Destination.Unwrap().Addr.AsSlice())), + common.Error(buffer.Write(response.Destination.Addr.AsSlice())), ) return rw.WriteBytes(writer, buffer.Bytes()) }