Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

slower scan that doesnt tank perf on host

roryclear84336e0670da
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index f6e32cd..712b01c 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -137,11 +137,14 @@
send(clientSocket, jsonData.bytes, jsonData.length, 0);
return;
}
+
if ([filePath hasPrefix:@"get-devices"]) {
- [self.scanner scanNetworkForPort:8080 completion:^(NSArray<NSString *> *openPorts) {
- NSLog(@"Scan DONE!!! Found IPs: %@", openPorts);
+ NSLog(@"get-devices??");
+
+ // Respond immediately with cached list
+ @synchronized (self.scanner.cachedOpenPorts) {
NSError *error;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:openPorts options:0 error:&error];
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.scanner.cachedOpenPorts options:0 error:&error];
if (!jsonData) {
NSLog(@"Error serializing JSON: %@", error);
@@ -153,9 +156,13 @@
NSString *httpHeader = [NSString stringWithFormat:@"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %lu\r\n\r\n", (unsigned long)jsonData.length];
send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
send(clientSocket, jsonData.bytes, jsonData.length, 0);
- return;
- }];
+ }
+
+ // Update cached list asynchronously
+ [self.scanner updateCachedOpenPortsForPort:8080];
+ return;
}
+
if ([filePath hasPrefix:@"main"]) {
NSString *playerFilePath = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"html"];
diff --git a/clearcam/PortScanner.h b/clearcam/PortScanner.h
index 4598ae4..b1f864e 100644
--- a/clearcam/PortScanner.h
+++ b/clearcam/PortScanner.h
@@ -4,6 +4,9 @@
- (void)scanNetworkForPort:(int)port completion:(void (^)(NSArray<NSString *> *openPorts))completion;
- (NSString *)getDeviceIPAddress;
+- (void)updateCachedOpenPortsForPort:(int)port;
+@property (nonatomic, strong) NSMutableArray<NSString *> *cachedOpenPorts;
+@property (nonatomic, strong) dispatch_queue_t scanQueue;
@end
diff --git a/clearcam/PortScanner.m b/clearcam/PortScanner.m
index 0f80cf6..85f9d56 100644
--- a/clearcam/PortScanner.m
+++ b/clearcam/PortScanner.m
@@ -6,6 +6,28 @@
@implementation PortScanner
+// Initialize cached list and scan queue
+- (instancetype)init {
+ self = [super init];
+ if (self) {
+ _cachedOpenPorts = [NSMutableArray array];
+ _scanQueue = dispatch_queue_create("com.example.PortScannerQueue", DISPATCH_QUEUE_SERIAL);
+ }
+ return self;
+}
+
+- (void)updateCachedOpenPortsForPort:(int)port {
+ dispatch_async(self.scanQueue, ^{
+ [self throttledScanNetworkForPort:port withBatchSize:10 completion:^(NSArray<NSString *> *openPorts) {
+ @synchronized (self.cachedOpenPorts) {
+ [self.cachedOpenPorts removeAllObjects];
+ [self.cachedOpenPorts addObjectsFromArray:openPorts];
+ }
+ NSLog(@"Cache updated with open ports: %@", self.cachedOpenPorts);
+ }];
+ });
+}
+
// Get the local IP address and subnet mask
- (NSDictionary *)getLocalIPInfo {
struct ifaddrs *interfaces = NULL;
@@ -103,12 +125,12 @@
return;
}
- //NSString *deviceIP = [self getDeviceIPAddress];
+ NSString *deviceIP = [self getDeviceIPAddress];
NSMutableArray<NSString *> *foundIPs = [NSMutableArray array];
- //if (deviceIP) { //todo, needed?
- // [foundIPs addObject:deviceIP];
- //}
+ if (deviceIP) {
+ [foundIPs addObject:deviceIP];
+ }
NSArray<NSString *> *ipList = [self getIPRangeFromIP:ipInfo[@"ip"] subnetMask:ipInfo[@"subnet"]];
NSLog(@"Scanning %lu IPs for open port %d...", (unsigned long)ipList.count, port);
@@ -135,6 +157,56 @@
}
}
+- (void)throttledScanNetworkForPort:(int)port withBatchSize:(int)batchSize completion:(void (^)(NSArray<NSString *> *openPorts))completion {
+ NSDictionary *ipInfo = [self getLocalIPInfo];
+ if (!ipInfo) {
+ NSLog(@"Failed to get local IP info.");
+ if (completion) completion(@[]);
+ return;
+ }
+
+ NSString *deviceIP = [self getDeviceIPAddress];
+ NSMutableArray<NSString *> *foundIPs = [NSMutableArray array];
+
+ if (deviceIP) {
+ [foundIPs addObject:deviceIP];
+ }
+
+ NSArray<NSString *> *ipList = [self getIPRangeFromIP:ipInfo[@"ip"] subnetMask:ipInfo[@"subnet"]];
+ NSLog(@"Scanning %lu IPs for open port %d...", (unsigned long)ipList.count, port);
+
+ dispatch_group_t scanGroup = dispatch_group_create();
+ dispatch_queue_t scanQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
+
+ for (NSInteger i = 0; i < ipList.count; i += batchSize) {
+ NSRange range = NSMakeRange(i, MIN(batchSize, ipList.count - i));
+ NSArray *batch = [ipList subarrayWithRange:range];
+
+ for (NSString *ip in batch) {
+ dispatch_group_enter(scanGroup);
+ dispatch_async(scanQueue, ^{
+ if ([self isPortOpen:ip port:port]) {
+ @synchronized(self) {
+ NSLog(@"[+] Open port %d found at %@", port, ip);
+ [foundIPs addObject:ip];
+ }
+ }
+ dispatch_group_leave(scanGroup);
+ });
+ }
+
+ // Throttle by waiting for the batch to complete before starting the next batch
+ dispatch_group_wait(scanGroup, DISPATCH_TIME_FOREVER);
+ }
+
+ dispatch_group_notify(scanGroup, dispatch_get_main_queue(), ^{
+ NSLog(@"Scan complete. Found %lu open ports.", (unsigned long)foundIPs.count);
+ if (completion) {
+ completion([foundIPs copy]);
+ }
+ });
+}
+
- (NSString *)getDeviceIPAddress {
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;