1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 18:45:23 +00:00

test case for on demand detour

This commit is contained in:
Darien Raymond 2016-01-21 16:22:56 +00:00
parent e346f179d2
commit d8c6102638
9 changed files with 32 additions and 15 deletions

View File

@ -58,7 +58,9 @@ func (this *SwitchAccount) Unmarshal(data []byte) error {
if len(data) < lenHost+1 { if len(data) < lenHost+1 {
return transport.CorruptedPacket return transport.CorruptedPacket
} }
this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost])) if lenHost > 0 {
this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
}
portStart := 1 + lenHost portStart := 1 + lenHost
if len(data) < portStart+2 { if len(data) < portStart+2 {
return transport.CorruptedPacket return transport.CorruptedPacket

View File

@ -12,6 +12,7 @@ func (this *VMessInboundHandler) generateCommand(buffer *alloc.Buffer) {
defer commandBytes.Release() defer commandBytes.Release()
if this.features != nil && this.features.Detour != nil { if this.features != nil && this.features.Detour != nil {
cmd = byte(1)
tag := this.features.Detour.ToTag tag := this.features.Detour.ToTag
if this.space.HasInboundHandlerManager() { if this.space.HasInboundHandlerManager() {
handlerManager := this.space.InboundHandlerManager() handlerManager := this.space.InboundHandlerManager()

View File

@ -139,7 +139,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
buffer := alloc.NewLargeBuffer().Clear() buffer := alloc.NewLargeBuffer().Clear()
defer buffer.Release() defer buffer.Release()
buffer.AppendBytes(request.ResponseHeader, byte(0)) buffer.AppendBytes(request.ResponseHeader, byte(0))
buffer.AppendBytes(byte(0), byte(0)) this.generateCommand(buffer)
if data, open := <-output; open { if data, open := <-output; open {
buffer.Append(data.Value) buffer.Append(data.Value)

View File

@ -13,7 +13,7 @@ func (this *VMessOutboundHandler) handleSwitchAccount(cmd *command.SwitchAccount
this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin) this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin)
} }
func (this *VMessOutboundHandler) handleCommand(cmdId byte, data []byte) { func (this *VMessOutboundHandler) handleCommand(dest v2net.Destination, cmdId byte, data []byte) {
cmd, err := command.CreateResponseCommand(cmdId) cmd, err := command.CreateResponseCommand(cmdId)
if err != nil { if err != nil {
log.Warning("VMessOut: Unknown response command (", cmdId, "): ", err) log.Warning("VMessOut: Unknown response command (", cmdId, "): ", err)
@ -25,6 +25,9 @@ func (this *VMessOutboundHandler) handleCommand(cmdId byte, data []byte) {
} }
switch typedCommand := cmd.(type) { switch typedCommand := cmd.(type) {
case *command.SwitchAccount: case *command.SwitchAccount:
if typedCommand.Host == nil {
typedCommand.Host = dest.Address()
}
this.handleSwitchAccount(typedCommand) this.handleSwitchAccount(typedCommand)
default: default:
} }

View File

@ -82,7 +82,7 @@ func (this *VMessOutboundHandler) startCommunicate(request *protocol.VMessReques
responseFinish.Lock() responseFinish.Lock()
go this.handleRequest(conn, request, firstPacket, input, &requestFinish) go this.handleRequest(conn, request, firstPacket, input, &requestFinish)
go this.handleResponse(conn, request, output, &responseFinish, (request.Command == protocol.CmdUDP)) go this.handleResponse(conn, request, dest, output, &responseFinish, (request.Command == protocol.CmdUDP))
requestFinish.Lock() requestFinish.Lock()
conn.CloseWrite() conn.CloseWrite()
@ -140,7 +140,7 @@ func headerMatch(request *protocol.VMessRequest, responseHeader byte) bool {
return request.ResponseHeader == responseHeader return request.ResponseHeader == responseHeader
} }
func (this *VMessOutboundHandler) handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<- *alloc.Buffer, finish *sync.Mutex, isUDP bool) { func (this *VMessOutboundHandler) handleResponse(conn net.Conn, request *protocol.VMessRequest, dest v2net.Destination, output chan<- *alloc.Buffer, finish *sync.Mutex, isUDP bool) {
defer finish.Unlock() defer finish.Unlock()
defer close(output) defer close(output)
responseKey := md5.Sum(request.RequestKey[:]) responseKey := md5.Sum(request.RequestKey[:])
@ -175,7 +175,7 @@ func (this *VMessOutboundHandler) handleResponse(conn net.Conn, request *protoco
} }
command := buffer.Value[2] command := buffer.Value[2]
data := buffer.Value[4 : 4+dataLen] data := buffer.Value[4 : 4+dataLen]
go this.handleCommand(command, data) go this.handleCommand(dest, command, data)
responseBegin = 4 + dataLen responseBegin = 4 + dataLen
} }

View File

@ -58,7 +58,7 @@ type ExpiringReceiver struct {
} }
func (this *ExpiringReceiver) Expired() bool { func (this *ExpiringReceiver) Expired() bool {
return this.until.After(time.Now()) return this.until.Before(time.Now())
} }
type ReceiverManager struct { type ReceiverManager struct {
@ -87,15 +87,16 @@ func (this *ReceiverManager) AddDetour(rec *Receiver, availableMin byte) {
for _, u := range rec.Accounts { for _, u := range rec.Accounts {
r.AddUser(u) r.AddUser(u)
} }
break
} }
} }
this.detourAccess.RUnlock() this.detourAccess.RUnlock()
expRec := &ExpiringReceiver{
Receiver: rec,
until: time.Now().Add(time.Duration(availableMin-1) * time.Minute),
}
if !destExists { if !destExists {
expRec := &ExpiringReceiver{
Receiver: rec,
until: time.Now().Add(time.Duration(availableMin-1) * time.Minute),
}
this.detourAccess.Lock() this.detourAccess.Lock()
this.detours = append(this.detours, expRec) this.detours = append(this.detours, expRec)
this.detourAccess.Unlock() this.detourAccess.Unlock()

View File

@ -205,6 +205,10 @@ func (this *Point) FilterPacketAndDispatch(packet v2net.Packet, link ray.Outboun
dispatcher.Dispatch(packet, link) dispatcher.Dispatch(packet, link)
} }
func (this *Point) GetHandler(tag string) (proxy.InboundConnectionHandler, int) { func (this *Point) GetHandler(context app.Context, tag string) (proxy.InboundConnectionHandler, int) {
return nil, 0 handler, found := this.taggedIdh[tag]
if !found {
return nil, 0
}
return handler.GetConnectionHandler()
} }

View File

@ -14,7 +14,12 @@
"level": 1, "level": 1,
"alterId": 10 "alterId": 10
} }
] ],
"features": {
"detour": {
"to": "detour"
}
}
} }
}, },
"outbound": { "outbound": {
@ -25,6 +30,7 @@
{ {
"protocol": "vmess", "protocol": "vmess",
"port": "50005-50009", "port": "50005-50009",
"tag": "detour",
"settings": { "settings": {
"clients": [ "clients": [
{ {

View File

@ -33,7 +33,7 @@ func TestTCPConnection(t *testing.T) {
socksPort := v2net.Port(50000) socksPort := v2net.Port(50000)
for i := 0; i < 10; i++ { for i := 0; i < 100; i++ {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1}, IP: []byte{127, 0, 0, 1},
Port: int(socksPort), Port: int(socksPort),