ios demo
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
//
|
||||
//
|
||||
// DemoHttpdnsScenario.m
|
||||
// NewHttpDNSTestDemo
|
||||
//
|
||||
// @author Created by Claude Code on 2025-10-23
|
||||
//
|
||||
|
||||
#import "DemoHttpdnsScenario.h"
|
||||
#import "DemoConfigLoader.h"
|
||||
#import <NewHTTPDNS/HttpdnsEdgeService.h>
|
||||
|
||||
@interface DemoHttpdnsScenarioConfig ()
|
||||
@end
|
||||
@@ -15,11 +14,8 @@
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_host = @"www.new.com";
|
||||
_ipType = HttpdnsQueryIPTypeBoth;
|
||||
_httpsEnabled = YES;
|
||||
_persistentCacheEnabled = YES;
|
||||
_reuseExpiredIPEnabled = YES;
|
||||
_host = @"demo.cloudxdr.com";
|
||||
_queryType = @"A";
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -27,18 +23,15 @@
|
||||
- (id)copyWithZone:(NSZone *)zone {
|
||||
DemoHttpdnsScenarioConfig *cfg = [[[self class] allocWithZone:zone] init];
|
||||
cfg.host = self.host;
|
||||
cfg.ipType = self.ipType;
|
||||
cfg.httpsEnabled = self.httpsEnabled;
|
||||
cfg.persistentCacheEnabled = self.persistentCacheEnabled;
|
||||
cfg.reuseExpiredIPEnabled = self.reuseExpiredIPEnabled;
|
||||
cfg.queryType = self.queryType;
|
||||
return cfg;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface DemoHttpdnsScenario () <HttpdnsLoggerProtocol, HttpdnsTTLDelegate>
|
||||
@interface DemoHttpdnsScenario ()
|
||||
|
||||
@property (nonatomic, strong) HttpDnsService *service;
|
||||
@property (nonatomic, strong) HttpdnsEdgeService *service;
|
||||
@property (nonatomic, strong) DemoHttpdnsScenarioConfig *config;
|
||||
@property (nonatomic, strong) NSMutableString *logBuffer;
|
||||
@property (nonatomic, strong) dispatch_queue_t logQueue;
|
||||
@@ -55,73 +48,76 @@
|
||||
_logBuffer = [NSMutableString string];
|
||||
_logQueue = dispatch_queue_create("com.new.httpdns.demo.log", DISPATCH_QUEUE_SERIAL);
|
||||
[self buildService];
|
||||
[self applyConfig:_config];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)buildService {
|
||||
DemoConfigLoader *cfg = [DemoConfigLoader shared];
|
||||
if (cfg.hasValidAccount) {
|
||||
if (cfg.aesSecretKey.length > 0) {
|
||||
self.service = [[HttpDnsService alloc] initWithAccountID:cfg.accountID secretKey:cfg.secretKey aesSecretKey:cfg.aesSecretKey];
|
||||
} else {
|
||||
self.service = [[HttpDnsService alloc] initWithAccountID:cfg.accountID secretKey:cfg.secretKey];
|
||||
}
|
||||
} else {
|
||||
self.service = [HttpDnsService sharedInstance];
|
||||
}
|
||||
[self.service setLogEnabled:YES];
|
||||
[self.service setNetworkingTimeoutInterval:8];
|
||||
[self.service setDegradeToLocalDNSEnabled:YES];
|
||||
self.service.ttlDelegate = self;
|
||||
[self.service setLogHandler:self];
|
||||
self.service = [[HttpdnsEdgeService alloc]
|
||||
initWithAppId:cfg.appId
|
||||
primaryServiceHost:cfg.primaryServiceHost
|
||||
backupServiceHost:nil
|
||||
servicePort:cfg.servicePort
|
||||
signSecret:cfg.signSecret];
|
||||
[self appendLog:[NSString stringWithFormat:@"[init] appId=%@ host=%@:%ld", cfg.appId, cfg.primaryServiceHost, (long)cfg.servicePort]];
|
||||
}
|
||||
|
||||
- (void)applyConfig:(DemoHttpdnsScenarioConfig *)config {
|
||||
self.config = [config copy];
|
||||
self.model.host = self.config.host;
|
||||
self.model.ipType = self.config.ipType;
|
||||
[self.service setHTTPSRequestEnabled:self.config.httpsEnabled];
|
||||
[self.service setPersistentCacheIPEnabled:self.config.persistentCacheEnabled];
|
||||
[self.service setReuseExpiredIPEnabled:self.config.reuseExpiredIPEnabled];
|
||||
}
|
||||
|
||||
- (void)resolve {
|
||||
[self resolveSyncNonBlocking];
|
||||
}
|
||||
|
||||
- (void)resolveSyncNonBlocking {
|
||||
NSString *queryHost = [self currentHost];
|
||||
HttpdnsQueryIPType ipType = self.config.ipType;
|
||||
NSString *host = self.config.host.length > 0 ? self.config.host : @"demo.cloudxdr.com";
|
||||
NSString *queryType = self.config.queryType.length > 0 ? self.config.queryType : @"A";
|
||||
NSTimeInterval startMs = [[NSDate date] timeIntervalSince1970] * 1000.0;
|
||||
HttpdnsResult *result = [self.service resolveHostSyncNonBlocking:queryHost byIpType:ipType];
|
||||
[self handleResult:result host:queryHost ipType:ipType start:startMs];
|
||||
|
||||
[self appendLog:[NSString stringWithFormat:@"[resolve] host=%@ type=%@", host, queryType]];
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[self.service resolveHost:host queryType:queryType completion:^(HttpdnsEdgeResolveResult *result, NSError *error) {
|
||||
[weakSelf handleResult:result host:host queryType:queryType start:startMs error:error];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)resolveSync {
|
||||
NSString *queryHost = [self currentHost];
|
||||
HttpdnsQueryIPType ipType = self.config.ipType;
|
||||
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
||||
NSTimeInterval startMs = [[NSDate date] timeIntervalSince1970] * 1000.0;
|
||||
HttpdnsResult *result = [self.service resolveHostSync:queryHost byIpType:ipType];
|
||||
[self handleResult:result host:queryHost ipType:ipType start:startMs];
|
||||
});
|
||||
[self resolveSyncNonBlocking];
|
||||
}
|
||||
|
||||
- (NSString *)logSnapshot {
|
||||
__block NSString *snapshot = @"";
|
||||
dispatch_sync(self.logQueue, ^{
|
||||
snapshot = [self.logBuffer copy];
|
||||
});
|
||||
return snapshot;
|
||||
}
|
||||
- (void)handleResult:(HttpdnsEdgeResolveResult *)result
|
||||
host:(NSString *)host
|
||||
queryType:(NSString *)queryType
|
||||
start:(NSTimeInterval)startMs
|
||||
error:(NSError *)error {
|
||||
NSTimeInterval elapsedMs = [[NSDate date] timeIntervalSince1970] * 1000.0 - startMs;
|
||||
|
||||
- (NSString *)currentHost {
|
||||
return self.config.host.length > 0 ? self.config.host : @"www.new.com";
|
||||
}
|
||||
if (error != nil) {
|
||||
[self appendLog:[NSString stringWithFormat:@"[error] %@", error.localizedDescription]];
|
||||
} else {
|
||||
[self appendLog:[NSString stringWithFormat:@"[result] requestId=%@ ipv4=%@ ipv6=%@ ttl=%ld elapsed=%.0fms",
|
||||
result.requestId, result.ipv4s, result.ipv6s, (long)result.ttl, elapsedMs]];
|
||||
}
|
||||
|
||||
- (void)handleResult:(HttpdnsResult *)result host:(NSString *)host ipType:(HttpdnsQueryIPType)ipType start:(NSTimeInterval)startMs {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
self.model.host = host;
|
||||
self.model.ipType = ipType;
|
||||
[self.model updateWithResult:result startTimeMs:startMs];
|
||||
if (result != nil) {
|
||||
self.model.ipv4s = result.ipv4s;
|
||||
self.model.ipv6s = result.ipv6s;
|
||||
self.model.ttlV4 = result.ipv4s.count > 0 ? result.ttl : 0;
|
||||
self.model.ttlV6 = result.ipv6s.count > 0 ? result.ttl : 0;
|
||||
self.model.error = nil;
|
||||
} else {
|
||||
self.model.ipv4s = @[];
|
||||
self.model.ipv6s = @[];
|
||||
self.model.error = error;
|
||||
}
|
||||
self.model.elapsedMs = elapsedMs;
|
||||
|
||||
id<DemoHttpdnsScenarioDelegate> delegate = self.delegate;
|
||||
if (delegate != nil) {
|
||||
[delegate scenario:self didUpdateModel:self.model];
|
||||
@@ -129,12 +125,9 @@
|
||||
});
|
||||
}
|
||||
|
||||
- (void)log:(NSString *)logStr {
|
||||
if (logStr.length == 0) {
|
||||
return;
|
||||
}
|
||||
NSString *line = [NSString stringWithFormat:@"%@ %@\n", [NSDate date], logStr];
|
||||
// 使用串行队列保证日志追加与快照的一致性
|
||||
- (void)appendLog:(NSString *)msg {
|
||||
if (msg.length == 0) return;
|
||||
NSString *line = [NSString stringWithFormat:@"%@ %@\n", [NSDate date], msg];
|
||||
dispatch_async(self.logQueue, ^{
|
||||
[self.logBuffer appendString:line];
|
||||
id<DemoHttpdnsScenarioDelegate> delegate = self.delegate;
|
||||
@@ -146,8 +139,12 @@
|
||||
});
|
||||
}
|
||||
|
||||
- (int64_t)httpdnsHost:(NSString *)host ipType:(NewHttpDNS_IPType)ipType ttl:(int64_t)ttl {
|
||||
return ttl;
|
||||
- (NSString *)logSnapshot {
|
||||
__block NSString *snapshot = @"";
|
||||
dispatch_sync(self.logQueue, ^{
|
||||
snapshot = [self.logBuffer copy];
|
||||
});
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user