2022-10-07 14:10:21 +00:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2022-09-28 12:08:16 +00:00
|
|
|
import { WebView, WebViewMessageEvent } from 'react-native-webview';
|
|
|
|
import { SafeAreaView, Text, Platform } from 'react-native';
|
|
|
|
import { torClient } from './services/Tor';
|
2022-10-07 14:10:21 +00:00
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
|
|
|
import NetInfo from '@react-native-community/netinfo';
|
2022-09-28 12:08:16 +00:00
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
const webViewRef = useRef<WebView>();
|
|
|
|
const uri = (Platform.OS === 'android' ? 'file:///android_asset/' : '') + 'Web.bundle/index.html';
|
|
|
|
|
2022-10-07 14:10:21 +00:00
|
|
|
const injectMessageResolve = (id: string, data: object) => {
|
2022-09-28 12:08:16 +00:00
|
|
|
const json = JSON.stringify(data);
|
|
|
|
webViewRef.current?.injectJavaScript(
|
|
|
|
`(function() {window.NativeRobosats.onMessageResolve(${id}, ${json});})();`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-07 14:10:21 +00:00
|
|
|
const injectMessage = (message: object) => {
|
|
|
|
const json = JSON.stringify(message);
|
|
|
|
webViewRef.current?.injectJavaScript(
|
|
|
|
`(function() {window.NativeRobosats?.onMessage(${json});})();`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-28 12:08:16 +00:00
|
|
|
const onMessage = async (event: WebViewMessageEvent) => {
|
|
|
|
const data = JSON.parse(event.nativeEvent.data);
|
|
|
|
if (data.category === 'http') {
|
2022-10-07 14:10:21 +00:00
|
|
|
sendTorStatus();
|
|
|
|
|
2022-09-28 12:08:16 +00:00
|
|
|
if (data.type === 'get') {
|
2022-10-07 14:10:21 +00:00
|
|
|
torClient
|
|
|
|
.get(data.path)
|
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
|
|
|
.finally(sendTorStatus);
|
2022-09-28 12:08:16 +00:00
|
|
|
} else if (data.type === 'post') {
|
2022-10-07 14:10:21 +00:00
|
|
|
torClient
|
|
|
|
.post(data.path, data.body, data.headers)
|
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
|
|
|
.finally(sendTorStatus);
|
2022-09-28 12:08:16 +00:00
|
|
|
} else if (data.type === 'delete') {
|
2022-10-07 14:10:21 +00:00
|
|
|
torClient
|
|
|
|
.delete(data.path, data.headers)
|
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
|
|
|
.finally(sendTorStatus);
|
2022-09-28 12:08:16 +00:00
|
|
|
} else if (data.type === 'xhr') {
|
2022-10-07 14:10:21 +00:00
|
|
|
torClient
|
|
|
|
.request(data.path)
|
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
|
|
|
.finally(sendTorStatus);
|
|
|
|
}
|
|
|
|
} else if (data.category === 'system') {
|
|
|
|
if (data.type === 'copyToClipboardString') {
|
|
|
|
Clipboard.setString(data.detail);
|
2022-09-28 12:08:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-07 14:10:21 +00:00
|
|
|
const sendTorStatus = async () => {
|
|
|
|
NetInfo.fetch().then(async (state) => {
|
|
|
|
let daemonStatus = 'ERROR';
|
|
|
|
if (state.isInternetReachable) {
|
|
|
|
try {
|
|
|
|
daemonStatus = await torClient.daemon.getDaemonStatus();
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
|
|
|
|
injectMessage({
|
|
|
|
category: 'system',
|
|
|
|
type: 'torStatus',
|
|
|
|
detail: daemonStatus,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2022-09-28 12:08:16 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeAreaView style={{ flex: 1 }}>
|
|
|
|
<WebView
|
|
|
|
source={{
|
|
|
|
uri,
|
|
|
|
}}
|
|
|
|
onMessage={onMessage}
|
|
|
|
// @ts-expect-error
|
|
|
|
ref={(ref) => (webViewRef.current = ref)}
|
2022-10-04 16:00:37 +00:00
|
|
|
overScrollMode='never'
|
2022-09-28 12:08:16 +00:00
|
|
|
javaScriptEnabled={true}
|
|
|
|
domStorageEnabled={true}
|
|
|
|
sharedCookiesEnabled={true}
|
|
|
|
thirdPartyCookiesEnabled={true}
|
|
|
|
originWhitelist={[uri]}
|
|
|
|
scalesPageToFit={true}
|
|
|
|
startInLoadingState={true}
|
|
|
|
mixedContentMode={'always'}
|
|
|
|
allowsInlineMediaPlayback={true}
|
|
|
|
allowsFullscreenVideo={false}
|
|
|
|
setBuiltInZoomControls={false}
|
|
|
|
allowingReadAccessToURL={uri}
|
|
|
|
allowFileAccess={true}
|
|
|
|
allowsBackForwardNavigationGestures={false}
|
|
|
|
mediaPlaybackRequiresUserAction={false}
|
|
|
|
allowsLinkPreview={false}
|
|
|
|
renderLoading={() => <Text>Loading RoboSats</Text>}
|
|
|
|
onError={(syntheticEvent) => <Text>{syntheticEvent.type}</Text>}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|