2008-03-29 23:38:38 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2008-2012 Transmission authors and contributors
|
2008-03-29 23:38:38 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "BlocklistDownloader.h"
|
2008-08-31 19:47:11 +00:00
|
|
|
#import "BlocklistDownloaderViewController.h"
|
2008-09-01 15:46:00 +00:00
|
|
|
#import "BlocklistScheduler.h"
|
2012-05-29 01:03:21 +00:00
|
|
|
#import "Controller.h"
|
2008-03-29 23:38:38 +00:00
|
|
|
|
|
|
|
@interface BlocklistDownloader (Private)
|
|
|
|
|
|
|
|
- (void) startDownload;
|
2011-01-25 01:43:11 +00:00
|
|
|
- (void) decompressBlocklist;
|
2008-03-29 23:38:38 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistDownloader
|
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
BlocklistDownloader * fBLDownloader = nil;
|
2008-08-31 20:48:10 +00:00
|
|
|
+ (BlocklistDownloader *) downloader
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
if (!fBLDownloader)
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
fBLDownloader = [[BlocklistDownloader alloc] init];
|
|
|
|
[fBLDownloader startDownload];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
return fBLDownloader;
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2008-08-31 20:26:41 +00:00
|
|
|
+ (BOOL) isRunning
|
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
return fBLDownloader != nil;
|
2008-08-31 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
- (void) setViewController: (BlocklistDownloaderViewController *) viewController
|
2008-04-06 04:49:39 +00:00
|
|
|
{
|
2008-08-31 19:47:11 +00:00
|
|
|
fViewController = viewController;
|
|
|
|
if (fViewController)
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
switch (fState)
|
|
|
|
{
|
|
|
|
case BLOCKLIST_DL_START:
|
|
|
|
[fViewController setStatusStarting];
|
|
|
|
break;
|
|
|
|
case BLOCKLIST_DL_DOWNLOADING:
|
|
|
|
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
|
|
|
|
break;
|
|
|
|
case BLOCKLIST_DL_PROCESSING:
|
|
|
|
[fViewController setStatusProcessing];
|
|
|
|
break;
|
|
|
|
}
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
2008-04-06 04:49:39 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
- (void) cancelDownload
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setFinished];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-04-06 03:47:03 +00:00
|
|
|
[fDownload cancel];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
[[BlocklistScheduler scheduler] updateSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
fBLDownloader = nil;
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 01:43:11 +00:00
|
|
|
//using the actual filename is the best bet
|
|
|
|
- (void) download: (NSURLDownload *) download decideDestinationWithSuggestedFilename: (NSString *) filename
|
|
|
|
{
|
|
|
|
[fDownload setDestination: [NSTemporaryDirectory() stringByAppendingPathComponent: filename] allowOverwrite: NO];
|
|
|
|
}
|
|
|
|
|
2010-04-28 11:03:38 +00:00
|
|
|
- (void) download: (NSURLDownload *) download didCreateDestination: (NSString *) path
|
|
|
|
{
|
2017-07-29 16:14:22 +00:00
|
|
|
fDestination = path;
|
2010-04-28 11:03:38 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
- (void) download: (NSURLDownload *) download didReceiveResponse: (NSURLResponse *) response
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
fState = BLOCKLIST_DL_DOWNLOADING;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
fCurrentSize = 0;
|
|
|
|
fExpectedSize = [response expectedContentLength];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) download: (NSURLDownload *) download didReceiveDataOfLength: (NSUInteger) length
|
|
|
|
{
|
2008-03-30 01:51:48 +00:00
|
|
|
fCurrentSize += length;
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
|
|
|
|
{
|
2010-11-13 16:07:27 +00:00
|
|
|
[fViewController setFailed: [error localizedDescription]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-10-31 19:24:26 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: @"BlocklistNewLastUpdate"];
|
2008-09-01 15:46:00 +00:00
|
|
|
[[BlocklistScheduler scheduler] updateSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
fBLDownloader = nil;
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) downloadDidFinish: (NSURLDownload *) download
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
fState = BLOCKLIST_DL_PROCESSING;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
[fViewController setStatusProcessing];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
NSAssert(fDestination != nil, @"the blocklist file destination has not been specified");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
[self decompressBlocklist];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2012-05-29 01:03:21 +00:00
|
|
|
const int count = tr_blocklistSetContent([(Controller *)[NSApp delegate] sessionHandle], [fDestination UTF8String]);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
//delete downloaded file
|
|
|
|
[[NSFileManager defaultManager] removeItemAtPath: fDestination error: NULL];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
if (count > 0)
|
|
|
|
[fViewController setFinished];
|
|
|
|
else
|
|
|
|
[fViewController setFailed: NSLocalizedString(@"The specified blocklist file did not contain any valid rules.",
|
|
|
|
"blocklist fail message")];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
//update last updated date for schedule
|
|
|
|
NSDate * date = [NSDate date];
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject: date forKey: @"BlocklistNewLastUpdate"];
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject: date forKey: @"BlocklistNewLastUpdateSuccess"];
|
|
|
|
[[BlocklistScheduler scheduler] updateSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
fBLDownloader = nil;
|
|
|
|
});
|
|
|
|
});
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 01:43:11 +00:00
|
|
|
- (BOOL) download: (NSURLDownload *) download shouldDecodeSourceDataOfMIMEType: (NSString *) encodingType
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistDownloader (Private)
|
|
|
|
|
|
|
|
- (void) startDownload
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
fState = BLOCKLIST_DL_START;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
[[BlocklistScheduler scheduler] cancelSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-10-31 19:05:46 +00:00
|
|
|
NSString * urlString = [[NSUserDefaults standardUserDefaults] stringForKey: @"BlocklistURL"];
|
|
|
|
if (!urlString)
|
|
|
|
urlString = @"";
|
2010-11-01 00:06:40 +00:00
|
|
|
else if (![urlString isEqualToString: @""] && [urlString rangeOfString: @"://"].location == NSNotFound)
|
|
|
|
urlString = [@"http://" stringByAppendingString: urlString];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-10-31 19:05:46 +00:00
|
|
|
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
fDownload = [[NSURLDownload alloc] initWithRequest: request delegate: self];
|
|
|
|
}
|
|
|
|
|
2011-01-25 01:43:11 +00:00
|
|
|
//.gz, .tar.gz, .tgz, and .bgz will be decompressed by NSURLDownload for us. However, we have to do .zip files manually.
|
|
|
|
- (void) decompressBlocklist
|
|
|
|
{
|
2017-01-24 17:53:16 +00:00
|
|
|
if ([[[fDestination pathExtension] lowercaseString] isEqualToString: @"zip"]) {
|
|
|
|
BOOL success = NO;
|
|
|
|
|
2011-01-25 01:43:11 +00:00
|
|
|
NSString * workingDirectory = [fDestination stringByDeletingLastPathComponent];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
|
|
|
//First, perform the actual unzipping
|
|
|
|
NSTask * unzip = [[NSTask alloc] init];
|
|
|
|
[unzip setLaunchPath: @"/usr/bin/unzip"];
|
|
|
|
[unzip setCurrentDirectoryPath: workingDirectory];
|
2017-07-08 08:23:05 +00:00
|
|
|
[unzip setArguments: @[
|
2011-01-25 01:43:11 +00:00
|
|
|
@"-o", /* overwrite */
|
|
|
|
@"-q", /* quiet! */
|
|
|
|
fDestination, /* source zip file */
|
2017-07-08 08:23:05 +00:00
|
|
|
@"-d", workingDirectory /*destination*/
|
|
|
|
]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
[unzip launch];
|
|
|
|
[unzip waitUntilExit];
|
|
|
|
|
|
|
|
if ([unzip terminationStatus] == 0)
|
|
|
|
success = YES;
|
|
|
|
}
|
|
|
|
@catch(id exc)
|
|
|
|
{
|
|
|
|
success = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
//Now find out what file we actually extracted; don't just assume it matches the zipfile's name
|
|
|
|
NSTask *zipinfo;
|
|
|
|
|
|
|
|
zipinfo = [[NSTask alloc] init];
|
|
|
|
[zipinfo setLaunchPath: @"/usr/bin/zipinfo"];
|
2017-07-08 08:23:05 +00:00
|
|
|
[zipinfo setArguments: @[
|
2011-01-25 01:43:11 +00:00
|
|
|
@"-1", /* just the filename */
|
2017-07-08 08:23:05 +00:00
|
|
|
fDestination /* source zip file */
|
|
|
|
]];
|
2017-01-24 17:53:16 +00:00
|
|
|
[zipinfo setStandardOutput: [NSPipe pipe]];
|
|
|
|
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
NSFileHandle * zipinfoOutput = [[zipinfo standardOutput] fileHandleForReading];
|
|
|
|
|
|
|
|
[zipinfo launch];
|
|
|
|
[zipinfo waitUntilExit];
|
|
|
|
|
2017-07-29 16:14:22 +00:00
|
|
|
NSString * actualFilename = [[NSString alloc] initWithData: [zipinfoOutput readDataToEndOfFile]
|
|
|
|
encoding: NSUTF8StringEncoding];
|
2017-01-24 17:53:16 +00:00
|
|
|
actualFilename = [actualFilename stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
|
|
|
NSString * newBlocklistPath = [workingDirectory stringByAppendingPathComponent: actualFilename];
|
|
|
|
|
|
|
|
//Finally, delete the ZIP file; we're done with it, and we'll return the unzipped blocklist
|
|
|
|
[[NSFileManager defaultManager] removeItemAtPath: fDestination error: NULL];
|
|
|
|
|
2017-07-29 16:14:22 +00:00
|
|
|
fDestination = newBlocklistPath;
|
2017-01-24 17:53:16 +00:00
|
|
|
}
|
2011-01-25 01:43:11 +00:00
|
|
|
@catch(id exc) {}
|
2017-01-24 17:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-25 01:43:11 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
@end
|