Skip to content

feat(socks5): per-CONNECT upstream routing (route_target) - #121

Merged
Antruly merged 2 commits into
masterfrom
feat/socks5-route-upstream
Nov 28, 2026
Merged

Antruly merged 2 commits into
masterfrom
feat/socks5-route-upstream

Conversation

@Antruly

@Antruly Antruly commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Motivation

A SOCKS5 server could only allow or deny a target (allow_target); it had no way to change where a session egresses. uvscope needs a per-destination decision: dial directly, or hand the connection to an upstream proxy (mainland direct / overseas through a proxy).

What

uvcpp_socks5_server_options::route_target:

std::function<bool(const uvcpp_socks5_endpoint& target,
                   uvcpp_socks5_upstream* upstream)> route_target;
  • Consulted once per CONNECT with the requested target.
  • true + a filled uvcpp_socks5_upstream{host, port, username, password}: the session dials the upstream first, reusing uvcpp_tcp_client::set_socks5_proxy() for greeting / auth / CONNECT. The REP sent to the client comes from the final result.
  • false: the existing direct path, unchanged.
  • Domain targets stay ATYP=DOMAIN, so the upstream resolves them; a throwing callback is treated as direct.
  • BIND and UDP ASSOCIATE are unchanged; the hook is CONNECT-only.
  • The success reply's BND.ADDR is the local address toward the upstream, not toward the target.

Compatibility

Additive. A default-constructed std::function leaves every existing server byte-for-byte on the old path.

Tests

Built with -DUVCPP_BUILD_TESTS=ON -DUVCPP_BUILD_WEB=OFF -DUVCPP_ENABLE_OPENSSL=OFF:

[socks5-server] route_target_upstream start
[socks5-server] route_target_upstream ok
[socks5-server] ALL PASSED

The new case chains an echo through a second SOCKS5 server and then verifies the same server still dials directly when the hook returns false. test_socks5_client_func and test_socks5_udp_func also still pass.

Add uvcpp_socks5_server_options::route_target, consulted once per CONNECT
with the requested target. Returning true fills a uvcpp_socks5_upstream
(host/port/username/password) and the session dials the upstream first,
reusing uvcpp_tcp_client::set_socks5_proxy() for the greeting/auth/CONNECT
negotiation; the REP sent to the client comes from the final result.
Returning false keeps the existing direct path.

Domain targets stay ATYP=DOMAIN so the upstream resolves them, a throwing
callback is treated as "direct", and BIND / UDP ASSOCIATE are unchanged.
The success reply's BND.ADDR is the local address toward the upstream.

Tests: socks5_server_func gains route_target_upstream, which chains an
echo through a second SOCKS5 server and then checks that the same server
still dials directly when the hook returns false.
The config-contract gate compiles every cpp block that is not marked as a
fragment; the new example has no library include, so it failed the
'snippet belongs to a module' check on Linux and Windows.
@Antruly
Antruly force-pushed the feat/socks5-route-upstream branch from 8a6f166 to 495e546 Compare November 28, 2026 11:34
@Antruly
Antruly merged commit fe7c1a3 into master Nov 28, 2026
63 of 66 checks passed
Antruly added a commit to Antruly/uvscope that referenced this pull request Nov 28, 2026
Add the decision layer the proxy clients will share:

- src/routing.*: IP/CIDR parsing, range normalisation (sort, merge and
  adjacency) and binary-search containment. Pure logic, no I/O, so
  --self-check covers it offline.
- Built-in groups: mainland (APNIC CN allocations generated by
  scripts/gen-geo-cidrs.py and embedded at configure time from
  data/geo/cn-cidrs.txt), overseas (complement of mainland among public
  addresses) and loopback. Built-ins are read-only.
- Custom groups store CIDR text under the routing.groups setting; rules
  under routing.rules keep an ordered {groupId, action, proxy} list plus a
  default action. The first group containing the destination wins.
- API: GET /api/routing, group create/update/delete, PUT
  /api/routing/rules and POST /api/routing/match (admin.settings).
- UI: a new Address Groups page with built-in cards, custom group editing,
  ordered rule editing and a destination matcher.
- Docs: API.md, ARCHITECTURE.md, README, help page, CHANGELOG.
- Tests: self-check assertions for parsing, containment, the embedded
  mainland data, the overseas complement, rule precedence and rejection;
  smoke-test assertions for the full API round-trip and the read-only /
  referenced-group guards.

Upstream proxy credentials are not persisted yet; a rule that carries a
password is rejected with a clear error instead of dropping the secret.
Wiring the rules into the SOCKS5 server needs libuvcpp's route_target hook
(upstream PR Antruly/libuvcpp#121); until that ships, servers dial directly.
@Antruly Antruly mentioned this pull request Nov 28, 2026
6 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant