command.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package inbound
  2. import (
  3. "hash/fnv"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. "github.com/v2ray/v2ray-core/proxy/vmess/command"
  8. )
  9. func (this *VMessInboundHandler) generateCommand(buffer *alloc.Buffer) {
  10. cmd := byte(0)
  11. commandBytes := alloc.NewSmallBuffer().Clear()
  12. defer commandBytes.Release()
  13. if this.features != nil && this.features.Detour != nil {
  14. cmd = byte(1)
  15. tag := this.features.Detour.ToTag
  16. if this.space.HasInboundHandlerManager() {
  17. handlerManager := this.space.InboundHandlerManager()
  18. handler, availableMin := handlerManager.GetHandler(tag)
  19. inboundHandler, ok := handler.(*VMessInboundHandler)
  20. if ok {
  21. if availableMin > 255 {
  22. availableMin = 255
  23. }
  24. log.Info("VMessIn: Pick detour handler for port ", inboundHandler.Port(), " for ", availableMin, " minutes.")
  25. user := inboundHandler.GetUser()
  26. saCmd := &command.SwitchAccount{
  27. Port: inboundHandler.Port(),
  28. ID: user.ID.UUID(),
  29. AlterIds: serial.Uint16Literal(len(user.AlterIDs)),
  30. Level: user.Level,
  31. ValidMin: byte(availableMin),
  32. }
  33. saCmd.Marshal(commandBytes)
  34. }
  35. }
  36. }
  37. if cmd == 0 || commandBytes.Len()+4 > 256 {
  38. buffer.AppendBytes(byte(0), byte(0))
  39. } else {
  40. buffer.AppendBytes(cmd, byte(commandBytes.Len()+4))
  41. fnv1hash := fnv.New32a()
  42. fnv1hash.Write(commandBytes.Value)
  43. hashValue := fnv1hash.Sum32()
  44. buffer.AppendBytes(byte(hashValue>>24), byte(hashValue>>16), byte(hashValue>>8), byte(hashValue))
  45. buffer.Append(commandBytes.Value)
  46. }
  47. }