feat: [sockets-timeout] ios - new framework, use new_timeout, ts updated

This commit is contained in:
Gus 2021-02-07 16:27:18 -05:00
parent 2bb8ee462f
commit ea04334ff7
6 changed files with 48 additions and 31 deletions

View File

@ -15,6 +15,10 @@ export default function App() {
'http://3g2upl4pq6kufc4m.onion'
);
const [hasStream, setHasStream] = React.useState(false);
const [
streamConnectionTimeoutMS,
setStreamConnectionTimeoutMS,
] = React.useState(15000);
React.useEffect(() => {
_init();
}, []);
@ -90,9 +94,12 @@ export default function App() {
let target =
'kciybn4d4vuqvobdl2kdp3r2rudqbqvsymqwg4jomzft6m6gaibaf6yd.onion:50001';
console.log('starting');
const conn = await client.createTcpConnection({ target }, (data, err) => {
console.log('tcp got msg', data, err);
});
const conn = await client.createTcpConnection(
{ target, connectionTimeout: streamConnectionTimeoutMS },
(data, err) => {
console.log('tcp got msg', data, err);
}
);
console.log('after');
tcpStream = conn;
setHasStream(true);
@ -133,6 +140,11 @@ export default function App() {
<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" />

View File

@ -6,43 +6,41 @@
typedef struct OwnedTorService OwnedTorService_t;
typedef struct TcpSocksStream TcpSocksStream_t;
typedef enum {
typedef enum ResultMessage_Tag {
Success,
Error,
} ResultMessage_Tag;
typedef struct {
char *_0;
} Error_Body;
typedef struct {
typedef struct ResultMessage {
ResultMessage_Tag tag;
union {
Error_Body error;
struct {
char *error;
};
};
} ResultMessage;
typedef struct {
typedef struct BoxedResult_OwnedTorService {
OwnedTorService_t *result;
ResultMessage message;
struct ResultMessage message;
} BoxedResult_OwnedTorService;
typedef struct {
typedef struct BoxedResult_TcpSocksStream {
TcpSocksStream_t *result;
ResultMessage message;
struct ResultMessage message;
} BoxedResult_TcpSocksStream;
typedef struct {
typedef struct Observer {
void *context;
void (*on_success)(char*, const void*);
void (*on_err)(char*, const void*);
} Observer;
BoxedResult_OwnedTorService *get_owned_TorService(const char *data_dir, uint16_t socks_port);
struct BoxedResult_OwnedTorService *get_owned_TorService(const char *data_dir, uint16_t socks_port);
/**
*# Safety
* Get the status of a OwnedTorService
* Get the status of a OwnedTorService_t
*/
char *get_status_of_owned_TorService(OwnedTorService_t *owned_client);
@ -50,23 +48,27 @@ char *get_status_of_owned_TorService(OwnedTorService_t *owned_client);
*# Safety
* Start a proxied TcpStream
*/
BoxedResult_TcpSocksStream *tcp_stream_start(const char *target, const char *proxy);
struct BoxedResult_TcpSocksStream *tcp_stream_start(const char *target,
const char *proxy,
uint64_t timeout_ms);
/**
*# Safety
* Send a Message over a tcpStream
*/
ResultMessage *tcp_stream_on_data(TcpSocksStream_t *stream, Observer observer);
struct ResultMessage *tcp_stream_on_data(TcpSocksStream_t *stream, struct Observer observer);
/**
*# Safety
* Send a Message over a tcpStream
*/
ResultMessage *tcp_stream_send_msg(TcpSocksStream_t *stream, const char *msg, uint64_t timeout);
struct ResultMessage *tcp_stream_send_msg(TcpSocksStream_t *stream,
const char *msg,
uint64_t timeout);
/**
*# Safety
* Destroy and release TcpSocksStream which will drop the connection
* Destroy and release TcpSocksStream_t which will drop the connection
*/
void tcp_stream_destroy(TcpSocksStream_t *stream);

View File

@ -35,6 +35,7 @@ RCT_EXTERN_METHOD(
RCT_EXTERN_METHOD(
startTcpConn:(NSString*)target
timeoutMs:(nonnull NSNumber*)timeoutMs
resolver:(RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)

View File

@ -177,7 +177,7 @@ class Tor: RCTEventEmitter {
return;
case Error:
// Convert RustByteSlice to String
if let error_body = call_result.message.error._0 {
if let error_body = call_result.message.error {
let error_string = String.init(cString: error_body);
reject("TOR.START",error_string,NSError.init(domain: "TOR", code: 0))
} else {
@ -248,8 +248,8 @@ class Tor: RCTEventEmitter {
["torTcpStreamData","torTcpStreamError"]
}
@objc(startTcpConn:resolver:rejecter:)
func startTcpConn(target:String,resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock){
@objc(startTcpConn:timeoutMs:resolver:rejecter:)
func startTcpConn(target:String,timeoutMs:NSNumber,resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock){
guard let socksProxy = self.proxySocksPort else {
reject("TOR.TCPCONN.startTcpConn","SocksProxy not detected, make sure Tor is started",NSError.init(domain: "TOR", code: 99));
return;
@ -259,7 +259,7 @@ class Tor: RCTEventEmitter {
reject("TOR.TCPCONN.starStrean","Stream for target \(target) already exists! Call stopConn",NSError.init(domain: "TOR", code: 01));
return;
}
let call_result = tcp_stream_start(target, "127.0.0.1:\(socksProxy)").pointee;
let call_result = tcp_stream_start(target, "127.0.0.1:\(socksProxy)",timeoutMs.uint64Value).pointee;
switch(call_result.message.tag){
case Success:
let stream = call_result.result;
@ -301,7 +301,7 @@ class Tor: RCTEventEmitter {
return;
case Error:
// Convert RustByteSlice to String
if let error_body = call_result.message.error._0 {
if let error_body = call_result.message.error {
let error_string = String.init(cString: error_body);
reject("TOR.TCPCONN.startTcpConn",error_string,NSError.init(domain: "TOR", code: 0))
} else {
@ -331,7 +331,7 @@ class Tor: RCTEventEmitter {
resolve(true);
return;
case Error:
if let error_body = result.error._0 {
if let error_body = result.error {
let error_string = String.init(cString: error_body);
reject("TOR.TCPCONN.sendTcpConnMsg",error_string,NSError.init(domain: "TOR", code: 0))
} else {

View File

@ -75,7 +75,7 @@ interface NativeTor {
headers: RequestHeaders,
trustInvalidSSL: boolean
): Promise<RequestResponse>;
startTcpConn(target: string): Promise<boolean>;
startTcpConn(target: string, timeoutMs: number): Promise<boolean>;
sendTcpConnMsg(
target: string,
msg: string,
@ -115,18 +115,20 @@ interface TcpStream {
* Note: Receiving an 'EOF' error from the target we're connected to signifies the end of a stream or the target dropped the connection.
* This will cause the module to drop the TcpConnection and remove all data event listeners.
* Should you wish to reconnect to the target you must initiate a new connection by calling createTcpConnection again.
* @param param {target: String, writeTimeout: Number} :
* @param param {target: string, writeTimeout: number, connectionTimeout: number } :
* `target` onion to connect to (ex: kciybn4d4vuqvobdl2kdp3r2rudqbqvsymqwg4jomzft6m6gaibaf6yd.onion:50001)
* 'writeTimeout' in seconds to wait before timing out on writing to the socket (Defaults to 7)
* 'connectionTimeout' in MilliSeconds to wait before timing out on connecting to the Target (Defaults to 15000 = 15 seconds)
* @param onData TcpConnDatahandler node style callback called when data or an error is received for this connection
* @returns TcpStream
*/
const createTcpConnection = async (
param: { target: string; writeTimeout?: number },
param: { target: string; connectionTimeout?: number; writeTimeout?: number },
onData: TcpConnDatahandler
): Promise<TcpStream> => {
const { target } = param;
await NativeModules.TorBridge.startTcpConn(target);
const connectionTimeout = param.connectionTimeout || 15000;
await NativeModules.TorBridge.startTcpConn(target, connectionTimeout);
let lsnr_handle: EmitterSubscription[] = [];
/**
* Handles errors from Tcp Connection