github.com/moqsien/xraycore@v1.8.5/common/net/port.go (about)

     1  package net
     2  
     3  import (
     4  	"encoding/binary"
     5  	"strconv"
     6  )
     7  
     8  // Port represents a network port in TCP and UDP protocol.
     9  type Port uint16
    10  
    11  // PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order.
    12  // @unsafe Caller must ensure that the byte array has at least 2 elements.
    13  func PortFromBytes(port []byte) Port {
    14  	return Port(binary.BigEndian.Uint16(port))
    15  }
    16  
    17  // PortFromInt converts an integer to a Port.
    18  // @error when the integer is not positive or larger then 65535
    19  func PortFromInt(val uint32) (Port, error) {
    20  	if val > 65535 {
    21  		return Port(0), newError("invalid port range: ", val)
    22  	}
    23  	return Port(val), nil
    24  }
    25  
    26  // PortFromString converts a string to a Port.
    27  // @error when the string is not an integer or the integral value is a not a valid Port.
    28  func PortFromString(s string) (Port, error) {
    29  	val, err := strconv.ParseUint(s, 10, 32)
    30  	if err != nil {
    31  		return Port(0), newError("invalid port range: ", s)
    32  	}
    33  	return PortFromInt(uint32(val))
    34  }
    35  
    36  // Value return the corresponding uint16 value of a Port.
    37  func (p Port) Value() uint16 {
    38  	return uint16(p)
    39  }
    40  
    41  // String returns the string presentation of a Port.
    42  func (p Port) String() string {
    43  	return strconv.Itoa(int(p))
    44  }
    45  
    46  // FromPort returns the beginning port of this PortRange.
    47  func (p *PortRange) FromPort() Port {
    48  	return Port(p.From)
    49  }
    50  
    51  // ToPort returns the end port of this PortRange.
    52  func (p *PortRange) ToPort() Port {
    53  	return Port(p.To)
    54  }
    55  
    56  // Contains returns true if the given port is within the range of a PortRange.
    57  func (p *PortRange) Contains(port Port) bool {
    58  	return p.FromPort() <= port && port <= p.ToPort()
    59  }
    60  
    61  // SinglePortRange returns a PortRange contains a single port.
    62  func SinglePortRange(p Port) *PortRange {
    63  	return &PortRange{
    64  		From: uint32(p),
    65  		To:   uint32(p),
    66  	}
    67  }
    68  
    69  type MemoryPortRange struct {
    70  	From Port
    71  	To   Port
    72  }
    73  
    74  func (r MemoryPortRange) Contains(port Port) bool {
    75  	return r.From <= port && port <= r.To
    76  }
    77  
    78  type MemoryPortList []MemoryPortRange
    79  
    80  func PortListFromProto(l *PortList) MemoryPortList {
    81  	mpl := make(MemoryPortList, 0, len(l.Range))
    82  	for _, r := range l.Range {
    83  		mpl = append(mpl, MemoryPortRange{From: Port(r.From), To: Port(r.To)})
    84  	}
    85  	return mpl
    86  }
    87  
    88  func (mpl MemoryPortList) Contains(port Port) bool {
    89  	for _, pr := range mpl {
    90  		if pr.Contains(port) {
    91  			return true
    92  		}
    93  	}
    94  	return false
    95  }