Private
Public Access
1
0
Files
Kordophone/kordophone/Bridge/MBIMBridge.m

264 lines
7.5 KiB
Mathematica
Raw Normal View History

//
// MBIMBridge.m
// MessagesBridge
//
// Created by James Magahern on 11/12/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import "MBIMBridge.h"
2019-01-22 23:31:36 -08:00
#import "MBIMBridge_Private.h"
2018-11-13 12:29:15 -08:00
#import "MBIMBridgeOperation.h"
2018-11-17 01:07:55 -08:00
#import "MBIMConcurrentHTTPServer.h"
#import "MBIMHTTPConnection.h"
2018-11-17 01:07:55 -08:00
#import "MBIMUpdateQueue.h"
2018-11-13 22:39:03 -08:00
#import "hooking.h"
2018-11-16 01:40:02 -08:00
#import <CocoaHTTPServer/HTTPServer.h>
#import <IMCore/IMCore.h>
#import <IMCore/IMCore_Private.h>
#import <IMFoundation/IMFoundation.h>
#import <IMFoundation/IMFoundation_Private.h>
2019-01-22 23:31:36 -08:00
static const UInt16 kDefaultPort = 5738;
2018-11-12 22:15:50 -08:00
static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
2019-01-22 23:31:36 -08:00
@interface MBIMBridge (/* INTERNAL */) {
__strong NSArray *_sslCertificateAndIdentity;
}
2018-11-17 01:07:55 -08:00
@property (nonatomic, strong) MBIMConcurrentHTTPServer *httpServer;
2018-11-13 12:29:15 -08:00
@property (nonatomic, strong) NSOperationQueue *operationQueue;
- (instancetype)_init;
@end
@implementation MBIMBridge
+ (instancetype)sharedInstance
{
static dispatch_once_t onceToken;
static __strong MBIMBridge *sharedBridge = nil;
dispatch_once(&onceToken, ^{
sharedBridge = [[MBIMBridge alloc] _init];
});
return sharedBridge;
}
- (instancetype)_init
{
self = [super init];
if (self) {
2019-01-22 23:31:36 -08:00
self.port = kDefaultPort;
2018-11-13 12:29:15 -08:00
_operationQueue = [[NSOperationQueue alloc] init];
2018-11-17 01:07:55 -08:00
_operationQueue.maxConcurrentOperationCount = 5;
}
return self;
}
2018-11-13 22:39:03 -08:00
- (void)_terminate
{
// *shrug*
2019-01-22 23:31:36 -08:00
exit(1);
}
- (NSArray *)sslCertificateAndIdentity
{
if (!_sslCertificateAndIdentity && self.sslCertPath) {
// Get the p12
NSError *error = nil;
NSData *certData = [NSData dataWithContentsOfFile:self.sslCertPath options:0 error:&error];
if (!certData || error) {
MBIMLogError(@"Unable to load SSL certificate from file: %@", [error localizedDescription]);
return nil;
}
CFArrayRef items = nil;
OSStatus status = SecPKCS12Import(
(__bridge CFDataRef)certData,
(__bridge CFDictionaryRef) @{
(__bridge id)kSecImportExportPassphrase : @"xNAq3vn)^PNu}[&gyQ4MZeV?J"
},
&items
);
if (status != noErr) {
MBIMLogError(@"Error importing PKCS12: SecPKCS12Import status: %d", status);
return nil;
}
CFDictionaryRef certDict = CFArrayGetValueAtIndex(items, 0);
if (!certDict) {
MBIMLogError(@"Error parsing the SSL certificate");
return nil;
}
SecIdentityRef identity = (SecIdentityRef)CFDictionaryGetValue(certDict, kSecImportItemIdentity);
_sslCertificateAndIdentity = @[ (__bridge id)identity ];
}
return _sslCertificateAndIdentity;
}
- (void)checkSSLCertificate
{
if (self.usesSSL) {
NSArray *certAndIdentity = [self sslCertificateAndIdentity];
if ([certAndIdentity count]) {
MBIMLogInfo(@"SSL Certificate looks okay");
} else {
MBIMLogFatal(@"Wasn't able to load SSL certificate. Bailing...");
[self _terminate];
}
}
2018-11-13 22:39:03 -08:00
}
#pragma mark -
#pragma mark Connection
- (void)connect
{
2018-11-13 22:39:03 -08:00
#if HOOK_IMAGENT
char *errorString = nil;
BOOL hooked = HookIMAgent(self.dylibPath, &errorString);
if (!hooked) {
NSString *errorNSString = [NSString stringWithUTF8String:errorString];
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Error hooking imagent: %@", errorNSString);
2018-11-13 22:39:03 -08:00
return;
}
#endif
2019-01-22 23:31:36 -08:00
[self registerForNotifications];
[sDaemonController setDelegate:self];
[sDaemonListener addHandler:self];
if (![sDaemonController hasListenerForID:MBIMBridgeToken]) {
if (![sDaemonController addListenerID:MBIMBridgeToken capabilities:(kFZListenerCapFileTransfers | kFZListenerCapManageStatus | kFZListenerCapChats | kFZListenerCapMessageHistory | kFZListenerCapIDQueries | kFZListenerCapSendMessages)]) {
2019-01-22 23:31:36 -08:00
MBIMLogFatal(@"Failed to connect to imagent");
2018-11-13 22:39:03 -08:00
[self _terminate];
}
}
2019-01-22 23:31:36 -08:00
[self checkSSLCertificate];
[self startWebServer];
}
- (void)disconnect
{
[sDaemonController removeListenerID:MBIMBridgeToken];
}
#pragma mark -
#pragma mark Notifications
- (void)registerForNotifications
{
(void)[IMChatRegistry sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_messageReceived:) name:IMChatMessageReceivedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_chatRegistryDidLoad:) name:IMChatRegistryDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_chatItemsDidChange:) name:IMChatItemsDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_unreadCountChanged:) name:IMChatRegistryUnreadCountChangedNotification object:nil];
}
- (void)_unreadCountChanged:(NSNotification *)notification
{
// Not a lot of useful information plumbed here...
}
- (void)_messageReceived:(NSNotification *)notification
{
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Received message from chat with GUID: %@", [[notification object] guid]);
2018-11-17 01:07:55 -08:00
IMChat *chat = [notification object];
IMMessage *message = [[notification userInfo] objectForKey:IMChatValueKey];
if (chat && message) {
if (![message isFromMe]) {
MBIMUpdateItem *updateItem = [[MBIMUpdateItem alloc] init];
updateItem.changedChat = chat;
updateItem.addedMessage = message;
2018-11-17 01:07:55 -08:00
[[MBIMUpdateQueue sharedInstance] enqueueUpdateItem:updateItem];
} else {
// TODO: care about messages from me?
}
}
}
- (void)_chatRegistryDidLoad:(NSNotification *)notification
{
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Loaded chat registry. %lu existing chats", (unsigned long)[sChatRegistry numberOfExistingChats]);
}
- (void)_chatItemsDidChange:(NSNotification *)notification
{
IMChat *chat = [notification object];
if (chat) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Chat items change for GUID: %@", [chat guid]);
MBIMUpdateItem *updateItem = [[MBIMUpdateItem alloc] init];
updateItem.changedChat = chat;
[[MBIMUpdateQueue sharedInstance] enqueueUpdateItem:updateItem];
}
}
2018-11-13 12:29:15 -08:00
#pragma mark -
#pragma mark Web Server initialization
- (void)startWebServer
{
2018-11-17 01:07:55 -08:00
self.httpServer = [[MBIMConcurrentHTTPServer alloc] init];
[self.httpServer setConnectionClass:[MBIMHTTPConnection class]];
2019-01-22 23:31:36 -08:00
[self.httpServer setPort:self.port];
NSError *error = nil;
if (![self.httpServer start:&error]) {
2019-01-22 23:31:36 -08:00
MBIMLogError(@"Error starting HTTP server: %@", [error localizedDescription]);
} else {
MBIMLogNotify(@"Started Kordophone HTTP server on port %u", self.port);
}
}
#pragma mark -
#pragma mark Daemon lifecycle
- (void)daemonControllerWillConnect
{
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Connecting to imagent...");
}
- (void)daemonControllerDidConnect
{
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"imagent responded.");
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
if (iMessageAccount) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Successfully got accounts from imagent");
MBIMLogInfo(@"iMessage account connected: %@", iMessageAccount);
} else {
2019-01-22 23:31:36 -08:00
MBIMLogFatal(@"ERROR: imagent returned no accounts (not entitled? speak with Agent Hook)");
2018-11-13 22:39:03 -08:00
[self _terminate];
}
}
- (void)daemonControllerDidDisconnect
{
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Disconnected from imagent");
}
- (void)daemonConnectionLost
{
2019-01-22 23:31:36 -08:00
MBIMLogError(@"Connection lost to imagent");
}
@end