v2ray-core/proxy/vmess/inbound/command.go

54 lines
1.6 KiB
Go
Raw Normal View History

2016-01-19 22:41:40 +00:00
package inbound
import (
2016-01-22 16:56:03 +00:00
"hash/fnv"
2016-01-19 22:41:40 +00:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-01-22 15:48:17 +00:00
"github.com/v2ray/v2ray-core/common/log"
2016-01-19 22:41:40 +00:00
"github.com/v2ray/v2ray-core/common/serial"
"github.com/v2ray/v2ray-core/proxy/vmess/command"
2016-02-25 13:38:41 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
2016-01-19 22:41:40 +00:00
)
2016-02-25 13:38:41 +00:00
func (this *VMessInboundHandler) generateCommand(request *protocol.VMessRequest, buffer *alloc.Buffer) {
2016-01-19 22:41:40 +00:00
cmd := byte(0)
commandBytes := alloc.NewSmallBuffer().Clear()
defer commandBytes.Release()
if this.features != nil && this.features.Detour != nil {
2016-01-24 23:43:55 +00:00
2016-01-19 22:41:40 +00:00
tag := this.features.Detour.ToTag
2016-01-31 16:01:28 +00:00
if this.inboundHandlerManager != nil {
handler, availableMin := this.inboundHandlerManager.GetHandler(tag)
2016-01-19 22:41:40 +00:00
inboundHandler, ok := handler.(*VMessInboundHandler)
if ok {
if availableMin > 255 {
availableMin = 255
2016-01-19 22:41:40 +00:00
}
2016-01-24 23:43:55 +00:00
cmd = byte(1)
2016-01-22 15:48:17 +00:00
log.Info("VMessIn: Pick detour handler for port ", inboundHandler.Port(), " for ", availableMin, " minutes.")
2016-02-25 13:38:41 +00:00
user := inboundHandler.GetUser(request.User.Email)
2016-01-19 22:41:40 +00:00
saCmd := &command.SwitchAccount{
Port: inboundHandler.Port(),
ID: user.ID.UUID(),
AlterIds: serial.Uint16Literal(len(user.AlterIDs)),
Level: user.Level,
ValidMin: byte(availableMin),
2016-01-19 22:41:40 +00:00
}
saCmd.Marshal(commandBytes)
}
}
}
2016-01-22 16:56:03 +00:00
if cmd == 0 || commandBytes.Len()+4 > 256 {
2016-01-19 22:41:40 +00:00
buffer.AppendBytes(byte(0), byte(0))
} else {
2016-01-22 16:56:03 +00:00
buffer.AppendBytes(cmd, byte(commandBytes.Len()+4))
fnv1hash := fnv.New32a()
fnv1hash.Write(commandBytes.Value)
hashValue := fnv1hash.Sum32()
buffer.AppendBytes(byte(hashValue>>24), byte(hashValue>>16), byte(hashValue>>8), byte(hashValue))
2016-01-19 22:41:40 +00:00
buffer.Append(commandBytes.Value)
}
}