1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/protocol/address.go

215 lines
5.1 KiB
Go
Raw Normal View History

2018-02-23 17:42:01 -05:00
package protocol
import (
"io"
2018-04-11 10:15:29 -04:00
"v2ray.com/core/common"
2018-02-23 17:42:01 -05:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2018-04-11 10:15:29 -04:00
"v2ray.com/core/common/signal"
2018-02-23 17:42:01 -05:00
)
type AddressOption func(*AddressParser)
func PortThenAddress() AddressOption {
return func(p *AddressParser) {
p.portFirst = true
}
}
func AddressFamilyByte(b byte, f net.AddressFamily) AddressOption {
return func(p *AddressParser) {
p.addrTypeMap[b] = f
p.addrByteMap[f] = b
}
}
type AddressTypeParser func(byte) byte
func WithAddressTypeParser(atp AddressTypeParser) AddressOption {
return func(p *AddressParser) {
p.typeParser = atp
}
}
2018-04-11 10:15:29 -04:00
// AddressParser is a utility for reading and writer addresses.
2018-02-23 17:42:01 -05:00
type AddressParser struct {
addrTypeMap map[byte]net.AddressFamily
addrByteMap map[net.AddressFamily]byte
portFirst bool
typeParser AddressTypeParser
}
2018-04-11 10:15:29 -04:00
// NewAddressParser creates a new AddressParser
2018-02-23 17:42:01 -05:00
func NewAddressParser(options ...AddressOption) *AddressParser {
p := &AddressParser{
addrTypeMap: make(map[byte]net.AddressFamily, 8),
addrByteMap: make(map[net.AddressFamily]byte, 8),
}
for _, opt := range options {
opt(p)
}
return p
}
func (p *AddressParser) readPort(b *buf.Buffer, reader io.Reader) (net.Port, error) {
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 2)); err != nil {
return 0, err
}
return net.PortFromBytes(b.BytesFrom(-2)), nil
}
func maybeIPPrefix(b byte) bool {
return b == '[' || (b >= '0' && b <= '9')
}
func isValidDomain(d string) bool {
for _, c := range d {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' || c == '.' || c == '_') {
return false
}
}
return true
}
2018-02-23 17:42:01 -05:00
func (p *AddressParser) readAddress(b *buf.Buffer, reader io.Reader) (net.Address, error) {
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 1)); err != nil {
return nil, err
}
addrType := b.Byte(b.Len() - 1)
if p.typeParser != nil {
addrType = p.typeParser(addrType)
}
addrFamily, valid := p.addrTypeMap[addrType]
if !valid {
return nil, newError("unknown address type: ", addrType)
}
switch addrFamily {
case net.AddressFamilyIPv4:
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 4)); err != nil {
return nil, err
}
return net.IPAddress(b.BytesFrom(-4)), nil
case net.AddressFamilyIPv6:
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 16)); err != nil {
return nil, err
}
return net.IPAddress(b.BytesFrom(-16)), nil
case net.AddressFamilyDomain:
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 1)); err != nil {
return nil, err
}
2018-04-02 14:00:50 -04:00
domainLength := int32(b.Byte(b.Len() - 1))
2018-04-03 18:29:30 -04:00
if err := b.AppendSupplier(buf.ReadFullFrom(reader, domainLength)); err != nil {
2018-02-23 17:42:01 -05:00
return nil, err
}
domain := string(b.BytesFrom(-domainLength))
if maybeIPPrefix(domain[0]) {
addr := net.ParseAddress(domain)
if addr.Family().IsIPv4() || addrFamily.IsIPv6() {
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-04-11 10:15:29 -04:00
// ReadAddressPort reads address and port from the given input.
2018-02-23 17:42:01 -05:00
func (p *AddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error) {
if buffer == nil {
buffer = buf.New()
defer buffer.Release()
}
2018-04-11 10:15:29 -04:00
var addr net.Address
var port net.Port
pTask := func() error {
lp, err := p.readPort(buffer, input)
2018-02-23 17:42:01 -05:00
if err != nil {
2018-04-11 10:15:29 -04:00
return err
2018-02-23 17:42:01 -05:00
}
2018-04-11 10:15:29 -04:00
port = lp
return nil
}
aTask := func() error {
a, err := p.readAddress(buffer, input)
2018-02-23 17:42:01 -05:00
if err != nil {
2018-04-11 10:15:29 -04:00
return err
2018-02-23 17:42:01 -05:00
}
2018-04-11 10:15:29 -04:00
addr = a
return nil
2018-02-23 17:42:01 -05:00
}
2018-04-11 10:15:29 -04:00
var err error
if p.portFirst {
err = signal.Execute(pTask, aTask)
} else {
err = signal.Execute(aTask, pTask)
2018-02-23 17:42:01 -05:00
}
if err != nil {
return nil, 0, err
}
return addr, port, nil
}
func (p *AddressParser) writePort(writer io.Writer, port net.Port) error {
2018-04-11 10:15:29 -04:00
return common.Error2(writer.Write(port.Bytes(nil)))
2018-02-23 17:42:01 -05:00
}
func (p *AddressParser) writeAddress(writer io.Writer, address net.Address) error {
tb, valid := p.addrByteMap[address.Family()]
if !valid {
return newError("unknown address family", address.Family())
}
switch address.Family() {
case net.AddressFamilyIPv4, net.AddressFamilyIPv6:
2018-04-11 10:15:29 -04:00
return signal.Execute(func() error {
return common.Error2(writer.Write([]byte{tb}))
}, func() error {
return common.Error2(writer.Write(address.IP()))
})
2018-02-23 17:42:01 -05:00
case net.AddressFamilyDomain:
domain := address.Domain()
2018-03-04 15:06:04 -05:00
if isDomainTooLong(domain) {
2018-02-23 17:42:01 -05:00
return newError("Super long domain is not supported: ", domain)
}
2018-04-11 10:15:29 -04:00
return signal.Execute(func() error {
return common.Error2(writer.Write([]byte{tb, byte(len(domain))}))
}, func() error {
return common.Error2(writer.Write([]byte(domain)))
})
default:
panic("Unknown family type.")
2018-02-23 17:42:01 -05:00
}
}
2018-04-11 10:15:29 -04:00
// WriteAddressPort writes address and port into the given writer.
2018-02-23 17:42:01 -05:00
func (p *AddressParser) WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error {
2018-04-11 10:15:29 -04:00
pTask := func() error {
return p.writePort(writer, port)
2018-02-23 17:42:01 -05:00
}
2018-04-11 10:15:29 -04:00
aTask := func() error {
return p.writeAddress(writer, addr)
2018-02-23 17:42:01 -05:00
}
2018-04-11 10:15:29 -04:00
if p.portFirst {
return signal.Execute(pTask, aTask)
2018-02-23 17:42:01 -05:00
}
2018-04-11 10:15:29 -04:00
return signal.Execute(aTask, pTask)
2018-02-23 17:42:01 -05:00
}