command.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package outbound
  2. import (
  3. "hash/fnv"
  4. "github.com/v2ray/v2ray-core/common/log"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. proto "github.com/v2ray/v2ray-core/common/protocol"
  7. "github.com/v2ray/v2ray-core/common/serial"
  8. "github.com/v2ray/v2ray-core/proxy/vmess/command"
  9. )
  10. func (this *VMessOutboundHandler) handleSwitchAccount(cmd *command.SwitchAccount) {
  11. user := proto.NewUser(proto.NewID(cmd.ID), cmd.Level, cmd.AlterIds.Value())
  12. dest := v2net.TCPDestination(cmd.Host, cmd.Port)
  13. this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin)
  14. }
  15. func (this *VMessOutboundHandler) handleCommand(dest v2net.Destination, cmdId byte, data []byte) {
  16. if len(data) < 4 {
  17. return
  18. }
  19. fnv1hash := fnv.New32a()
  20. fnv1hash.Write(data[4:])
  21. actualHashValue := fnv1hash.Sum32()
  22. expectedHashValue := serial.BytesLiteral(data[:4]).Uint32Value()
  23. if actualHashValue != expectedHashValue {
  24. return
  25. }
  26. data = data[4:]
  27. cmd, err := command.CreateResponseCommand(cmdId)
  28. if err != nil {
  29. log.Warning("VMessOut: Unknown response command (", cmdId, "): ", err)
  30. return
  31. }
  32. if err := cmd.Unmarshal(data); err != nil {
  33. log.Warning("VMessOut: Failed to parse response command: ", err)
  34. return
  35. }
  36. switch typedCommand := cmd.(type) {
  37. case *command.SwitchAccount:
  38. if typedCommand.Host == nil {
  39. typedCommand.Host = dest.Address()
  40. }
  41. this.handleSwitchAccount(typedCommand)
  42. default:
  43. }
  44. }