-
Notifications
You must be signed in to change notification settings - Fork 23
/
API.x
322 lines (309 loc) · 11.2 KB
/
API.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
321
322
#import <CommonCrypto/CommonDigest.h>
#import <YouTubeHeader/YTLikeStatus.h>
#import <HBLog.h>
#import "Settings.h"
static NSString *getUserID() {
return [[NSUserDefaults standardUserDefaults] stringForKey:UserIDKey];
}
static BOOL isRegistered() {
return [[NSUserDefaults standardUserDefaults] boolForKey:RegistrationConfirmedKey];
}
static int toRYDLikeStatus(YTLikeStatus likeStatus) {
switch (likeStatus) {
case YTLikeStatusLike:
return 1;
case YTLikeStatusDislike:
return -1;
default:
return 0;
}
}
static const char *charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// Ported to objc from RYD browser extension
static NSString *generateUserID() {
NSString *existingID = getUserID();
if (existingID) return existingID;
HBLogDebug(@"%@", @"generateUserID()");
char userID[36 + 1];
for (int i = 0; i < 36; ++i)
userID[i] = charset[arc4random_uniform(62)];
userID[36] = '\0';
NSString *result = [NSString stringWithUTF8String:userID];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:result forKey:UserIDKey];
[defaults synchronize];
return result;
}
// Ported to objc from RYD browser extension
static int countLeadingZeroes(uint8_t *hash) {
int zeroes = 0;
int value = 0;
for (int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) {
value = hash[i];
if (value == 0)
zeroes += 8;
else {
int count = 1;
if (value >> 4 == 0) {
count += 4;
value <<= 4;
}
if (value >> 6 == 0) {
count += 2;
value <<= 2;
}
zeroes += count - (value >> 7);
break;
}
}
return zeroes;
}
// Ported to objc from RYD browser extension
static NSString *btoa(NSString *input) {
NSMutableString *output = [NSMutableString string];
for (int i = 0; i < input.length; i += 3) {
int groupsOfSix[4] = { -1, -1, -1, -1 };
unichar ci = [input characterAtIndex:i];
groupsOfSix[0] = ci >> 2;
groupsOfSix[1] = (ci & 0x03) << 4;
if (input.length > i + 1) {
unichar ci1 = [input characterAtIndex:i + 1];
groupsOfSix[1] |= ci1 >> 4;
groupsOfSix[2] = (ci1 & 0x0f) << 2;
}
if (input.length > i + 2) {
unichar ci2 = [input characterAtIndex:i + 2];
groupsOfSix[2] |= ci2 >> 6;
groupsOfSix[3] = ci2 & 0x3f;
}
for (int j = 0; j < 4; ++j) {
if (groupsOfSix[j] == -1)
[output appendString:@"="];
else
[output appendFormat:@"%c", charset[groupsOfSix[j]]];
}
}
return output;
}
void fetch(
NSString *endpoint,
NSString *method,
NSDictionary *body,
void (^dataHandler)(NSDictionary *data),
BOOL (^responseCodeHandler)(NSUInteger responseCode),
void (^networkErrorHandler)(void),
void (^dataErrorHandler)(void)
) {
HBLogDebug(@"fetch() (%@, %@)", endpoint, method);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @(API_URL), endpoint]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
urlRequest.HTTPMethod = method;
if (body) {
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
if (dataErrorHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
dataErrorHandler();
});
}
return;
}
HBLogDebug(@"fetch() POST body: %@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
urlRequest.HTTPBody = data;
} else
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[[session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSUInteger responseCode = [httpResponse statusCode];
if (responseCodeHandler) {
if (!responseCodeHandler(responseCode))
return;
}
if (error || responseCode != 200) {
HBLogDebug(@"fetch() error requesting: %@ (%lu)", error, responseCode);
if (networkErrorHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
networkErrorHandler();
});
}
return;
}
NSError *jsonError;
NSDictionary *myData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingFragmentsAllowed error:&jsonError];
if (jsonError) {
HBLogDebug(@"fetch() error decoding response: %@", jsonError);
if (dataErrorHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
dataErrorHandler();
});
}
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
dataHandler(myData);
});
}] resume];
}
// Ported to objc from RYD browser extension
static NSString *solvePuzzle(NSDictionary *data) {
NSString *solution = nil;
NSString *challenge = data[@"challenge"];
int difficulty = [data[@"difficulty"] intValue];
NSData *cd = [[NSData alloc] initWithBase64EncodedString:challenge options:0];
NSString *decoded = [[NSString alloc] initWithData:cd encoding:NSASCIIStringEncoding];
uint8_t c[decoded.length];
char *buffer = (char *)calloc(20, sizeof(char));
uint32_t *uInt32View = (uint32_t *)buffer;
for (int i = 0; i < decoded.length; ++i)
c[i] = [decoded characterAtIndex:i];
int maxCount = (1 << difficulty) * 3;
for (int i = 4; i < 20; ++i)
buffer[i] = c[i - 4];
for (int i = 0; i < maxCount; ++i) {
uInt32View[0] = i;
uint8_t hash[CC_SHA512_DIGEST_LENGTH] = {0};
CC_SHA512(buffer, 20, hash);
if (countLeadingZeroes(hash) >= difficulty) {
char chars[4] = { buffer[0], buffer[1], buffer[2], buffer[3] };
NSString *s = [[NSString alloc] initWithBytes:chars length:4 encoding:NSASCIIStringEncoding];
solution = btoa(s);
HBLogDebug(@"solvePuzzle() success (%@)", solution);
break;
}
}
free(buffer);
if (!solution)
HBLogDebug(@"%@", @"solvePuzzle() failed");
return solution;
}
// Ported to objc from RYD browser extension
static void registerUser() {
NSString *userId = generateUserID();
HBLogDebug(@"registerUser() (%@)", userId);
NSString *puzzleEndpoint = [NSString stringWithFormat:@"/puzzle/registration?userId=%@", userId];
fetch(
puzzleEndpoint,
@"GET",
nil,
^(NSDictionary *data) {
NSString *solution = solvePuzzle(data);
if (!solution) {
HBLogDebug(@"%@", @"registerUser() skipped");
return;
}
fetch(
puzzleEndpoint,
@"POST",
@{ @"solution": solution },
^(NSDictionary *data) {
if ([data isKindOfClass:[NSNumber class]] && ![(NSNumber *)data boolValue]) {
HBLogInfo(@"%@", @"registerUser() failed");
return;
}
if (!isRegistered()) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RegistrationConfirmedKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
HBLogDebug(@"registerUser() success or already registered: %@", data);
},
NULL,
^() {
HBLogDebug(@"%@", @"registerUser() puzzle failed (network)");
},
^() {
HBLogDebug(@"%@", @"registerUser() puzzle failed (data)");
}
);
},
NULL,
^() {
HBLogDebug(@"%@", @"registerUser() failed (network)");
},
^() {
HBLogDebug(@"%@", @"registerUser() failed (data)");
}
);
}
// Ported to objc from RYD browser extension
void _sendVote(NSString *videoId, YTLikeStatus s, int retryCount) {
if (retryCount <= 0) return;
NSString *userId = getUserID();
if (!userId || !isRegistered()) {
registerUser();
return;
}
int likeStatus = toRYDLikeStatus(s);
HBLogDebug(@"sendVote(%@, %d)", videoId, likeStatus);
fetch(
@"/interact/vote",
@"POST",
@{
@"userId": userId,
@"videoId": videoId,
@"value": @(likeStatus)
},
^(NSDictionary *data) {
NSString *solution = solvePuzzle(data);
if (!solution) {
HBLogDebug(@"%@", @"sendVote() skipped");
return;
}
fetch(
@"/interact/confirmVote",
@"POST",
@{
@"userId": userId,
@"videoId": videoId,
@"solution": solution
},
^(NSDictionary *data) {
HBLogDebug(@"%@", @"sendVote() success");
},
NULL,
^() {
HBLogDebug(@"%@", @"sendVote() confirm failed (network)");
},
^() {
HBLogDebug(@"%@", @"sendVote() confirm failed (data)");
}
);
},
^BOOL(NSUInteger responseCode) {
if (responseCode == 401) {
HBLogDebug(@"%@", @"sendVote() error 401, trying again");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
registerUser();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
_sendVote(videoId, s, retryCount - 1);
});
});
return NO;
}
return YES;
},
^() {
HBLogDebug(@"%@", @"sendVote() failed (network)");
},
^() {
HBLogDebug(@"%@", @"sendVote() failed (data)");
}
);
}
void sendVote(NSString *videoId, YTLikeStatus s) {
_sendVote(videoId, s, 3);
}
%ctor {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:DidResetUserIDKey]) {
[defaults setBool:YES forKey:DidResetUserIDKey];
NSString *userID = [defaults stringForKey:UserIDKey];
if ([userID containsString:@"+"] || [userID containsString:@"/"]) {
[defaults removeObjectForKey:UserIDKey];
[defaults removeObjectForKey:RegistrationConfirmedKey];
}
[defaults synchronize];
}
}