mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-06 05:17:38 +03:00
Improve serializer
This commit is contained in:
parent
c6a69b4912
commit
a755de3bbd
2 changed files with 37 additions and 25 deletions
|
@ -6,4 +6,5 @@ const (
|
||||||
AddressFamilyIPv4 Family = 0x01
|
AddressFamilyIPv4 Family = 0x01
|
||||||
AddressFamilyIPv6 Family = 0x04
|
AddressFamilyIPv6 Family = 0x04
|
||||||
AddressFamilyFqdn Family = 0x03
|
AddressFamilyFqdn Family = 0x03
|
||||||
|
AddressFamilyEmpty Family = 0xff
|
||||||
)
|
)
|
||||||
|
|
|
@ -50,14 +50,20 @@ func NewSerializer(options ...SerializerOption) *Serializer {
|
||||||
|
|
||||||
func (s *Serializer) WriteAddress(buffer *buf.Buffer, addr Socksaddr) error {
|
func (s *Serializer) WriteAddress(buffer *buf.Buffer, addr Socksaddr) error {
|
||||||
var af Family
|
var af Family
|
||||||
if addr.IsIPv4() {
|
if !addr.IsValid() {
|
||||||
|
af = AddressFamilyFqdn
|
||||||
|
} else if addr.IsIPv4() {
|
||||||
af = AddressFamilyIPv4
|
af = AddressFamilyIPv4
|
||||||
} else if addr.IsIPv6() {
|
} else if addr.IsIPv6() {
|
||||||
af = AddressFamilyIPv6
|
af = AddressFamilyIPv6
|
||||||
} else {
|
} else {
|
||||||
af = AddressFamilyFqdn
|
af = AddressFamilyFqdn
|
||||||
}
|
}
|
||||||
err := buffer.WriteByte(s.familyByteMap[af])
|
afByte, loaded := s.familyByteMap[af]
|
||||||
|
if !loaded {
|
||||||
|
return E.New("unsupported address")
|
||||||
|
}
|
||||||
|
err := buffer.WriteByte(afByte)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -70,7 +76,14 @@ func (s *Serializer) WriteAddress(buffer *buf.Buffer, addr Socksaddr) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Serializer) AddressLen(addr Socksaddr) int {
|
func (s *Serializer) AddressLen(addr Socksaddr) int {
|
||||||
if addr.IsIPv4() {
|
if !addr.IsValid() {
|
||||||
|
_, supportEmpty := s.familyByteMap[AddressFamilyEmpty]
|
||||||
|
if supportEmpty {
|
||||||
|
return 1
|
||||||
|
} else {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
} else if addr.IsIPv4() {
|
||||||
return 5
|
return 5
|
||||||
} else if addr.IsIPv6() {
|
} else if addr.IsIPv6() {
|
||||||
return 17
|
return 17
|
||||||
|
@ -129,27 +142,25 @@ func (s *Serializer) ReadAddress(reader io.Reader) (Socksaddr, error) {
|
||||||
return Socksaddr{}, E.Cause(err, "read fqdn")
|
return Socksaddr{}, E.Cause(err, "read fqdn")
|
||||||
}
|
}
|
||||||
return ParseSocksaddrHostPort(fqdn, 0), nil
|
return ParseSocksaddrHostPort(fqdn, 0), nil
|
||||||
default:
|
|
||||||
switch family {
|
|
||||||
case AddressFamilyIPv4:
|
case AddressFamilyIPv4:
|
||||||
var addr [4]byte
|
var addr [4]byte
|
||||||
err = common.Error(reader.Read(addr[:]))
|
_, err = io.ReadFull(reader, addr[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Socksaddr{}, E.Cause(err, "read ipv4 address")
|
return Socksaddr{}, E.Cause(err, "read ipv4 address")
|
||||||
}
|
}
|
||||||
return Socksaddr{Addr: netip.AddrFrom4(addr)}, nil
|
return Socksaddr{Addr: netip.AddrFrom4(addr)}, nil
|
||||||
case AddressFamilyIPv6:
|
case AddressFamilyIPv6:
|
||||||
var addr [16]byte
|
var addr [16]byte
|
||||||
err = common.Error(reader.Read(addr[:]))
|
_, err = io.ReadFull(reader, addr[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Socksaddr{}, E.Cause(err, "read ipv6 address")
|
return Socksaddr{}, E.Cause(err, "read ipv6 address")
|
||||||
}
|
}
|
||||||
|
|
||||||
return Socksaddr{Addr: netip.AddrFrom16(addr)}.Unwrap(), nil
|
return Socksaddr{Addr: netip.AddrFrom16(addr)}.Unwrap(), nil
|
||||||
|
case AddressFamilyEmpty:
|
||||||
|
return Socksaddr{}, nil
|
||||||
default:
|
default:
|
||||||
return Socksaddr{}, E.New("unknown address family: ", af)
|
return Socksaddr{}, E.New("unknown address family: ", af)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Serializer) ReadPort(reader io.Reader) (uint16, error) {
|
func (s *Serializer) ReadPort(reader io.Reader) (uint16, error) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue