mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 11:57:39 +03:00
Improve serializer
This commit is contained in:
parent
c6a69b4912
commit
a755de3bbd
2 changed files with 37 additions and 25 deletions
|
@ -3,7 +3,8 @@ package metadata
|
|||
type Family = byte
|
||||
|
||||
const (
|
||||
AddressFamilyIPv4 Family = 0x01
|
||||
AddressFamilyIPv6 Family = 0x04
|
||||
AddressFamilyFqdn Family = 0x03
|
||||
AddressFamilyIPv4 Family = 0x01
|
||||
AddressFamilyIPv6 Family = 0x04
|
||||
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 {
|
||||
var af Family
|
||||
if addr.IsIPv4() {
|
||||
if !addr.IsValid() {
|
||||
af = AddressFamilyFqdn
|
||||
} else if addr.IsIPv4() {
|
||||
af = AddressFamilyIPv4
|
||||
} else if addr.IsIPv6() {
|
||||
af = AddressFamilyIPv6
|
||||
} else {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -70,7 +76,14 @@ func (s *Serializer) WriteAddress(buffer *buf.Buffer, addr Socksaddr) error {
|
|||
}
|
||||
|
||||
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
|
||||
} else if addr.IsIPv6() {
|
||||
return 17
|
||||
|
@ -129,26 +142,24 @@ func (s *Serializer) ReadAddress(reader io.Reader) (Socksaddr, error) {
|
|||
return Socksaddr{}, E.Cause(err, "read fqdn")
|
||||
}
|
||||
return ParseSocksaddrHostPort(fqdn, 0), nil
|
||||
default:
|
||||
switch family {
|
||||
case AddressFamilyIPv4:
|
||||
var addr [4]byte
|
||||
err = common.Error(reader.Read(addr[:]))
|
||||
if err != nil {
|
||||
return Socksaddr{}, E.Cause(err, "read ipv4 address")
|
||||
}
|
||||
return Socksaddr{Addr: netip.AddrFrom4(addr)}, nil
|
||||
case AddressFamilyIPv6:
|
||||
var addr [16]byte
|
||||
err = common.Error(reader.Read(addr[:]))
|
||||
if err != nil {
|
||||
return Socksaddr{}, E.Cause(err, "read ipv6 address")
|
||||
}
|
||||
|
||||
return Socksaddr{Addr: netip.AddrFrom16(addr)}.Unwrap(), nil
|
||||
default:
|
||||
return Socksaddr{}, E.New("unknown address family: ", af)
|
||||
case AddressFamilyIPv4:
|
||||
var addr [4]byte
|
||||
_, err = io.ReadFull(reader, addr[:])
|
||||
if err != nil {
|
||||
return Socksaddr{}, E.Cause(err, "read ipv4 address")
|
||||
}
|
||||
return Socksaddr{Addr: netip.AddrFrom4(addr)}, nil
|
||||
case AddressFamilyIPv6:
|
||||
var addr [16]byte
|
||||
_, err = io.ReadFull(reader, addr[:])
|
||||
if err != nil {
|
||||
return Socksaddr{}, E.Cause(err, "read ipv6 address")
|
||||
}
|
||||
return Socksaddr{Addr: netip.AddrFrom16(addr)}.Unwrap(), nil
|
||||
case AddressFamilyEmpty:
|
||||
return Socksaddr{}, nil
|
||||
default:
|
||||
return Socksaddr{}, E.New("unknown address family: ", af)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue