mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-10-13 04:24:03 -04:00
simplify port range parsing
This commit is contained in:
@@ -1,15 +1,38 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrorInvalidPortRage indicates an error during port range parsing.
|
||||
ErrorInvalidPortRange = errors.New("Invalid port range.")
|
||||
)
|
||||
|
||||
type Port serial.Uint16Literal
|
||||
|
||||
func PortFromBytes(port []byte) Port {
|
||||
return Port(serial.BytesLiteral(port).Uint16Value())
|
||||
}
|
||||
|
||||
func PortFromInt(v int) (Port, error) {
|
||||
if v <= 0 || v > 65535 {
|
||||
return Port(0), ErrorInvalidPortRange
|
||||
}
|
||||
return Port(v), nil
|
||||
}
|
||||
|
||||
func PortFromString(s string) (Port, error) {
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return Port(0), ErrorInvalidPortRange
|
||||
}
|
||||
return PortFromInt(v)
|
||||
}
|
||||
|
||||
func (this Port) Value() uint16 {
|
||||
return uint16(this)
|
||||
}
|
||||
|
Reference in New Issue
Block a user