Wei-Shaw/sub2api · error

proxy host is required

Error message

proxy host is required

What it means

Returned by validateDataProxy (backend/internal/handler/admin/account_data.go:659) when a proxy entry's host is empty after TrimSpace — the protocol was present but the hostname/IP is missing. Host is required even for schemes where it might seem optional; there is no default.

Source

Thrown at backend/internal/handler/admin/account_data.go:659

	}
	if payload.Version != 0 && payload.Version != dataVersion {
		return fmt.Errorf("unsupported data version: %d", payload.Version)
	}
	if payload.Proxies == nil {
		return errors.New("proxies is required")
	}
	if payload.Accounts == nil {
		return errors.New("accounts is required")
	}
	return nil
}

func validateDataProxy(item DataProxy) error {
	if strings.TrimSpace(item.Protocol) == "" {
		return errors.New("proxy protocol is required")
	}
	if strings.TrimSpace(item.Host) == "" {
		return errors.New("proxy host is required")
	}
	if item.Port <= 0 || item.Port > 65535 {
		return errors.New("proxy port is invalid")
	}
	switch item.Protocol {
	case "http", "https", "socks5", "socks5h":
	default:
		return fmt.Errorf("proxy protocol is invalid: %s", item.Protocol)
	}
	if item.Status != "" {
		normalizedStatus := normalizeProxyStatus(item.Status)
		if normalizedStatus != service.StatusActive && normalizedStatus != "inactive" {
			return fmt.Errorf("proxy status is invalid: %s", item.Status)
		}
	}
	return nil
}

View on GitHub (pinned to 073e92d171)

Solutions

  1. Set host to the proxy's hostname or IP on the entry.
  2. If your source stores a full URL, split it into protocol/host/port fields before import.
  3. Ensure no entry was truncated when copying JSON.

Example fix

// before
{ "protocol": "http", "port": 8080 }

// after
{ "protocol": "http", "host": "proxy.example.com", "port": 8080 }
Defensive patterns

Strategy: validation

Validate before calling

function hasProxyHost(entry: Record<string, unknown>): boolean {
  return typeof entry.host === 'string' && entry.host.trim() !== ''
}

Type guard

function isNonEmptyHost(h: unknown): h is string {
  return typeof h === 'string' && h.trim().length > 0 && /^[^:]+(:\d+)?$/.test(h.trim()) === false || (typeof h === 'string' && h.trim() !== '' && !h.includes(':'))
}

Prevention

When it happens

Trigger: A proxies[] entry with protocol and port but host absent, blank, or whitespace-only; URL parsing that put everything in a single field ('host': "socks5://127.0.0.1:1080" with port 0 does not hit this but a plain empty host does).

Common situations: Form where host field was skipped; data migrated from a format with a combined URL string; env-var-driven configs pasted with the host line missing.

Related errors


AI-assisted analysis of Wei-Shaw/sub2api@073e92d171 (2026-08-15). Data as JSON: /api/errors/f1e965d9beef8b26. Report an issue: GitHub.