Skip to content

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

add devices endpoint

roryclear9a126f216c40
diff --git a/clearcam/FileServer.h b/clearcam/FileServer.h
index 142c8f3..e159742 100644
--- a/clearcam/FileServer.h
+++ b/clearcam/FileServer.h
@@ -1,4 +1,5 @@
#import <Foundation/Foundation.h>
+#import "PortScanner.h"
@interface FileServer : NSObject
@@ -6,5 +7,6 @@
@property (nonatomic, strong) NSMutableDictionary *segmentsDict;
@property (nonatomic, assign) NSInteger segment_length;
@property (nonatomic, strong) NSDate *last_req_time;
+@property (nonatomic, strong) PortScanner *scanner;
@end
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index 665388e..f6e32cd 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -8,6 +8,7 @@
#import <errno.h>
#import <AVFoundation/AVFoundation.h>
#import "SettingsManager.h"
+#import "PortScanner.h"
@interface FileServer ()
@property (nonatomic, strong) NSString *basePath;
@@ -19,6 +20,7 @@
- (void)start {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.segment_length = 60;
+ self.scanner = [[PortScanner alloc] init];
self.last_req_time = [NSDate now];
self.segmentsDict = [[NSMutableDictionary alloc] init];
self.basePath = [self getDocumentsDirectory];
@@ -135,6 +137,26 @@
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);
+ NSError *error;
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:openPorts options:0 error:&error];
+
+ if (!jsonData) {
+ NSLog(@"Error serializing JSON: %@", error);
+ NSString *httpHeader = @"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n";
+ send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
+ return;
+ }
+
+ 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;
+ }];
+ }
+
if ([filePath hasPrefix:@"main"]) {
NSString *playerFilePath = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"html"];
if (playerFilePath && [[NSFileManager defaultManager] fileExistsAtPath:playerFilePath]) {
diff --git a/clearcam/PortScanner.m b/clearcam/PortScanner.m
index b589101..da1822b 100644
--- a/clearcam/PortScanner.m
+++ b/clearcam/PortScanner.m
@@ -114,25 +114,25 @@
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_HIGH, 0);
for (NSString *ip in ipList) {
- dispatch_group_async(scanGroup, scanQueue, ^{
+ dispatch_group_enter(scanGroup);
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
if ([self isPortOpen:ip port:port]) {
@synchronized(self) {
NSLog(@"[+] Open port %d found at %@", port, ip);
[foundIPs addObject:ip];
}
}
+ dispatch_group_leave(scanGroup);
});
}
- dispatch_group_notify(scanGroup, dispatch_get_main_queue(), ^{
- NSLog(@"Scan complete. Found %lu open ports.", (unsigned long)foundIPs.count);
- if (completion) {
- completion([foundIPs copy]);
- }
- });
+ dispatch_group_wait(scanGroup, DISPATCH_TIME_FOREVER);
+ NSLog(@"Scan complete. Found %lu open ports.", (unsigned long)foundIPs.count);
+ if (completion) {
+ completion([foundIPs copy]);
+ }
}
- (NSString *)getDeviceIPAddress {
diff --git a/clearcam/ViewController.m b/clearcam/ViewController.m
index 25acd16..81eea79 100644
--- a/clearcam/ViewController.m
+++ b/clearcam/ViewController.m
@@ -3,7 +3,6 @@
#import <Metal/Metal.h>
#import "Yolo.h"
#import "FileServer.h"
-#import "PortScanner.h"
@interface ViewController ()
@@ -36,13 +35,6 @@ NSMutableDictionary *classColorMap;
- (void)viewDidLoad {
[super viewDidLoad];
-
- PortScanner *scanner = [[PortScanner alloc] init];
- NSLog(@"Scan started...");
- [scanner scanNetworkForPort:8080 completion:^(NSArray<NSString *> *openPorts) {
- NSLog(@"Scan DONE!!! Found IPs: %@", openPorts);
- }];
-
self.current_segment_squares = [[NSMutableArray alloc] init];
self.digits = [NSMutableDictionary dictionary];
self.digits[@"0"] = @[@[ @0, @0, @3, @1], @[ @0, @1, @1 , @3], @[ @2, @1, @1 , @3], @[ @0, @4, @3 , @1]];