本地dns优先,失败后使用阿里httpdns
This commit is contained in:
parent
f63f7d216c
commit
496501a76f
|
|
@ -2305,6 +2305,7 @@
|
|||
C0D3A12C2647ED99008E6FD6 /* UIView+Sizes.h in Headers */ = {isa = PBXBuildFile; fileRef = C0D3A12A2647ED99008E6FD6 /* UIView+Sizes.h */; };
|
||||
C0D3A12D2647ED99008E6FD6 /* UIView+Sizes.m in Sources */ = {isa = PBXBuildFile; fileRef = C0D3A12B2647ED99008E6FD6 /* UIView+Sizes.m */; };
|
||||
C0D3A12F2647EDA5008E6FD6 /* ELPCircleRotateImageAsset.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C0D3A12E2647EDA5008E6FD6 /* ELPCircleRotateImageAsset.xcassets */; };
|
||||
C0D451D727955E8200609928 /* Define.m in Sources */ = {isa = PBXBuildFile; fileRef = C0D451D627955E8200609928 /* Define.m */; };
|
||||
C0E746D5239E4A7D001BCD6D /* wechatlogin.png in Resources */ = {isa = PBXBuildFile; fileRef = C0E746D4239E4A7C001BCD6D /* wechatlogin.png */; };
|
||||
C0E7D6C023A1D11500256A10 /* WXApiRequestHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = C0E7D6BC23A1D11200256A10 /* WXApiRequestHandler.h */; };
|
||||
C0E7D6C123A1D11500256A10 /* WXApiManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C0E7D6BD23A1D11200256A10 /* WXApiManager.h */; };
|
||||
|
|
@ -5784,6 +5785,7 @@
|
|||
C0D3A12A2647ED99008E6FD6 /* UIView+Sizes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+Sizes.h"; sourceTree = "<group>"; };
|
||||
C0D3A12B2647ED99008E6FD6 /* UIView+Sizes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+Sizes.m"; sourceTree = "<group>"; };
|
||||
C0D3A12E2647EDA5008E6FD6 /* ELPCircleRotateImageAsset.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = ELPCircleRotateImageAsset.xcassets; sourceTree = "<group>"; };
|
||||
C0D451D627955E8200609928 /* Define.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Define.m; sourceTree = "<group>"; };
|
||||
C0E746D4239E4A7C001BCD6D /* wechatlogin.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wechatlogin.png; sourceTree = "<group>"; };
|
||||
C0E7D6BC23A1D11200256A10 /* WXApiRequestHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXApiRequestHandler.h; sourceTree = "<group>"; };
|
||||
C0E7D6BD23A1D11200256A10 /* WXApiManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXApiManager.h; sourceTree = "<group>"; };
|
||||
|
|
@ -10063,6 +10065,7 @@
|
|||
883E791E1D48ACFF0030E075 /* dataUnity */,
|
||||
883E79211D48ACFF0030E075 /* DB */,
|
||||
883E79261D48ACFF0030E075 /* Define.h */,
|
||||
C0D451D627955E8200609928 /* Define.m */,
|
||||
883E79341D48ACFF0030E075 /* service.html */,
|
||||
883E79381D48ACFF0030E075 /* wifi_error.html */,
|
||||
88742C461D645CE70074F1F8 /* shopCertification.html */,
|
||||
|
|
@ -15584,6 +15587,7 @@
|
|||
881672151EA5DE2B00BEBF23 /* UMComSimpleCommentEditView.m in Sources */,
|
||||
8868FE561E66C8BC002F7F33 /* DengJiShuoMingViewTopCell.m in Sources */,
|
||||
88F5EFB91D48D08100CC7CAF /* UIWebView+AFNetworking.m in Sources */,
|
||||
C0D451D727955E8200609928 /* Define.m in Sources */,
|
||||
8867F5B41E654DFD00AF021C /* DengJiView0Cell.m in Sources */,
|
||||
883E785A1D48A5E50030E075 /* XuToLightModel.m in Sources */,
|
||||
883E785E1D48A5E50030E075 /* CenterViewController.m in Sources */,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Reference in New Issue