Skip to content

Commit

Permalink
Add support for authentication codes via MDNS on iOS (flutter#8625)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkonyi authored Apr 18, 2019
1 parent 57f8abb commit 763bbca
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ - (void)publishServiceProtocolPort:(std::string)uri {
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");
Expand All @@ -106,19 +119,21 @@ - (void)publishServiceProtocolPort:(std::string)uri {
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.";
} else {
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
Expand Down

0 comments on commit 763bbca

Please sign in to comment.