2018-02-23 17:42:01 -05:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
2018-02-23 17:42:01 -05:00
|
|
|
)
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
type AddressOption func(*option)
|
2018-02-23 17:42:01 -05:00
|
|
|
|
|
|
|
func PortThenAddress() AddressOption {
|
2018-11-14 13:16:46 -05:00
|
|
|
return func(p *option) {
|
2018-02-23 17:42:01 -05:00
|
|
|
p.portFirst = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddressFamilyByte(b byte, f net.AddressFamily) AddressOption {
|
2018-11-14 13:16:46 -05:00
|
|
|
if b >= 16 {
|
|
|
|
panic("address family byte too big")
|
|
|
|
}
|
|
|
|
return func(p *option) {
|
2018-02-23 17:42:01 -05:00
|
|
|
p.addrTypeMap[b] = f
|
|
|
|
p.addrByteMap[f] = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddressTypeParser func(byte) byte
|
|
|
|
|
|
|
|
func WithAddressTypeParser(atp AddressTypeParser) AddressOption {
|
2018-11-14 13:16:46 -05:00
|
|
|
return func(p *option) {
|
2018-02-23 17:42:01 -05:00
|
|
|
p.typeParser = atp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
type AddressSerializer interface {
|
|
|
|
ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error)
|
|
|
|
WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error
|
|
|
|
}
|
|
|
|
|
|
|
|
const afInvalid = 255
|
|
|
|
|
|
|
|
type option struct {
|
|
|
|
addrTypeMap [16]net.AddressFamily
|
|
|
|
addrByteMap [16]byte
|
2018-02-23 17:42:01 -05:00
|
|
|
portFirst bool
|
|
|
|
typeParser AddressTypeParser
|
|
|
|
}
|
|
|
|
|
2018-04-11 10:15:29 -04:00
|
|
|
// NewAddressParser creates a new AddressParser
|
2018-11-14 13:16:46 -05:00
|
|
|
func NewAddressParser(options ...AddressOption) AddressSerializer {
|
|
|
|
var o option
|
|
|
|
for i := range o.addrByteMap {
|
|
|
|
o.addrByteMap[i] = afInvalid
|
|
|
|
}
|
|
|
|
for i := range o.addrTypeMap {
|
|
|
|
o.addrTypeMap[i] = net.AddressFamily(afInvalid)
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|
|
|
|
for _, opt := range options {
|
2018-11-14 13:16:46 -05:00
|
|
|
opt(&o)
|
|
|
|
}
|
|
|
|
|
|
|
|
ap := &addressParser{
|
|
|
|
addrByteMap: o.addrByteMap,
|
|
|
|
addrTypeMap: o.addrTypeMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.typeParser != nil {
|
|
|
|
ap.typeParser = o.typeParser
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.portFirst {
|
|
|
|
return portFirstAddressParser{ap: ap}
|
|
|
|
}
|
|
|
|
|
|
|
|
return portLastAddressParser{ap: ap}
|
|
|
|
}
|
|
|
|
|
|
|
|
type portFirstAddressParser struct {
|
|
|
|
ap *addressParser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p portFirstAddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error) {
|
|
|
|
if buffer == nil {
|
|
|
|
buffer = buf.New()
|
|
|
|
defer buffer.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
port, err := readPort(buffer, input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := p.ap.readAddress(buffer, input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return addr, port, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p portFirstAddressParser) WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error {
|
|
|
|
if err := writePort(writer, port); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.ap.writeAddress(writer, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type portLastAddressParser struct {
|
|
|
|
ap *addressParser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p portLastAddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error) {
|
|
|
|
if buffer == nil {
|
|
|
|
buffer = buf.New()
|
|
|
|
defer buffer.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := p.ap.readAddress(buffer, input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
port, err := readPort(buffer, input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, port, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p portLastAddressParser) WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error {
|
|
|
|
if err := p.ap.writeAddress(writer, addr); err != nil {
|
|
|
|
return err
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|
2018-11-14 13:16:46 -05:00
|
|
|
|
|
|
|
return writePort(writer, port)
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
func readPort(b *buf.Buffer, reader io.Reader) (net.Port, error) {
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFullFrom(reader, 2); err != nil {
|
2018-02-23 17:42:01 -05:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return net.PortFromBytes(b.BytesFrom(-2)), nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
func writePort(writer io.Writer, port net.Port) error {
|
|
|
|
return common.Error2(serial.WriteUint16(writer, port.Value()))
|
|
|
|
}
|
|
|
|
|
2018-03-01 05:32:55 -05:00
|
|
|
func maybeIPPrefix(b byte) bool {
|
|
|
|
return b == '[' || (b >= '0' && b <= '9')
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidDomain(d string) bool {
|
|
|
|
for _, c := range d {
|
2018-03-04 11:08:58 -05:00
|
|
|
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' || c == '.' || c == '_') {
|
2018-03-01 05:32:55 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
type addressParser struct {
|
|
|
|
addrTypeMap [16]net.AddressFamily
|
|
|
|
addrByteMap [16]byte
|
|
|
|
typeParser AddressTypeParser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *addressParser) readAddress(b *buf.Buffer, reader io.Reader) (net.Address, error) {
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFullFrom(reader, 1); err != nil {
|
2018-02-23 17:42:01 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addrType := b.Byte(b.Len() - 1)
|
|
|
|
if p.typeParser != nil {
|
|
|
|
addrType = p.typeParser(addrType)
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
if addrType >= 16 {
|
|
|
|
return nil, newError("unknown address type: ", addrType)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrFamily := p.addrTypeMap[addrType]
|
|
|
|
if addrFamily == net.AddressFamily(afInvalid) {
|
2018-02-23 17:42:01 -05:00
|
|
|
return nil, newError("unknown address type: ", addrType)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch addrFamily {
|
|
|
|
case net.AddressFamilyIPv4:
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFullFrom(reader, 4); err != nil {
|
2018-02-23 17:42:01 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return net.IPAddress(b.BytesFrom(-4)), nil
|
|
|
|
case net.AddressFamilyIPv6:
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFullFrom(reader, 16); err != nil {
|
2018-02-23 17:42:01 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return net.IPAddress(b.BytesFrom(-16)), nil
|
|
|
|
case net.AddressFamilyDomain:
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFullFrom(reader, 1); err != nil {
|
2018-02-23 17:42:01 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-02 14:00:50 -04:00
|
|
|
domainLength := int32(b.Byte(b.Len() - 1))
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFullFrom(reader, domainLength); err != nil {
|
2018-02-23 17:42:01 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-01 05:32:55 -05:00
|
|
|
domain := string(b.BytesFrom(-domainLength))
|
|
|
|
if maybeIPPrefix(domain[0]) {
|
|
|
|
addr := net.ParseAddress(domain)
|
2018-11-16 05:13:36 -05:00
|
|
|
if addr.Family().IsIP() {
|
2018-03-01 05:32:55 -05:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isValidDomain(domain) {
|
|
|
|
return nil, newError("invalid domain name: ", domain)
|
|
|
|
}
|
|
|
|
return net.DomainAddress(domain), nil
|
2018-02-23 17:42:01 -05:00
|
|
|
default:
|
|
|
|
panic("impossible case")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
func (p *addressParser) writeAddress(writer io.Writer, address net.Address) error {
|
|
|
|
tb := p.addrByteMap[address.Family()]
|
|
|
|
if tb == afInvalid {
|
|
|
|
return newError("unknown address family", address.Family())
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
switch address.Family() {
|
|
|
|
case net.AddressFamilyIPv4, net.AddressFamilyIPv6:
|
2019-02-22 06:54:26 -05:00
|
|
|
if _, err := writer.Write([]byte{tb}); err != nil {
|
2018-04-11 10:15:29 -04:00
|
|
|
return err
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|
2018-11-14 13:16:46 -05:00
|
|
|
if _, err := writer.Write(address.IP()); err != nil {
|
2018-04-11 10:15:29 -04:00
|
|
|
return err
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|
|
|
|
case net.AddressFamilyDomain:
|
|
|
|
domain := address.Domain()
|
2023-04-07 08:03:46 -04:00
|
|
|
if IsDomainTooLong(domain) {
|
2018-02-23 17:42:01 -05:00
|
|
|
return newError("Super long domain is not supported: ", domain)
|
|
|
|
}
|
2018-11-15 10:04:13 -05:00
|
|
|
|
2019-02-22 06:54:26 -05:00
|
|
|
if _, err := writer.Write([]byte{tb, byte(len(domain))}); err != nil {
|
2018-11-14 13:16:46 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-02-22 06:54:26 -05:00
|
|
|
if _, err := writer.Write([]byte(domain)); err != nil {
|
2018-11-14 13:16:46 -05:00
|
|
|
return err
|
|
|
|
}
|
2018-04-11 10:15:29 -04:00
|
|
|
default:
|
|
|
|
panic("Unknown family type.")
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|
2018-04-11 10:15:29 -04:00
|
|
|
|
2018-11-14 13:16:46 -05:00
|
|
|
return nil
|
2018-02-23 17:42:01 -05:00
|
|
|
}
|