Add support for authentication codes via MDNS on iOS (#8625)

This commit is contained in:
Ben Konyi 2019-04-18 12:12:29 -07:00 committed by GitHub
parent 57f8abb73f
commit 763bbca0cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -98,6 +98,19 @@
NSString* serviceName =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
// Check to see if there's an authentication code. If there is, we'll provide
// it as a txt record so flutter tools can establish a connection.
auto path = std::string{[[url path] UTF8String]};
if (!path.empty()) {
// Remove leading "/"
path = path.substr(1);
}
NSData* pathData = [[[NSData alloc] initWithBytes:path.c_str() length:path.length()] autorelease];
NSDictionary* txtDict = @{
@"authCode" : pathData,
};
NSData* txtData = [NSNetService dataFromTXTRecordDictionary:txtDict];
#if TARGET_IPHONE_SIMULATOR
DNSServiceFlags flags = kDNSServiceFlagsDefault;
uint32_t interfaceIndex = if_nametoindex("lo0");
@ -106,8 +119,8 @@
uint16_t port = [[url port] intValue];
int err = DNSServiceRegister(&_dnsServiceRef, flags, interfaceIndex, [serviceName UTF8String],
registrationType, domain, NULL, htons(port), 0, NULL,
registrationCallback, NULL);
registrationType, domain, NULL, htons(port), txtData.length,
txtData.bytes, registrationCallback, NULL);
if (err != 0) {
FML_LOG(ERROR) << "Failed to register observatory port with mDNS.";
@ -115,10 +128,12 @@
DNSServiceSetDispatchQueue(_dnsServiceRef, dispatch_get_main_queue());
}
#else // TARGET_IPHONE_SIMULATOR
_netService.reset([[NSNetService alloc] initWithDomain:@"local."
type:@"_dartobservatory._tcp."
name:serviceName
port:[[url port] intValue]]);
NSNetService* netServiceTmp = [[NSNetService alloc] initWithDomain:@"local."
type:@"_dartobservatory._tcp."
name:serviceName
port:[[url port] intValue]];
[netServiceTmp setTXTRecordData:txtData];
_netService.reset(netServiceTmp);
[_netService.get() setDelegate:self];
[_netService.get() publish];
#endif // TARGET_IPHONE_SIMULATOR