From 06d4c3788960be0938cff3e47b436c638db7423a Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 17 May 2017 21:13:01 +0200 Subject: [PATCH] integrate mux in vmess server --- app/proxyman/mux/mux.go | 7 ++-- common/protocol/headers.go | 1 + proxy/vmess/encoding/client.go | 25 +++++++------ proxy/vmess/encoding/server.go | 62 ++++++++++++++++---------------- proxy/vmess/inbound/inbound.go | 6 ++++ proxy/vmess/outbound/outbound.go | 3 ++ 6 files changed, 60 insertions(+), 44 deletions(-) diff --git a/app/proxyman/mux/mux.go b/app/proxyman/mux/mux.go index 63ff42a14..39e7a825b 100644 --- a/app/proxyman/mux/mux.go +++ b/app/proxyman/mux/mux.go @@ -82,12 +82,13 @@ type Client struct { concurrency uint32 } -var muxCoolDestination = net.TCPDestination(net.DomainAddress("v1.mux.cool"), net.Port(9527)) +var muxCoolAddress = net.DomainAddress("v1.mux.cool") +var muxCoolPort = net.Port(9527) // NewClient creates a new mux.Client. func NewClient(p proxy.Outbound, dialer proxy.Dialer, m *ClientManager) (*Client, error) { ctx, cancel := context.WithCancel(context.Background()) - ctx = proxy.ContextWithTarget(ctx, muxCoolDestination) + ctx = proxy.ContextWithTarget(ctx, net.TCPDestination(muxCoolAddress, muxCoolPort)) pipe := ray.NewRay(ctx) go p.Process(ctx, pipe, dialer) c := &Client{ @@ -274,7 +275,7 @@ func NewServer(ctx context.Context) *Server { } func (s *Server) Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error) { - if dest != muxCoolDestination { + if dest.Address != muxCoolAddress { return s.dispatcher.Dispatch(ctx, dest) } diff --git a/common/protocol/headers.go b/common/protocol/headers.go index 9ea26aa55..b9ecd1a9f 100644 --- a/common/protocol/headers.go +++ b/common/protocol/headers.go @@ -13,6 +13,7 @@ type RequestCommand byte const ( RequestCommandTCP = RequestCommand(0x01) RequestCommandUDP = RequestCommand(0x02) + RequestCommandMux = RequestCommand(0x03) ) func (c RequestCommand) TransferType() TransferType { diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index 28437d8d6..40f948156 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -81,18 +81,21 @@ func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ } security := byte(padingLen<<4) | byte(header.Security) buffer = append(buffer, security, byte(0), byte(header.Command)) - buffer = header.Port.Bytes(buffer) - switch header.Address.Family() { - case net.AddressFamilyIPv4: - buffer = append(buffer, AddrTypeIPv4) - buffer = append(buffer, header.Address.IP()...) - case net.AddressFamilyIPv6: - buffer = append(buffer, AddrTypeIPv6) - buffer = append(buffer, header.Address.IP()...) - case net.AddressFamilyDomain: - buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain()))) - buffer = append(buffer, header.Address.Domain()...) + if header.Command != protocol.RequestCommandMux { + buffer = header.Port.Bytes(buffer) + + switch header.Address.Family() { + case net.AddressFamilyIPv4: + buffer = append(buffer, AddrTypeIPv4) + buffer = append(buffer, header.Address.IP()...) + case net.AddressFamilyIPv6: + buffer = append(buffer, AddrTypeIPv6) + buffer = append(buffer, header.Address.IP()...) + case net.AddressFamilyDomain: + buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain()))) + buffer = append(buffer, header.Address.Domain()...) + } } if padingLen > 0 { diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index dde8f76fb..594766094 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -170,38 +170,40 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request // 1 bytes reserved request.Command = protocol.RequestCommand(buffer[37]) - request.Port = net.PortFromBytes(buffer[38:40]) + if request.Command != protocol.RequestCommandMux { + request.Port = net.PortFromBytes(buffer[38:40]) - switch buffer[40] { - case AddrTypeIPv4: - _, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes - bufferLen += 4 - if err != nil { - return nil, newError("failed to read IPv4 address").Base(err) + switch buffer[40] { + case AddrTypeIPv4: + _, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes + bufferLen += 4 + if err != nil { + return nil, newError("failed to read IPv4 address").Base(err) + } + request.Address = net.IPAddress(buffer[41:45]) + case AddrTypeIPv6: + _, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes + bufferLen += 16 + if err != nil { + return nil, newError("failed to read IPv6 address").Base(err) + } + request.Address = net.IPAddress(buffer[41:57]) + case AddrTypeDomain: + _, err = io.ReadFull(decryptor, buffer[41:42]) + if err != nil { + return nil, newError("failed to read domain address").Base(err) + } + domainLength := int(buffer[41]) + if domainLength == 0 { + return nil, newError("zero length domain").Base(err) + } + _, err = io.ReadFull(decryptor, buffer[42:42+domainLength]) + if err != nil { + return nil, newError("failed to read domain address").Base(err) + } + bufferLen += 1 + domainLength + request.Address = net.DomainAddress(string(buffer[42 : 42+domainLength])) } - request.Address = net.IPAddress(buffer[41:45]) - case AddrTypeIPv6: - _, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes - bufferLen += 16 - if err != nil { - return nil, newError("failed to read IPv6 address").Base(err) - } - request.Address = net.IPAddress(buffer[41:57]) - case AddrTypeDomain: - _, err = io.ReadFull(decryptor, buffer[41:42]) - if err != nil { - return nil, newError("failed to read domain address").Base(err) - } - domainLength := int(buffer[41]) - if domainLength == 0 { - return nil, newError("zero length domain").Base(err) - } - _, err = io.ReadFull(decryptor, buffer[42:42+domainLength]) - if err != nil { - return nil, newError("failed to read domain address").Base(err) - } - bufferLen += 1 + domainLength - request.Address = net.DomainAddress(string(buffer[42 : 42+domainLength])) } if padingLen > 0 { diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 250d8b2db..9e7c49a5d 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -185,6 +185,12 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i } return err } + + if request.Command == protocol.RequestCommandMux { + request.Address = net.DomainAddress("v1.mux.com") + request.Port = net.Port(0) + } + log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "") log.Trace(newError("received request for ", request.Destination())) diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 239c16e51..9fb6d9425 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -76,6 +76,9 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial if target.Network == net.Network_UDP { command = protocol.RequestCommandUDP } + //if target.Address.Family().IsDomain() && target.Address.Domain() == "v1.mux.com" { + // command = protocol.RequestCommandMux + //} request := &protocol.RequestHeader{ Version: encoding.Version, User: rec.PickUser(),