mirror of https://github.com/v2ray/v2ray-core
integrate mux in vmess server
parent
fcafd4e8f8
commit
06d4c37889
|
@ -82,12 +82,13 @@ type Client struct {
|
||||||
concurrency uint32
|
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.
|
// NewClient creates a new mux.Client.
|
||||||
func NewClient(p proxy.Outbound, dialer proxy.Dialer, m *ClientManager) (*Client, error) {
|
func NewClient(p proxy.Outbound, dialer proxy.Dialer, m *ClientManager) (*Client, error) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
ctx = proxy.ContextWithTarget(ctx, muxCoolDestination)
|
ctx = proxy.ContextWithTarget(ctx, net.TCPDestination(muxCoolAddress, muxCoolPort))
|
||||||
pipe := ray.NewRay(ctx)
|
pipe := ray.NewRay(ctx)
|
||||||
go p.Process(ctx, pipe, dialer)
|
go p.Process(ctx, pipe, dialer)
|
||||||
c := &Client{
|
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) {
|
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)
|
return s.dispatcher.Dispatch(ctx, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ type RequestCommand byte
|
||||||
const (
|
const (
|
||||||
RequestCommandTCP = RequestCommand(0x01)
|
RequestCommandTCP = RequestCommand(0x01)
|
||||||
RequestCommandUDP = RequestCommand(0x02)
|
RequestCommandUDP = RequestCommand(0x02)
|
||||||
|
RequestCommandMux = RequestCommand(0x03)
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c RequestCommand) TransferType() TransferType {
|
func (c RequestCommand) TransferType() TransferType {
|
||||||
|
|
|
@ -81,18 +81,21 @@ func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
|
||||||
}
|
}
|
||||||
security := byte(padingLen<<4) | byte(header.Security)
|
security := byte(padingLen<<4) | byte(header.Security)
|
||||||
buffer = append(buffer, security, byte(0), byte(header.Command))
|
buffer = append(buffer, security, byte(0), byte(header.Command))
|
||||||
buffer = header.Port.Bytes(buffer)
|
|
||||||
|
|
||||||
switch header.Address.Family() {
|
if header.Command != protocol.RequestCommandMux {
|
||||||
case net.AddressFamilyIPv4:
|
buffer = header.Port.Bytes(buffer)
|
||||||
buffer = append(buffer, AddrTypeIPv4)
|
|
||||||
buffer = append(buffer, header.Address.IP()...)
|
switch header.Address.Family() {
|
||||||
case net.AddressFamilyIPv6:
|
case net.AddressFamilyIPv4:
|
||||||
buffer = append(buffer, AddrTypeIPv6)
|
buffer = append(buffer, AddrTypeIPv4)
|
||||||
buffer = append(buffer, header.Address.IP()...)
|
buffer = append(buffer, header.Address.IP()...)
|
||||||
case net.AddressFamilyDomain:
|
case net.AddressFamilyIPv6:
|
||||||
buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
|
buffer = append(buffer, AddrTypeIPv6)
|
||||||
buffer = append(buffer, header.Address.Domain()...)
|
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 {
|
if padingLen > 0 {
|
||||||
|
|
|
@ -170,38 +170,40 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||||
// 1 bytes reserved
|
// 1 bytes reserved
|
||||||
request.Command = protocol.RequestCommand(buffer[37])
|
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] {
|
switch buffer[40] {
|
||||||
case AddrTypeIPv4:
|
case AddrTypeIPv4:
|
||||||
_, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes
|
_, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes
|
||||||
bufferLen += 4
|
bufferLen += 4
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, newError("failed to read IPv4 address").Base(err)
|
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 {
|
if padingLen > 0 {
|
||||||
|
|
|
@ -185,6 +185,12 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i
|
||||||
}
|
}
|
||||||
return err
|
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.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "")
|
||||||
log.Trace(newError("received request for ", request.Destination()))
|
log.Trace(newError("received request for ", request.Destination()))
|
||||||
|
|
||||||
|
|
|
@ -76,6 +76,9 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
|
||||||
if target.Network == net.Network_UDP {
|
if target.Network == net.Network_UDP {
|
||||||
command = protocol.RequestCommandUDP
|
command = protocol.RequestCommandUDP
|
||||||
}
|
}
|
||||||
|
//if target.Address.Family().IsDomain() && target.Address.Domain() == "v1.mux.com" {
|
||||||
|
// command = protocol.RequestCommandMux
|
||||||
|
//}
|
||||||
request := &protocol.RequestHeader{
|
request := &protocol.RequestHeader{
|
||||||
Version: encoding.Version,
|
Version: encoding.Version,
|
||||||
User: rec.PickUser(),
|
User: rec.PickUser(),
|
||||||
|
|
Loading…
Reference in New Issue