XTLS/Xray-core · error

invalid port range: {val}

Error message

invalid port range: {val}

What it means

Returned by net.PortFromInt when the uint32 value exceeds 65535, the maximum representable TCP/UDP port. The Port type is a uint16, so any larger input cannot be stored and is rejected before truncation could silently corrupt it.

Source

Thrown at common/net/port.go:23

	"strconv"

	"github.com/xtls/xray-core/common/errors"
)

// Port represents a network port in TCP and UDP protocol.
type Port uint16

// PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order.
// @unsafe Caller must ensure that the byte array has at least 2 elements.
func PortFromBytes(port []byte) Port {
	return Port(binary.BigEndian.Uint16(port))
}

// PortFromInt converts an integer to a Port.
// @error when the integer is not positive or larger then 65535
func PortFromInt(val uint32) (Port, error) {
	if val > 65535 {
		return Port(0), errors.New("invalid port range: ", val)
	}
	return Port(val), nil
}

// PortFromString converts a string to a Port.
// @error when the string is not an integer or the integral value is a not a valid Port.
func PortFromString(s string) (Port, error) {
	val, err := strconv.ParseUint(s, 10, 32)
	if err != nil {
		return Port(0), errors.New("invalid port range: ", s)
	}
	return PortFromInt(uint32(val))
}

// Value return the corresponding uint16 value of a Port.
func (p Port) Value() uint16 {
	return uint16(p)
}

View on GitHub (pinned to 7d214f8b09)

Solutions

  1. Fix the configuration value to the 1-65535 range
  2. Validate parsed integers against 65535 before converting to Port
  3. If the value comes from a byte stream, use PortFromBytes which reads exactly 2 big-endian bytes and cannot overflow

Example fix

// before
port, err := net.PortFromInt(cfg.Port) // cfg.Port = 70000

// after
if cfg.Port > 65535 {
    return errors.New("port out of range in config")
}
port, err := net.PortFromInt(cfg.Port)
Defensive patterns

Strategy: validation

Validate before calling

func validPortInt(v uint32) bool { return v >= 1 && v <= 65535 }
if !validPortInt(cfg.Port) { return fmt.Errorf("port %d out of range", cfg.Port) }

Type guard

func isValidPortInt(v uint32) bool { return v <= 65535 }

Prevention

When it happens

Trigger: Calling PortFromInt(val) with val > 65535, typically after parsing configuration values (inbound/outbound port settings) or deriving a port from arithmetic that overflowed the valid range.

Common situations: Config files with a typo like port 70000, a port computed from an offset (base + shift) exceeding the range, or unvalidated user input parsed with ParseUint(..., 32) reaching PortFromInt.

Related errors


AI-assisted analysis of XTLS/Xray-core@7d214f8b09 (2026-08-15). Data as JSON: /api/errors/f00a1dbca024cebd. Report an issue: GitHub.