v2rayA/v2rayA · error

ports duplicate. check it

Error message

ports duplicate. check it

What it means

SetPorts updates global listening port settings (Socks5, Http, Socks2/Http2, Vmess etc.). It inserts every non-zero requested port into a set and, if the count of non-zero ports exceeds the set size, at least two ports are equal and it rejects the change with 'ports duplicate. check it'.

Source

Thrown at service/server/service/ports.go:42

	}
	if ports.Http != 0 {
		set[ports.Http] = struct{}{}
		cnt++
	}
	if ports.Socks5WithPac != 0 {
		set[ports.Socks5WithPac] = struct{}{}
		cnt++
	}
	if ports.HttpWithPac != 0 {
		set[ports.HttpWithPac] = struct{}{}
		cnt++
	}
	if ports.Vmess != 0 {
		set[ports.Vmess] = struct{}{}
		cnt++
	}
	if cnt > len(set) {
		return fmt.Errorf("ports duplicate. check it")
	}
	detectSyntax := make([]string, 0)
	if ports.Socks5 != origin.Socks5 {
		origin.Socks5 = ports.Socks5
		if origin.Socks5 != 0 {
			detectSyntax = append(detectSyntax, strconv.Itoa(origin.Socks5)+":tcp,udp")
		}
	}
	if ports.Http != origin.Http {
		origin.Http = ports.Http
		if origin.Http != 0 {
			detectSyntax = append(detectSyntax, strconv.Itoa(origin.Http)+":tcp")
		}
	}
	if ports.Socks5WithPac != origin.Socks5WithPac {
		origin.Socks5WithPac = ports.Socks5WithPac
		if origin.Socks5WithPac != 0 {
			detectSyntax = append(detectSyntax, strconv.Itoa(origin.Socks5WithPac)+":tcp,udp")

View on GitHub (pinned to 71e5442fc5)

Solutions

  1. Ensure every non-zero port field in the request has a distinct value
  2. Set unused protocol fields to 0 (0 means disabled and is exempt from the duplicate check)
  3. Check the current origin settings and pick non-conflicting ports

Example fix

// before
{"socks5":1080,"http":1080,"vmess":0} // duplicate
// after
{"socks5":1080,"http":8080,"vmess":0}
Defensive patterns

Strategy: validation

Validate before calling

func hasDuplicatePorts(p configure.Ports) bool {
    set := map[int]struct{}{}
    cnt := 0
    for _, v := range []int{p.Socks5, p.Http, p.Socks2, p.Http2, p.Vmess} {
        if v != 0 {
            set[v] = struct{}{}
            cnt++
        }
    }
    return cnt > len(set)
}
if hasDuplicatePorts(req.Ports) {
    return fmt.Errorf("ports must be unique (0 disables a listener)")
}

Try / catch

err := service.SetPorts(ports)
if err != nil && strings.Contains(err.Error(), "ports duplicate") {
    return fmt.Errorf("please give each protocol a distinct port")
}

Prevention

When it happens

Trigger: Calling SetPorts (via PutPorts API) with a Ports object where two or more non-zero fields share the same port number, e.g. Socks5 == Http == 1080, or Vmess equals Socks5.

Common situations: Frontend form letting users enter the same port twice; clients copying one port into all fields; defaults colliding with a user-entered port.

Related errors


AI-assisted analysis of v2rayA/v2rayA@71e5442fc5 (2026-09-05). Data as JSON: /api/errors/4c00f1f1abaade1a. Report an issue: GitHub.