-
Notifications
You must be signed in to change notification settings - Fork 12
/
PSAndromeda.mm
250 lines (212 loc) · 8.5 KB
/
PSAndromeda.mm
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
// Copyright (c) 2021 udevs
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#import "PSAndromeda.h"
#import "Vexillarius.h"
#import "PrivateHeaders.h"
#import <stdio.h>
static xpc_object_t generalQueriesMessage(BOOL fastUnlock){
xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_bool(message, kPerseusEvent, YES);
xpc_object_t queries = xpc_array_create(NULL, 0);
xpc_array_set_int64(queries, ((size_t)(-1)), PSQueryTypeWristState);
xpc_array_set_int64(queries, ((size_t)(-1)), PSQueryTypeLockState);
xpc_array_set_int64(queries, ((size_t)(-1)), PSQueryTypeNearby);
xpc_array_set_int64(queries, ((size_t)(-1)), PSQueryTypeActive);
xpc_array_set_int64(queries, ((size_t)(-1)), PSQueryTypeConnected);
xpc_array_set_int64(queries, ((size_t)(-1)), (fastUnlock?PSQueryTypeRSSICache:PSQueryTypeRSSI));
xpc_array_set_int64(queries, ((size_t)(-1)), PSQueryTypeGizmoName);
xpc_dictionary_set_value(message, kPerseusQuery, queries);
return message;
}
#ifndef PERSEUSPREFS
static const char* fullImagePathNamed(const char* name, const char* fileExt, int mode){
static char str[128];
snprintf(str, sizeof(str), "/Library/Application Support/Perseus.bundle/%s-%s.%s", name, (mode==0?"Light":"Dark"), fileExt);
return str;
}
xpc_object_t vexillariusMessage(const char *title, const char *subTitle, const char *imageName, double timeout){
xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_double(message, VXKey.timeout, timeout?:2.0);
xpc_dictionary_set_string(message, VXKey.title, title);
xpc_dictionary_set_string(message, VXKey.subtitle, subTitle);
if (@available(iOS 13.0, *)){
if ([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleLight){
const char *imgPath = fullImagePathNamed(imageName, "png", 1);
xpc_dictionary_set_string(message, VXKey.leadingImagePath, imgPath);
}else{
const char *imgPath = fullImagePathNamed(imageName, "png", 0);
xpc_dictionary_set_string(message, VXKey.leadingImagePath, imgPath);
}
}
return message;
}
#endif //PERSEUSPREFS
static xpc_object_t invalidateRSSIMessage(){
xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_bool(message, kPerseusEvent, YES);
xpc_object_t queries = xpc_array_create(NULL, 0);
xpc_array_set_bool(queries, ((size_t)(-1)), PSQueryTypeInvalidateRSSI);
xpc_dictionary_set_value(message, kPerseusQuery, queries);
return message;
}
static xpc_connection_t sdXPCConnection(){
xpc_connection_t connection =
xpc_connection_create_mach_service("com.apple.sharingd.nsxpc", NULL, 0);
xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
});
xpc_connection_resume(connection);
return connection;
}
void sendPerseusQueryWithReply(xpc_object_t message, xpc_handler_t handler){
xpc_connection_t sdConnection = sdXPCConnection();
if (sdConnection){
xpc_connection_send_message_with_reply(sdConnection, message, dispatch_get_main_queue(), ^(xpc_object_t reply){
if (handler){
if (xpc_get_type(reply) == XPC_TYPE_DICTIONARY){
xpc_object_t result = xpc_dictionary_get_value(reply, "result");
handler(result);
}else{
handler(xpc_dictionary_create(NULL, NULL, 0));
}
}
});
}
}
void sendGeneralPerseusQueryWithReply(BOOL fastUnlock, xpc_handler_t handler){
sendPerseusQueryWithReply(generalQueriesMessage(fastUnlock), handler);
}
void sendInvalidateRSSIPerseusQueryWithReply(xpc_handler_t handler){
sendPerseusQueryWithReply(invalidateRSSIMessage(), handler);
}
#ifndef PERSEUSPREFS
static xpc_connection_t vxXPCConnection(){
xpc_connection_t connection =
xpc_connection_create_mach_service("com.udevs.vexillarius", NULL, 0);
xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
});
xpc_connection_resume(connection);
return connection;
}
void sendVexillariusMessage(xpc_object_t message){
xpc_connection_t vxConnection = vxXPCConnection();
if (vxConnection){
xpc_connection_send_message(vxConnection, message);
}
}
static void replyWithObject(xpc_object_t replyObject, xpc_object_t event){
xpc_connection_t remote = xpc_dictionary_get_remote_connection(event);
xpc_object_t reply = xpc_dictionary_create_reply(event);
xpc_dictionary_set_value(reply, "result", replyObject);
xpc_connection_send_message(remote, reply);
}
void handlePerseusEvent(xpc_object_t event){
xpc_object_t queries = xpc_dictionary_get_value(event, kPerseusQuery);
xpc_object_t xpcReplyObject = xpc_dictionary_create(NULL, NULL, 0);
if (xpc_get_type(queries) == XPC_TYPE_ARRAY){
//Ensure latest connection state
SDPairedDeviceAgent *pairedDeviceAgent = [objc_getClass("SDPairedDeviceAgent") sharedAgent];
dispatch_sync(pairedDeviceAgent.dispatchQueue, ^{
NSMutableDictionary *msg = [@{
@"met":@1,
@"rin":@1
} mutableCopy];
if (@available(iOS 14.0, *)){
[pairedDeviceAgent _idsSendStateUpdate:msg asWaking:YES];
}else{
[pairedDeviceAgent _idsSendStateUpdate:msg];
}
});
SDStatusMonitor *statusMonitor = [objc_getClass("SDStatusMonitor") sharedMonitor];
IDSService *service = [pairedDeviceAgent valueForKey:@"_idsService"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.isDefaultPairedDevice == 1"];
IDSDevice *defaultPairedDevice = [[service.devices filteredArrayUsingPredicate:predicate] firstObject];
xpc_array_apply(queries, ^_Bool(size_t index, xpc_object_t value){
xpc_type_t valueType = xpc_get_type(value);
if (valueType == XPC_TYPE_INT64){
switch (xpc_int64_get_value(value)){
case PSQueryTypeWristState:{
xpc_dictionary_set_int64(xpcReplyObject, "pairedWatchWristState", (int64_t)statusMonitor.pairedWatchWristState);
break;
}
case PSQueryTypeLockState:{
xpc_dictionary_set_int64(xpcReplyObject, "pairedWatchLockState", (int64_t)statusMonitor.pairedWatchLockState);
break;
}
case PSQueryTypeNearby:{
if (defaultPairedDevice){
xpc_dictionary_set_bool(xpcReplyObject, "nearby", defaultPairedDevice.nearby);
}
break;
}
case PSQueryTypeActive:{
if (defaultPairedDevice){
xpc_dictionary_set_bool(xpcReplyObject, "active", defaultPairedDevice.isActive);
}
break;
}
case PSQueryTypeConnected:{
if (defaultPairedDevice){
xpc_dictionary_set_bool(xpcReplyObject, "connected", defaultPairedDevice.connected);
}
break;
}
case PSQueryTypeInvalidateRSSI:{
currentRssi = 1;
break;
}
case PSQueryTypeRSSI:{
xpc_dictionary_set_int64(xpcReplyObject, "rssi", currentRssi);
break;
}
case PSQueryTypeGizmoName:{
if (defaultPairedDevice){
xpc_dictionary_set_string(xpcReplyObject, "name", [defaultPairedDevice.name UTF8String]);
}
break;
}
case PSQueryTypeRSSICache:{
if (cachedRssi < 0 && currentRssi > 0){
xpc_dictionary_set_int64(xpcReplyObject, "rssi", cachedRssi);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (currentRssi > 0){
CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)FORCE_LOCK_NN, NULL, NULL, kCFNotificationDeliverImmediately);
}
});
}else{
xpc_dictionary_set_int64(xpcReplyObject, "rssi", currentRssi);
}
break;
}
default:
break;
}
}
return YES;
});
}
replyWithObject(xpcReplyObject, event);
}
id valueForKey(NSString *key){
CFStringRef appID = (__bridge CFStringRef)PERSEUS_IDENTIFIER;
CFPreferencesAppSynchronize(appID);
CFArrayRef keyList = CFPreferencesCopyKeyList(appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
if (keyList != NULL){
BOOL containsKey = CFArrayContainsValue(keyList, CFRangeMake(0, CFArrayGetCount(keyList)), (__bridge CFStringRef)key);
CFRelease(keyList);
if (!containsKey) return nil;
return CFBridgingRelease(CFPreferencesCopyAppValue((__bridge CFStringRef)key, appID));
}
return nil;
}
#endif //PERSEUSPREFS