command.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. tag := this.features.Detour.ToTag
  15. if this.inboundHandlerManager != nil {
  16. handler, availableMin := this.inboundHandlerManager.GetHandler(tag)
  17. inboundHandler, ok := handler.(*VMessInboundHandler)
  18. if ok {
  19. if availableMin > 255 {
  20. availableMin = 255
  21. }
  22. cmd = byte(1)
  23. log.Info("VMessIn: Pick detour handler for port ", inboundHandler.Port(), " for ", availableMin, " minutes.")
  24. user := inboundHandler.GetUser()
  25. saCmd := &command.SwitchAccount{
  26. Port: inboundHandler.Port(),
  27. ID: user.ID.UUID(),
  28. AlterIds: serial.Uint16Literal(len(user.AlterIDs)),
  29. Level: user.Level,
  30. ValidMin: byte(availableMin),
  31. }
  32. saCmd.Marshal(commandBytes)
  33. }
  34. }
  35. }
  36. if cmd == 0 || commandBytes.Len()+4 > 256 {
  37. buffer.AppendBytes(byte(0), byte(0))
  38. } else {
  39. buffer.AppendBytes(cmd, byte(commandBytes.Len()+4))
  40. fnv1hash := fnv.New32a()
  41. fnv1hash.Write(commandBytes.Value)
  42. hashValue := fnv1hash.Sum32()
  43. buffer.AppendBytes(byte(hashValue>>24), byte(hashValue>>16), byte(hashValue>>8), byte(hashValue))
  44. buffer.Append(commandBytes.Value)
  45. }
  46. }