github.com/imannamdari/v2ray-core/v5@v5.0.5/common/protocol/address.go (about)

     1  package protocol
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/imannamdari/v2ray-core/v5/common"
     7  	"github.com/imannamdari/v2ray-core/v5/common/buf"
     8  	"github.com/imannamdari/v2ray-core/v5/common/net"
     9  	"github.com/imannamdari/v2ray-core/v5/common/serial"
    10  )
    11  
    12  type AddressOption func(*option)
    13  
    14  func PortThenAddress() AddressOption {
    15  	return func(p *option) {
    16  		p.portFirst = true
    17  	}
    18  }
    19  
    20  func AddressFamilyByte(b byte, f net.AddressFamily) AddressOption {
    21  	if b >= 16 {
    22  		panic("address family byte too big")
    23  	}
    24  	return func(p *option) {
    25  		p.addrTypeMap[b] = f
    26  		p.addrByteMap[f] = b
    27  	}
    28  }
    29  
    30  type AddressTypeParser func(byte) byte
    31  
    32  func WithAddressTypeParser(atp AddressTypeParser) AddressOption {
    33  	return func(p *option) {
    34  		p.typeParser = atp
    35  	}
    36  }
    37  
    38  type AddressSerializer interface {
    39  	ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error)
    40  	WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error
    41  }
    42  
    43  const afInvalid = 255
    44  
    45  type option struct {
    46  	addrTypeMap [16]net.AddressFamily
    47  	addrByteMap [16]byte
    48  	portFirst   bool
    49  	typeParser  AddressTypeParser
    50  }
    51  
    52  // NewAddressParser creates a new AddressParser
    53  func NewAddressParser(options ...AddressOption) AddressSerializer {
    54  	var o option
    55  	for i := range o.addrByteMap {
    56  		o.addrByteMap[i] = afInvalid
    57  	}
    58  	for i := range o.addrTypeMap {
    59  		o.addrTypeMap[i] = net.AddressFamily(afInvalid)
    60  	}
    61  	for _, opt := range options {
    62  		opt(&o)
    63  	}
    64  
    65  	ap := &addressParser{
    66  		addrByteMap: o.addrByteMap,
    67  		addrTypeMap: o.addrTypeMap,
    68  	}
    69  
    70  	if o.typeParser != nil {
    71  		ap.typeParser = o.typeParser
    72  	}
    73  
    74  	if o.portFirst {
    75  		return portFirstAddressParser{ap: ap}
    76  	}
    77  
    78  	return portLastAddressParser{ap: ap}
    79  }
    80  
    81  type portFirstAddressParser struct {
    82  	ap *addressParser
    83  }
    84  
    85  func (p portFirstAddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error) {
    86  	if buffer == nil {
    87  		buffer = buf.New()
    88  		defer buffer.Release()
    89  	}
    90  
    91  	port, err := readPort(buffer, input)
    92  	if err != nil {
    93  		return nil, 0, err
    94  	}
    95  
    96  	addr, err := p.ap.readAddress(buffer, input)
    97  	if err != nil {
    98  		return nil, 0, err
    99  	}
   100  	return addr, port, nil
   101  }
   102  
   103  func (p portFirstAddressParser) WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error {
   104  	if err := writePort(writer, port); err != nil {
   105  		return err
   106  	}
   107  
   108  	return p.ap.writeAddress(writer, addr)
   109  }
   110  
   111  type portLastAddressParser struct {
   112  	ap *addressParser
   113  }
   114  
   115  func (p portLastAddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error) {
   116  	if buffer == nil {
   117  		buffer = buf.New()
   118  		defer buffer.Release()
   119  	}
   120  
   121  	addr, err := p.ap.readAddress(buffer, input)
   122  	if err != nil {
   123  		return nil, 0, err
   124  	}
   125  
   126  	port, err := readPort(buffer, input)
   127  	if err != nil {
   128  		return nil, 0, err
   129  	}
   130  
   131  	return addr, port, nil
   132  }
   133  
   134  func (p portLastAddressParser) WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error {
   135  	if err := p.ap.writeAddress(writer, addr); err != nil {
   136  		return err
   137  	}
   138  
   139  	return writePort(writer, port)
   140  }
   141  
   142  func readPort(b *buf.Buffer, reader io.Reader) (net.Port, error) {
   143  	if _, err := b.ReadFullFrom(reader, 2); err != nil {
   144  		return 0, err
   145  	}
   146  	return net.PortFromBytes(b.BytesFrom(-2)), nil
   147  }
   148  
   149  func writePort(writer io.Writer, port net.Port) error {
   150  	return common.Error2(serial.WriteUint16(writer, port.Value()))
   151  }
   152  
   153  func maybeIPPrefix(b byte) bool {
   154  	return b == '[' || (b >= '0' && b <= '9')
   155  }
   156  
   157  func isValidDomain(d string) bool {
   158  	for _, c := range d {
   159  		if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' || c == '.' || c == '_') {
   160  			return false
   161  		}
   162  	}
   163  	return true
   164  }
   165  
   166  type addressParser struct {
   167  	addrTypeMap [16]net.AddressFamily
   168  	addrByteMap [16]byte
   169  	typeParser  AddressTypeParser
   170  }
   171  
   172  func (p *addressParser) readAddress(b *buf.Buffer, reader io.Reader) (net.Address, error) {
   173  	if _, err := b.ReadFullFrom(reader, 1); err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	addrType := b.Byte(b.Len() - 1)
   178  	if p.typeParser != nil {
   179  		addrType = p.typeParser(addrType)
   180  	}
   181  
   182  	if addrType >= 16 {
   183  		return nil, newError("unknown address type: ", addrType)
   184  	}
   185  
   186  	addrFamily := p.addrTypeMap[addrType]
   187  	if addrFamily == net.AddressFamily(afInvalid) {
   188  		return nil, newError("unknown address type: ", addrType)
   189  	}
   190  
   191  	switch addrFamily {
   192  	case net.AddressFamilyIPv4:
   193  		if _, err := b.ReadFullFrom(reader, 4); err != nil {
   194  			return nil, err
   195  		}
   196  		return net.IPAddress(b.BytesFrom(-4)), nil
   197  	case net.AddressFamilyIPv6:
   198  		if _, err := b.ReadFullFrom(reader, 16); err != nil {
   199  			return nil, err
   200  		}
   201  		return net.IPAddress(b.BytesFrom(-16)), nil
   202  	case net.AddressFamilyDomain:
   203  		if _, err := b.ReadFullFrom(reader, 1); err != nil {
   204  			return nil, err
   205  		}
   206  		domainLength := int32(b.Byte(b.Len() - 1))
   207  		if _, err := b.ReadFullFrom(reader, domainLength); err != nil {
   208  			return nil, err
   209  		}
   210  		domain := string(b.BytesFrom(-domainLength))
   211  		if maybeIPPrefix(domain[0]) {
   212  			addr := net.ParseAddress(domain)
   213  			if addr.Family().IsIP() {
   214  				return addr, nil
   215  			}
   216  		}
   217  		if !isValidDomain(domain) {
   218  			return nil, newError("invalid domain name: ", domain)
   219  		}
   220  		return net.DomainAddress(domain), nil
   221  	default:
   222  		panic("impossible case")
   223  	}
   224  }
   225  
   226  func (p *addressParser) writeAddress(writer io.Writer, address net.Address) error {
   227  	tb := p.addrByteMap[address.Family()]
   228  	if tb == afInvalid {
   229  		return newError("unknown address family", address.Family())
   230  	}
   231  
   232  	switch address.Family() {
   233  	case net.AddressFamilyIPv4, net.AddressFamilyIPv6:
   234  		if _, err := writer.Write([]byte{tb}); err != nil {
   235  			return err
   236  		}
   237  		if _, err := writer.Write(address.IP()); err != nil {
   238  			return err
   239  		}
   240  	case net.AddressFamilyDomain:
   241  		domain := address.Domain()
   242  		if isDomainTooLong(domain) {
   243  			return newError("Super long domain is not supported: ", domain)
   244  		}
   245  
   246  		if _, err := writer.Write([]byte{tb, byte(len(domain))}); err != nil {
   247  			return err
   248  		}
   249  		if _, err := writer.Write([]byte(domain)); err != nil {
   250  			return err
   251  		}
   252  	default:
   253  		panic("Unknown family type.")
   254  	}
   255  
   256  	return nil
   257  }