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.

  1. Upload server/hole-punch.php to your web root, for example: https://your-domain.example/hole-punch.php.
  2. 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.
  3. By default, the endpoint creates a private subdirectory of PHP's temporary directory. If your host restricts it, set HOLE_PUNCH_DATA_DIR at 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.
  4. 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.

  1. Upload the PHP HTTPS endpoint as described above.
  2. 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.
  3. 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.
  4. 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

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.