-
Notifications
You must be signed in to change notification settings - Fork 19
/
Tweak.x
320 lines (302 loc) · 13.6 KB
/
Tweak.x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#import "Tweak.h"
struct SBIconImageInfo iconspecs;
//Settings
BOOL receiver;
BOOL errorlog;
BOOL lockstateenabled;
int pcspecifier;
//Settings
int methodspecifier;
BOOL keyauthentication;
NSString *user;
NSString *ip;
NSString *port;
NSString *password;
NSString *command;
NSString *finalCommand;
NSArray *arguments;
//Notifications
NSString *pc;
NSString *title;
NSMutableString *finalTitle;
NSString *message;
NSMutableString *finalMessage;
NSString *bundleID;
NSString *appName;
BOOL locked;
//For the error output
NSPipe *out;
static BBServer *notificationserver = nil;
static void loadPrefs() {
NSMutableDictionary *prefs = [NSMutableDictionary dictionaryWithContentsOfFile:@"/User/Library/Preferences/com.greg0109.forwardnotifierprefs.plist"];
receiver = prefs[@"receiver"] ? [prefs[@"receiver"] boolValue] : NO;
errorlog = prefs[@"errorlog"] ? [prefs[@"errorlog"] boolValue] : NO;
lockstateenabled = prefs[@"lockstateenabled"] ? [prefs[@"lockstateenabled"] boolValue] : YES;
pcspecifier = prefs[@"pcspecifier"] ? [prefs[@"pcspecifier"] intValue] : 0;
methodspecifier = prefs[@"methodspecifier"] ? [prefs[@"methodspecifier"] intValue] : 0;
keyauthentication = prefs[@"keyauthentication"] ? [prefs[@"keyauthentication"] boolValue] : NO;
user = prefs[@"user"] && !([prefs[@"user"] isEqualToString:@""]) ? [prefs[@"user"] stringValue] : @"user";
ip = prefs[@"ip"] && !([prefs[@"ip"] isEqualToString:@""]) ? [prefs[@"ip"] stringValue] : @"ip";
port = prefs[@"port"] && !([prefs[@"port"] isEqualToString:@""]) ? [prefs[@"port"] stringValue] : @"22";
password = prefs[@"password"] && !([prefs[@"password"] isEqualToString:@""]) ? [prefs[@"password"] stringValue] : @"password";
user = [user stringByReplacingOccurrencesOfString:@" " withString:@""];
ip = [ip stringByReplacingOccurrencesOfString:@" " withString:@""];
password = [password stringByReplacingOccurrencesOfString:@" " withString:@""];
}
static dispatch_queue_t getBBServerQueue() {
static dispatch_queue_t queue;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
void *handle = dlopen(NULL, RTLD_GLOBAL);
if (handle) {
dispatch_queue_t __weak *pointer = (__weak dispatch_queue_t *) dlsym(handle, "__BBServerQueue");
if (pointer) {
queue = *pointer;
}
dlclose(handle);
}
});
return queue;
}
%hook BBServer
-(id)initWithQueue:(id)arg1 {
notificationserver = %orig;
return notificationserver;
}
-(id)initWithQueue:(id)arg1 dataProviderManager:(id)arg2 syncService:(id)arg3 dismissalSyncCache:(id)arg4 observerListener:(id)arg5 utilitiesListener:(id)arg6 conduitListener:(id)arg7 systemStateListener:(id)arg8 settingsListener:(id)arg9 {
notificationserver = %orig;
return notificationserver;
}
- (void)dealloc {
if (notificationserver == self) {
notificationserver = nil;
}
%orig;
}
%end
void testnotif(NSString *titletest, NSString *messagetest) {
BBBulletin *bulletin = [[%c(BBBulletin) alloc] init];
bulletin.title = titletest;
bulletin.message = messagetest;
bulletin.sectionID = @"com.apple.Preferences";
bulletin.bulletinID = [[NSProcessInfo processInfo] globallyUniqueString];
bulletin.recordID = [[NSProcessInfo processInfo] globallyUniqueString];
bulletin.publisherBulletinID = [[NSProcessInfo processInfo] globallyUniqueString];
bulletin.date = [NSDate date];
bulletin.defaultAction = [%c(BBAction) actionWithLaunchBundleID:@"prefs:root=ForwardNotifier" callblock:nil];
dispatch_sync(getBBServerQueue(), ^{
[notificationserver publishBulletin:bulletin destinations:14];
});
}
BOOL isItLocked() {
if (lockstateenabled) {
locked = [[%c(SBLockStateAggregator) sharedInstance] lockState];
} else {
locked = TRUE;
}
return locked;
}
void sanitizeText() { //Thanks Tom for the idea of using \ everywhere :P
finalTitle = [@"" mutableCopy];
for (int i=0; i<title.length; i++) {
NSString *charSelected = [title substringWithRange:NSMakeRange(i, 1)];
if ([charSelected isEqualToString:@" "]) {
charSelected = @" ";
} else {
charSelected = [NSString stringWithFormat:@"\\%@",charSelected];
}
[finalTitle appendString:charSelected];
}
finalMessage = [@"" mutableCopy];
for (int i=0; i<message.length; i++) {
NSString *charSelected = [message substringWithRange:NSMakeRange(i, 1)];
if ([charSelected isEqualToString:@" "]) {
charSelected = @" ";
} else {
charSelected = [NSString stringWithFormat:@"\\%@",charSelected];
}
[finalMessage appendString:charSelected];
}
}
void pushnotif(BOOL override) {
if (!override) {
isItLocked();
} else {
locked = TRUE;
}
if (methodspecifier == 0) { // SSH
if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"ForwardNotifier-Status"] isEqual:@"1"] && (locked)) {
dispatch_queue_t sendnotif = dispatch_queue_create("Send Notif", NULL);
dispatch_async(sendnotif, ^{
pc = [NSString stringWithFormat:@"%@@%@",user,ip];
sanitizeText();
if (pcspecifier == 0) { // Linux
finalCommand = [NSString stringWithFormat:@"\"$(echo %@)\" \"$(echo %@)\"", finalTitle, finalMessage];
command = [NSString stringWithFormat:@"notify-send -i applications-development %@",finalCommand];
NSLog(@"ForwardNotifier: %@", command);
} else if (pcspecifier == 1) { // MacOS
finalCommand = [NSString stringWithFormat:@"-title \"$(echo %@)\" -message \"$(echo %@)\"", finalTitle, finalMessage];
command = [NSString stringWithFormat:@"/usr/local/bin/terminal-notifier -sound pop %@",finalCommand];
} else if (pcspecifier == 2) { // iOS
finalCommand = [NSString stringWithFormat:@"\"$(echo %@)\" \"$(echo %@)\"", finalTitle, finalMessage];
command = [NSString stringWithFormat:@"ForwardNotifierReceiver %@",finalCommand];
} else if (pcspecifier == 3) { // Windows
finalCommand = [NSString stringWithFormat:@"-title \"$(echo %@)\" -message \"$(echo %@)\"", finalTitle, finalMessage];
command = [NSString stringWithFormat:@"ForwardNotifierReceiver %@",finalCommand];
}
if (keyauthentication) {
if ([port isEqual:@"22"]) {
arguments = @[@"-i",password,pc,command];
} else {
arguments = @[@"-i",password,pc,@"-p",port,command];
}
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/ssh"];
[task setArguments:arguments];
out = [NSPipe pipe];
[task setStandardError:out];
[task launch];
[task waitUntilExit];
} else {
if ([port isEqual:@"22"]) {
arguments = @[@"-p",password,@"ssh",@"-o",@"StrictHostKeyChecking=no",pc,command];
} else {
arguments = @[@"-p",password,@"ssh",@"-o",@"StrictHostKeyChecking=no",pc,@"-p",port,command];
}
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/sshpass"];
[task setArguments:arguments];
out = [NSPipe pipe];
[task setStandardError:out];
[task launch];
[task waitUntilExit];
}
NSFileHandle * read = [out fileHandleForReading];
NSData * dataRead = [read readDataToEndOfFile];
NSString *erroroutput = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];
if ([erroroutput length] > 2 && errorlog) {
testnotif(@"ForwardNotifier Error",erroroutput);
}
});
}
} else if (methodspecifier == 1) { // Crossplatform Server
// Get Icon data
SBApplicationIcon *icon = [((SBIconController *)[%c(SBIconController) sharedInstance]).model expectedIconForDisplayIdentifier:bundleID];
UIImage *image = nil;
iconspecs.size = CGSizeMake(60, 60);
iconspecs.scale = [UIScreen mainScreen].scale;
iconspecs.continuousCornerRadius = 12;
image = [icon generateIconImageWithInfo:iconspecs];
NSData *iconData = UIImagePNGRepresentation(image);
NSString *iconBase64;
if (![title isEqualToString:@"ForwardNotifier Test"]) {
iconBase64 = [iconData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
} else {
iconBase64 = forwardNotifierIconBase64;
}
// Base64 both title and message
NSData *titleData = [title dataUsingEncoding: NSUTF8StringEncoding];
NSString *titleBase64 = [titleData base64EncodedStringWithOptions:0];
NSData *messageData = [message dataUsingEncoding: NSUTF8StringEncoding];
NSString *messageBase64 = [messageData base64EncodedStringWithOptions:0];
if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"ForwardNotifier-Status"] isEqual:@"1"] && (locked)) {
dispatch_queue_t sendnotif = dispatch_queue_create("Send Notif", NULL);
dispatch_async(sendnotif, ^{
title = [title stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""\""];
message = [message stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""\""];
if (pcspecifier == 0) { // Linux
command = [NSString stringWithFormat:@"{\"Title\": \"%@\", \"Message\": \"%@\", \"OS\": \"Linux\", \"img\": \"%@\", \"appname\": \"%@\"}",titleBase64,messageBase64,iconBase64,appName];
} else if (pcspecifier == 1) { // MacOS
command = [NSString stringWithFormat:@"{\"Title\": \"%@\", \"Message\": \"%@\", \"OS\": \"MacOS\", \"img\": \"%@\"}",titleBase64,messageBase64,iconBase64];
} else if (pcspecifier == 2) { // iOS
command = [NSString stringWithFormat:@"{\"Title\": \"%@\", \"Message\": \"%@\", \"OS\": \"iOS\", \"img\": \"%@\"}",titleBase64,messageBase64,iconBase64];
} else if (pcspecifier == 3) { // Windows
command = [NSString stringWithFormat:@"{\"Title\": \"%@\", \"Message\": \"%@\", \"OS\": \"Windows\", \"img\": \"%@\"}",titleBase64,messageBase64,iconBase64];
}
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/curl"];
[task setArguments:@[@"-sS",[NSString stringWithFormat:@"%@:8000",ip],@"-d",command ]];
out = [NSPipe pipe];
[task setStandardError:out];
[task launch];
[task waitUntilExit];
NSFileHandle * read = [out fileHandleForReading];
NSData * dataRead = [read readDataToEndOfFile];
NSString *erroroutput = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];
if ([erroroutput length] > 2 && errorlog) {
if ([erroroutput containsString:@"curl"] && ![erroroutput containsString:@"Empty reply"]) {
testnotif(@"ForwardNotifier Error",erroroutput);
}
}
});
}
}
}
%group sender
%hook BBServer
-(void)publishBulletin:(BBBulletin *)arg1 destinations:(unsigned long long)arg2 {
%orig;
title = arg1.content.title;
message = arg1.content.message;
bundleID = arg1.sectionID;
SBApplication *app = [[%c(SBApplicationController) sharedInstance] applicationWithBundleIdentifier:bundleID];
appName = app.displayName;
if (([title length] != 0) || ([message length] != 0)) {
if ([title length] == 0) {
title = app.displayName;
}
if (![title containsString:@"ForwardNotifier"] && [arg1.date timeIntervalSinceNow] > -2) { //This helps avoid the notifications to get forwarded again after a respring, which makes them avoid respring loops. If notifications are 2 seconds old, then won't get forwarded.
NSMutableDictionary *applist = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.greg0109.forwardnotifierblacklist"];
if (![applist valueForKey:arg1.sectionID] || [[NSString stringWithFormat:@"%@",[applist valueForKey:arg1.sectionID]] isEqual:@"0"]) {
pushnotif(FALSE);
}
} else if ([title isEqualToString:@"ForwardNotifier Test"]) {
pushnotif(TRUE);
}
}
}
%end
%hook SpringBoard
-(void)applicationDidFinishLaunching:(id)arg1 {
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:@"com.greg0109.forwardnotifierreceiver/notification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
NSString *titlenotif = notification.userInfo[@"title"];
NSString *messagenotif = notification.userInfo[@"message"];
if ([titlenotif isEqualToString:@"ActivateForwardNotifier"]) {
if ([messagenotif isEqualToString:@"true"]) {
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"ForwardNotifier-Status"];
} else if ([messagenotif isEqualToString:@"false"]) {
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"ForwardNotifier-Status"];
}
} else {
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"ForwardNotifier-Status"];
title = @"ForwardNotifier Test";
message = @"This is a test notification";
testnotif(title,message);
}
}];
%orig;
}
%end
%end
%group devicereceiver
%hook SpringBoard
-(void)applicationDidFinishLaunching:(id)arg1 {
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:@"com.greg0109.forwardnotifierreceiver/notification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
NSString *titlenotif = notification.userInfo[@"title"];
NSString *messagenotif = notification.userInfo[@"message"];
testnotif(titlenotif,messagenotif);
}];
%orig;
}
%end
%end
%ctor {
loadPrefs();
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.greg0109.forwardnotifierprefs.settingschanged"), NULL, CFNotificationSuspensionBehaviorCoalesce);
%init();
if (receiver) {
%init(devicereceiver);
} else {
%init(sender);
}
}