1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-28 10:15:23 +00:00

integrate mux in vmess server

This commit is contained in:
Darien Raymond 2017-05-17 21:13:01 +02:00
parent fcafd4e8f8
commit 06d4c37889
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
6 changed files with 60 additions and 44 deletions

View File

@ -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)
}

View File

@ -13,6 +13,7 @@ type RequestCommand byte
const (
RequestCommandTCP = RequestCommand(0x01)
RequestCommandUDP = RequestCommand(0x02)
RequestCommandMux = RequestCommand(0x03)
)
func (c RequestCommand) TransferType() TransferType {

View File

@ -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 {

View File

@ -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 {

View File

@ -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()))

View File

@ -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(),