accounts.go 869 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package command
  2. import (
  3. "io"
  4. "time"
  5. "github.com/v2ray/v2ray-core/common/serial"
  6. "github.com/v2ray/v2ray-core/common/uuid"
  7. "github.com/v2ray/v2ray-core/transport"
  8. )
  9. func init() {
  10. RegisterResponseCommand(1, func() Command { return new(SwitchAccount) })
  11. }
  12. // Size: 16 + 8 = 24
  13. type SwitchAccount struct {
  14. ID *uuid.UUID
  15. ValidUntil time.Time
  16. }
  17. func (this *SwitchAccount) Marshal(writer io.Writer) (int, error) {
  18. idBytes := this.ID.Bytes()
  19. timestamp := this.ValidUntil.Unix()
  20. timeBytes := serial.Int64Literal(timestamp).Bytes()
  21. writer.Write(idBytes)
  22. writer.Write(timeBytes)
  23. return 24, nil
  24. }
  25. func (this *SwitchAccount) Unmarshal(data []byte) error {
  26. if len(data) != 24 {
  27. return transport.CorruptedPacket
  28. }
  29. this.ID, _ = uuid.ParseBytes(data[0:16])
  30. this.ValidUntil = time.Unix(serial.BytesLiteral(data[16:24]).Int64Value(), 0)
  31. return nil
  32. }