pHolePunch
A small Android Java app using public STUN servers and an ordinary PHP HTTPS page. No third-party app libraries. Two phones enter the same PHP URL and shared secret, discover each other, open a direct UDP path, and exchange encrypted text messages. Your PHP host only exchanges connection metadata; it never relays chat messages.
Server requirements
Ordinary PHP 8.0+ hosting with HTTPS, a valid certificate, and a writable private directory is sufficient. No PHP CLI service, VPS, UDP listener, database, or PHP sockets extension is needed. The phones contact public UDP STUN servers to discover their public IP/port; PHP stores those reported endpoints and introduces matching peers.
- Upload
server/hole-punch.phpto your web root, for example:https://your-domain.example/hole-punch.php. - Open that URL in a browser. It should return a small JSON service/version response. This verifies PHP is running; registration additionally needs writable private storage.
- By default, the endpoint creates a private subdirectory of PHP's temporary directory.
If your host restricts it, set
HOLE_PUNCH_DATA_DIRat the top of the script to an absolute writable directory outside your web root, such as/home/your-account/private/hole-punch. All PHP workers must share that directory. - Use the exact final HTTPS URL in the app. Redirects are disabled. Make sure hosting bot challenges do not intercept this endpoint's JSON POST requests.
The registry contains room discovery hashes and endpoint metadata, never shared secrets or chat messages. Sessions expire after 45 seconds without registration and are pruned on subsequent registrations. Concurrent requests use a file lock. Rooms admit two peers and the registry is capped at 1,000 rooms.
Build and run
Open this folder in Android Studio. Use JDK 17 and Android SDK 35. The project uses Android Gradle Plugin 8.9.3 and Gradle 8.11.1. Android 8.0 (API 26) or newer is required.
./gradlew assembleDebug
Windows: gradlew.bat assembleDebug. The APK is at
app/build/outputs/apk/debug/app-debug.apk. Install the same APK on both phones.
- Upload the PHP HTTPS endpoint as described above.
- Enter its full HTTPS URL and the same strong, randomly generated secret on both phones. Use at least 16 characters; 32 random characters is better. Only two active peers can use one secret. STUN servers are configured automatically.
- Tap Connect on each phone. Keep both apps open. Once Direct connection appears, send messages. Messages show pending and delivered states, with up to eight attempts.
- To verify direct traffic, temporarily make the PHP URL unavailable after connection; chat should continue. New connections still need discovery. Disconnect/reconnect after either phone changes networks or restarts its session.
The current testing build prefills https://zcxv.cc/fm/hole-punch.php and the shared
secret 1234123412341234 on launch. Both fields remain editable. These are public test
defaults; replace the secret for private conversations. The app does not save edits
to these fields between launches.
Test your network
Install version 1.2 on both phones, connect, then tap Test network (binary payloads) on one phone. The other phone automatically receives and acknowledges probes. Keep both apps open; run the test from the other phone afterward to sample both directions.
The test sends 10 randomly generated encrypted binary probes at each exact UDP payload size: 256, 512, 1000, 1200, 1400, 2048 bytes. Those sizes include encryption and protocol metadata, but exclude UDP/IP headers. Binary content is 81 bytes smaller than the listed packet size. Acknowledgements are 113 UDP bytes and echo a SHA-256 digest of the received binary content. Payloads include zero bytes and non-text byte values.
Each probe is sent once, at least 150 ms apart, with a one-second acknowledgement deadline. Results show ACKs out of 10, average round-trip time for acknowledged probes, and local send errors. A missing ACK may mean the probe or its acknowledgement was lost, delayed, or rejected; it is not a precise one-way loss rate. Ten successes sample delivery and do not establish a path MTU. Large probes may undergo IP fragmentation. This is not a bandwidth benchmark. The button becomes Cancel network test while running. A successful test takes roughly 10–15 seconds; missing ACKs can extend it to about a minute. Total probe content on the network is modest (about 64 KB plus ACKs).
For example, UDP 1200 B (1119 binary bytes): 10/10 ACKs, avg RTT 22.0 ms means all
10 requests at that size were received with matching content and acknowledged within
one second. The test uses only the established direct socket; HTTP and STUN remain off.
Text chat continues to work during the test. Arbitrary file transfers and reassembly
are not implemented.
Connection design
- Each phone generates a new session ID and opens one UDP socket. A native Java STUN
Binding client queries Cloudflare
stun.cloudflare.com:3478, with Googlestun.l.google.com:19302as a fallback. STUN uses the same socket as chat. Responses must match the server address, magic cookie, and random transaction ID; IPv4 XOR-MAPPED-ADDRESS is supported, with MAPPED-ADDRESS as a compatibility fallback. Included STUN fingerprints are validated. See RFC 8489, Cloudflare's service documentation, and WebRTC's Google STUN example. - While connecting, the phone POSTs its public UDP endpoint and up to eight private interface addresses to PHP every two seconds. The response contains the matching peer or a waiting state. The public endpoint is refreshed through STUN every 15 seconds. PHP preserves the STUN endpoint; an HTTP source port is a TCP port, not the UDP port. A separate HTTP worker keeps three-second HTTP timeouts off the UDP handshake loop.
- Each phone sends encrypted hello packets to the public and local endpoints. The local endpoints support same-Wi-Fi connections when the router cannot loop traffic through its public address. Wi-Fi client isolation may still prevent this.
- A hello acknowledgement verifies bidirectional communication. The winning endpoint carries all subsequent messages, acknowledgements, and three-second keepalives. HTTP polling and STUN requests stop when the direct connection succeeds; any in-flight HTTP request is cancelled. Direct keepalives maintain the selected peer path.
- Local addresses are exchanged even before STUN succeeds, allowing same-Wi-Fi peers to try connecting if public STUN is unavailable.
- Messages use platform AES-256-GCM with random 96-bit nonces. Keys are derived from the shared secret using SHA-256 with separate prefixes for discovery and encryption. Peer session IDs are authenticated inside each encrypted packet. The server receives a discovery hash rather than the secret or the encryption key. Message IDs suppress duplicate display for the most recent 1,024 incoming messages.
- Connect attempts end after 90 seconds. An established peer that stops responding for 20 seconds ends the session. There is no automatic relay or background service.
Limits
This is a small foreground chat prototype, not a full ICE implementation. IPv4 only. UDP blocking, restrictive/symmetric NAT, carrier-grade NAT, or Wi-Fi isolation can prevent a direct connection. No server-free technique can guarantee reachability on all networks; this app reports failure and never falls back to a relay. See RFC 5128.
Use a high-entropy secret: anyone who knows it can join the room, and weak secrets can be guessed offline from the discovery hash. Discovery metadata is protected in transit by HTTPS, but your PHP host can see it and can disrupt discovery. STUN providers see the UDP source endpoint. Neither receives direct chat payloads. Public STUN availability is an external dependency and is not guaranteed. Add hosting request rate limits if you expose this endpoint widely. Delivery acknowledgements are bounded retries, not a durable ordered messaging system. A missing acknowledgement does not prove the message was not delivered. Apps do not store message history or secrets.
Validation
See VALIDATION.md for checks actually performed in this workspace and the remaining
two-device NAT test. A localhost test cannot establish that a particular mobile carrier
or router will permit hole punching.