XTLS/Xray-core · error

Invalid Trojan port.

Error message

Invalid Trojan port.

What it means

Thrown when the Trojan server entry has a port of 0 — either the 'port' key is missing/null (defaults to 0) or is explicitly 0. Port 0 is treated as invalid for a Trojan endpoint.

Source

Thrown at infra/conf/trojan.go:68

				Level:    c.Level,
				Email:    c.Email,
				Password: c.Password,
				Flow:     c.Flow,
			},
		}
	}
	if len(c.Servers) != 1 {
		return nil, errors.New(`Trojan settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple Trojan outbounds and routing balancer instead`)
	}

	config := &trojan.ClientConfig{}

	for _, rec := range c.Servers {
		if rec.Address == nil {
			return nil, errors.New("Trojan server address is not set.")
		}
		if rec.Port == 0 {
			return nil, errors.New("Invalid Trojan port.")
		}
		if rec.Password == "" {
			return nil, errors.New("Trojan password is not specified.")
		}
		if rec.Flow != "" {
			return nil, errors.PrintRemovedFeatureError(`Flow for Trojan`, ``)
		}

		config.Server = &protocol.ServerEndpoint{
			Address: rec.Address.Build(),
			Port:    uint32(rec.Port),
			User: &protocol.User{
				Level: uint32(rec.Level),
				Email: rec.Email,
				Account: serial.ToTypedMessage(&trojan.Account{
					Password: rec.Password,
				}),
			},

View on GitHub (pinned to 7d214f8b09)

Solutions

  1. Set a valid TCP port 1-65535, e.g. "port": 443
  2. Make sure the value is a JSON number, not a string
  3. Check the server actually listens on that port (commonly 443)

Example fix

// before
{ "address": "trojan.example.com", "port": 0, "password": "secret" }
// after
{ "address": "trojan.example.com", "port": 443, "password": "secret" }
Defensive patterns

Strategy: validation

Validate before calling

port := gjson.Get(serverRaw, "port").Uint()
if port == 0 || port > 65535 {
    return fmt.Errorf("trojan server port must be 1-65535, got %d", port)
}

Prevention

When it happens

Trigger: Omitting "port" from the servers object, setting "port": 0, or a JSON string port ""443"" that fails to decode into the numeric field.

Common situations: Copy-paste from a share link where the port got lost, or quoting the port ("port": "443") so it never lands in the numeric Port field.

Related errors


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