A React Native module for implementing WireGuard VPN functionality in iOS and Android applications. This package provides a native implementation of WireGuard tunneling with a simple JavaScript interface.
- React Native >= 0.72.0
- iOS 12.0 or later
- Android API level 21 (Android 5.0) or later
- CocoaPods for iOS development
# Using npm
npm install react-native-wireguard-vpn --save
# Using yarn
yarn add react-native-wireguard-vpn
- Install CocoaPods dependencies:
cd ios && pod install && cd ..
- Add the following entries to your Info.plist:
<key>NSLocalNetworkUsageDescription</key>
<string>This app requires access to network features for VPN functionality</string>
<key>UIBackgroundModes</key>
<array>
<string>network-authentication</string>
<string>network</string>
</array>
- Add Network Extension capability to your iOS project:
- Open your project in Xcode
- Select your target
- Go to "Signing & Capabilities"
- Click "+" and add "Network Extensions"
- Enable "Packet Tunnel" under Network Extensions
- Add the following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.BIND_VPN_SERVICE" />
- Update your app's build.gradle to ensure compatibility:
android {
compileSdkVersion 34
defaultConfig {
minSdkVersion 21
targetSdkVersion 34
}
}
import WireGuardVpnModule, {
WireGuardConfig,
WireGuardStatus
} from 'react-native-wireguard-vpn';
// Configuration object
const config: WireGuardConfig = {
privateKey: 'YOUR_PRIVATE_KEY',
publicKey: 'SERVER_PUBLIC_KEY',
serverAddress: 'SERVER_IP',
serverPort: 51820,
allowedIPs: ['0.0.0.0/0'],
dns: ['1.1.1.1'],
mtu: 1450,
presharedKey: 'OPTIONAL_PRESHARED_KEY' // Optional
};
// Initialize VPN service
await WireGuardVpnModule.initialize();
// Connect to VPN
try {
await WireGuardVpnModule.connect(config);
console.log('Connected successfully');
} catch (error) {
console.error('Connection failed:', error);
}
// Check VPN status
const status: WireGuardStatus = await WireGuardVpnModule.getStatus();
console.log('VPN Status:', status);
// Disconnect from VPN
await WireGuardVpnModule.disconnect();
Initializes the VPN service. Must be called before any other operations.
Connects to the VPN using the provided configuration.
Disconnects from the VPN.
Gets the current VPN connection status.
Checks if WireGuard VPN is supported on the device.
interface WireGuardConfig {
privateKey: string;
publicKey: string;
serverAddress: string;
serverPort: number;
allowedIPs: string[];
dns?: string[];
mtu?: number;
presharedKey?: string;
}
interface WireGuardStatus {
isConnected: boolean;
tunnelState: 'ACTIVE' | 'INACTIVE' | 'ERROR';
error?: string;
}
- Ensure your app has the necessary permissions granted
- Check logcat for detailed error messages
- Make sure the VPN service is properly declared in AndroidManifest.xml
- Verify Network Extension capability is properly configured
- Check Xcode logs for detailed error messages
- Ensure proper signing and provisioning profiles are set up
Check out the example app in our GitHub repository for a complete implementation.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
For bugs and feature requests, please create an issue on our GitHub repository.