sdk final

This commit is contained in:
robin
2026-03-05 16:53:59 +08:00
parent a10f3f3740
commit 491ade1bc3
44 changed files with 1595 additions and 960 deletions

View File

@@ -1,11 +1,12 @@
//
//
// 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
@@ -14,8 +15,12 @@
- (instancetype)init {
if (self = [super init]) {
_host = @"demo.cloudxdr.com";
_queryType = @"A";
NSString *serviceDomain = [DemoConfigLoader shared].serviceDomain;
_host = serviceDomain.length > 0 ? serviceDomain : @"www.new.com";
_ipType = HttpdnsQueryIPTypeBoth;
// _httpsEnabled = YES;
// _persistentCacheEnabled = YES;
// _reuseExpiredIPEnabled = YES;
}
return self;
}
@@ -23,7 +28,10 @@
- (id)copyWithZone:(NSZone *)zone {
DemoHttpdnsScenarioConfig *cfg = [[[self class] allocWithZone:zone] init];
cfg.host = self.host;
cfg.queryType = self.queryType;
cfg.ipType = self.ipType;
// cfg.httpsEnabled = self.httpsEnabled;
// cfg.persistentCacheEnabled = self.persistentCacheEnabled;
// cfg.reuseExpiredIPEnabled = self.reuseExpiredIPEnabled;
return cfg;
}
@@ -48,76 +56,175 @@
_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];
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]];
if (cfg.hasValidConfig) {
self.service = [[HttpdnsEdgeService alloc] initWithAppId:cfg.appId apiUrl:cfg.apiUrl signSecret:cfg.signSecret];
[self log:[NSString stringWithFormat:@"Init HttpdnsEdgeService success!"]];
[self log:[NSString stringWithFormat:@"== Config details =="]];
[self log:[NSString stringWithFormat:@"appId: %@", cfg.appId]];
[self log:[NSString stringWithFormat:@"apiUrl: %@", cfg.apiUrl]];
[self log:[NSString stringWithFormat:@"serviceDomain: %@", cfg.serviceDomain]];
[self log:[NSString stringWithFormat:@"signSecret length: %tu", cfg.signSecret.length]];
} else {
[self log:@"Init HttpdnsEdgeService failed! Missing required fields."];
}
}
- (void)applyConfig:(DemoHttpdnsScenarioConfig *)config {
self.config = [config copy];
self.model.host = self.config.host;
self.model.ipType = self.config.ipType;
[self log:[NSString stringWithFormat:@"Apply UI config: host=%@, ipType=%ld", self.config.host, (long)self.config.ipType]];
}
- (void)resolve {
[self resolveSyncNonBlocking];
}
- (void)resolveSyncNonBlocking {
NSString *host = self.config.host.length > 0 ? self.config.host : @"demo.cloudxdr.com";
NSString *queryType = self.config.queryType.length > 0 ? self.config.queryType : @"A";
NSString *queryHost = [self currentHost];
HttpdnsQueryIPType ipType = self.config.ipType;
NSTimeInterval startMs = [[NSDate date] timeIntervalSince1970] * 1000.0;
[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 {
[self resolveSyncNonBlocking];
}
- (void)handleResult:(HttpdnsEdgeResolveResult *)result
host:(NSString *)host
queryType:(NSString *)queryType
start:(NSTimeInterval)startMs
error:(NSError *)error {
NSTimeInterval elapsedMs = [[NSDate date] timeIntervalSince1970] * 1000.0 - startMs;
if (error != nil) {
[self appendLog:[NSString stringWithFormat:@"[error] %@", error.localizedDescription]];
[self log:[NSString stringWithFormat:@"--- Start Resolve Request ---"]];
[self log:[NSString stringWithFormat:@"Target Host: %@", queryHost]];
if (ipType == HttpdnsQueryIPTypeBoth) {
[self log:@"Query Type: BOTH (mapped to concurrent A & AAAA)"];
dispatch_group_t group = dispatch_group_create();
__block HttpdnsEdgeResolveResult *resA = nil;
__block NSError *errA = nil;
__block HttpdnsEdgeResolveResult *resAAAA = nil;
__block NSError *errAAAA = nil;
dispatch_group_enter(group);
[self.service resolveHost:queryHost queryType:@"A" completion:^(HttpdnsEdgeResolveResult * _Nullable result, NSError * _Nullable error) {
resA = result;
errA = error;
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[self.service resolveHost:queryHost queryType:@"AAAA" completion:^(HttpdnsEdgeResolveResult * _Nullable result, NSError * _Nullable error) {
resAAAA = result;
errAAAA = error;
dispatch_group_leave(group);
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[self log:@"--- Edge Service Callback Triggered (BOTH) ---"];
HttpdnsEdgeResolveResult *merged = [[HttpdnsEdgeResolveResult alloc] init];
merged.ipv4s = resA ? resA.ipv4s : @[];
merged.ipv6s = resAAAA ? resAAAA.ipv6s : @[];
merged.ttl = 0;
if (resA && resAAAA) {
merged.ttl = MIN(resA.ttl, resAAAA.ttl);
} else if (resA) {
merged.ttl = resA.ttl;
} else if (resAAAA) {
merged.ttl = resAAAA.ttl;
}
if (resA.requestId) merged.requestId = resA.requestId;
else if (resAAAA.requestId) merged.requestId = resAAAA.requestId;
NSError *finalErr = nil;
if (errA && errAAAA) {
finalErr = errA;
}
[self handleEdgeResult:merged error:finalErr host:queryHost ipType:ipType start:startMs];
});
} else {
[self appendLog:[NSString stringWithFormat:@"[result] requestId=%@ ipv4=%@ ipv6=%@ ttl=%ld elapsed=%.0fms",
result.requestId, result.ipv4s, result.ipv6s, (long)result.ttl, elapsedMs]];
NSString *qtype = (ipType == HttpdnsQueryIPTypeIpv6) ? @"AAAA" : @"A";
[self log:[NSString stringWithFormat:@"Query Type: %@", qtype]];
[self.service resolveHost:queryHost queryType:qtype completion:^(HttpdnsEdgeResolveResult * _Nullable result, NSError * _Nullable error) {
[self log:[NSString stringWithFormat:@"--- Edge Service Callback Triggered ---"]];
[self handleEdgeResult:result error:error host:queryHost ipType:ipType start:startMs];
}];
}
}
- (NSString *)logSnapshot {
__block NSString *snapshot = @"";
dispatch_sync(self.logQueue, ^{
snapshot = [self.logBuffer copy];
});
return snapshot;
}
- (NSString *)currentHost {
return self.config.host.length > 0 ? self.config.host : @"demo.cloudxdr.com";
}
- (void)handleEdgeResult:(HttpdnsEdgeResolveResult *)result error:(NSError *)error host:(NSString *)host ipType:(HttpdnsQueryIPType)ipType start:(NSTimeInterval)startMs {
dispatch_async(dispatch_get_main_queue(), ^{
self.model.host = host;
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.ipType = ipType;
NSTimeInterval endMs = [[NSDate date] timeIntervalSince1970] * 1000.0;
self.model.elapsedMs = endMs - startMs;
if (error) {
self.model.ipv4s = @[];
self.model.ipv6s = @[];
self.model.error = error;
self.model.ttlV4 = 0;
self.model.ttlV6 = 0;
[self log:[NSString stringWithFormat:@"Edge Resolve Failed!"]];
[self log:[NSString stringWithFormat:@"Error domain: %@", error.domain]];
[self log:[NSString stringWithFormat:@"Error code: %ld", (long)error.code]];
[self log:[NSString stringWithFormat:@"Error description: %@", error.localizedDescription]];
if (error.userInfo) {
[self log:[NSString stringWithFormat:@"Error user info: %@", error.userInfo]];
}
} else {
self.model.ipv4s = result.ipv4s ?: @[];
self.model.ipv6s = result.ipv6s ?: @[];
self.model.ttlV4 = result.ttl;
self.model.ttlV6 = result.ttl;
self.model.businessRequestResult = @"Waiting for request...";
[self log:[NSString stringWithFormat:@"Edge Resolve Success!"]];
[self log:[NSString stringWithFormat:@"Host: %@", host]];
[self log:[NSString stringWithFormat:@"IPv4s: %@", result.ipv4s]];
[self log:[NSString stringWithFormat:@"IPv6s: %@", result.ipv6s]];
[self log:[NSString stringWithFormat:@"TTL: %ld", (long)result.ttl]];
[self log:[NSString stringWithFormat:@"Request ID: %@", result.requestId]];
// Fire off a business request to demonstrate real usage if we have an IP
if (result.ipv4s.count > 0 || result.ipv6s.count > 0) {
[self log:@"\n--- Start Business Request Demo ---"];
// Construct a URL for the business domain
NSURL *businessURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@", host]];
[self log:[NSString stringWithFormat:@"Requesting URL: %@", businessURL.absoluteString]];
[self.service requestURL:businessURL method:@"GET" headers:nil body:nil completion:^(NSData * _Nullable data, NSHTTPURLResponse * _Nullable response, NSError * _Nullable reqError) {
dispatch_async(dispatch_get_main_queue(), ^{
if (reqError) {
NSString *err = [NSString stringWithFormat:@"Failed: %@", reqError.localizedDescription];
[self log:[NSString stringWithFormat:@"Business Request %@", err]];
self.model.businessRequestResult = err;
} else {
[self log:[NSString stringWithFormat:@"Business Request Success! Status Code: %ld", (long)response.statusCode]];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (responseString.length > 200) {
responseString = [[responseString substringToIndex:200] stringByAppendingString:@"... (truncated)"];
}
[self log:[NSString stringWithFormat:@"Response Body:\n%@", responseString]];
self.model.businessRequestResult = [NSString stringWithFormat:@"HTTP %ld\n%@", (long)response.statusCode, responseString];
}
id<DemoHttpdnsScenarioDelegate> bDelegate = self.delegate;
if (bDelegate != nil) {
[bDelegate scenario:self didUpdateModel:self.model];
}
});
}];
}
}
self.model.elapsedMs = elapsedMs;
id<DemoHttpdnsScenarioDelegate> delegate = self.delegate;
if (delegate != nil) {
[delegate scenario:self didUpdateModel:self.model];
@@ -125,9 +232,13 @@
});
}
- (void)appendLog:(NSString *)msg {
if (msg.length == 0) return;
NSString *line = [NSString stringWithFormat:@"%@ %@\n", [NSDate date], msg];
// logger & ttl delegates removed since EdgeService lacks these protocol hooks in this SDK version
- (void)log:(NSString *)logStr {
if (logStr.length == 0) {
return;
}
NSString *line = [NSString stringWithFormat:@"%@ %@\n", [NSDate date], logStr];
dispatch_async(self.logQueue, ^{
[self.logBuffer appendString:line];
id<DemoHttpdnsScenarioDelegate> delegate = self.delegate;
@@ -139,12 +250,6 @@
});
}
- (NSString *)logSnapshot {
__block NSString *snapshot = @"";
dispatch_sync(self.logQueue, ^{
snapshot = [self.logBuffer copy];
});
return snapshot;
}
@end