command.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package inbound
  2. import (
  3. "github.com/v2ray/v2ray-core/common/alloc"
  4. "github.com/v2ray/v2ray-core/common/log"
  5. "github.com/v2ray/v2ray-core/common/serial"
  6. "github.com/v2ray/v2ray-core/proxy/vmess/command"
  7. )
  8. func (this *VMessInboundHandler) generateCommand(buffer *alloc.Buffer) {
  9. cmd := byte(0)
  10. commandBytes := alloc.NewSmallBuffer().Clear()
  11. defer commandBytes.Release()
  12. if this.features != nil && this.features.Detour != nil {
  13. cmd = byte(1)
  14. tag := this.features.Detour.ToTag
  15. if this.space.HasInboundHandlerManager() {
  16. handlerManager := this.space.InboundHandlerManager()
  17. handler, availableMin := handlerManager.GetHandler(tag)
  18. inboundHandler, ok := handler.(*VMessInboundHandler)
  19. if ok {
  20. if availableMin > 255 {
  21. availableMin = 255
  22. }
  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 commandBytes.Len() > 256 {
  37. buffer.AppendBytes(byte(0), byte(0))
  38. } else {
  39. buffer.AppendBytes(cmd, byte(commandBytes.Len()))
  40. buffer.Append(commandBytes.Value)
  41. }
  42. }