mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 12:27:37 +03:00
Refactor socksaddr
This commit is contained in:
parent
9378ae739c
commit
b35c53ca8f
54 changed files with 1191 additions and 666 deletions
|
@ -3,6 +3,7 @@ package metadata
|
|||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net/netip"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
|
@ -41,28 +42,27 @@ func NewSerializer(options ...SerializerOption) *Serializer {
|
|||
return s
|
||||
}
|
||||
|
||||
func (s *Serializer) WriteAddress(writer io.Writer, addr Addr) error {
|
||||
func (s *Serializer) WriteAddress(writer io.Writer, addr Socksaddr) error {
|
||||
err := rw.WriteByte(writer, s.familyByteMap[addr.Family()])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if addr.Family().IsIP() {
|
||||
err = rw.WriteBytes(writer, addr.Addr().AsSlice())
|
||||
if addr.Addr.IsValid() {
|
||||
err = rw.WriteBytes(writer, addr.Addr.AsSlice())
|
||||
} else {
|
||||
domain := addr.Fqdn()
|
||||
err = WriteString(writer, "fqdn", domain)
|
||||
err = WriteString(writer, "fqdn", addr.Fqdn)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Serializer) AddressLen(addr Addr) int {
|
||||
func (s *Serializer) AddressLen(addr Socksaddr) int {
|
||||
switch addr.Family() {
|
||||
case AddressFamilyIPv4:
|
||||
return 5
|
||||
case AddressFamilyIPv6:
|
||||
return 17
|
||||
default:
|
||||
return 2 + len(addr.Fqdn())
|
||||
return 2 + len(addr.Fqdn)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,10 +70,10 @@ func (s *Serializer) WritePort(writer io.Writer, port uint16) error {
|
|||
return binary.Write(writer, binary.BigEndian, port)
|
||||
}
|
||||
|
||||
func (s *Serializer) WriteAddrPort(writer io.Writer, destination *AddrPort) error {
|
||||
func (s *Serializer) WriteAddrPort(writer io.Writer, destination Socksaddr) error {
|
||||
var err error
|
||||
if !s.portFirst {
|
||||
err = s.WriteAddress(writer, destination.Addr)
|
||||
err = s.WriteAddress(writer, destination)
|
||||
} else {
|
||||
err = s.WritePort(writer, destination.Port)
|
||||
}
|
||||
|
@ -81,48 +81,50 @@ func (s *Serializer) WriteAddrPort(writer io.Writer, destination *AddrPort) erro
|
|||
return err
|
||||
}
|
||||
if s.portFirst {
|
||||
err = s.WriteAddress(writer, destination.Addr)
|
||||
err = s.WriteAddress(writer, destination)
|
||||
} else {
|
||||
err = s.WritePort(writer, destination.Port)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Serializer) AddrPortLen(destination *AddrPort) int {
|
||||
return s.AddressLen(destination.Addr) + 2
|
||||
func (s *Serializer) AddrPortLen(destination Socksaddr) int {
|
||||
return s.AddressLen(destination) + 2
|
||||
}
|
||||
|
||||
func (s *Serializer) ReadAddress(reader io.Reader) (Addr, error) {
|
||||
func (s *Serializer) ReadAddress(reader io.Reader) (Socksaddr, error) {
|
||||
af, err := rw.ReadByte(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return Socksaddr{}, err
|
||||
}
|
||||
family := s.familyMap[af]
|
||||
switch family {
|
||||
case AddressFamilyFqdn:
|
||||
fqdn, err := ReadString(reader)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "read fqdn")
|
||||
return Socksaddr{}, E.Cause(err, "read fqdn")
|
||||
}
|
||||
return AddrFqdn(fqdn), nil
|
||||
return Socksaddr{
|
||||
Fqdn: fqdn,
|
||||
}, nil
|
||||
default:
|
||||
switch family {
|
||||
case AddressFamilyIPv4:
|
||||
var addr [4]byte
|
||||
err = common.Error(reader.Read(addr[:]))
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "read ipv4 address")
|
||||
return Socksaddr{}, E.Cause(err, "read ipv4 address")
|
||||
}
|
||||
return Addr4(addr), nil
|
||||
return Socksaddr{Addr: netip.AddrFrom4(addr)}, nil
|
||||
case AddressFamilyIPv6:
|
||||
var addr [16]byte
|
||||
err = common.Error(reader.Read(addr[:]))
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "read ipv6 address")
|
||||
return Socksaddr{}, E.Cause(err, "read ipv6 address")
|
||||
}
|
||||
return Addr16(addr), nil
|
||||
return Socksaddr{Addr: netip.AddrFrom16(addr)}, nil
|
||||
default:
|
||||
return nil, E.New("unknown address family: ", af)
|
||||
return Socksaddr{}, E.New("unknown address family: ", af)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -135,8 +137,8 @@ func (s *Serializer) ReadPort(reader io.Reader) (uint16, error) {
|
|||
return binary.BigEndian.Uint16(port), nil
|
||||
}
|
||||
|
||||
func (s *Serializer) ReadAddrPort(reader io.Reader) (destination *AddrPort, err error) {
|
||||
var addr Addr
|
||||
func (s *Serializer) ReadAddrPort(reader io.Reader) (destination Socksaddr, err error) {
|
||||
var addr Socksaddr
|
||||
var port uint16
|
||||
if !s.portFirst {
|
||||
addr, err = s.ReadAddress(reader)
|
||||
|
@ -154,7 +156,8 @@ func (s *Serializer) ReadAddrPort(reader io.Reader) (destination *AddrPort, err
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
return AddrPortFrom(addr, port), nil
|
||||
addr.Port = port
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
func ReadString(reader io.Reader) (string, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue