2020-08-28 03:51:09 -04:00
|
|
|
// +build !confonly
|
|
|
|
|
2020-07-28 11:00:23 -04:00
|
|
|
package encoding
|
|
|
|
|
2020-10-04 00:41:45 -04:00
|
|
|
//go:generate go run v2ray.com/core/common/errors/errorgen
|
2020-08-28 03:51:09 -04:00
|
|
|
|
2020-07-28 11:00:23 -04:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/proxy/vless"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Version = byte(0)
|
|
|
|
)
|
|
|
|
|
|
|
|
var addrParser = protocol.NewAddressParser(
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6),
|
|
|
|
protocol.PortThenAddress(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// EncodeRequestHeader writes encoded request header into the given writer.
|
|
|
|
func EncodeRequestHeader(writer io.Writer, request *protocol.RequestHeader, requestAddons *Addons) error {
|
|
|
|
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
if err := buffer.WriteByte(request.Version); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write request version").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := buffer.Write(request.User.Account.(*vless.MemoryAccount).ID.Bytes()); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write request user id").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := EncodeHeaderAddons(&buffer, requestAddons); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to encode request header addons").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := buffer.WriteByte(byte(request.Command)); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write request command").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Command != protocol.RequestCommandMux {
|
|
|
|
if err := addrParser.WriteAddressPort(&buffer, request.Address, request.Port); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write request address and port").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writer.Write(buffer.Bytes()); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write request header").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeRequestHeader decodes and returns (if successful) a RequestHeader from an input stream.
|
2020-10-02 23:23:24 -04:00
|
|
|
func DecodeRequestHeader(isfb bool, first *buf.Buffer, reader io.Reader, validator *vless.Validator) (*protocol.RequestHeader, *Addons, error, bool) {
|
2020-07-28 11:00:23 -04:00
|
|
|
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
2020-10-02 23:23:24 -04:00
|
|
|
request := new(protocol.RequestHeader)
|
2020-07-28 11:00:23 -04:00
|
|
|
|
2020-10-02 23:23:24 -04:00
|
|
|
if isfb {
|
|
|
|
request.Version = first.Byte(0)
|
|
|
|
} else {
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 1); err != nil {
|
|
|
|
return nil, nil, newError("failed to read request version").Base(err), false
|
|
|
|
}
|
|
|
|
request.Version = buffer.Byte(0)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch request.Version {
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
var id [16]byte
|
2020-10-02 23:23:24 -04:00
|
|
|
|
|
|
|
if isfb {
|
|
|
|
copy(id[:], first.BytesRange(1, 17))
|
|
|
|
} else {
|
|
|
|
buffer.Clear()
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 16); err != nil {
|
|
|
|
return nil, nil, newError("failed to read request user id").Base(err), false
|
|
|
|
}
|
|
|
|
copy(id[:], buffer.Bytes())
|
|
|
|
}
|
2020-07-28 11:00:23 -04:00
|
|
|
|
|
|
|
if request.User = validator.Get(id); request.User == nil {
|
2020-10-02 23:23:24 -04:00
|
|
|
return nil, nil, newError("invalid request user id"), isfb
|
|
|
|
}
|
|
|
|
|
|
|
|
if isfb {
|
|
|
|
first.Advance(17)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
requestAddons, err := DecodeHeaderAddons(&buffer, reader)
|
|
|
|
if err != nil {
|
2020-10-02 23:23:24 -04:00
|
|
|
return nil, nil, newError("failed to decode request header addons").Base(err), false
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Clear()
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 1); err != nil {
|
2020-10-02 23:23:24 -04:00
|
|
|
return nil, nil, newError("failed to read request command").Base(err), false
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
request.Command = protocol.RequestCommand(buffer.Byte(0))
|
|
|
|
switch request.Command {
|
|
|
|
case protocol.RequestCommandMux:
|
|
|
|
request.Address = net.DomainAddress("v1.mux.cool")
|
|
|
|
request.Port = 0
|
|
|
|
case protocol.RequestCommandTCP, protocol.RequestCommandUDP:
|
|
|
|
if addr, port, err := addrParser.ReadAddressPort(&buffer, reader); err == nil {
|
|
|
|
request.Address = addr
|
|
|
|
request.Port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Address == nil {
|
2020-10-02 23:23:24 -04:00
|
|
|
return nil, nil, newError("invalid request address"), false
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
2020-10-02 23:23:24 -04:00
|
|
|
return request, requestAddons, nil, false
|
2020-07-28 11:00:23 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
|
2020-10-02 23:23:24 -04:00
|
|
|
return nil, nil, newError("invalid request version"), isfb
|
2020-07-28 11:00:23 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeResponseHeader writes encoded response header into the given writer.
|
|
|
|
func EncodeResponseHeader(writer io.Writer, request *protocol.RequestHeader, responseAddons *Addons) error {
|
|
|
|
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
if err := buffer.WriteByte(request.Version); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write response version").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := EncodeHeaderAddons(&buffer, responseAddons); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to encode response header addons").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writer.Write(buffer.Bytes()); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write response header").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeResponseHeader decodes and returns (if successful) a ResponseHeader from an input stream.
|
2020-09-23 21:59:14 -04:00
|
|
|
func DecodeResponseHeader(reader io.Reader, request *protocol.RequestHeader) (*Addons, error) {
|
2020-07-28 11:00:23 -04:00
|
|
|
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 1); err != nil {
|
2020-09-23 21:59:14 -04:00
|
|
|
return nil, newError("failed to read response version").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if buffer.Byte(0) != request.Version {
|
2020-09-23 21:59:14 -04:00
|
|
|
return nil, newError("unexpected response version. Expecting ", int(request.Version), " but actually ", int(buffer.Byte(0)))
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
responseAddons, err := DecodeHeaderAddons(&buffer, reader)
|
|
|
|
if err != nil {
|
2020-09-23 21:59:14 -04:00
|
|
|
return nil, newError("failed to decode response header addons").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 21:59:14 -04:00
|
|
|
return responseAddons, nil
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|