Skip to content

feat(android): add udpgw_server parameter to JNI run() - #247

Closed
yyoyoian-pixel wants to merge 1 commit into
tun2proxy:masterfrom
yyoyoian-pixel:feat/udpgw-jni-param
Closed

yyoyoian-pixel wants to merge 1 commit into
tun2proxy:masterfrom
yyoyoian-pixel:feat/udpgw-jni-param

Conversation

@yyoyoian-pixel

Copy link
Copy Markdown

Summary

Adds an optional udpgw_server JString parameter to the Android JNI run() function, enabling Android apps to configure UDP forwarding via the existing udpgw support without resorting to tun2proxy_run_with_cli_args or forking the crate.

Motivation

Android VPN apps using tun2proxy via JNI currently have no way to pass --udpgw-server to the run() entry point. The only options are:

  1. Call tun2proxy_run_with_cli_args via dlsym — fragile, and the force-exit timeout thread kills the Android process
  2. Fork/vendor tun2proxy — maintainers of downstream projects (e.g. MasterHttpRelayVPN-RUST) have explicitly rejected this pattern

Adding the parameter to the JNI function is the clean solution — 13 lines, no new dependencies, gated behind #[cfg(feature = "udpgw")].

Changes

src/android.rs:

  • Added udpgw_server: JString<'_> parameter to Java_com_github_shadowsocks_bg_Tun2proxy_run
  • When the udpgw feature is enabled and the string is non-empty, parses it as SocketAddr and calls args.udpgw_server(addr)
  • When the feature is disabled, the parameter is accepted but ignored

Use case

This enables UDP forwarding (DNS, QUIC, VoIP calls) through a tunnel-node's udpgw handler. The Android app passes something like "198.18.0.1:7300" as the udpgw server address, and tun2proxy opens a TCP connection to it through the SOCKS5 proxy, multiplexing all UDP flows via the udpgw wire protocol.

Compatibility

The JNI function signature changes (one additional parameter). Callers must update their Kotlin/Java external fun declarations to include the new udpgwServer: String parameter. Passing an empty string preserves the previous behavior.

@yyoyoian-pixel

Copy link
Copy Markdown
Author

@ssrlive can we have a review on this one please?

Add an optional `udpgw_server` JString parameter to the Android JNI
`run()` function. When non-empty (e.g. "198.18.0.1:7300"), tun2proxy
will forward UDP packets via the udpgw protocol through a TCP
connection to the specified address.

This enables Android VPN apps using tun2proxy to leverage the existing
udpgw support for UDP forwarding (DNS, QUIC, VoIP) without needing
to call `tun2proxy_run_with_cli_args` or fork the crate.

The parameter is gated behind `#[cfg(feature = "udpgw")]` — when the
feature is disabled, the extra JString argument is accepted but
ignored, keeping the JNI signature stable.
yyoyoian-pixel added a commit to yyoyoian-pixel/MasterHttpRelayVPN-RUST that referenced this pull request Apr 25, 2026
Use the idiomatic Rust [patch.crates-io] mechanism instead of a git
submodule. Points to yyoyoian-pixel/tun2proxy fork with the udpgw
JNI parameter patch (upstream PR: tun2proxy/tun2proxy#247).

Will be removed once upstream ships the change in tun2proxy >= 0.8.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@ssrlive

ssrlive commented Apr 26, 2026

Copy link
Copy Markdown
Member

I examined the tun2proxy_run_with_cli_args and Java_com_github_shadowsocks_bg_Tun2proxy_run APIs.
I found that the former offers users more customization options and greater flexibility; I am puzzled as to why you insist on choosing the latter. Theoretically, the latter's only advantages are its ability to output logs to the Logdog console and its reliance on a single .so file rather than two—features that were actually designed with me, the maintainer, in mind, and which general users need not concern themselves with.

therealaleph pushed a commit to therealaleph/MasterHttpRelayVPN-RUST that referenced this pull request Apr 26, 2026
…table VoIP, faster browsing - needs new tunnel deployment for udpgw (#222)

* feat: native udpgw protocol alongside existing UDP associate

Why udpgw is needed even with UDP associate:

UDP associate (udp_open/udp_data) creates one tunnel session per UDP
destination and polls each independently. On high-latency or shaky
networks this compounds — N simultaneous UDP flows need N separate
polling loops, each paying its own batch round-trip overhead. Google
Meet calls, which fire dozens of concurrent STUN + RTP flows, stall
or fail entirely because the per-destination polling can't keep up.

udpgw multiplexes ALL UDP over one persistent TCP-like session using
conn_id framing. One batch op carries frames for many destinations.
Persistent sockets per (conn_id, dest) with continuous reader tasks
keep source ports stable — critical for protocols like Telegram VoIP
and STUN that expect replies on the same port.

Both paths coexist — they serve different traffic:
  - UDP associate (SOCKS5): apps that negotiate SOCKS5 UDP relay
  - udpgw (198.18.0.1:7300): TUN-captured UDP (DNS, QUIC, Meet, etc.)

tun2proxy vendored as git submodule at v0.7.20 with one transparent
commit adding udpgw_server to the Android JNI run() function.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: block QUIC (UDP 443) and DNS (UDP 53) from udpgw

QUIC through udpgw is slower than TCP/HTTP2 through the batch pipeline
— blocking it forces browsers to fall back to TCP, improving YouTube
and general browsing speed.

DNS is better handled by tun2proxy's virtual DNS / SOCKS5 UDP associate
path which is more reliable for single request-response exchanges.

VoIP (Telegram, Meet) still flows through udpgw normally.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: replace submodule with [patch.crates-io] for tun2proxy udpgw

Use the idiomatic Rust [patch.crates-io] mechanism instead of a git
submodule. Points to yyoyoian-pixel/tun2proxy fork with the udpgw
JNI parameter patch (upstream PR: tun2proxy/tun2proxy#247).

Will be removed once upstream ships the change in tun2proxy >= 0.8.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* chore: pin tun2proxy patch SHA in Cargo.lock

Locks tun2proxy at dfc24ed1 so the patch resolution is recorded and
any branch rewrite is visible in the lockfile diff.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: use AbortHandle for ConnSocket readers to prevent FD leaks

JoinHandle::drop detaches the task without aborting it. When
udpgw_server_task is cancelled (session close), the post-loop
cleanup never runs and per-(conn_id, dest) reader tasks become
zombies holding Arc<UdpSocket> file descriptors.

AbortHandle::drop aborts the task automatically, so cleanup is
correct by construction regardless of how the parent task exits.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

---------

Co-authored-by: yyoyoian-pixel <[email protected]>
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
@yyoyoian-pixel

Copy link
Copy Markdown
Author

@ssrlive right, i didn't know we already have support for that, i'm testing and if all works will close. thanks!

@yyoyoian-pixel

Copy link
Copy Markdown
Author

thanks, cli args working.

@ssrlive

ssrlive commented Apr 27, 2026 •

Copy link
Copy Markdown
Member

Hi @yyoyoian-pixel ,
I have refactored the Java_com_github_shadowsocks_bg_Tun2proxy_run function; it now shares the same semantics as tun2proxy_run_with_cli_args.
Please let me know if you have time to test it out.
Since this is a breaking change, I will be releasing it as a new major version: 0.8.x.

b2c3659

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.

2 participants