mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-05 12:57:38 +03:00
Improve 4in6 processing (break change)
This commit is contained in:
parent
4ce9815aca
commit
8311d6e970
4 changed files with 20 additions and 36 deletions
|
@ -22,11 +22,21 @@ func (ap Socksaddr) IsIP() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsIPv4() bool {
|
func (ap Socksaddr) IsIPv4() bool {
|
||||||
return ap.Addr.Is4()
|
return ap.Addr.Is4() || ap.Addr.Is4In6()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsIPv6() bool {
|
func (ap Socksaddr) IsIPv6() bool {
|
||||||
return ap.Addr.Is6()
|
return ap.Addr.Is6() && !ap.Addr.Is4In6()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ap Socksaddr) Unwrap() Socksaddr {
|
||||||
|
if ap.Addr.Is4In6() {
|
||||||
|
return Socksaddr{
|
||||||
|
Addr: netip.AddrFrom4(ap.Addr.As4()),
|
||||||
|
Port: ap.Port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ap Socksaddr) IsFqdn() bool {
|
func (ap Socksaddr) IsFqdn() bool {
|
||||||
|
@ -88,25 +98,14 @@ func UDPAddr(ap netip.AddrPort) *net.UDPAddr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddrPortFrom(ip net.IP, port uint16) netip.AddrPort {
|
func AddrPortFrom(ip net.IP, port uint16) netip.AddrPort {
|
||||||
addr, _ := netip.AddrFromSlice(ip)
|
return netip.AddrPortFrom(AddrFromIP(ip), port)
|
||||||
return netip.AddrPortFrom(addr, port)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SocksaddrFrom(ip net.IP, port uint16) Socksaddr {
|
func SocksaddrFrom(addr netip.Addr, port uint16) Socksaddr {
|
||||||
return SocksaddrFromNetIP(AddrPortFrom(ip, port))
|
|
||||||
}
|
|
||||||
|
|
||||||
func SocksaddrFromAddrPort(addr netip.Addr, port uint16) Socksaddr {
|
|
||||||
return SocksaddrFromNetIP(netip.AddrPortFrom(addr, port))
|
return SocksaddrFromNetIP(netip.AddrPortFrom(addr, port))
|
||||||
}
|
}
|
||||||
|
|
||||||
func SocksaddrFromNetIP(ap netip.AddrPort) Socksaddr {
|
func SocksaddrFromNetIP(ap netip.AddrPort) Socksaddr {
|
||||||
if ap.Addr().Is4In6() {
|
|
||||||
return Socksaddr{
|
|
||||||
Addr: netip.AddrFrom4(ap.Addr().As4()),
|
|
||||||
Port: ap.Port(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Socksaddr{
|
return Socksaddr{
|
||||||
Addr: ap.Addr(),
|
Addr: ap.Addr(),
|
||||||
Port: ap.Port(),
|
Port: ap.Port(),
|
||||||
|
@ -163,17 +162,11 @@ func AddrPortFromNet(netAddr net.Addr) netip.AddrPort {
|
||||||
|
|
||||||
func AddrFromIP(ip net.IP) netip.Addr {
|
func AddrFromIP(ip net.IP) netip.Addr {
|
||||||
addr, _ := netip.AddrFromSlice(ip)
|
addr, _ := netip.AddrFromSlice(ip)
|
||||||
if addr.Is4In6() {
|
|
||||||
addr = netip.AddrFrom4(addr.As4())
|
|
||||||
}
|
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseAddr(s string) netip.Addr {
|
func ParseAddr(s string) netip.Addr {
|
||||||
addr, _ := netip.ParseAddr(s)
|
addr, _ := netip.ParseAddr(s)
|
||||||
if addr.Is4In6() {
|
|
||||||
addr = netip.AddrFrom4(addr.As4())
|
|
||||||
}
|
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,9 +180,6 @@ func ParseSocksaddr(address string) Socksaddr {
|
||||||
|
|
||||||
func ParseSocksaddrHostPort(host string, port uint16) Socksaddr {
|
func ParseSocksaddrHostPort(host string, port uint16) Socksaddr {
|
||||||
netAddr, err := netip.ParseAddr(host)
|
netAddr, err := netip.ParseAddr(host)
|
||||||
if netAddr.Is4In6() {
|
|
||||||
netAddr = netip.AddrFrom4(netAddr.As4())
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Socksaddr{
|
return Socksaddr{
|
||||||
Fqdn: host,
|
Fqdn: host,
|
||||||
|
@ -206,9 +196,6 @@ func ParseSocksaddrHostPort(host string, port uint16) Socksaddr {
|
||||||
func ParseSocksaddrHostPortStr(host string, portStr string) Socksaddr {
|
func ParseSocksaddrHostPortStr(host string, portStr string) Socksaddr {
|
||||||
port, _ := strconv.Atoi(portStr)
|
port, _ := strconv.Atoi(portStr)
|
||||||
netAddr, err := netip.ParseAddr(host)
|
netAddr, err := netip.ParseAddr(host)
|
||||||
if netAddr.Is4In6() {
|
|
||||||
netAddr = netip.AddrFrom4(netAddr.As4())
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Socksaddr{
|
return Socksaddr{
|
||||||
Fqdn: host,
|
Fqdn: host,
|
||||||
|
|
|
@ -56,7 +56,7 @@ func (s *Serializer) WriteAddress(writer io.Writer, addr Socksaddr) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if addr.Addr.IsValid() {
|
if addr.Addr.IsValid() {
|
||||||
err = rw.WriteBytes(writer, addr.Addr.AsSlice())
|
err = rw.WriteBytes(writer, addr.Unwrap().Addr.AsSlice())
|
||||||
} else {
|
} else {
|
||||||
err = WriteSocksString(writer, addr.Fqdn)
|
err = WriteSocksString(writer, addr.Fqdn)
|
||||||
}
|
}
|
||||||
|
@ -129,11 +129,8 @@ func (s *Serializer) ReadAddress(reader io.Reader) (Socksaddr, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Socksaddr{}, E.Cause(err, "read ipv6 address")
|
return Socksaddr{}, E.Cause(err, "read ipv6 address")
|
||||||
}
|
}
|
||||||
netAddr := netip.AddrFrom16(addr)
|
|
||||||
if netAddr.Is4In6() {
|
return Socksaddr{Addr: netip.AddrFrom16(addr)}.Unwrap(), nil
|
||||||
netAddr = netip.AddrFrom4(netAddr.As4())
|
|
||||||
}
|
|
||||||
return Socksaddr{Addr: netAddr}, nil
|
|
||||||
default:
|
default:
|
||||||
return Socksaddr{}, E.New("unknown address family: ", af)
|
return Socksaddr{}, E.New("unknown address family: ", af)
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ func DialSerial(ctx context.Context, dialer Dialer, network string, destination
|
||||||
var err error
|
var err error
|
||||||
var connErrors []error
|
var connErrors []error
|
||||||
for _, address := range destinationAddresses {
|
for _, address := range destinationAddresses {
|
||||||
conn, err = dialer.DialContext(ctx, network, M.SocksaddrFromAddrPort(address, destination.Port))
|
conn, err = dialer.DialContext(ctx, network, M.SocksaddrFrom(address, destination.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
connErrors = append(connErrors, err)
|
connErrors = append(connErrors, err)
|
||||||
continue
|
continue
|
||||||
|
@ -33,7 +33,7 @@ func ListenSerial(ctx context.Context, dialer Dialer, destination M.Socksaddr, d
|
||||||
var err error
|
var err error
|
||||||
var connErrors []error
|
var connErrors []error
|
||||||
for _, address := range destinationAddresses {
|
for _, address := range destinationAddresses {
|
||||||
conn, err = dialer.ListenPacket(ctx, M.SocksaddrFromAddrPort(address, destination.Port))
|
conn, err = dialer.ListenPacket(ctx, M.SocksaddrFrom(address, destination.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
connErrors = append(connErrors, err)
|
connErrors = append(connErrors, err)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -112,7 +112,7 @@ func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authent
|
||||||
case socks4.CommandConnect:
|
case socks4.CommandConnect:
|
||||||
responseAddr := request.Destination
|
responseAddr := request.Destination
|
||||||
if !responseAddr.IsIPv4() {
|
if !responseAddr.IsIPv4() {
|
||||||
responseAddr = M.SocksaddrFromAddrPort(netip.IPv4Unspecified(), responseAddr.Port)
|
responseAddr = M.SocksaddrFrom(netip.IPv4Unspecified(), responseAddr.Port)
|
||||||
}
|
}
|
||||||
err = socks4.WriteResponse(conn, socks4.Response{
|
err = socks4.WriteResponse(conn, socks4.Response{
|
||||||
ReplyCode: socks4.ReplyCodeGranted,
|
ReplyCode: socks4.ReplyCodeGranted,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue