带阿里标识的版本
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
//
|
||||
// HttpDnsLocker.h
|
||||
// AlicloudHttpDNS
|
||||
// TrustHttpDNS
|
||||
//
|
||||
// Created by 王贇 on 2023/8/16.
|
||||
// Copyright © 2023 alibaba-inc.com. All rights reserved.
|
||||
// Copyright © 2023 trustapp.com. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef HttpDnsLocker_h
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//
|
||||
// HttpDnsLocker.m
|
||||
// AlicloudHttpDNS
|
||||
// TrustHttpDNS
|
||||
//
|
||||
// Created by 王贇 on 2023/8/16.
|
||||
// Copyright © 2023 alibaba-inc.com. All rights reserved.
|
||||
// Copyright © 2023 trustapp.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import "HttpDnsLocker.h"
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//
|
||||
// HttpdnsHostObjectInMemoryCache.h
|
||||
// AlicloudHttpDNS
|
||||
// TrustHttpDNS
|
||||
//
|
||||
// Created by xuyecan on 2024/9/28.
|
||||
// Copyright © 2024 alibaba-inc.com. All rights reserved.
|
||||
// Copyright © 2024 trustapp.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
@@ -11,10 +11,10 @@
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
// 这个字典在HTTPDNS中只用于存储HttpdnsHostObject对象,这个对象是整个框架的核心对象,用于缓存和处理域名解析结果
|
||||
// 通常从缓存中获得这个对象之后,会根据不同场景改变一些字段的值,而且很可能发生在不同线程中
|
||||
// 这个字典在HTTPDNS中只用于存储HttpdnsHostObject对象,这个对象是整个框架的核心对象,用于缓存和处理域名解析结<EFBFBD><EFBFBD>?
|
||||
// 通常从缓存中获得这个对象之后,会根据不同场景改变一些字段的值,而且很可能发生在不同线程<EFBFBD><EFBFBD>?
|
||||
// 而不同线程从缓存中直接读取共享对象的话,很有可能发生线程竞争的情况,多线程访问某个对象的同一个字段,在swift环境有较高概率发生crash
|
||||
// 因此,除了确保字典操作的线程安全,拿出对象的时候,也直接copy一个复制对象返回(HttpdnsHostObject对象实现了NSCopying协议)
|
||||
// 因此,除了确保字典操作的线程安全,拿出对象的时候,也直接copy一个复制对象返<EFBFBD><EFBFBD>?HttpdnsHostObject对象实现了NSCopying协议)
|
||||
@interface HttpdnsHostObjectInMemoryCache : NSObject
|
||||
|
||||
- (void)setHostObject:(HttpdnsHostObject *)object forCacheKey:(NSString *)key;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//
|
||||
// HttpdnsHostObjectInMemoryCache.m
|
||||
// AlicloudHttpDNS
|
||||
// TrustHttpDNS
|
||||
//
|
||||
// Created by xuyecan on 2024/9/28.
|
||||
// Copyright © 2024 alibaba-inc.com. All rights reserved.
|
||||
// Copyright © 2024 trustapp.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import "HttpdnsHostObjectInMemoryCache.h"
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//
|
||||
// HttpdnsIPQualityDetector.h
|
||||
// AlicloudHttpDNS
|
||||
// TrustHttpDNS
|
||||
//
|
||||
// Created by xuyecan on 2025/3/13.
|
||||
// Copyright © 2025 alibaba-inc.com. All rights reserved.
|
||||
// Copyright © 2025 trustapp.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
@@ -11,10 +11,10 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* IP质量检测回调
|
||||
* @param cacheKey 缓存键
|
||||
* IP质量检测回<EFBFBD><EFBFBD>?
|
||||
* @param cacheKey 缓存<EFBFBD><EFBFBD>?
|
||||
* @param ip IP地址
|
||||
* @param costTime 连接耗时(毫秒),-1表示连接失败
|
||||
* @param costTime 连接耗时(毫秒)<EFBFBD><EFBFBD>?1表示连接失败
|
||||
*/
|
||||
typedef void(^HttpdnsIPQualityCallback)(NSString *cacheKey, NSString *ip, NSInteger costTime);
|
||||
|
||||
@@ -32,10 +32,10 @@ typedef void(^HttpdnsIPQualityCallback)(NSString *cacheKey, NSString *ip, NSInte
|
||||
|
||||
/**
|
||||
* 调度一个IP连接质量检测任务,不会阻塞当前线程
|
||||
* @param cacheKey 缓存键,通常是域名
|
||||
* @param cacheKey 缓存键,通常是域<EFBFBD><EFBFBD>?
|
||||
* @param ip 要检测的IP地址
|
||||
* @param port 连接端口,如果为nil则默认使用80
|
||||
* @param callback 检测完成后的回调
|
||||
* @param port 连接端口,如果为nil则默认使<EFBFBD><EFBFBD>?0
|
||||
* @param callback 检测完成后的回<EFBFBD><EFBFBD>?
|
||||
*/
|
||||
- (void)scheduleIPQualityDetection:(NSString *)cacheKey
|
||||
ip:(NSString *)ip
|
||||
@@ -45,12 +45,12 @@ typedef void(^HttpdnsIPQualityCallback)(NSString *cacheKey, NSString *ip, NSInte
|
||||
#pragma mark - Methods exposed for testing
|
||||
|
||||
/**
|
||||
* 执行IP连接质量检测
|
||||
* @param cacheKey 缓存键,通常是域名
|
||||
* 执行IP连接质量检<EFBFBD><EFBFBD>?
|
||||
* @param cacheKey 缓存键,通常是域<EFBFBD><EFBFBD>?
|
||||
* @param ip 要检测的IP地址
|
||||
* @param port 连接端口,如果为nil则默认使用80
|
||||
* @param callback 检测完成后的回调
|
||||
* @note 此方法主要用于测试
|
||||
* @param port 连接端口,如果为nil则默认使<EFBFBD><EFBFBD>?0
|
||||
* @param callback 检测完成后的回<EFBFBD><EFBFBD>?
|
||||
* @note 此方法主要用于测<EFBFBD><EFBFBD>?
|
||||
*/
|
||||
- (void)executeDetection:(NSString *)cacheKey
|
||||
ip:(NSString *)ip
|
||||
@@ -58,21 +58,21 @@ typedef void(^HttpdnsIPQualityCallback)(NSString *cacheKey, NSString *ip, NSInte
|
||||
callback:(HttpdnsIPQualityCallback)callback;
|
||||
|
||||
/**
|
||||
* 建立TCP连接并测量连接时间
|
||||
* 建立TCP连接并测量连接时<EFBFBD><EFBFBD>?
|
||||
* @param ip 要连接的IP地址
|
||||
* @param port 连接端口
|
||||
* @return 连接耗时(毫秒),-1表示连接失败
|
||||
* @note 此方法主要用于测试
|
||||
* @return 连接耗时(毫秒)<EFBFBD><EFBFBD>?1表示连接失败
|
||||
* @note 此方法主要用于测<EFBFBD><EFBFBD>?
|
||||
*/
|
||||
- (NSInteger)tcpConnectToIP:(NSString *)ip port:(int)port;
|
||||
|
||||
/**
|
||||
* 添加待处理任务
|
||||
* @param cacheKey 缓存键,通常是域名
|
||||
* 添加待处理任<EFBFBD><EFBFBD>?
|
||||
* @param cacheKey 缓存键,通常是域<EFBFBD><EFBFBD>?
|
||||
* @param ip 要检测的IP地址
|
||||
* @param port 连接端口
|
||||
* @param callback 检测完成后的回调
|
||||
* @note 此方法主要用于测试
|
||||
* @param callback 检测完成后的回<EFBFBD><EFBFBD>?
|
||||
* @note 此方法主要用于测<EFBFBD><EFBFBD>?
|
||||
*/
|
||||
- (void)addPendingTask:(NSString *)cacheKey
|
||||
ip:(NSString *)ip
|
||||
@@ -80,14 +80,14 @@ typedef void(^HttpdnsIPQualityCallback)(NSString *cacheKey, NSString *ip, NSInte
|
||||
callback:(HttpdnsIPQualityCallback)callback;
|
||||
|
||||
/**
|
||||
* 处理待处理任务队列
|
||||
* @note 此方法主要用于测试
|
||||
* 处理待处理任务队<EFBFBD><EFBFBD>?
|
||||
* @note 此方法主要用于测<EFBFBD><EFBFBD>?
|
||||
*/
|
||||
- (void)processPendingTasksIfNeeded;
|
||||
|
||||
/**
|
||||
* 处理所有待处理任务
|
||||
* @note 此方法主要用于测试
|
||||
* @note 此方法主要用于测<EFBFBD><EFBFBD>?
|
||||
*/
|
||||
- (void)processPendingTasks;
|
||||
|
||||
|
||||
@@ -463,7 +463,7 @@ static void TMReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRea
|
||||
return HttpdnsReachableVia5G;
|
||||
}
|
||||
}
|
||||
// 默认以使用最广泛的4G兜底
|
||||
// 默认以使用最广泛<EFBFBD><EFBFBD>?G兜底
|
||||
return HttpdnsReachableVia4G;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -222,8 +222,8 @@
|
||||
|
||||
/**
|
||||
生成sessionId
|
||||
App打开生命周期只生成一次,不做持久化
|
||||
sessionId为12位,采用base62编码
|
||||
App打开生命周期只生成一次,不做持久<EFBFBD><EFBFBD>?
|
||||
sessionId<EFBFBD><EFBFBD>?2位,采用base62编码
|
||||
*/
|
||||
+ (NSString *)generateSessionID {
|
||||
static NSString *sessionId = nil;
|
||||
@@ -256,11 +256,11 @@
|
||||
+ (NSData *)encryptDataAESCBC:(NSData *)plaintext
|
||||
withKey:(NSData *)key
|
||||
error:(NSError **)error {
|
||||
// 检查输入参数
|
||||
// 检查输入参<EFBFBD><EFBFBD>?
|
||||
if (plaintext == nil || [plaintext length] == 0 || key == nil || [key length] != kCCKeySizeAES128) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:ALICLOUD_HTTPDNS_ERROR_DOMAIN
|
||||
code:ALICLOUD_HTTPDNS_ENCRYPT_INVALID_PARAMS_ERROR_CODE
|
||||
*error = [NSError errorWithDomain:Trust_HTTPDNS_ERROR_DOMAIN
|
||||
code:Trust_HTTPDNS_ENCRYPT_INVALID_PARAMS_ERROR_CODE
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Invalid input parameters"}];
|
||||
}
|
||||
return nil;
|
||||
@@ -271,18 +271,18 @@
|
||||
int result = SecRandomCopyBytes(kSecRandomDefault, kCCBlockSizeAES128, iv.mutableBytes);
|
||||
if (result != 0) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:ALICLOUD_HTTPDNS_ERROR_DOMAIN
|
||||
code:ALICLOUD_HTTPDNS_ENCRYPT_RANDOM_IV_ERROR_CODE
|
||||
*error = [NSError errorWithDomain:Trust_HTTPDNS_ERROR_DOMAIN
|
||||
code:Trust_HTTPDNS_ENCRYPT_RANDOM_IV_ERROR_CODE
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Failed to generate random IV"}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
// 计算加密后的数据长度 (可能需要填充)
|
||||
// 计算加密后的数据长度 (可能需要填<EFBFBD><EFBFBD>?
|
||||
size_t bufferSize = [plaintext length] + kCCBlockSizeAES128;
|
||||
size_t encryptedSize = 0;
|
||||
|
||||
// 创建输出缓冲区
|
||||
// 创建输出缓冲<EFBFBD><EFBFBD>?
|
||||
NSMutableData *cipherData = [NSMutableData dataWithLength:bufferSize];
|
||||
|
||||
// 执行加密
|
||||
@@ -301,17 +301,17 @@
|
||||
|
||||
if (cryptStatus != kCCSuccess) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:ALICLOUD_HTTPDNS_ERROR_DOMAIN
|
||||
code:ALICLOUD_HTTPDNS_ENCRYPT_FAILED_ERROR_CODE
|
||||
*error = [NSError errorWithDomain:Trust_HTTPDNS_ERROR_DOMAIN
|
||||
code:Trust_HTTPDNS_ENCRYPT_FAILED_ERROR_CODE
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Encryption failed with status: %d", cryptStatus]}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
// 调整加密数据的长度,只保留实际加密内容
|
||||
// 调整加密数据的长度,只保留实际加密内<EFBFBD><EFBFBD>?
|
||||
[cipherData setLength:encryptedSize];
|
||||
|
||||
// 将IV和加密数据合并在一起
|
||||
// 将IV和加密数据合并在一<EFBFBD><EFBFBD>?
|
||||
NSMutableData *resultData = [NSMutableData dataWithData:iv];
|
||||
[resultData appendData:cipherData];
|
||||
|
||||
@@ -338,7 +338,7 @@
|
||||
return nil;
|
||||
}
|
||||
|
||||
// 移除可能存在的空格
|
||||
// 移除可能存在的空<EFBFBD><EFBFBD>?
|
||||
NSString *cleanedString = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""];
|
||||
|
||||
// 确保字符串长度为偶数
|
||||
@@ -391,25 +391,25 @@
|
||||
+ (NSData *)decryptDataAESCBC:(NSData *)ciphertext
|
||||
withKey:(NSData *)key
|
||||
error:(NSError **)error {
|
||||
// 检查输入参数
|
||||
// 检查输入参<EFBFBD><EFBFBD>?
|
||||
if (ciphertext == nil || [ciphertext length] <= kCCBlockSizeAES128 || key == nil || [key length] != kCCKeySizeAES128) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:ALICLOUD_HTTPDNS_ERROR_DOMAIN
|
||||
code:ALICLOUD_HTTPDNS_ENCRYPT_INVALID_PARAMS_ERROR_CODE
|
||||
*error = [NSError errorWithDomain:Trust_HTTPDNS_ERROR_DOMAIN
|
||||
code:Trust_HTTPDNS_ENCRYPT_INVALID_PARAMS_ERROR_CODE
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Invalid input parameters for decryption"}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
// 提取IV(前16字节)和实际的密文
|
||||
// 提取IV(前16字节)和实际的密<EFBFBD><EFBFBD>?
|
||||
NSData *iv = [ciphertext subdataWithRange:NSMakeRange(0, kCCBlockSizeAES128)];
|
||||
NSData *actualCiphertext = [ciphertext subdataWithRange:NSMakeRange(kCCBlockSizeAES128, ciphertext.length - kCCBlockSizeAES128)];
|
||||
|
||||
// 计算解密后可能的缓冲区大小
|
||||
// 计算解密后可能的缓冲区大<EFBFBD><EFBFBD>?
|
||||
size_t bufferSize = actualCiphertext.length + kCCBlockSizeAES128;
|
||||
size_t decryptedSize = 0;
|
||||
|
||||
// 创建输出缓冲区
|
||||
// 创建输出缓冲<EFBFBD><EFBFBD>?
|
||||
NSMutableData *decryptedData = [NSMutableData dataWithLength:bufferSize];
|
||||
|
||||
// 执行解密
|
||||
@@ -427,14 +427,14 @@
|
||||
|
||||
if (cryptStatus != kCCSuccess) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:ALICLOUD_HTTPDNS_ERROR_DOMAIN
|
||||
code:ALICLOUD_HTTPDNS_ENCRYPT_FAILED_ERROR_CODE
|
||||
*error = [NSError errorWithDomain:Trust_HTTPDNS_ERROR_DOMAIN
|
||||
code:Trust_HTTPDNS_ENCRYPT_FAILED_ERROR_CODE
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Decryption failed with status: %d", cryptStatus]}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
// 调整解密数据的长度,只保留实际解密内容
|
||||
// 调整解密数据的长度,只保留实际解密内<EFBFBD><EFBFBD>?
|
||||
[decryptedData setLength:decryptedSize];
|
||||
|
||||
return decryptedData;
|
||||
@@ -453,14 +453,14 @@
|
||||
|
||||
if (dnsService.ttlDelegate && [dnsService.ttlDelegate respondsToSelector:@selector(httpdnsHost:ipType:ttl:)]) {
|
||||
if ([self isNotEmptyArray:[hostObject getV4Ips]]) {
|
||||
int64_t customV4TTL = [dnsService.ttlDelegate httpdnsHost:host ipType:AlicloudHttpDNS_IPTypeV4 ttl:hostObject.v4ttl];
|
||||
int64_t customV4TTL = [dnsService.ttlDelegate httpdnsHost:host ipType:TrustHttpDNS_IPTypeV4 ttl:hostObject.v4ttl];
|
||||
if (customV4TTL > 0) {
|
||||
hostObject.v4ttl = customV4TTL;
|
||||
}
|
||||
}
|
||||
|
||||
if ([self isNotEmptyArray:[hostObject getV6Ips]]) {
|
||||
int64_t customV6TTL = [dnsService.ttlDelegate httpdnsHost:host ipType:AlicloudHttpDNS_IPTypeV6 ttl:hostObject.v6ttl];
|
||||
int64_t customV6TTL = [dnsService.ttlDelegate httpdnsHost:host ipType:TrustHttpDNS_IPTypeV6 ttl:hostObject.v6ttl];
|
||||
if (customV6TTL > 0) {
|
||||
hostObject.v6ttl = customV6TTL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user