version_cmd.go 1001 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package proxyproto
  2. // ProtocolVersionAndCommand represents proxy protocol version and command.
  3. type ProtocolVersionAndCommand byte
  4. const (
  5. LOCAL = '\x20'
  6. PROXY = '\x21'
  7. )
  8. var supportedCommand = map[ProtocolVersionAndCommand]bool{
  9. LOCAL: true,
  10. PROXY: true,
  11. }
  12. // IsLocal returns true if the protocol version is \x2 and command is LOCAL, false otherwise.
  13. func (pvc ProtocolVersionAndCommand) IsLocal() bool {
  14. return 0x20 == pvc&0xF0 && 0x00 == pvc&0x0F
  15. }
  16. // IsProxy returns true if the protocol version is \x2 and command is PROXY, false otherwise.
  17. func (pvc ProtocolVersionAndCommand) IsProxy() bool {
  18. return 0x20 == pvc&0xF0 && 0x01 == pvc&0x0F
  19. }
  20. // IsUnspec returns true if the protocol version or command is unspecified, false otherwise.
  21. func (pvc ProtocolVersionAndCommand) IsUnspec() bool {
  22. return !(pvc.IsLocal() || pvc.IsProxy())
  23. }
  24. func (pvc ProtocolVersionAndCommand) toByte() byte {
  25. if pvc.IsLocal() {
  26. return LOCAL
  27. } else if pvc.IsProxy() {
  28. return PROXY
  29. }
  30. return LOCAL
  31. }