forked from GetiPlayerAutomator/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetiPlayerProxy.m
335 lines (313 loc) · 12.8 KB
/
GetiPlayerProxy.m
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
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// GetiPlayerProxy.m
// Get_iPlayer GUI
//
// Created by Thomas Willson on 8/7/14.
//
//
#import "GetiPlayerProxy.h"
@implementation GetiPlayerProxy
- (id)init
{
if (!(self = [super init])) return nil;
proxyDict = [NSMutableDictionary dictionary];
return self;
}
- (id)initWithLogger:(LogController *)logger {
if (![self init]) return nil;
self->logger = logger;
return self;
}
- (void)loadProxyInBackgroundForSelector:(SEL)selector withObject:(id)object onTarget:(id)target silently:(BOOL)silent
{
[self updateProxyLoadStatus:YES message:@"Loading proxy settings..."];
NSLog(@"INFO: Loading proxy settings...");
[logger addToLog:@"\n\nINFO: Loading proxy settings..."];
[proxyDict removeAllObjects];
proxyDict[@"selector"] = [NSValue valueWithPointer:selector];
proxyDict[@"target"] = target;
currentIsSilent = silent;
if (object)
proxyDict[@"object"] = object;
NSString *proxyOption = [[NSUserDefaults standardUserDefaults] valueForKey:@"Proxy"];
if ([proxyOption isEqualToString:@"Custom"])
{
NSString *customProxy = [[NSUserDefaults standardUserDefaults] valueForKey:@"CustomProxy"];
NSLog(@"INFO: Custom Proxy: address=[%@] length=%ld", customProxy, [customProxy length]);
[logger addToLog:[NSString stringWithFormat:@"INFO: Custom Proxy: address=[%@] length=%ld", customProxy, [customProxy length]]];
NSString *proxyValue = [customProxy stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([proxyValue length] == 0)
{
NSLog(@"WARNING: Custom proxy setting was blank. No proxy will be used.");
[logger addToLog:@"WARNING: Custom proxy setting was blank. No proxy will be used."];
if (!currentIsSilent)
{
NSAlert *alert = [NSAlert alertWithMessageText:@"Custom proxy setting was blank.\nDownloads may fail.\nDo you wish to continue?"
defaultButton:@"No"
alternateButton:@"Yes"
otherButton:nil
informativeTextWithFormat:@""];
[alert setAlertStyle:NSCriticalAlertStyle];
if ([alert runModal] == NSAlertDefaultReturn)
{
[self cancelProxyLoad];
}
else
{
[self failProxyLoad];
}
}
else
{
[self failProxyLoad];
}
}
else
{
proxyDict[@"proxy"] = [[HTTPProxy alloc] initWithString:proxyValue];
[self finishProxyLoad];
}
}
else if ([proxyOption isEqualToString:@"Provided"])
{
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSValue valueWithPointer:selector],@"selector",target,@"target", nil];
if (object){
[userInfo addEntriesFromDictionary:@{@"object": object}];
}
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://tom-tech.com/get_iplayer/proxy.txt"]];
[request setUserInfo:userInfo];
[request setDelegate:self];
[request setDidFailSelector:@selector(providedProxyDidFinish:)];
[request setDidFinishSelector:@selector(providedProxyDidFinish:)];
[request setTimeOutSeconds:10];
[request setNumberOfTimesToRetryOnTimeout:2];
[self updateProxyLoadStatus:YES message:[NSString stringWithFormat:@"Loading provided proxy (may take up to %ld seconds)...", (NSInteger)[request timeOutSeconds]]];
NSLog(@"INFO: Loading provided proxy (may take up to %ld seconds)...", (NSInteger)[request timeOutSeconds]);
[logger addToLog:[NSString stringWithFormat:@"INFO: Loading provided proxy (may take up to %ld seconds)...", (NSInteger)[request timeOutSeconds]*2]];
[request startAsynchronous];
}
else
{
NSLog(@"INFO: No proxy to load");
[logger addToLog:@"INFO: No proxy to load"];
[self finishProxyLoad];
}
}
- (void)providedProxyDidFinish:(ASIHTTPRequest *)request
{
NSData *urlData = [request responseData];
if ([request responseStatusCode] != 200 || !urlData)
{
NSLog(@"WARNING: Provided proxy could not be retrieved. No proxy will be used.");
[logger addToLog:@"WARNING: Provided proxy could not be retrieved. No proxy will be used."];
if (!currentIsSilent)
{
NSError *error = [request error];
NSAlert *alert = [NSAlert alertWithMessageText:@"Provided proxy could not be retrieved.\nDownloads may fail.\nDo you wish to continue?"
defaultButton:@"No"
alternateButton:@"Yes"
otherButton:nil
informativeTextWithFormat:@"Error: %@", (error ? [error localizedDescription] : @"Unknown error")];
[alert setAlertStyle:NSCriticalAlertStyle];
if ([alert runModal] == NSAlertDefaultReturn)
[self cancelProxyLoad];
else
[self failProxyLoad];
}
else
{
[self failProxyLoad];
}
}
else
{
NSString *proxyValue = [[[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding] lowercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([proxyValue length] == 0)
{
NSLog(@"WARNING: Provided proxy value was blank. No proxy will be used.");
[logger addToLog:@"WARNING: Provided proxy value was blank. No proxy will be used."];
if (!currentIsSilent)
{
NSAlert *alert = [NSAlert alertWithMessageText:@"Provided proxy value was blank.\nDownloads may fail.\nDo you wish to continue?"
defaultButton:@"No"
alternateButton:@"Yes"
otherButton:nil
informativeTextWithFormat:@""];
[alert setAlertStyle:NSCriticalAlertStyle];
if ([alert runModal] == NSAlertDefaultReturn)
[self cancelProxyLoad];
else
[self failProxyLoad];
}
else
{
[self failProxyLoad];
}
}
else
{
proxyDict[@"proxy"] = [[HTTPProxy alloc] initWithString:proxyValue];
[self finishProxyLoad];
}
}
}
- (void)cancelProxyLoad
{
[self returnFromProxyLoadWithError:[NSError errorWithDomain:@"Proxy" code:kProxyLoadCancelled userInfo:@{NSLocalizedDescriptionKey: @"Proxy Load Cancelled"}]];
}
- (void)failProxyLoad
{
[self returnFromProxyLoadWithError:[NSError errorWithDomain:@"Proxy" code:kProxyLoadFailed userInfo:@{NSLocalizedDescriptionKey: @"Proxy Load Failed"}]];
}
- (void)finishProxyLoad
{
NSLog(@"INFO: Proxy load complete.");
[logger addToLog:@"INFO: Proxy load complete."];
if (proxyDict[@"proxy"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"TestProxy"])
{
[self testProxyOnLoad];
return;
}
[self returnFromProxyLoadWithError:nil];
}
- (void)testProxyOnLoad
{
HTTPProxy *proxy = proxyDict[@"proxy"];
if (proxy)
{
if (!proxy.host || [proxy.host length] == 0 || [proxy.host rangeOfString:@"(null)"].location != NSNotFound)
{
NSLog(@"WARNING: Invalid proxy host: address=%@ length=%ld", proxy.host, [proxy.host length]);
[logger addToLog:[NSString stringWithFormat:@"WARNING: Invalid proxy host: address=%@ length=%ld", proxy.host, [proxy.host length]]];
if (!currentIsSilent)
{
NSAlert *alert = [NSAlert alertWithMessageText:@"Invalid proxy host.\nDownloads may fail.\nDo you wish to continue?"
defaultButton:@"No"
alternateButton:@"Yes"
otherButton:nil
informativeTextWithFormat:@"Invalid proxy host: address=[%@] length=%ld", proxy.host, [proxy.host length]];
[alert setAlertStyle:NSCriticalAlertStyle];
if ([alert runModal] == NSAlertDefaultReturn)
[self cancelProxyLoad];
else
[self failProxyTest];
}
else
{
[self failProxyLoad];
}
return;
}
NSString *testURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"ProxyTestURL"];
if (!testURL)
testURL = @"http://www.google.com";
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:testURL]];
[request setDelegate:self];
[request setDidFailSelector:@selector(proxyTestDidFinish:)];
[request setDidFinishSelector:@selector(proxyTestDidFinish:)];
[request setTimeOutSeconds:30];
[request setProxyType:proxy.type];
[request setProxyHost:proxy.host];
if (proxy.port) {
[request setProxyPort:proxy.port];
} else {
if ([proxy.type isEqualToString:(NSString *)kCFProxyTypeHTTPS]) {
[request setProxyPort:443];
} else {
[request setProxyPort:80];
}
}
if (proxy.user) {
[request setProxyUsername:proxy.user];
[request setProxyPassword:proxy.password];
}
[self updateProxyLoadStatus:YES message:[NSString stringWithFormat:@"Testing proxy (may take up to %ld seconds)...", (NSInteger)[request timeOutSeconds]]];
NSLog(@"INFO: Testing proxy (may take up to %ld seconds)...", (NSInteger)[request timeOutSeconds]);
[logger addToLog:[NSString stringWithFormat:@"INFO: Testing proxy (may take up to %ld seconds)...", (NSInteger)[request timeOutSeconds]]];
[request startAsynchronous];
}
else
{
NSLog(@"INFO: No proxy to test");
[logger addToLog:@"INFO: No proxy to test"];
[self finishProxyTest];
}
}
- (void)proxyTestDidFinish:(ASIHTTPRequest *)request
{
if ([request responseStatusCode] != 200)
{
NSLog(@"WARNING: Proxy failed to load test page: %@", [request url]);
[logger addToLog:[NSString stringWithFormat:@"WARNING: Proxy failed to load test page: %@", [request url]]];
if (!currentIsSilent)
{
NSError *error = [request error];
NSAlert *alert = [NSAlert alertWithMessageText:@"Proxy failed to load test page.\nDownloads may fail.\nDo you wish to continue?"
defaultButton:@"No"
alternateButton:@"Yes"
otherButton:nil
informativeTextWithFormat:@"Failed to load %@ within %ld seconds\nUsing proxy: %@\nError: %@", [request url], (NSInteger)[request timeOutSeconds], [proxyDict[@"proxy"] url], (error ? [error localizedDescription] : @"Unknown error")];
[alert setAlertStyle:NSCriticalAlertStyle];
if ([alert runModal] == NSAlertDefaultReturn)
[self cancelProxyLoad];
else
[self failProxyTest];
}
else
{
[self failProxyTest];
}
}
else
{
[self finishProxyTest];
}
}
- (void)failProxyTest
{
[self returnFromProxyLoadWithError:[NSError errorWithDomain:@"Proxy" code:kProxyLoadFailed userInfo:@{NSLocalizedDescriptionKey: @"Proxy Test Failed"}]];
}
- (void)finishProxyTest
{
NSLog(@"INFO: Proxy test complete.");
[logger addToLog:@"INFO: Proxy test complete."];
[self returnFromProxyLoadWithError:nil];
}
- (void)returnFromProxyLoadWithError:(NSError *)error
{
if (proxyDict[@"proxy"])
{
NSLog(@"INFO: Using proxy: %@", [proxyDict[@"proxy"] url]);
[logger addToLog:[NSString stringWithFormat:@"INFO: Using proxy: %@", [proxyDict[@"proxy"]url]]];
}
else
{
NSLog(@"INFO: No proxy will be used");
[logger addToLog:@"INFO: No proxy will be used"];
}
[self updateProxyLoadStatus:NO message:nil];
if (error) {
proxyDict[@"error"] = error;
}
[proxyDict[@"target"] performSelector:[proxyDict[@"selector"] pointerValue] withObject:proxyDict[@"object"] withObject:proxyDict];
}
- (void)updateProxyLoadStatus:(BOOL)working message:(NSString *)message
{
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (working)
{
userInfo[@"indeterminate"] = @YES;
userInfo[@"animated"] = @YES;
[nc postNotificationName:@"setPercentage" object:self userInfo:userInfo];
[nc postNotificationName:@"setCurrentProgress" object:self userInfo:@{@"string" : message}];
}
else
{
userInfo[@"indeterminate"] = @NO;
userInfo[@"animated"] = @NO;
[nc postNotificationName:@"setPercentage" object:self userInfo:userInfo];
[nc postNotificationName:@"setCurrentProgress" object:self userInfo:@{@"string" : @""}];
}
}
@end