React Native WireGuard VPN package for iOS and Android
npm install react-native-wireguard-vpn-connectA 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
``bashUsing npm
npm install react-native-wireguard-vpn --save
$3
1. Install CocoaPods dependencies:
`bash
cd ios && pod install && cd ..
`2. Add the following entries to your Info.plist:
`xml
NSLocalNetworkUsageDescription
This app requires access to network features for VPN functionality
UIBackgroundModes
network-authentication
network
`3. 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
$3
1. Add the following permissions to your AndroidManifest.xml:
`xml
`2. Update your app's build.gradle to ensure compatibility:
`gradle
android {
compileSdkVersion 34
defaultConfig {
minSdkVersion 21
targetSdkVersion 34
}
}
`Usage
`typescript
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();
`API Reference
$3
####
initialize(): Promise
Initializes the VPN service. Must be called before any other operations.####
connect(config: WireGuardConfig): Promise
Connects to the VPN using the provided configuration.####
disconnect(): Promise
Disconnects from the VPN.####
getStatus(): Promise
Gets the current VPN connection status.####
isSupported(): Promise
Checks if WireGuard VPN is supported on the device.$3
`typescript
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;
}
``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.