2023-05-01 19:11:08 +00:00
|
|
|
import React, { useRef } from 'react';
|
2022-09-28 12:08:16 +00:00
|
|
|
import { WebView, WebViewMessageEvent } from 'react-native-webview';
|
2022-10-15 18:39:43 +00:00
|
|
|
import { SafeAreaView, Text, Platform, Appearance } from 'react-native';
|
2022-10-10 12:40:22 +00:00
|
|
|
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-10-10 12:40:22 +00:00
|
|
|
import EncryptedStorage from 'react-native-encrypted-storage';
|
2023-05-18 13:29:49 +00:00
|
|
|
import { name as app_name, version as app_version } from './package.json';
|
2022-09-28 12:08:16 +00:00
|
|
|
|
2022-10-15 18:39:43 +00:00
|
|
|
const backgroundColors = {
|
|
|
|
light: 'white',
|
|
|
|
dark: 'black',
|
|
|
|
};
|
|
|
|
|
2022-09-28 12:08:16 +00:00
|
|
|
const App = () => {
|
2022-10-16 08:53:18 +00:00
|
|
|
const colorScheme = Appearance.getColorScheme() ?? 'light';
|
2022-10-10 12:40:22 +00:00
|
|
|
const torClient = new TorClient();
|
2022-09-28 12:08:16 +00:00
|
|
|
const webViewRef = useRef<WebView>();
|
|
|
|
const uri = (Platform.OS === 'android' ? 'file:///android_asset/' : '') + 'Web.bundle/index.html';
|
|
|
|
|
2022-10-10 12:40:22 +00:00
|
|
|
const injectMessageResolve = (id: string, data?: object) => {
|
2022-10-16 17:04:10 +00:00
|
|
|
const json = JSON.stringify(data || {});
|
2022-09-28 12:08:16 +00:00
|
|
|
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});})();`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-05-01 19:11:08 +00:00
|
|
|
const init = (responseId: string) => {
|
2022-10-15 18:39:43 +00:00
|
|
|
const loadCookie = async (key: string) => {
|
|
|
|
return await EncryptedStorage.getItem(key).then((value) => {
|
2022-10-10 12:40:22 +00:00
|
|
|
if (value) {
|
|
|
|
const json = JSON.stringify({ key, value });
|
|
|
|
webViewRef.current?.injectJavaScript(
|
|
|
|
`(function() {window.NativeRobosats?.loadCookie(${json});})();`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-04 12:54:06 +00:00
|
|
|
loadCookie('robot_token');
|
2022-11-07 10:13:02 +00:00
|
|
|
loadCookie('settings_fontsize_basic');
|
|
|
|
loadCookie('settings_language');
|
|
|
|
loadCookie('settings_mode');
|
2023-05-05 17:19:45 +00:00
|
|
|
loadCookie('settings_light_qr');
|
2022-11-07 16:45:05 +00:00
|
|
|
loadCookie('settings_network');
|
2023-05-01 19:11:08 +00:00
|
|
|
loadCookie('garage').then(() => injectMessageResolve(responseId));
|
2022-10-10 12:40:22 +00:00
|
|
|
};
|
|
|
|
|
2023-03-09 20:09:39 +00:00
|
|
|
const onCatch = (dataId: string, event: any) => {
|
|
|
|
let json = '{}';
|
|
|
|
let code = 500;
|
|
|
|
if (event.message) {
|
|
|
|
const reponse = /Request Response Code \((?<code>\d*)\)\: (?<json>\{.*\})/.exec(
|
|
|
|
event.message,
|
|
|
|
);
|
|
|
|
json = reponse?.groups?.json ?? '{}';
|
2023-03-10 23:47:25 +00:00
|
|
|
code = reponse?.groups?.code ? parseInt(reponse?.groups?.code) : 500;
|
2023-03-09 20:09:39 +00:00
|
|
|
}
|
|
|
|
injectMessageResolve(dataId, {
|
|
|
|
headers: {},
|
|
|
|
respCode: code,
|
|
|
|
json: JSON.parse(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
|
2022-11-07 16:45:05 +00:00
|
|
|
.get(data.baseUrl, data.path, data.headers)
|
2022-10-07 14:10:21 +00:00
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
2023-03-09 20:09:39 +00:00
|
|
|
.catch((e) => onCatch(data.id, e))
|
2022-10-07 14:10:21 +00:00
|
|
|
.finally(sendTorStatus);
|
2022-09-28 12:08:16 +00:00
|
|
|
} else if (data.type === 'post') {
|
2022-10-07 14:10:21 +00:00
|
|
|
torClient
|
2022-11-07 16:45:05 +00:00
|
|
|
.post(data.baseUrl, data.path, data.body, data.headers)
|
2022-10-07 14:10:21 +00:00
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
2023-03-09 20:09:39 +00:00
|
|
|
.catch((e) => onCatch(data.id, e))
|
2022-10-07 14:10:21 +00:00
|
|
|
.finally(sendTorStatus);
|
2022-09-28 12:08:16 +00:00
|
|
|
} else if (data.type === 'delete') {
|
2022-10-07 14:10:21 +00:00
|
|
|
torClient
|
2022-11-07 16:45:05 +00:00
|
|
|
.delete(data.baseUrl, data.path, data.headers)
|
2022-10-07 14:10:21 +00:00
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
2023-03-09 20:09:39 +00:00
|
|
|
.catch((e) => onCatch(data.id, e))
|
2022-10-07 14:10:21 +00:00
|
|
|
.finally(sendTorStatus);
|
2022-09-28 12:08:16 +00:00
|
|
|
} else if (data.type === 'xhr') {
|
2022-10-07 14:10:21 +00:00
|
|
|
torClient
|
2022-11-07 16:45:05 +00:00
|
|
|
.request(data.baseUrl, data.path)
|
2022-10-07 14:10:21 +00:00
|
|
|
.then((response: object) => {
|
|
|
|
injectMessageResolve(data.id, response);
|
|
|
|
})
|
2023-03-09 20:09:39 +00:00
|
|
|
.catch((e) => onCatch(data.id, e))
|
2022-10-07 14:10:21 +00:00
|
|
|
.finally(sendTorStatus);
|
|
|
|
}
|
|
|
|
} else if (data.category === 'system') {
|
2022-10-10 12:40:22 +00:00
|
|
|
if (data.type === 'init') {
|
|
|
|
init(data.id);
|
|
|
|
} else if (data.type === 'copyToClipboardString') {
|
2022-10-07 14:10:21 +00:00
|
|
|
Clipboard.setString(data.detail);
|
2022-10-10 12:40:22 +00:00
|
|
|
} else if (data.type === 'setCookie') {
|
|
|
|
setCookie(data.key, data.detail);
|
|
|
|
} else if (data.type === 'deleteCookie') {
|
|
|
|
EncryptedStorage.removeItem(data.key);
|
2022-09-28 12:08:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-10 12:40:22 +00:00
|
|
|
const setCookie = async (key: string, value: string) => {
|
|
|
|
try {
|
|
|
|
await EncryptedStorage.setItem(key, value);
|
|
|
|
const storedValue = await EncryptedStorage.getItem(key);
|
|
|
|
injectMessage({
|
|
|
|
category: 'system',
|
|
|
|
type: 'setCookie',
|
|
|
|
key,
|
|
|
|
detail: storedValue,
|
|
|
|
});
|
|
|
|
} catch (error) {}
|
|
|
|
};
|
|
|
|
|
2023-03-09 20:09:39 +00:00
|
|
|
const sendTorStatus = async (event?: any) => {
|
2022-10-07 14:10:21 +00:00
|
|
|
NetInfo.fetch().then(async (state) => {
|
|
|
|
let daemonStatus = 'ERROR';
|
|
|
|
if (state.isInternetReachable) {
|
|
|
|
try {
|
|
|
|
daemonStatus = await torClient.daemon.getDaemonStatus();
|
2022-10-16 17:04:10 +00:00
|
|
|
} catch {}
|
2022-10-07 14:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
injectMessage({
|
|
|
|
category: 'system',
|
|
|
|
type: 'torStatus',
|
|
|
|
detail: daemonStatus,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2022-09-28 12:08:16 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-15 18:39:43 +00:00
|
|
|
<SafeAreaView style={{ flex: 1, backgroundColor: backgroundColors[colorScheme] }}>
|
2022-09-28 12:08:16 +00:00
|
|
|
<WebView
|
|
|
|
source={{
|
|
|
|
uri,
|
|
|
|
}}
|
|
|
|
onMessage={onMessage}
|
|
|
|
// @ts-expect-error
|
2023-05-18 13:29:49 +00:00
|
|
|
userAgent={`${app_name} v${app_version} Android`}
|
2022-10-16 08:53:18 +00:00
|
|
|
style={{ backgroundColor: backgroundColors[colorScheme] }}
|
2022-09-28 12:08:16 +00:00
|
|
|
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}
|
2022-10-15 18:39:43 +00:00
|
|
|
allowsBackForwardNavigationGestures={true}
|
2022-09-28 12:08:16 +00:00
|
|
|
mediaPlaybackRequiresUserAction={false}
|
|
|
|
allowsLinkPreview={false}
|
2022-10-15 18:39:43 +00:00
|
|
|
renderLoading={() => <Text></Text>}
|
2022-09-28 12:08:16 +00:00
|
|
|
onError={(syntheticEvent) => <Text>{syntheticEvent.type}</Text>}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|