|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | +import { SafeAreaView, StyleSheet, Platform, View, ScrollView, Text, Button } from 'react-native'; |
| 3 | +import RNSimpleOpenvpn, { addVpnStateListener, removeVpnStateListener } from 'react-native-simple-openvpn'; |
| 4 | + |
| 5 | +const isIPhone = Platform.OS === 'ios'; |
| 6 | +const PRIMARY_COLOR = 'skyblue'; |
| 7 | + |
| 8 | +const App = () => { |
| 9 | + const [log, setLog] = useState(''); |
| 10 | + const logScrollView = useRef(null); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + async function observeVpn() { |
| 14 | + if (isIPhone) { |
| 15 | + await RNSimpleOpenvpn.observeState(); |
| 16 | + } |
| 17 | + |
| 18 | + addVpnStateListener((e) => { |
| 19 | + updateLog(JSON.stringify(e), undefined, 2); |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + observeVpn(); |
| 24 | + |
| 25 | + return async () => { |
| 26 | + if (isIPhone) { |
| 27 | + await RNSimpleOpenvpn.stopObserveState(); |
| 28 | + } |
| 29 | + |
| 30 | + removeVpnStateListener(); |
| 31 | + }; |
| 32 | + }); |
| 33 | + |
| 34 | + async function startOvpn() { |
| 35 | + try { |
| 36 | + await RNSimpleOpenvpn.connect({ |
| 37 | + remoteAddress: '', |
| 38 | + ovpnFileName: 'Japan', // Japan or Russian (android assets folder) |
| 39 | + assetsPath: '', |
| 40 | + providerBundleIdentifier: 'com.your.network.extension.bundle.id', |
| 41 | + localizedDescription: 'TestRNSimpleOvpn', |
| 42 | + }); |
| 43 | + } catch (error) { |
| 44 | + updateLog(error); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + async function stopOvpn() { |
| 49 | + try { |
| 50 | + await RNSimpleOpenvpn.disconnect(); |
| 51 | + } catch (error) { |
| 52 | + updateLog(error); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + function printVpnState() { |
| 57 | + updateLog(JSON.stringify(RNSimpleOpenvpn.VpnState, undefined, 2)); |
| 58 | + } |
| 59 | + |
| 60 | + function updateLog(newLog) { |
| 61 | + const now = new Date().toLocaleTimeString(); |
| 62 | + setLog(`${log}\n[${now}] ${newLog}`); |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <SafeAreaView style={styles.container}> |
| 67 | + <View style={styles.btnContainer}> |
| 68 | + <Button title="Connect" color={PRIMARY_COLOR} onPress={startOvpn} /> |
| 69 | + <Button title="Disconnect" color={PRIMARY_COLOR} onPress={stopOvpn} /> |
| 70 | + <Button title="Vpn State" color={PRIMARY_COLOR} onPress={printVpnState} /> |
| 71 | + <Button title="Clean Log" color={PRIMARY_COLOR} onPress={() => setLog('')} /> |
| 72 | + </View> |
| 73 | + <View style={styles.logContainer}> |
| 74 | + <ScrollView |
| 75 | + ref={logScrollView} |
| 76 | + style={styles.logScroll} |
| 77 | + onContentSizeChange={() => logScrollView.current.scrollToEnd({ animted: true })} |
| 78 | + > |
| 79 | + <Text>{log}</Text> |
| 80 | + </ScrollView> |
| 81 | + </View> |
| 82 | + </SafeAreaView> |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +const styles = StyleSheet.create({ |
| 87 | + container: { |
| 88 | + flex: 1, |
| 89 | + paddingTop: '20%', |
| 90 | + alignItems: 'center', |
| 91 | + }, |
| 92 | + btnContainer: { |
| 93 | + width: '80%', |
| 94 | + height: '25%', |
| 95 | + justifyContent: 'space-between', |
| 96 | + }, |
| 97 | + logContainer: { |
| 98 | + width: '80%', |
| 99 | + height: '50%', |
| 100 | + borderColor: PRIMARY_COLOR, |
| 101 | + borderWidth: 2, |
| 102 | + marginTop: 10, |
| 103 | + padding: 10, |
| 104 | + }, |
| 105 | + logScroll: { |
| 106 | + flex: 1, |
| 107 | + }, |
| 108 | +}); |
| 109 | + |
| 110 | +export default App; |
0 commit comments