mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-05 04:47:40 +03:00
Remove 4in6 cast
This commit is contained in:
parent
86d7d51023
commit
b12b8b7fd2
7 changed files with 36 additions and 24 deletions
|
@ -30,7 +30,7 @@ func (w *ExtendedUDPConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
|
||||||
return M.Socksaddr{}, err
|
return M.Socksaddr{}, err
|
||||||
}
|
}
|
||||||
buffer.Truncate(n)
|
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 {
|
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 {
|
if err != nil {
|
||||||
return M.Socksaddr{}, err
|
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 {
|
func (w *ExtendedPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common/debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Socksaddr struct {
|
type Socksaddr struct {
|
||||||
|
@ -19,15 +21,15 @@ func (ap Socksaddr) Network() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsIP() bool {
|
func (ap Socksaddr) IsIP() bool {
|
||||||
return ap.Addr.IsValid() && ap.Fqdn == ""
|
return ap.Addr.IsValid()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsIPv4() bool {
|
func (ap Socksaddr) IsIPv4() bool {
|
||||||
return ap.Addr.Is4() || ap.Addr.Is4In6()
|
return ap.Addr.Is4()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsIPv6() bool {
|
func (ap Socksaddr) IsIPv6() bool {
|
||||||
return ap.Addr.Is6() && !ap.Addr.Is4In6()
|
return ap.Addr.Is6()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) Unwrap() Socksaddr {
|
func (ap Socksaddr) Unwrap() Socksaddr {
|
||||||
|
@ -41,7 +43,7 @@ func (ap Socksaddr) Unwrap() Socksaddr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsFqdn() bool {
|
func (ap Socksaddr) IsFqdn() bool {
|
||||||
return !ap.Addr.IsValid() && IsDomainName(ap.Fqdn)
|
return IsDomainName(ap.Fqdn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsValid() bool {
|
func (ap Socksaddr) IsValid() bool {
|
||||||
|
@ -49,6 +51,9 @@ func (ap Socksaddr) IsValid() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) AddrString() string {
|
func (ap Socksaddr) AddrString() string {
|
||||||
|
if debug.Enabled {
|
||||||
|
ap.CheckBadAddr()
|
||||||
|
}
|
||||||
if ap.Addr.IsValid() {
|
if ap.Addr.IsValid() {
|
||||||
return ap.Addr.String()
|
return ap.Addr.String()
|
||||||
} else {
|
} else {
|
||||||
|
@ -57,12 +62,18 @@ func (ap Socksaddr) AddrString() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IPAddr() *net.IPAddr {
|
func (ap Socksaddr) IPAddr() *net.IPAddr {
|
||||||
|
if debug.Enabled {
|
||||||
|
ap.CheckBadAddr()
|
||||||
|
}
|
||||||
return &net.IPAddr{
|
return &net.IPAddr{
|
||||||
IP: ap.Addr.AsSlice(),
|
IP: ap.Addr.AsSlice(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) TCPAddr() *net.TCPAddr {
|
func (ap Socksaddr) TCPAddr() *net.TCPAddr {
|
||||||
|
if debug.Enabled {
|
||||||
|
ap.CheckBadAddr()
|
||||||
|
}
|
||||||
return &net.TCPAddr{
|
return &net.TCPAddr{
|
||||||
IP: ap.Addr.AsSlice(),
|
IP: ap.Addr.AsSlice(),
|
||||||
Port: int(ap.Port),
|
Port: int(ap.Port),
|
||||||
|
@ -70,6 +81,9 @@ func (ap Socksaddr) TCPAddr() *net.TCPAddr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) UDPAddr() *net.UDPAddr {
|
func (ap Socksaddr) UDPAddr() *net.UDPAddr {
|
||||||
|
if debug.Enabled {
|
||||||
|
ap.CheckBadAddr()
|
||||||
|
}
|
||||||
return &net.UDPAddr{
|
return &net.UDPAddr{
|
||||||
IP: ap.Addr.AsSlice(),
|
IP: ap.Addr.AsSlice(),
|
||||||
Port: int(ap.Port),
|
Port: int(ap.Port),
|
||||||
|
@ -77,24 +91,22 @@ func (ap Socksaddr) UDPAddr() *net.UDPAddr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) AddrPort() netip.AddrPort {
|
func (ap Socksaddr) AddrPort() netip.AddrPort {
|
||||||
|
if debug.Enabled {
|
||||||
|
ap.CheckBadAddr()
|
||||||
|
}
|
||||||
return *(*netip.AddrPort)(unsafe.Pointer(&ap))
|
return *(*netip.AddrPort)(unsafe.Pointer(&ap))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) String() string {
|
func (ap Socksaddr) String() string {
|
||||||
|
if debug.Enabled {
|
||||||
|
ap.CheckBadAddr()
|
||||||
|
}
|
||||||
return net.JoinHostPort(ap.AddrString(), strconv.Itoa(int(ap.Port)))
|
return net.JoinHostPort(ap.AddrString(), strconv.Itoa(int(ap.Port)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TCPAddr(ap netip.AddrPort) *net.TCPAddr {
|
func (ap Socksaddr) CheckBadAddr() {
|
||||||
return &net.TCPAddr{
|
if ap.Addr.Is4In6() || ap.Addr.IsValid() && ap.Fqdn != "" {
|
||||||
IP: ap.Addr().AsSlice(),
|
panic("bad socksaddr")
|
||||||
Port: int(ap.Port()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func UDPAddr(ap netip.AddrPort) *net.UDPAddr {
|
|
||||||
return &net.UDPAddr{
|
|
||||||
IP: ap.Addr().AsSlice(),
|
|
||||||
Port: int(ap.Port()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ func (s *Serializer) WriteAddress(buffer *buf.Buffer, addr Socksaddr) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if addr.Addr.IsValid() {
|
if addr.Addr.IsValid() {
|
||||||
_, err = buffer.Write(addr.Unwrap().Addr.AsSlice())
|
_, err = buffer.Write(addr.Addr.AsSlice())
|
||||||
} else {
|
} else {
|
||||||
err = WriteSocksString(buffer, addr.Fqdn)
|
err = WriteSocksString(buffer, addr.Fqdn)
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -2,4 +2,4 @@ module github.com/sagernet/sing
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require golang.org/x/sys v0.0.0-20220913120320-3275c407cedc
|
require golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,2 +1,2 @@
|
||||||
golang.org/x/sys v0.0.0-20220913120320-3275c407cedc h1:dpclq5m2YrqPGStKmtw7IcNbKLfbIqKXvNxDJKdIKYc=
|
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
|
||||||
golang.org/x/sys v0.0.0-20220913120320-3275c407cedc/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|
|
@ -18,5 +18,5 @@ func SourceAddress(request *http.Request) M.Socksaddr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return address
|
return address.Unwrap()
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,7 @@ func WriteRequest(writer io.Writer, request Request) error {
|
||||||
binary.Write(buffer, binary.BigEndian, request.Destination.Port),
|
binary.Write(buffer, binary.BigEndian, request.Destination.Port),
|
||||||
)
|
)
|
||||||
if request.Destination.IsIPv4() {
|
if request.Destination.IsIPv4() {
|
||||||
common.Must1(buffer.Write(request.Destination.Unwrap().Addr.AsSlice()))
|
common.Must1(buffer.Write(request.Destination.Addr.AsSlice()))
|
||||||
} else {
|
} else {
|
||||||
// 0.0.0.X
|
// 0.0.0.X
|
||||||
common.Must(buffer.WriteZeroN(3))
|
common.Must(buffer.WriteZeroN(3))
|
||||||
|
@ -153,7 +153,7 @@ func WriteResponse(writer io.Writer, response Response) error {
|
||||||
buffer.WriteByte(0),
|
buffer.WriteByte(0),
|
||||||
buffer.WriteByte(response.ReplyCode),
|
buffer.WriteByte(response.ReplyCode),
|
||||||
binary.Write(buffer, binary.BigEndian, response.Destination.Port),
|
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())
|
return rw.WriteBytes(writer, buffer.Bytes())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue