11package com.reactnativetor
22
3+ import android.content.Context
34import android.util.Log
45import com.facebook.react.bridge.*
56import com.sifir.tor.DataObserver
@@ -11,12 +12,11 @@ import java.net.InetSocketAddress
1112import java.net.ServerSocket
1213import java.net.Proxy;
1314import java.security.cert.X509Certificate
14- import javax.net.ssl.SSLContext
15- import javax.net.ssl.TrustManager
16- import javax.net.ssl.X509TrustManager
1715import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
16+ import org.json.JSONObject
1817import java.util.UUID;
1918import java.util.concurrent.*
19+ import javax.net.ssl.*
2020
2121
2222/* *
@@ -52,13 +52,17 @@ class DataObserverEmitter(
5252 }
5353}
5454
55+
5556class TorModule (reactContext : ReactApplicationContext ) : ReactContextBaseJavaModule(reactContext) {
57+ private var _client : OkHttpClient ? = null ;
5658 private var service: OwnedTorService ? = null ;
5759 private var proxy: Proxy ? = null ;
5860 private var _starting : Boolean = false ;
5961 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 ));
6266
6367
6468 /* *
@@ -87,7 +91,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
8791
8892 return OkHttpClient .Builder ()
8993 .sslSocketFactory(sslSocketFactory, trustAllCerts[0 ] as X509TrustManager )
90- .hostnameVerifier { _, _ -> true }
94+ .hostnameVerifier( HostnameVerifier { _: String , _: SSLSession -> true })
9195 }
9296
9397 override fun getName (): String {
@@ -124,24 +128,33 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
124128 method : String ,
125129 jsonBody : String ,
126130 headers : ReadableMap ,
131+ // FIXME move this to startDeamon call
127132 trustAllSSl : Boolean ,
128133 promise : Promise
129134 ) {
130135 if (service == null ) {
131136 promise.reject(Throwable (" Service Not Initialized!, Call startDaemon first" ));
137+ return ;
132138 }
133139
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+ }
140153
141154 val param = TaskParam (method, url, jsonBody, headers.toHashMap())
142155 executorService.execute {
143156 try {
144- val task = TorBridgeRequest (promise, client , param);
157+ val task = TorBridgeRequest (promise, _client !! , param);
145158 task.run ()
146159 } catch (e: Exception ) {
147160 Log .d(" TorBridge" , " error on request: $e " )
@@ -152,27 +165,38 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
152165
153166
154167 @ReactMethod
155- fun startDaemon (timeoutMs : Double , promise : Promise ) {
168+ fun startDaemon (timeoutMs : Double , clientTimeoutSeconds : Double , promise : Promise ) {
169+ Log .d(" TorBridge" , " ->startDaemon" )
156170 if (service != null ) {
157171 promise.reject(Throwable (" Service already running, call stopDaemon first" ))
172+ return ;
158173 }
159174 if (this ._starting ) {
160175 promise.reject(Throwable (" Service already starting" ))
176+ return ;
161177 }
162178 _starting = true ;
163179 executorService.execute {
164180 val socksPort = findFreePort();
165181 val path = this .reactApplicationContext.cacheDir.toString();
166- val param = StartParam (socksPort, path, timeoutMs.toLong() )
182+ val param = StartParam (socksPort, path, timeoutMs)
167183 try {
168184 TorBridgeStartAsync (param, {
169185 service = it
170186 proxy = Proxy (Proxy .Type .SOCKS , InetSocketAddress (" 0.0.0.0" , socksPort))
171187 _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+
172196 promise.resolve(socksPort);
173197 }, {
174198 _starting = false ;
175- promise.reject(it);
199+ promise.reject(" StartDaemon Error " , " Error starting Tor Daemon " , it);
176200 }).run ();
177201
178202 } catch (e: Exception ) {
@@ -217,7 +241,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
217241 if (service == null ) {
218242 throw Exception (" Tor service not running, call startDaemon first" )
219243 }
220- TcpStreamStart (target, " 0.0.0.0:${service?.socksPort} " , timeoutMs.toLong() , {
244+ TcpStreamStart (target, " 0.0.0.0:${service?.socksPort} " , timeoutMs, {
221245 // Assign UUID to connection to manage it
222246 val uuid = UUID .randomUUID();
223247 val connId = uuid.toString();
@@ -243,7 +267,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
243267 throw Throwable (" Tor Service not running, call startDaemon first" )
244268 }
245269 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" );
247271 stream.send_data(msg, timeoutSec.toLong());
248272 promise.resolve(true );
249273 } catch (e: Exception ) {
@@ -265,4 +289,7 @@ class TorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
265289 promise.reject(e)
266290 }
267291 }
292+
268293}
294+
295+
0 commit comments