-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
159 lines (146 loc) · 3.94 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { useState, useEffect } from 'react';
import {
SafeAreaView,
StyleSheet,
View,
Text,
TouchableOpacity,
Alert,
} from 'react-native';
import { WireGuardVpnModule } from 'react-native-wireguard-vpn';
const App = () => {
const [isConnected, setIsConnected] = useState(false);
const [status, setStatus] = useState('');
useEffect(() => {
initializeVPN();
}, []);
const initializeVPN = async () => {
try {
await WireGuardVpnModule.initialize();
console.log('VPN initialized successfully');
} catch (error) {
console.error('VPN initialization failed:', error);
Alert.alert('Error', 'Failed to initialize VPN');
}
};
const connectVPN = async () => {
try {
const config = {
privateKey: 'YOUR_PRIVATE_KEY', // Replace with your private key
publicKey: 'YOUR_SERVER_PUBLIC_KEY', // Replace with your server's public key
serverAddress: 'YOUR_SERVER_ADDRESS', // Replace with your server address
serverPort: 51820,
allowedIPs: ['0.0.0.0/0'],
dns: ['8.8.8.8'],
mtu: 1420,
};
await WireGuardVpnModule.connect(config);
setIsConnected(true);
Alert.alert('Success', 'VPN connected successfully');
} catch (error) {
console.error('VPN connection failed:', error);
Alert.alert('Error', 'Failed to connect to VPN');
}
};
const disconnectVPN = async () => {
try {
await WireGuardVpnModule.disconnect();
setIsConnected(false);
Alert.alert('Success', 'VPN disconnected successfully');
} catch (error) {
console.error('VPN disconnection failed:', error);
Alert.alert('Error', 'Failed to disconnect VPN');
}
};
const checkStatus = async () => {
try {
const vpnStatus = await WireGuardVpnModule.getStatus();
setStatus(JSON.stringify(vpnStatus, null, 2));
console.log('VPN Status:', vpnStatus);
} catch (error) {
console.error('Failed to get VPN status:', error);
Alert.alert('Error', 'Failed to get VPN status');
}
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.content}>
<Text style={styles.title}>WireGuard VPN Test</Text>
<TouchableOpacity
style={[styles.button, isConnected ? styles.disconnectButton : styles.connectButton]}
onPress={isConnected ? disconnectVPN : connectVPN}>
<Text style={styles.buttonText}>
{isConnected ? 'Disconnect VPN' : 'Connect VPN'}
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={checkStatus}>
<Text style={styles.buttonText}>Check Status</Text>
</TouchableOpacity>
{status ? (
<View style={styles.statusContainer}>
<Text style={styles.statusTitle}>VPN Status:</Text>
<Text style={styles.statusText}>{status}</Text>
</View>
) : null}
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f5f5',
},
content: {
flex: 1,
padding: 20,
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 30,
color: '#333',
},
button: {
backgroundColor: '#007AFF',
paddingHorizontal: 20,
paddingVertical: 12,
borderRadius: 8,
marginVertical: 10,
width: '80%',
},
connectButton: {
backgroundColor: '#34C759',
},
disconnectButton: {
backgroundColor: '#FF3B30',
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
textAlign: 'center',
},
statusContainer: {
marginTop: 20,
padding: 15,
backgroundColor: '#fff',
borderRadius: 8,
width: '90%',
},
statusTitle: {
fontSize: 16,
fontWeight: '600',
marginBottom: 10,
color: '#333',
},
statusText: {
fontSize: 14,
color: '#666',
},
});
export default App;