{"record":{"id":"3255efebc47fac14","repo":"XTLS/Xray-core","slug":"invalid-port-range-3255ef","errorCode":null,"errorMessage":"invalid port range {} -> {}","messagePattern":"invalid port range (.+?) -> (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"infra/conf/common.go","lineNumber":204,"sourceCode":"\t\treturn fmt.Sprintf(\"%d-%d\", port.From, port.To)\n\t}\n}\n\n// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON\nfunc (v *PortRange) UnmarshalJSON(data []byte) error {\n\tport, err := parseIntPort(data)\n\tif err == nil {\n\t\tv.From = uint32(port)\n\t\tv.To = uint32(port)\n\t\treturn nil\n\t}\n\n\tfrom, to, err := parseJSONStringPort(data)\n\tif err == nil {\n\t\tv.From = uint32(from)\n\t\tv.To = uint32(to)\n\t\tif v.From > v.To || v.To > math.MaxUint16 {\n\t\t\treturn errors.New(\"invalid port range \", v.From, \" -> \", v.To)\n\t\t}\n\t\treturn nil\n\t}\n\n\treturn errors.New(\"invalid port range: \", string(data))\n}\n\ntype PortList struct {\n\tRange []PortRange\n}\n\nfunc (list *PortList) Build() *net.PortList {\n\tportList := new(net.PortList)\n\tfor _, r := range list.Range {\n\t\tportList.Range = append(portList.Range, r.Build())\n\t}\n\treturn portList\n}","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/infra/conf/common.go#L186-L222","documentation":"Thrown by PortRange.UnmarshalJSON in infra/conf/common.go when a string-form port range parses but is inconsistent: either From > To (reversed range like \"9000-8000\") or To exceeds 65535 (the max TCP/UDP port). The message interpolates the concrete From -> To values.","triggerScenarios":"\"port\": \"9000-8000\" (reversed), \"port\": \"1-70000\" (upper bound over MaxUint16), or an env: variable resolving to such a range. Both parseIntPort (single int) and parseJSONStringPort succeed at parsing, then the consistency check fails.","commonSituations":"Port scanners / proxy chains configured with large ephemeral ranges (e.g. 49152-65535 is fine but typos like 65635 appear); reversed ranges from copy-paste; env vars like $REMOTE_PORT_RANGE misconfigured.","solutions":["Fix the range so From <= To and To <= 65535: \"port\": \"8000-9000\"","If you meant a single port, use a plain integer: \"port\": 443","For env-driven ranges, print the resolved variable and correct it"],"exampleFix":"// before\n\"port\": \"9000-8000\"\n\n// after\n\"port\": \"8000-9000\"","handlingStrategy":"validation","validationCode":"func validatePortRange(from, to int) error {\n    if from > to {\n        return fmt.Errorf(\"range reversed: %d -> %d\", from, to)\n    }\n    if to > 65535 || from < 1 {\n        return fmt.Errorf(\"ports must be 1-65535, got %d-%d\", from, to)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := json.Unmarshal(data, &pr); err != nil {\n    if strings.Contains(err.Error(), \"invalid port range\") {\n        return fmt.Errorf(\"port range %s: ensure from <= to and to <= 65535\", data)\n    }\n    return err\n}","preventionTips":["Always write ranges low-high","Double-check the upper bound against 65535 when copying ephemeral ranges"],"tags":["config","port","validation","xray"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}