1
1
package com.reactnativetor
2
2
3
+ import android.content.Context
3
4
import android.util.Log
4
5
import com.facebook.react.bridge.*
5
6
import com.sifir.tor.DataObserver
@@ -11,12 +12,11 @@ import java.net.InetSocketAddress
11
12
import java.net.ServerSocket
12
13
import java.net.Proxy;
13
14
import java.security.cert.X509Certificate
14
- import javax.net.ssl.SSLContext
15
- import javax.net.ssl.TrustManager
16
- import javax.net.ssl.X509TrustManager
17
15
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
16
+ import org.json.JSONObject
18
17
import java.util.UUID;
19
18
import java.util.concurrent.*
19
+ import javax.net.ssl.*
20
20
21
21
22
22
/* *
@@ -52,13 +52,17 @@ class DataObserverEmitter(
52
52
}
53
53
}
54
54
55
+
55
56
class TorModule (reactContext : ReactApplicationContext ) : ReactContextBaseJavaModule(reactContext) {
57
+ private var _client : OkHttpClient ? = null ;
56
58
private var service: OwnedTorService ? = null ;
57
59
private var proxy: Proxy ? = null ;
58
60
private var _starting : Boolean = false ;
59
61
private var _streams : HashMap <String , TcpSocksStream > = HashMap ();
60
- // private val executorService: ExecutorService = Executors.newFixedThreadPool(4)
61
- private val executorService : ThreadPoolExecutor = ThreadPoolExecutor (4 ,4 , 0L , TimeUnit .MILLISECONDS , LinkedBlockingQueue <Runnable >());
62
+
63
+ // private val executorService: ExecutorService = Executors.newFixedThreadPool(4)
64
+ private val executorService: ThreadPoolExecutor =
65
+ ThreadPoolExecutor (1 , 1 , 0L , TimeUnit .MILLISECONDS , LinkedBlockingQueue <Runnable >(50 ));
62
66
63
67
64
68
/* *
@@ -87,7 +91,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
87
91
88
92
return OkHttpClient .Builder ()
89
93
.sslSocketFactory(sslSocketFactory, trustAllCerts[0 ] as X509TrustManager )
90
- .hostnameVerifier { _, _ -> true }
94
+ .hostnameVerifier( HostnameVerifier { _: String , _: SSLSession -> true })
91
95
}
92
96
93
97
override fun getName (): String {
@@ -124,24 +128,33 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
124
128
method : String ,
125
129
jsonBody : String ,
126
130
headers : ReadableMap ,
131
+ // FIXME move this to startDeamon call
127
132
trustAllSSl : Boolean ,
128
133
promise : Promise
129
134
) {
130
135
if (service == null ) {
131
136
promise.reject(Throwable (" Service Not Initialized!, Call startDaemon first" ));
137
+ return ;
132
138
}
133
139
134
- var client = (if (trustAllSSl) getUnsafeOkHttpClient() else OkHttpClient ().newBuilder())
135
- .proxy(proxy)
136
- .connectTimeout(10 , TimeUnit .SECONDS )
137
- .writeTimeout(10 , TimeUnit .SECONDS )
138
- .readTimeout(10 , TimeUnit .SECONDS )
139
- .build()
140
+ // if(_client !is OkHttpClient){
141
+ // _client = (if (trustAllSSl) getUnsafeOkHttpClient() else OkHttpClient().newBuilder())
142
+ // .proxy(proxy)
143
+ // .connectTimeout(10, TimeUnit.SECONDS)
144
+ // .writeTimeout(10, TimeUnit.SECONDS)
145
+ // .readTimeout(10, TimeUnit.SECONDS)
146
+ // .build()
147
+ // }
148
+
149
+ if (_client !is OkHttpClient ){
150
+ promise.reject(Throwable (" Request http client not Initialized!, Call startDaemon first" ));
151
+ return ;
152
+ }
140
153
141
154
val param = TaskParam (method, url, jsonBody, headers.toHashMap())
142
155
executorService.execute {
143
156
try {
144
- val task = TorBridgeRequest (promise, client , param);
157
+ val task = TorBridgeRequest (promise, _client !! , param);
145
158
task.run ()
146
159
} catch (e: Exception ) {
147
160
Log .d(" TorBridge" , " error on request: $e " )
@@ -152,27 +165,38 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
152
165
153
166
154
167
@ReactMethod
155
- fun startDaemon (timeoutMs : Double , promise : Promise ) {
168
+ fun startDaemon (timeoutMs : Double , clientTimeoutSeconds : Double , promise : Promise ) {
169
+ Log .d(" TorBridge" , " ->startDaemon" )
156
170
if (service != null ) {
157
171
promise.reject(Throwable (" Service already running, call stopDaemon first" ))
172
+ return ;
158
173
}
159
174
if (this ._starting ) {
160
175
promise.reject(Throwable (" Service already starting" ))
176
+ return ;
161
177
}
162
178
_starting = true ;
163
179
executorService.execute {
164
180
val socksPort = findFreePort();
165
181
val path = this .reactApplicationContext.cacheDir.toString();
166
- val param = StartParam (socksPort, path, timeoutMs.toLong() )
182
+ val param = StartParam (socksPort, path, timeoutMs)
167
183
try {
168
184
TorBridgeStartAsync (param, {
169
185
service = it
170
186
proxy = Proxy (Proxy .Type .SOCKS , InetSocketAddress (" 0.0.0.0" , socksPort))
171
187
_starting = false ;
188
+
189
+ _client = getUnsafeOkHttpClient()
190
+ .proxy(proxy)
191
+ .connectTimeout(clientTimeoutSeconds.toLong(), TimeUnit .SECONDS )
192
+ .writeTimeout(clientTimeoutSeconds.toLong(), TimeUnit .SECONDS )
193
+ .readTimeout(clientTimeoutSeconds.toLong(), TimeUnit .SECONDS )
194
+ .build();
195
+
172
196
promise.resolve(socksPort);
173
197
}, {
174
198
_starting = false ;
175
- promise.reject(it);
199
+ promise.reject(" StartDaemon Error " , " Error starting Tor Daemon " , it);
176
200
}).run ();
177
201
178
202
} catch (e: Exception ) {
@@ -217,7 +241,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
217
241
if (service == null ) {
218
242
throw Exception (" Tor service not running, call startDaemon first" )
219
243
}
220
- TcpStreamStart (target, " 0.0.0.0:${service?.socksPort} " , timeoutMs.toLong() , {
244
+ TcpStreamStart (target, " 0.0.0.0:${service?.socksPort} " , timeoutMs, {
221
245
// Assign UUID to connection to manage it
222
246
val uuid = UUID .randomUUID();
223
247
val connId = uuid.toString();
@@ -243,7 +267,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
243
267
throw Throwable (" Tor Service not running, call startDaemon first" )
244
268
}
245
269
var stream = _streams [connId]
246
- ? : throw Throwable (" Stream for connectionId $connId is not initialized, call startTcpConn first" );
270
+ ? : throw Throwable (" Stream for connectionId $connId is not initialized, call startTcpConn first" );
247
271
stream.send_data(msg, timeoutSec.toLong());
248
272
promise.resolve(true );
249
273
} catch (e: Exception ) {
@@ -265,4 +289,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
265
289
promise.reject(e)
266
290
}
267
291
}
292
+
268
293
}
294
+
295
+
0 commit comments