本地dns优先,失败后使用阿里httpdns

This commit is contained in:
kai60
2022-01-19 11:07:58 +08:00
parent f63f7d216c
commit 496501a76f
4 changed files with 177 additions and 44 deletions
+98 -41
View File
@@ -88,7 +88,7 @@
#import "IfishADTimerViewController.h"
#import "AvoidCrash.h"
#import <AlicloudHttpDNS/AlicloudHttpDNS.h>
#import <arpa/inet.h>
@interface AppDelegate ()<HttpDNSDegradationDelegate,HttpdnsLoggerProtocol>
@property(nonatomic,strong)ICSDrawerController*ics;
@@ -389,56 +389,113 @@ if (kScreenSize.height>480) {
-(void)setupHTTPDNS
{
HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:HTTPDNS_ACCOUNTID];
// 为HTTPDNS服务设置降级机制
[httpdns setDelegateForDegradationFilter:self];
// 允许返回过期的IP
[httpdns setExpiredIPEnabled:YES];
// 打开HTTPDNS Log,线上建议关闭
[httpdns setLogEnabled:YES];
[httpdns setLogHandler:self];
/*
* 设置HTTPDNS域名解析请求类型(HTTP/HTTPS),若不调用该接口,默认为HTTP请求;
* SDK内部HTTP请求基于CFNetwork实现,不受ATS限制。
*/
//[httpdns setHTTPSRequestEnabled:YES];
// edited
NSArray *preResolveHosts = @[DOMAIN_NAME];
// NSArray* preResolveHosts = @[@"pic1cdn.igetget.com"];
// 设置预解析域名列表
[httpdns setPreResolveHosts:preResolveHosts];
[httpdns setTimeoutInterval:15];
httpdns=[HttpDnsService sharedInstance];
__block NSString*ip = [httpdns getIpByHostAsync:DOMAIN_NAME];
Boolean result,bResolved;
CFHostRef hostRef;
CFArrayRef addresses = NULL;
NSMutableArray * ipsArr = [[NSMutableArray alloc] init];
CFStringRef hostNameRef = CFStringCreateWithCString(kCFAllocatorDefault, [DOMAIN_NAME UTF8String], kCFStringEncodingASCII);
hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostNameRef);
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
if (result == TRUE) {
addresses = CFHostGetAddressing(hostRef, &result);
}
bResolved = result == TRUE ? true : false;
if(bResolved)
{
struct sockaddr_in* remoteAddr;
for(int i = 0; i < CFArrayGetCount(addresses); i++)
{
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i);
remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);
if(remoteAddr != NULL)
{
//获取IP地址
char ip[16];
strcpy(ip, inet_ntoa(remoteAddr->sin_addr));
NSString * ipStr = [NSString stringWithCString:ip encoding:NSUTF8StringEncoding];
[ipsArr addObject:ipStr];
}
}
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"33333 === ip === %@ === time cost: %0.3fs", ipsArr,end - start);
CFRelease(hostNameRef);
CFRelease(hostRef);
BOOL validIP=NO;
NSString*ip=[ipsArr firstObject];
if (ip.length)
{
DOMAIN_NAME=ip;
NSArray*component=[ip componentsSeparatedByString:@"."];
NSString*first = [component firstObject];
if (first.intValue>0)
{
validIP=YES;
}
}
else
if (!validIP)
{
dispatch_queue_t queue= dispatch_queue_create("dns", DISPATCH_QUEUE_CONCURRENT);
dispatch_semaphore_t sema =dispatch_semaphore_create(0);
dispatch_async(queue, ^{
while (!ip) {
ip = [httpdns getIpByHostAsync:DOMAIN_NAME];
if (ip)
{
dispatch_semaphore_signal(sema);
break;
}
}
});
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)));
HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:HTTPDNS_ACCOUNTID secretKey:HTTPDNS_SECRET];
// 为HTTPDNS服务设置降级机制
[httpdns setDelegateForDegradationFilter:self];
// 允许返回过期的IP
[httpdns setExpiredIPEnabled:YES];
// 打开HTTPDNS Log,线上建议关闭
[httpdns setLogEnabled:YES];
[httpdns setLogHandler:self];
/*
* 设置HTTPDNS域名解析请求类型(HTTP/HTTPS),若不调用该接口,默认为HTTP请求;
* SDK内部HTTP请求基于CFNetwork实现,不受ATS限制。
*/
//[httpdns setHTTPSRequestEnabled:YES];
// edited
NSArray *preResolveHosts = @[DOMAIN_NAME];
// NSArray* preResolveHosts = @[@"pic1cdn.igetget.com"];
// 设置预解析域名列表
[httpdns setPreResolveHosts:preResolveHosts];
[httpdns setTimeoutInterval:15];
httpdns=[HttpDnsService sharedInstance];
Define* define=[Define sharedDefine];
__block NSString*ip = [httpdns getIpByHostAsync:DOMAIN_NAME];
if (ip.length)
{
DOMAIN_NAME=ip;
define.server=ip;
}
else
{
dispatch_queue_t queue= dispatch_queue_create("dns", DISPATCH_QUEUE_CONCURRENT);
dispatch_semaphore_t sema =dispatch_semaphore_create(0);
dispatch_async(queue, ^{
while (!ip) {
ip = [httpdns getIpByHostAsync:DOMAIN_NAME];
if (ip)
{
dispatch_semaphore_signal(sema);
break;
}
}
});
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)));
if (ip.length)
{
define.server=ip;
}
}
}
}
- (BOOL)shouldDegradeHTTPDNS:(NSString *)hostName;
{