Skip to content

Commit 8efad38

Browse files
committed
Support Obfuscation & Filtering
1 parent 56410eb commit 8efad38

File tree

3 files changed

+147
-66
lines changed

3 files changed

+147
-66
lines changed

ios/RNInstabug/InstabugNetworkLoggerBridge.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#import <React/RCTBridgeModule.h>
22
#import <React/RCTEventEmitter.h>
3+
#import <Instabug/IBGTypes.h>
34

4-
5+
typedef void (^ IBGURLRequestAsyncObfuscationCompletedHandler)(NSURLRequest * _Nonnull request);
6+
typedef void (^IBGURLRequestResponseAsyncFilteringCompletedHandler)(BOOL keep);
57
@interface InstabugNtworkLoggerBridge : RCTEventEmitter <RCTBridgeModule>
6-
//@property NSMutableDictionary<NSString *, IBGURLRequestAsyncObfuscationCompletedHandler> *dictionary;
8+
9+
@property NSMutableDictionary<NSString *, IBGURLRequestAsyncObfuscationCompletedHandler> * _Nonnull requestObfuscationCompletionDictionary;
10+
@property NSMutableDictionary<NSString *, NetworkObfuscationCompletionBlock> * _Nonnull responseObfuscationCompletionDictionary;
11+
@property NSMutableDictionary<NSString *, IBGURLRequestResponseAsyncFilteringCompletedHandler> * _Nonnull requestFilteringCompletionDictionary;
12+
@property NSMutableDictionary<NSString *, IBGURLRequestResponseAsyncFilteringCompletedHandler> * _Nonnull responseFilteringCompletionDictionary;
13+
714
/*
815
+------------------------------------------------------------------------+
916
| NetworkLogger Module |

ios/RNInstabug/InstabugNetworkLoggerBridge.m

Lines changed: 137 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77
#import "InstabugNetworkLoggerBridge.h"
88
#import "Util/IBGNetworkLogger+CP.h"
99

10-
1110
@implementation InstabugNtworkLoggerBridge
12-
//@property NSMutableDictionary<NSString *, IBGURLRequestAsyncObfuscationCompletedHandler> *dictionary;
1311

1412

13+
- (instancetype)init {
14+
self = [super init];
15+
if (self) {
16+
_requestObfuscationCompletionDictionary = [[NSMutableDictionary alloc] init];
17+
_responseObfuscationCompletionDictionary = [[NSMutableDictionary alloc] init];
18+
_requestFilteringCompletionDictionary = [[NSMutableDictionary alloc] init];
19+
_responseFilteringCompletionDictionary = [[NSMutableDictionary alloc] init];
20+
}
21+
return self;
22+
}
1523

1624
- (dispatch_queue_t)methodQueue {
1725
return dispatch_get_main_queue();
@@ -50,70 +58,136 @@ -(void)stopObserving {
5058
resolve(@(IBGNetworkLogger.isNativeNetworkInterceptionFeatureEnabled));
5159
}
5260

53-
//RCT_EXPORT_METHOD(registerNetworkLogsListener){
54-
//
55-
// [IBGNetworkLogger setRequestAsyncObfuscationHandler:^(NSURLRequest *requestToBeObfuscated, IBGURLRequestAsyncObfuscationCompletedHandler completion) {
56-
//
57-
// NSString *tempId = [[[NSUUID alloc] init] UUIDString];
58-
// self.dictionary[tempId] = completion;
59-
//
60-
// // Ensure the URL, HTTP body, and headers are in the correct format
61-
// NSString *urlString = requestToBeObfuscated.URL.absoluteString ?: @"";
62-
// NSString *bodyString = [[NSString alloc] initWithData:requestToBeObfuscated.HTTPBody encoding:NSUTF8StringEncoding] ?: @"";
63-
// NSDictionary *headerDict = requestToBeObfuscated.allHTTPHeaderFields ?: @{};
64-
//
65-
// // Create the dictionary to send
66-
// NSDictionary *dict = @{
67-
// @"tempId": tempId,
68-
// @"url": urlString,
69-
// @"requestBody": bodyString,
70-
// @"requestHeader": headerDict
71-
// };
72-
//
73-
// // Send the event
74-
// [self sendEventWithName:@"IBGNetworkLoggerHandler" body:dict];
75-
//
76-
// }];
77-
//}
61+
RCT_EXPORT_METHOD(registerNetworkLogsListener) {
62+
63+
[IBGNetworkLogger setRequestAsyncObfuscationHandler:^(NSURLRequest * _Nonnull requestToBeObfuscated,
64+
void (^ _Nonnull completion)(NSURLRequest * _Nonnull)) {
65+
NSString *tempId = [[[NSUUID alloc] init] UUIDString];
66+
self.requestObfuscationCompletionDictionary[tempId] = completion;
67+
68+
// Ensure the URL, HTTP body, and headers are in the correct format
69+
NSString *urlString = requestToBeObfuscated.URL.absoluteString ?: @"";
70+
NSString *bodyString = [[NSString alloc] initWithData:requestToBeObfuscated.HTTPBody encoding:NSUTF8StringEncoding] ?: @"";
71+
NSDictionary *headerDict = requestToBeObfuscated.allHTTPHeaderFields ?: @{};
72+
73+
// Create the dictionary to send
74+
NSDictionary *dict = @{
75+
@"tempId": tempId,
76+
@"url": urlString,
77+
@"requestBody": bodyString,
78+
@"requestHeader": headerDict
79+
};
80+
81+
// Send the event
82+
[self sendEventWithName:@"IBGNetworkLoggerHandler" body:dict];
83+
84+
}];
85+
86+
87+
[IBGNetworkLogger setResponseObfuscationHandler:^(NSData * _Nullable responseData, NSURLResponse * _Nonnull response, NetworkObfuscationCompletionBlock _Nonnull completion) {
88+
89+
NSString *tempId = [[[NSUUID alloc] init] UUIDString];
90+
self.responseObfuscationCompletionDictionary[tempId] = completion;
91+
92+
93+
// MARK: TODO: Convert Response To Dictionary & Pass it To React Native
94+
95+
}];
96+
97+
98+
99+
[IBGNetworkLogger setRequestFilteringHandler:^(NSURLRequest * _Nonnull request, void (^ _Nonnull completion)(BOOL)) {
100+
101+
NSString *tempId = [[[NSUUID alloc] init] UUIDString];
102+
self.requestFilteringCompletionDictionary[tempId] = completion;
103+
104+
105+
// MARK: TODO: Convert Request To Dictionary & Pass it To React Native
106+
107+
}];
108+
109+
110+
[IBGNetworkLogger setResponseFilteringHandler:^(NSURLResponse * _Nonnull request, void (^ _Nonnull completion)(BOOL)) {
111+
112+
NSString *tempId = [[[NSUUID alloc] init] UUIDString];
113+
self.responseFilteringCompletionDictionary[tempId] = completion;
114+
115+
116+
// MARK: TODO: Convert Request To Dictionary & Pass it To React Native
117+
118+
}];
119+
120+
121+
122+
123+
}
78124

79125

80-
//RCT_EXPORT_METHOD(updateNetworkLogSnapshot:(NSString * _Nonnull)jsonString) {
81-
// // Properly initialize the NSMutableURLRequest
82-
// NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
83-
//
84-
// // Convert jsonString to NSData
85-
// NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
86-
//
87-
// // Parse the JSON into a dictionary
88-
// NSError *error = nil;
89-
// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
90-
//
91-
// // Check for JSON parsing errors
92-
// if (error) {
93-
// NSLog(@"Failed to parse JSON: %@", error);
126+
RCT_EXPORT_METHOD(updateNetworkLogSnapshot:(NSString * _Nonnull)jsonString) {
127+
// Properly initialize the NSMutableURLRequest
128+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
129+
130+
// Convert jsonString to NSData
131+
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
132+
133+
// Parse the JSON into a dictionary
134+
NSError *error = nil;
135+
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
136+
137+
// Check for JSON parsing errors
138+
if (error) {
139+
NSLog(@"Failed to parse JSON: %@", error);
140+
return;
141+
}
142+
143+
// Set the URL, HTTP body, and headers
144+
request.URL = [NSURL URLWithString:dict[@"url"]];
145+
request.HTTPBody = [dict[@"requestBody"] dataUsingEncoding:NSUTF8StringEncoding];
146+
147+
// Ensure requestHeader is a dictionary
148+
if ([dict[@"requestHeader"] isKindOfClass:[NSDictionary class]]) {
149+
request.allHTTPHeaderFields = dict[@"requestHeader"];
150+
} else {
151+
NSLog(@"Invalid requestHeader format");
94152
// return;
95-
// }
96-
//
97-
// // Set the URL, HTTP body, and headers
98-
// request.URL = [NSURL URLWithString:dict[@"url"]];
99-
// request.HTTPBody = [dict[@"requestBody"] dataUsingEncoding:NSUTF8StringEncoding];
100-
//
101-
// // Ensure requestHeader is a dictionary
102-
// if ([dict[@"requestHeader"] isKindOfClass:[NSDictionary class]]) {
103-
// request.allHTTPHeaderFields = dict[@"requestHeader"];
104-
// } else {
105-
// NSLog(@"Invalid requestHeader format");
106-
//// return;
107-
// }
108-
//
109-
// // Ensure self.completion is not nil before calling it
110-
// NSString *tempId = dict[@"tempId"];
111-
// if ([tempId isKindOfClass:[NSString class]] && self.dictionary[tempId] != nil) {
112-
// ((IBGURLRequestObfuscationHandler)self.dictionary[tempId])(request);
113-
// } else {
114-
// NSLog(@"Not Available Completion");
115-
// }
116-
//}
153+
}
154+
155+
// Ensure self.completion is not nil before calling it
156+
NSString *tempId = dict[@"tempId"];
157+
if ([tempId isKindOfClass:[NSString class]] && self.requestObfuscationCompletionDictionary[tempId] != nil) {
158+
((IBGURLRequestAsyncObfuscationCompletedHandler)self.requestObfuscationCompletionDictionary[tempId])(request);
159+
} else {
160+
NSLog(@"Not Available Completion");
161+
}
162+
163+
164+
// MARK: Might need to moved this into another method.
165+
NSURLResponse *response; // Must be initialized from React Native Objects
166+
NSData *responseData; // Must be initialized from React Native Objects
167+
if ([tempId isKindOfClass:[NSString class]] && self.responseObfuscationCompletionDictionary[tempId] != nil) {
168+
169+
((NetworkObfuscationCompletionBlock)self.responseObfuscationCompletionDictionary[tempId])(responseData, response);
170+
} else {
171+
NSLog(@"Not Available Completion");
172+
}
173+
174+
175+
176+
if ([tempId isKindOfClass:[NSString class]] && self.responseFilteringCompletionDictionary[tempId] != nil) {
177+
// ⬇️ YES == Response will be saved, NO == will be ignored
178+
((IBGURLRequestResponseAsyncFilteringCompletedHandler)self.responseFilteringCompletionDictionary[tempId])(YES);
179+
} else {
180+
NSLog(@"Not Available Completion");
181+
}
182+
183+
if ([tempId isKindOfClass:[NSString class]] && self.requestFilteringCompletionDictionary[tempId] != nil) {
184+
// ⬇️ YES == Request will be saved, NO == will be ignored
185+
((IBGURLRequestResponseAsyncFilteringCompletedHandler)self.requestFilteringCompletionDictionary[tempId])(YES);
186+
} else {
187+
NSLog(@"Not Available Completion");
188+
}
189+
190+
}
117191

118192
RCT_EXPORT_METHOD(setNetworkLoggingRequestFilterPredicateIOS: (BOOL)value){
119193

ios/RNInstabug/Util/IBGNetworkLogger+CP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
3333

3434
+ (void)setRequestAsyncObfuscationHandler:(void (^)(NSURLRequest * requestToBeObfuscated, void (^ completion)(NSURLRequest * obfuscatedRequest)))asyncObfuscationHandler;
3535
+ (void)setRequestFilteringHandler:(void (^)(NSURLRequest * request, void (^completion)(BOOL keep)))requestFilteringHandler;
36-
+ (void)setResponseFilteringHandler:(void (^)(NSURLResponse * response, void (^comppletion)(BOOL keep)))responseFilteringHandler;
36+
+ (void)setResponseFilteringHandler:(void (^)(NSURLResponse * response, void (^completion)(BOOL keep)))responseFilteringHandler;
3737

3838
@end
3939

0 commit comments

Comments
 (0)