Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: Add option to sync using binary protos #1364

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Source/common/SNTConfigurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@
///
@property(readonly, nonatomic) NSURL *syncBaseURL;

///
/// If enabled, syncing will use binary protobufs for transfer instead
/// of JSON. Defaults to NO.
///
@property(readonly, nonatomic) BOOL syncEnableProtoTransfer;

///
/// Proxy settings for syncing.
/// This dictionary is passed directly to NSURLSession. The allowed keys
Expand Down
7 changes: 7 additions & 0 deletions Source/common/SNTConfigurator.m
russellhancox marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ @implementation SNTConfigurator
/// The keys managed by a mobileconfig.
static NSString *const kStaticRules = @"StaticRules";
static NSString *const kSyncBaseURLKey = @"SyncBaseURL";
static NSString *const kSyncEnableProtoTransfer = @"SyncEnableProtoTransfer";
static NSString *const kSyncProxyConfigKey = @"SyncProxyConfiguration";
static NSString *const kSyncExtraHeadersKey = @"SyncExtraHeaders";
static NSString *const kSyncEnableCleanSyncEventUpload = @"SyncEnableCleanSyncEventUpload";
Expand Down Expand Up @@ -234,6 +235,7 @@ - (instancetype)initWithSyncStateFile:(NSString *)syncStateFilePath
kModeNotificationLockdown : string,
kStaticRules : array,
kSyncBaseURLKey : string,
kSyncEnableProtoTransfer : number,
kSyncEnableCleanSyncEventUpload : number,
kSyncProxyConfigKey : dictionary,
kSyncExtraHeadersKey : dictionary,
Expand Down Expand Up @@ -719,6 +721,11 @@ - (NSURL *)syncBaseURL {
return url;
}

- (BOOL)syncEnableProtoTransfer {
NSNumber *number = self.configState[kSyncEnableProtoTransfer];
return number ? [number boolValue] : NO;
}

- (NSDictionary *)syncProxyConfig {
return self.configState[kSyncProxyConfigKey];
}
Expand Down
43 changes: 28 additions & 15 deletions Source/santasyncservice/SNTSyncStage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,42 @@ - (NSString *)stageURL {
}

- (NSMutableURLRequest *)requestWithMessage:(google::protobuf::Message *)message {
NSData *requestBody = [NSData data];
if (message) {
google::protobuf::json::PrintOptions options{
.always_print_enums_as_ints = false,
.preserve_proto_field_names = true,
};
std::string json;
absl::Status status = google::protobuf::json::MessageToJsonString(*message, &json, options);

if (!status.ok()) {
SLOGE(@"Failed to convert protobuf to JSON: %s", status.ToString().c_str());
if (!message) return [self requestWithData:[NSData data] contentType:nil];

if ([[SNTConfigurator configurator] syncEnableProtoTransfer]) {
std::string data;
if (!message->SerializeToString(&data)) {
SLOGE(@"Failed to serialize protobuf");
return nil;
}

requestBody = [NSData dataWithBytes:json.data() length:json.size()];
return [self requestWithData:[NSData dataWithBytes:data.data() length:data.size()]
contentType:@"application/x-protobuf"];
}
return [self requestWithData:requestBody];

google::protobuf::json::PrintOptions options{
.always_print_enums_as_ints = false,
.preserve_proto_field_names = true,
};
std::string json;
absl::Status status = google::protobuf::json::MessageToJsonString(*message, &json, options);

if (!status.ok()) {
SLOGE(@"Failed to convert protobuf to JSON: %s", status.ToString().c_str());
return nil;
}

SLOGD(@"Request JSON: %s", json.c_str());
return [self requestWithData:[NSData dataWithBytes:json.data() length:json.size()]
contentType:@"application/json"];
}

- (NSMutableURLRequest *)requestWithData:(NSData *)requestBody {
- (NSMutableURLRequest *)requestWithData:(NSData *)requestBody contentType:(NSString *)contentType {
if (contentType.length) contentType = @"application/octet-stream";

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[self stageURL]];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSString *xsrfHeader = self.syncState.xsrfTokenHeader ?: kDefaultXSRFTokenHeader;
[req setValue:self.syncState.xsrfToken forHTTPHeaderField:xsrfHeader];

Expand Down
1 change: 1 addition & 0 deletions docs/deployment/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ also known as mobileconfig files, which are in an Apple-specific XML format.
| ModeNotificationMonitor | String | The notification text to display when the client goes into Monitor mode. Defaults to "Switching into Monitor mode". |
| ModeNotificationLockdown | String | The notification text to display when the client goes into Lockdown mode. Defaults to "Switching into Lockdown mode". |
| SyncBaseURL | String | The base URL of the sync server. |
| SyncEnableProtoTransfer | Bool | If true, sync will happen using binary protos instead of JSON. Defaults to false. |
| SyncProxyConfiguration | Dictionary | The proxy configuration to use when syncing. See the [Apple Documentation](https://developer.apple.com/documentation/cfnetwork/global_proxy_settings_constants) for details on the keys that can be used in this dictionary. |
| SyncEnableCleanSyncEventUpload | Bool | If true, events will be uploaded to the sync server even if a clean sync is requested. Defaults to false. |
| ClientAuthCertificateFile | String | If set, this contains the location of a PKCS#12 certificate to be used for sync authentication. |
Expand Down
Loading