-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathApp.tsx
185 lines (179 loc) · 5.37 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
import * as React from 'react';
import { StyleSheet, View, Text, Button, TextInput } from 'react-native';
import TorBridge from 'react-native-tor';
type Await<T> = T extends PromiseLike<infer U> ? U : T;
const client = TorBridge();
let tcpStream: Await<
ReturnType<typeof client['createTcpConnection']>
> | null = null;
export default function App() {
const [socksPort, setSocksPort] = React.useState<number | undefined>();
const [trustSSL, setTrustSSL] = React.useState<boolean>(true);
const [onion, setOnion] = React.useState<string | undefined>(
'http://3g2upl4pq6kufc4m.onion'
);
const [hasStream, setHasStream] = React.useState(false);
const [
streamConnectionTimeoutMS,
setStreamConnectionTimeoutMS,
] = React.useState(15000);
React.useEffect(() => {
_init();
}, []);
const _init = async () => {
try {
// Init and do a few basic calls to test all is good
console.log('App init');
await client.startIfNotStarted();
} catch (err) {
console.error('Error starting daemon', err);
await client.stopIfRunning();
}
};
const startTor = async () => {
try {
const port = await client.startIfNotStarted();
console.log('Tor started socks port', port);
setSocksPort(port);
} catch (err) {
await client.stopIfRunning();
console.error(err);
}
};
const stopTor = async () => {
try {
await client.stopIfRunning();
} catch (err) {
console.error(err);
}
setSocksPort(undefined);
console.log('Tor stopped');
};
const getOnion = async () => {
try {
if (!onion) throw 'No onion detected';
let resp = await client.get(onion, undefined, trustSSL);
console.log('got resp', resp);
} catch (err) {
console.error(err);
}
};
const postOnion = async () => {
try {
if (!onion) throw 'No onion detected';
let resp = await client.post(
onion,
JSON.stringify({ q: 'hello' }),
{
'Content-Type': 'application/json',
// also supports
// 'Content-Type': 'application/x-www-form-urlencoded',
},
trustSSL
);
console.log('got resp', resp);
} catch (err) {
console.error(err);
}
};
const getStatus = async () => {
try {
let resp = await client.getDaemonStatus();
console.log('got status', resp);
} catch (err) {
console.error('Error getDeamonStatus', err);
}
};
const sendTcpMsg = async () => {
try {
let msg = `{ "id": 1, "method": "blockchain.scripthash.get_balance", "params": ["716decbe1660861c3d93906cb1d98ee68b154fd4d23aed9783859c1271b52a9c"] }\n`;
if (!tcpStream) throw 'Stream not set';
await tcpStream.write(msg);
} catch (err) {
console.error('Error SendingTcpMSg', err);
}
};
const startTcpStream = async () => {
try {
let target =
'kciybn4d4vuqvobdl2kdp3r2rudqbqvsymqwg4jomzft6m6gaibaf6yd.onion:50001';
console.log('starting');
const conn = await client.createTcpConnection(
{ target, connectionTimeout: streamConnectionTimeoutMS },
(data, err) => {
console.log('tcp got msg', data, err);
}
);
console.log('after');
tcpStream = conn;
setHasStream(true);
} catch (err) {
console.error('Error StartingTcpStream', err);
}
};
const closeTcpStream = async () => {
try {
if (!tcpStream) throw 'Stream not set';
await tcpStream.close();
tcpStream = null;
setHasStream(false);
} catch (err) {
console.error('Error SendingTcpMSg', err);
}
};
return (
<View style={styles.container}>
<View>
<Button onPress={startTor} title="Start Tor">
<Text>Start Tor</Text>
</Button>
<Button onPress={stopTor} title="Stop Tor" disabled={!socksPort}>
<Text>Stop Tor</Text>
</Button>
<Button onPress={getStatus} title="Get Status">
<Text>Get Daemon Status</Text>
</Button>
{!!socksPort && (
<View>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={setOnion}
value={onion}
/>
<Button onPress={getOnion} title="Get onion" />
<Button onPress={postOnion} title="POST onion" />
<Button onPress={startTcpStream} title="Start Tcp Stream" />
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={(x) => setStreamConnectionTimeoutMS(Number(x))}
value={String(streamConnectionTimeoutMS)}
/>
{hasStream && (
<View>
<Button onPress={sendTcpMsg} title="Send Tcp Msg" />
<Button onPress={closeTcpStream} title="Close Tcp Stream" />
</View>
)}
<View>
<Text> Trust Self Signed SSL Toggle</Text>
<Button
onPress={() => setTrustSSL(!trustSSL)}
title={
trustSSL ? 'Trusting Invalid SSL' : 'Not Trusting Invalid SSL'
}
/>
</View>
</View>
)}
</View>
<Text>SocksPort: {socksPort}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});