Skip to content

Commit

Permalink
santactl/sync: Split out Log Upload request generation and Rule Downl…
Browse files Browse the repository at this point in the history
…oad rule parsing from main request methods.
  • Loading branch information
russellhancox committed May 18, 2015
1 parent 78bb9a1 commit 0db3b6d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 51 deletions.
41 changes: 23 additions & 18 deletions Source/santactl/sync/SNTCommandSyncLogUpload.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,46 @@ + (void)performSyncInSession:(NSURLSession *)session
[NSString stringWithFormat:@"multipart/form-data; charset=UTF-8; boundary=%@", boundary];
[req setValue:contentType forHTTPHeaderField:@"Content-Type"];

NSArray *logsToUpload = [SNTCommandSyncLogUpload logsToUpload];

// Upload the logs
[[session uploadTaskWithRequest:req
fromData:[self requestBodyWithLogs:logsToUpload andBoundary:boundary]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
long statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
LOGE(@"HTTP Response: %d %@",
statusCode,
[[NSHTTPURLResponse localizedStringForStatusCode:statusCode] capitalizedString]);
handler(NO);
} else {
LOGI(@"Uploaded %d logs", [logsToUpload count]);
handler(YES);
}
}] resume];
}

+ (NSData *)requestBodyWithLogs:(NSArray *)logsToUpload andBoundary:(NSString *)boundary {
// Prepare the body of the request, encoded as a multipart/form-data.
// Along the way, gzip the individual log files and append .gz to their filenames.
NSMutableData *reqBody = [[NSMutableData alloc] init];
NSArray *logsToUpload = [SNTCommandSyncLogUpload logsToUpload];
for (NSString *log in logsToUpload) {
[reqBody appendData:
[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[reqBody appendData:
[[NSString stringWithFormat:@"Content-Disposition: form-data; "
@"name=\"%@\"; "
@"filename=\"%@.gz\"\r\n", kLogUploadField, [log lastPathComponent]]
dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
[reqBody appendData:
[@"Content-Type: application/x-gzip\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[reqBody appendData:[[NSData dataWithContentsOfFile:log] gzipCompressed]];
[reqBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[reqBody appendData:
[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// Upload the logs
[[session uploadTaskWithRequest:req
fromData:reqBody
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
long statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
LOGE(@"HTTP Response: %d %@",
statusCode,
[[NSHTTPURLResponse localizedStringForStatusCode:statusCode] capitalizedString]);
handler(NO);
} else {
LOGI(@"Uploaded %d logs", [logsToUpload count]);
handler(YES);
}
}] resume];
return reqBody;
}

+ (NSArray *)logsToUpload {
Expand Down
76 changes: 43 additions & 33 deletions Source/santactl/sync/SNTCommandSyncRuleDownload.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ + (void)ruleDownloadWithCursor:(NSString *)cursor
[[session dataTaskWithRequest:req completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if ([(NSHTTPURLResponse *)response statusCode] != 200) {
LOGD(@"HTTP Response Code: %d", [(NSHTTPURLResponse *)response statusCode]);
long statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
LOGE(@"HTTP Response: %d %@",
statusCode,
[[NSHTTPURLResponse localizedStringForStatusCode:statusCode] capitalizedString]);
handler(NO);
} else {
NSDictionary *resp = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Expand All @@ -72,37 +75,8 @@ + (void)ruleDownloadWithCursor:(NSString *)cursor

NSArray *receivedRules = resp[kRules];
for (NSDictionary *rule in receivedRules) {
if (![rule isKindOfClass:[NSDictionary class]]) continue;

SNTRule *newRule = [[SNTRule alloc] init];
newRule.shasum = rule[kRuleSHA256];

if ([rule[kRulePolicy] isEqual:kRulePolicyWhitelist]) {
newRule.state = RULESTATE_WHITELIST;
} else if ([rule[kRulePolicy] isEqual:kRulePolicyBlacklist]) {
newRule.state = RULESTATE_BLACKLIST;
} else if ([rule[kRulePolicy] isEqual:kRulePolicySilentBlacklist]) {
newRule.state = RULESTATE_SILENT_BLACKLIST;
} else if ([rule[kRulePolicy] isEqual:kRulePolicyRemove]) {
newRule.state = RULESTATE_REMOVE;
} else {
continue;
}

if ([rule[kRuleType] isEqual:kRuleTypeBinary]) {
newRule.type = RULETYPE_BINARY;
} else if ([rule[kRuleType] isEqual:kRuleTypeCertificate]) {
newRule.type = RULETYPE_CERT;
} else {
continue;
}

NSString *customMsg = rule[kRuleCustomMsg];
if (customMsg) {
newRule.customMsg = customMsg;
}

[syncState.downloadedRules addObject:newRule];
SNTRule *r = [self ruleFromDictionary:rule];
if (r) [syncState.downloadedRules addObject:r];
}

if (resp[kCursor]) {
Expand All @@ -128,4 +102,40 @@ + (void)ruleDownloadWithCursor:(NSString *)cursor
}] resume];
}

+ (SNTRule *)ruleFromDictionary:(NSDictionary *)dict {
if (![dict isKindOfClass:[NSDictionary class]]) return nil;

SNTRule *newRule = [[SNTRule alloc] init];
newRule.shasum = dict[kRuleSHA256];

NSString *policyString = dict[kRulePolicy];
if ([policyString isEqual:kRulePolicyWhitelist]) {
newRule.state = RULESTATE_WHITELIST;
} else if ([policyString isEqual:kRulePolicyBlacklist]) {
newRule.state = RULESTATE_BLACKLIST;
} else if ([policyString isEqual:kRulePolicySilentBlacklist]) {
newRule.state = RULESTATE_SILENT_BLACKLIST;
} else if ([policyString isEqual:kRulePolicyRemove]) {
newRule.state = RULESTATE_REMOVE;
} else {
return nil;
}

NSString *ruleTypeString = dict[kRuleType];
if ([ruleTypeString isEqual:kRuleTypeBinary]) {
newRule.type = RULETYPE_BINARY;
} else if ([ruleTypeString isEqual:kRuleTypeCertificate]) {
newRule.type = RULETYPE_CERT;
} else {
return nil;
}

NSString *customMsg = dict[kRuleCustomMsg];
if (customMsg) {
newRule.customMsg = customMsg;
}

return newRule;
}

@end

0 comments on commit 0db3b6d

Please sign in to comment.