本地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;
{
+11 -3
View File
@@ -144,9 +144,9 @@ alpha:1.0]
static NSString* DOMAIN_NAME =@"app.ifish7.com";
//正式环境
#define SOCKETPORT DOMAIN_NAME
#define JIEKOUPORT [NSString stringWithFormat:@"https://%@",DOMAIN_NAME]
#define JIEKOUPORTHTTP [NSString stringWithFormat:@"https://%@",DOMAIN_NAME]
#define SOCKETPORT [Define sharedDefine].server
#define JIEKOUPORT [[Define sharedDefine] getRealSever]
#define JIEKOUPORTHTTP [[Define sharedDefine] getRealSever]
//测试环境
//#define SOCKETPORT @"test.ifish7.com:9955"
//#define JIEKOUPORT @"http://139.196.24.156:7080"
@@ -509,3 +509,11 @@ alpha:1.0]
#define Push_Message_UnReadCount [NSString stringWithFormat:@"%@/api/updateUser/userMessageNoReadCount.do",JIEKOUPORT]
/*=====================add V4.9.8=======================================*/
#endif /* Define_h */
#import <Foundation/Foundation.h>
@interface Define : NSObject
@property (nonatomic, strong) NSString *server;
+ (Define *)sharedDefine;
-(NSString*)getRealSever;
@end
+64
View File
@@ -0,0 +1,64 @@
//
// Define.m
// Ifish
//
// Created by 祝发冬 on 2022/1/17.
// Copyright © 2022 lianlian. All rights reserved.
//
#import "Define.h"
@implementation Define
static Define *defi = nil;
/// 获取单例
+ (Define *)sharedDefine
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
defi = [[super allocWithZone:NULL] init];
});
return defi;
}
+(id) allocWithZone:(struct _NSZone *)zone
{
return [Define sharedDefine] ;
}
-(id) copyWithZone:(struct _NSZone *)zone
{
return [Define sharedDefine] ;
}
#pragma mark - 初始化
- (instancetype)init
{
self = [super init];
if (self)
{
self.server=DOMAIN_NAME;
}
return self;
}
-(NSString*)getRealSever
{
NSString*server=self.server;
if (![self.server isEqualToString:DOMAIN_NAME]) {
server=[NSString stringWithFormat:@"http://%@:7080",server];
}
else
{
server=[NSString stringWithFormat:@"https://%@",server];
}
return server;
}
- (void)setServer:(NSString *)server
{
if (server.length)
{
_server=server;
}
}
@end