-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
194 lines (184 loc) ยท 5.11 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import React, { useState } from 'react';
import {
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
View,
FlatList,
Dimensions,
} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import OpenVPN from 'react-native-simple-openvpn'; // Import the OpenVPN library
import RNFS from 'react-native-fs'; // Import react-native-fs to handle file system
const { height } = Dimensions.get('window');
const App: React.FC = () => {
const [isConnected, setIsConnected] = useState(false);
const [selectedServer, setSelectedServer] = useState('0'); // Default to Fastest
const servers = [
{ id: '0', name: '๐ Fastest' },
{ id: '1', name: '๐บ๐ธ United States' },
{ id: '2', name: '๐ฉ๐ช Germany' },
{ id: '3', name: '๐ฏ๐ต Japan' },
{ id: '4', name: '๐ฆ๐บ Australia' },
{ id: '5', name: '๐ฌ๐ง United Kingdom' },
{ id: '6', name: '๐จ๐ฆ Canada' },
{ id: '7', name: '๐ซ๐ท France' },
{ id: '8', name: '๐ง๐ท Brazil' },
{ id: '9', name: '๐ฎ๐น Italy' },
{ id: '10', name: '๐ช๐ธ Spain' },
{ id: '11', name: '๐ซ๐ฎ Finland' },
{ id: '12', name: '๐ณ๐ฑ Netherlands' },
{ id: '13', name: '๐ธ๐ช Sweden' },
{ id: '14', name: '๐ฒ๐ฝ Mexico' }
];
const renderServerItem = ({ item }: { item: typeof servers[0] }) => (
<TouchableOpacity
style={[
styles.serverItem,
selectedServer === item.id && styles.selectedServer,
// item.id !== '0' && styles.disabledServer, // Apply disabled style
]}
onPress={() => {
// Only allow selecting "Fastest"
// if (item.id === '0') {
// setSelectedServer(item.id);
// }
setSelectedServer(item.id);
}}
// disabled={item.id !== '0'} // Disable all options except Fastest
>
<Text style={styles.serverName}>{item.name}</Text>
</TouchableOpacity>
);
const renderHeader = () => (
<View style={styles.headerContainer}>
<Text style={styles.title}>VPN</Text>
<Text style={styles.statusText}>
{isConnected ? 'Connected to ' + (selectedServer ? servers.find(s => s.id === selectedServer)?.name : 'a server') : 'Disconnected'}
</Text>
</View>
);
const handleConnect = async () => {
console.log(RNFS.DocumentDirectoryPath);
// const filePath = `${RNFS.DocumentDirectoryPath}/your-config-file.ovpn`; // Adjust the path as necessary
try {
// const data = await RNFS.readFile(filePath, 'utf8');
// await OpenVPN.connect({ ovpn: data }); // Connect using the .ovpn file data
setIsConnected(true);
} catch (error) {
console.error('VPN Connection Error:', error);
}
};
const handleDisconnect = async () => {
try {
await OpenVPN.disconnect();
setIsConnected(false);
} catch (error) {
console.error('VPN Disconnection Error:', error);
}
};
return (
<LinearGradient
colors={['#0A0E21', '#1F1F3D']}
start={{ x: 0, y: 0 }}
end={{ x: 0, y: 1 }}
style={styles.gradient}
>
<SafeAreaView style={styles.container}>
{renderHeader()}
<FlatList
data={servers}
renderItem={renderServerItem}
keyExtractor={(item) => item.id}
contentContainerStyle={[styles.serverList, { paddingBottom: 250 }]}
/>
<View style={styles.buttonContainer}>
<TouchableOpacity
onPress={isConnected ? handleDisconnect : handleConnect}
style={[
styles.connectButton,
isConnected ? styles.connectedButton : styles.disconnectedButton,
]}
>
<Text style={styles.connectButtonText}>
{isConnected ? '๐' : '๐'}
</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
</LinearGradient>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 0,
padding: 20,
},
gradient: {
flex: 1,
width: '100%',
},
headerContainer: {
alignItems: 'center',
paddingVertical: 30,
},
title: {
fontSize: 32,
fontWeight: 'bold',
color: '#FFFFFF',
marginBottom: 10,
},
statusText: {
fontSize: 18,
color: '#FFFFFF',
},
buttonContainer: {
position: 'absolute',
bottom: 100,
left: 0,
right: 0,
alignItems: 'center',
},
connectButton: {
width: 120,
height: 120,
borderRadius: 60,
justifyContent: 'center',
alignItems: 'center',
},
connectedButton: {
backgroundColor: '#5cb85c',
},
disconnectedButton: {
backgroundColor: '#d9534f',
},
connectButtonText: {
fontSize: 50,
color: '#FFFFFF',
},
serverList: {
paddingHorizontal: 20,
paddingBottom: 20,
},
serverItem: {
flexDirection: 'row',
alignItems: 'center',
padding: 15,
marginBottom: 10,
backgroundColor: 'rgba(255, 255, 255, 0.1)',
borderRadius: 10,
},
selectedServer: {
backgroundColor: 'rgba(255, 215, 0, 0.3)',
},
disabledServer: {
opacity: 0.5, // Make disabled servers more transparent
},
serverName: {
fontSize: 18,
color: '#FFFFFF',
},
});
export default App;