Maintaining a stable VPN connection on mobile devices is critical for site administrators, developers, and enterprise users who rely on uninterrupted access to remote resources and secure channels. Trojan, a high-performance, TLS-based proxy protocol, is widely used to bypass censorship and secure traffic. However, mobile networks are inherently unstable: switching between Wi‑Fi and cellular, aggressive OS-level process killing, and short TCP timeouts can all interrupt a Trojan session. This article explains practical, technical methods to configure Trojan VPN clients on Android and iOS for robust, automatic reconnection behavior, including client-side settings, OS configuration, keepalive strategies, and diagnostic techniques.
Understanding why mobile VPNs drop and how Trojan works
Before diving into configurations, it’s important to understand the failure modes that cause VPN disconnects on mobile devices. Common reasons include:
- Network transitions (Wi‑Fi ↔︎ 4G/5G) cause IP address changes and break TCP/TLS sessions.
- Mobile OS battery optimizations put background apps to sleep or restrict network access.
- Short TCP keepalive and idle timeouts on cellular carriers or NATs.
- TLS session expiration or handshake failures when servers rotate certificates or use short ticket lifetimes.
- Application-level bugs or crashes in the Trojan client.
Trojan uses TLS as its transport, often over TCP or WebSocket. Reliable reconnection strategies must therefore address both transport layer (TCP/TLS) and application layer (reestablishing the proxy connection and resuming routing rules).
Core strategies for automatic reconnection
Implementing an effective auto-reconnect strategy requires several coordinated elements:
- Short, regular keepalive pings—to prevent NAT timeouts and detect dead peers quickly.
- Rapid, adaptive reconnection attempts—using exponential backoff to avoid tight retry loops that waste battery and bandwidth.
- OS-level configuration—to avoid app termination and allow background network access.
- Fallback endpoints and DNS strategies—to handle server failover and avoid dependency on a single resolver.
Heartbeat / keepalive configuration
For Trojan clients that support custom keepalive parameters (or when using a wrapper such as trojan‑go or client apps with WebSocket support), configure a periodic ping. Typical settings:
- Interval: 15–60 seconds (shorter intervals detect failures faster but consume more power).
- Payload: minimal heartbeat frame or application-level ping such as an HTTP GET to a lightweight path when using WebSocket.
- TCP keepalive: enable and set SO_KEEPALIVE with probes every 30–60 seconds; on Android this is often controlled by the app.
Example WebSocket ping pseudo-config (trojan-go style):
<pre>{
“transport”: {
“type”: “ws”,
“path”: “/ws”,
“headers”: {
“Host”: “example.com”
},
“pingInterval”: 30
}
}</pre>
Note: exact JSON keys vary by client. The objective is to ensure the client sends regular WebSocket pings or application-level keepalive frames.
Adaptive reconnection logic
An effective reconnect algorithm balances user experience and resource usage. Recommended pattern:
- On disconnect detection, immediately attempt a reconnection.
- If reconnection fails, retry with an exponential backoff: 1s, 2s, 4s, 8s, up to a ceiling (e.g., 60–120s).
- Reset backoff to the minimum after a successful connection.
- Limit retries per minute to prevent carrier network throttling and avoid draining battery.
Clients should also detect transient errors (DNS failure, network change) and run a short sequence of DNS refreshes and network checks before repeated reconnection attempts.
Android: practical steps to keep Trojan clients running and reconnecting
Android offers flexible controls but also aggressive battery-saving features. Follow these steps for reliable operation.
Use a client that supports Always‑On VPN or foreground service
- If the Trojan client is implemented using Android’s VPNService API (like some Trojan/V2Ray wrappers), enable the system “Always‑On VPN” to prevent disconnects on network change and to force VPN traffic through the client.
- If Always‑On is not available, ensure the client runs a foreground service with a persistent notification. Foreground services are much less likely to be killed by the system.
Disable battery optimizations and background restrictions
- In Settings → Apps → [Your Trojan app] → Battery, choose “Unrestricted” or “Don’t optimize”.
- Disable data saver restrictions for the app so it can access data in the background.
Grant autostart and network permissions
On many OEM skins (Xiaomi, Huawei, Oppo), additionally enable autostart for the app and allow background execution. These settings are necessary to survive reboots and aggressive process management.
Configure DNS fallback and IPv6 handling
- Use a client option to specify multiple DNS servers, including a public IPv4 DNS and an IPv6 resolver if your carrier supports IPv6. This prevents DNS resolution failures during network transitions.
- If you encounter mixed-stack routing issues, test disabling IPv6 in the client or server to force consistent behavior.
Example Android client settings checklist
- Enable WebSocket transport with pingInterval=30s
- Set reconnection strategy: initialDelay=1s, maxDelay=60s, maxAttempts=0 (infinite) with backoff
- Allow background data and remove battery optimization
- Enable Always‑On VPN or foreground service
iOS: constraints, options, and recommended configurations
iOS imposes stricter background limits and lacks the same level of user control as Android. Nevertheless, enterprise and advanced users can achieve robust reconnection using the right apps and profile settings.
Use a reputable client with Auto‑Reconnect and Connect‑On‑Demand
Because Trojan is not a built-in protocol on iOS, you must use a third‑party client such as Shadowrocket, Quantumult, or custom apps that support Trojan/Trojan‑GO. Look for features:
- Auto‑Reconnect on network change
- Background reconnect capability
- Support for TLS session resumption and certificate pinning
Configuration profile with Connect On Demand (for IKEv2-like clients)
For enterprise deployments, consider deploying a configuration profile (MDM or Apple Configurator) that sets up VPN with “Connect On Demand” rules. While this is native to IKEv2 and not Trojan, you can combine a native IKEv2 tunnel for baseline connectivity and a Trojan client for proxying when available. The Connect On Demand rules can bring up VPN automatically when certain domains are accessed.
Use App-level settings for quick detection
- Enable network change detection and set short connection check intervals (e.g., 20–30s).
- Configure multiple servers and fallback domains within the app to avoid DNS-based downtime.
Server-side and TLS considerations to improve reconnection success
Client-side tweaks are necessary but server-side hardening reduces failed reconnections:
TLS session resumption and ticket lifetime
- Enable TLS session tickets and extend ticket lifetime reasonably to allow clients to resume sessions after short network outages.
- Implement OCSP stapling to reduce handshake time.
Multiple endpoints and DNS strategy
- Publish multiple A/AAAA records behind your domain to provide automatic server failover.
- Use short DNS TTLs for rapid IP rotation if you expect to change server IPs, but balance with caching behaviors of mobile carriers.
WebSocket vs TCP transport
WebSocket transport (over TLS) is friendlier to NAT and middleboxes and supports application-level pings. For mobile clients, WebSocket + pingInterval is often the most reliable combination. TCP works too, but you must set TCP keepalive aggressively.
Troubleshooting and monitoring reconnection behavior
When auto-reconnect doesn’t work as expected, gather the following diagnostics:
- Client logs showing disconnect reason (network unreachable, TLS handshake failure, DNS lookup failure, 0RTT rejected, etc.).
- Server-side logs for incoming TLS handshake or abandoned connections.
- Packet captures (where possible) to inspect keepalive and handshake timing.
- Test using different carriers and Wi‑Fi networks to reproduce the issue.
Key checks:
- Is the client restarting or being killed by the OS? Check process/service uptime.
- Are TLS errors occurring (certificate mismatch, expired ticket)?
- Does switching from Wi‑Fi to cellular cause a clean reconnect attempt?
Advanced automation: Tasker, Automate, Shortcuts
For power users and enterprise deployments that need deterministic behavior, piggyback on automation tools:
Android: Tasker/Automate
- Create a profile that triggers on network change and runs a shell command or Intent to restart the Trojan client.
- Monitor the client process; if it exits, Tasker can start it automatically and log the event.
iOS: Shortcuts and MDM
iOS restricts background automation, but MDM solutions can push profiles and scripts that enforce connection behavior. Use enterprise MDM for managed devices to guarantee reconnection policies.
Security considerations
Automatic reconnection must be implemented securely:
- Validate TLS certificates and consider certificate pinning to prevent man‑in‑the‑middle attacks during automatic reconnects.
- Rotate server keys carefully and ensure clients can retrieve updated certificates without human intervention (use OCSP and short but sufficient key lifetimes).
- Avoid storing plaintext credentials on the device; use secure storage APIs provided by Android/iOS.
Checklist: Putting it all together
- Choose a Trojan client that supports WebSocket/TCP keepalives, auto-reconnect, and foreground operation.
- Configure keepalive interval (20–60s) and TCP keepalive options if available.
- Enable adaptive reconnection with exponential backoff.
- On Android, enable Always‑On VPN or foreground service, disable battery optimizations, and allow autostart.
- On iOS, use a third‑party client with auto‑reconnect and consider MDM profiles for managed fleets.
- Harden servers: enable TLS session resumption, multiple endpoints, and choose transport (WebSocket) that is resilient to NAT timeouts.
- Monitor logs and implement automated restart policies via Tasker/MDM where appropriate.
By combining application-level keepalives, adaptive reconnection logic, careful OS configuration, and server-side tuning, you can achieve a resilient Trojan VPN deployment on mobile devices that “never loses connection” in practice. For enterprise and managed-device scenarios, integrate these steps into your provisioning and MDM workflows to ensure consistent behavior across fleets.
For configuration examples, advanced guides, and commercial-grade dedicated IP options that work well with Trojan and mobile clients, visit Dedicated-IP-VPN at https://dedicated-ip-vpn.com/.