forked from RoboSats/robosats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
187 lines (174 loc) · 6 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
import React, { useRef } from 'react';
import { WebView, WebViewMessageEvent } from 'react-native-webview';
import { SafeAreaView, Text, Platform, Appearance } from 'react-native';
import TorClient from './services/Tor';
import Clipboard from '@react-native-clipboard/clipboard';
import NetInfo from '@react-native-community/netinfo';
import EncryptedStorage from 'react-native-encrypted-storage';
import { name as app_name, version as app_version } from './package.json';
const backgroundColors = {
light: 'white',
dark: 'black',
};
const App = () => {
const colorScheme = Appearance.getColorScheme() ?? 'light';
const torClient = new TorClient();
const webViewRef = useRef<WebView>();
const uri = (Platform.OS === 'android' ? 'file:///android_asset/' : '') + 'Web.bundle/index.html';
const injectMessageResolve = (id: string, data?: object) => {
const json = JSON.stringify(data || {});
webViewRef.current?.injectJavaScript(
`(function() {window.NativeRobosats.onMessageResolve(${id}, ${json});})();`,
);
};
const injectMessage = (message: object) => {
const json = JSON.stringify(message);
webViewRef.current?.injectJavaScript(
`(function() {window.NativeRobosats?.onMessage(${json});})();`,
);
};
const init = (responseId: string) => {
const loadCookie = async (key: string) => {
return await EncryptedStorage.getItem(key).then((value) => {
if (value) {
const json = JSON.stringify({ key, value });
webViewRef.current?.injectJavaScript(
`(function() {window.NativeRobosats?.loadCookie(${json});})();`,
);
}
});
};
loadCookie('settings_fontsize_basic');
loadCookie('settings_language');
loadCookie('settings_mode');
loadCookie('settings_light_qr');
loadCookie('settings_network');
loadCookie('garage_slots').then(() => injectMessageResolve(responseId));
};
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 ?? '{}';
code = reponse?.groups?.code ? parseInt(reponse?.groups?.code) : 500;
}
injectMessageResolve(dataId, {
headers: {},
respCode: code,
json: JSON.parse(json),
});
};
const onMessage = async (event: WebViewMessageEvent) => {
const data = JSON.parse(event.nativeEvent.data);
if (data.category === 'http') {
sendTorStatus();
if (data.type === 'get') {
torClient
.get(data.baseUrl, data.path, data.headers)
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
} else if (data.type === 'post') {
torClient
.post(data.baseUrl, data.path, data.body, data.headers)
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
} else if (data.type === 'delete') {
torClient
.delete(data.baseUrl, data.path, data.headers)
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
} else if (data.type === 'xhr') {
torClient
.request(data.baseUrl, data.path)
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
}
} else if (data.category === 'system') {
if (data.type === 'init') {
init(data.id);
} else if (data.type === 'copyToClipboardString') {
Clipboard.setString(data.detail);
} else if (data.type === 'setCookie') {
setCookie(data.key, data.detail);
} else if (data.type === 'deleteCookie') {
EncryptedStorage.removeItem(data.key);
}
}
};
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) {}
};
const sendTorStatus = async (event?: any) => {
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,
});
});
};
return (
<SafeAreaView style={{ flex: 1, backgroundColor: backgroundColors[colorScheme] }}>
<WebView
source={{
uri,
}}
onMessage={onMessage}
// @ts-expect-error
userAgent={`${app_name} v${app_version} Android`}
style={{ backgroundColor: backgroundColors[colorScheme] }}
ref={(ref) => (webViewRef.current = ref)}
overScrollMode='never'
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={true}
mediaPlaybackRequiresUserAction={false} // Allow autoplay
allowsLinkPreview={false}
renderLoading={() => <Text></Text>}
onError={(syntheticEvent) => <Text>{syntheticEvent.type}</Text>}
/>
</SafeAreaView>
);
};
export default App;