mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-02-20 23:47:21 -05:00
test case for on demand detour
This commit is contained in:
parent
e346f179d2
commit
d8c6102638
@ -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
|
||||||
|
@ -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()
|
||||||
|
@ -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)
|
||||||
|
@ -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:
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
|
@ -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": [
|
||||||
{
|
{
|
||||||
|
@ -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),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user