port.go 561 B

1234567891011121314151617181920212223242526272829303132
  1. package net
  2. import (
  3. "github.com/v2ray/v2ray-core/common/serial"
  4. )
  5. type Port serial.Uint16Literal
  6. func PortFromBytes(port []byte) Port {
  7. return Port(uint16(port[0])<<8 + uint16(port[1]))
  8. }
  9. func (this Port) Value() uint16 {
  10. return uint16(this)
  11. }
  12. func (this Port) Bytes() []byte {
  13. return []byte{byte(this >> 8), byte(this)}
  14. }
  15. func (this Port) String() string {
  16. return serial.Uint16Literal(this).String()
  17. }
  18. type PortRange struct {
  19. From Port
  20. To Port
  21. }
  22. func (this PortRange) Contains(port Port) bool {
  23. return this.From <= port && port <= this.To
  24. }