Selaa lähdekoodia

integrate mux in vmess server

Darien Raymond 8 vuotta sitten
vanhempi
commit
06d4c37889

+ 4 - 3
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)
 	}
 

+ 1 - 0
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 {

+ 15 - 12
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 {

+ 33 - 31
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])
-
-	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)
+	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)
+			}
+			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]))
 		}
-		bufferLen += 1 + domainLength
-		request.Address = net.DomainAddress(string(buffer[42 : 42+domainLength]))
 	}
 
 	if padingLen > 0 {

+ 6 - 0
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()))
 

+ 3 - 0
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(),