151 lines
4.6 KiB
Objective-C
151 lines
4.6 KiB
Objective-C
//
|
||
// HttpdnsLocalResolver.m
|
||
// NewHttpDNS
|
||
//
|
||
// Created by xuyecan on 2025/3/16.
|
||
// Copyright 穢 2025 trustapp.com. All rights reserved.
|
||
//
|
||
|
||
#import "HttpdnsLocalResolver.h"
|
||
#import <netdb.h>
|
||
#import <arpa/inet.h>
|
||
#import <ifaddrs.h>
|
||
#import <sys/socket.h>
|
||
|
||
#import "HttpdnsService.h"
|
||
#import "HttpdnsUtil.h"
|
||
#import "HttpdnsHostObject.h"
|
||
|
||
@implementation HttpdnsLocalResolver
|
||
|
||
- (HttpdnsHostObject *)resolve:(HttpdnsRequest *)request {
|
||
// 1. 撉諹<E69289>颲枏<E9A2B2><E69E8F><EFBFBD>㺭
|
||
NSString *host = request.host;
|
||
if (host.length == 0) {
|
||
return nil; // 瘝⊥<E7989D>銝餅㦤<E9A485>滚虾閫<E899BE><E996AB>
|
||
}
|
||
|
||
HttpDnsService *service = [HttpDnsService getInstanceByAccountId:request.accountId];
|
||
if (!service) {
|
||
service = [HttpDnsService sharedInstance];
|
||
}
|
||
|
||
// 2. <20><><EFBFBD>DNS閫<53><E996AB><EFBFBD>滨蔭
|
||
struct addrinfo hints;
|
||
memset(&hints, 0, sizeof(hints));
|
||
hints.ai_family = AF_UNSPEC; // <20>峕𧒄<E5B395>舀<EFBFBD>IPv4<76>䬠Pv6
|
||
hints.ai_socktype = SOCK_STREAM; // TCP (撖道NS閫<53><E996AB><EFBFBD>亥秩<E4BAA5>𡁜虜銝漤<E98A9D>閬?
|
||
|
||
// 3. <20>扯<EFBFBD>getaddrinfo閫<6F><E996AB>
|
||
struct addrinfo *res = NULL;
|
||
int ret = getaddrinfo([host UTF8String], NULL, &hints, &res);
|
||
if (ret != 0 || res == NULL) {
|
||
// DNS閫<53><E996AB>憭梯揖
|
||
if (res) {
|
||
freeaddrinfo(res);
|
||
}
|
||
return nil;
|
||
}
|
||
|
||
// 4. <20>園<EFBFBD><E59C92><EFBFBD><EFBFBD>釟Pv4<76>䬠Pv6<76>啣<EFBFBD>
|
||
NSMutableArray<NSString *> *ipv4Array = [NSMutableArray array];
|
||
NSMutableArray<NSString *> *ipv6Array = [NSMutableArray array];
|
||
|
||
for (struct addrinfo *p = res; p != NULL; p = p->ai_next) {
|
||
if (p->ai_family == AF_INET || p->ai_family == AF_INET6) {
|
||
char hostBuffer[NI_MAXHOST];
|
||
memset(hostBuffer, 0, sizeof(hostBuffer));
|
||
|
||
if (getnameinfo(p->ai_addr, (socklen_t)p->ai_addrlen,
|
||
hostBuffer, sizeof(hostBuffer),
|
||
NULL, 0, NI_NUMERICHOST) == 0) {
|
||
NSString *ipString = [NSString stringWithUTF8String:hostBuffer];
|
||
if (p->ai_family == AF_INET) {
|
||
[ipv4Array addObject:ipString];
|
||
} else {
|
||
[ipv6Array addObject:ipString];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
freeaddrinfo(res);
|
||
|
||
// 5. <20>寞旿queryIpType蝖桀<E89D96>靽萘<E99DBD><E89098>芯<EFBFBD>IP蝐餃<E89D90>
|
||
BOOL wantIPv4 = NO;
|
||
BOOL wantIPv6 = NO;
|
||
|
||
switch (request.queryIpType) {
|
||
case HttpdnsQueryIPTypeAuto:
|
||
// Auto璅∪<E79285>嚗𡁜<E59A97><F0A1819C>𨀣<EFBFBD>IPv4<76>坔<EFBFBD>蝏<EFBFBD><E89D8F><EFBFBD>痹<EFBFBD>憒<EFBFBD><E68692><EFBFBD>釟Pv6<76>嗘<EFBFBD><E59798><EFBFBD>鉄
|
||
// <20>䭾辺隞嗉挽蝵庫antIPv4銝旖ES
|
||
wantIPv4 = YES;
|
||
// 憒<><E68692>DNS餈𥪜<E9A488>鈭<EFBFBD>Pv6<76>啣<EFBFBD>嚗<EFBFBD><E59A97>銋笔<E98A8B><E7AC94>侵Pv6
|
||
wantIPv6 = (ipv6Array.count > 0);
|
||
break;
|
||
|
||
case HttpdnsQueryIPTypeIpv4:
|
||
wantIPv4 = YES;
|
||
break;
|
||
|
||
case HttpdnsQueryIPTypeIpv6:
|
||
wantIPv6 = YES;
|
||
break;
|
||
|
||
case HttpdnsQueryIPTypeBoth:
|
||
wantIPv4 = YES;
|
||
wantIPv6 = YES;
|
||
break;
|
||
}
|
||
|
||
// 6. <20><>遣<EFBFBD><E981A3>蝏<EFBFBD><E89D8F>HttpdnsIpObject<63>啁<EFBFBD>
|
||
NSMutableArray<HttpdnsIpObject *> *v4IpObjects = [NSMutableArray array];
|
||
NSMutableArray<HttpdnsIpObject *> *v6IpObjects = [NSMutableArray array];
|
||
|
||
if (wantIPv4) {
|
||
for (NSString *ipStr in ipv4Array) {
|
||
HttpdnsIpObject *ipObj = [[HttpdnsIpObject alloc] init];
|
||
[ipObj setIp:ipStr]; // ipObj.ip = ipStr
|
||
// connectedRT暺䁅恕銝?
|
||
[v4IpObjects addObject:ipObj];
|
||
}
|
||
}
|
||
if (wantIPv6) {
|
||
for (NSString *ipStr in ipv6Array) {
|
||
HttpdnsIpObject *ipObj = [[HttpdnsIpObject alloc] init];
|
||
[ipObj setIp:ipStr];
|
||
[v6IpObjects addObject:ipObj];
|
||
}
|
||
}
|
||
|
||
// 7. <20>𥕦遣撟嗅‵<E59785><E280B5>ttpdnsHostObject
|
||
HttpdnsHostObject *hostObject = [[HttpdnsHostObject alloc] init];
|
||
[hostObject setHostName:host]; // hostName = request.host
|
||
[hostObject setV4Ips:v4IpObjects];
|
||
[hostObject setV6Ips:v6IpObjects];
|
||
|
||
// IPv4<76>䬠Pv6<76><36><EFBFBD>霈三TL銝?0蝘?
|
||
[hostObject setV4TTL:60];
|
||
[hostObject setV6TTL:60];
|
||
|
||
// <20>芸<EFBFBD>銋缆tl
|
||
[HttpdnsUtil processCustomTTL:hostObject forHost:host service:service];
|
||
|
||
// 敶枏<E695B6><E69E8F>園𡢿(<28>?970撟港誑<E6B8AF>亦<EFBFBD>蝘埝㺭)
|
||
int64_t now = (int64_t)[[NSDate date] timeIntervalSince1970];
|
||
|
||
// <20>湔鰵<E6B994><E9B0B5><EFBFBD>擧䰻霂X𧒄<EFBCB8>?
|
||
[hostObject setLastIPv4LookupTime:now];
|
||
[hostObject setLastIPv6LookupTime:now];
|
||
|
||
// <20><>扇<EFBFBD>臬炏瘝⊥<E7989D>IPv4<76>𦎾Pv6霈啣<E99C88>
|
||
[hostObject setHasNoIpv4Record:(v4IpObjects.count == 0)];
|
||
[hostObject setHasNoIpv6Record:(v6IpObjects.count == 0)];
|
||
|
||
// 憒<><E68692><EFBFBD><EFBFBD>閬<EFBFBD><E996AC><EFBFBD>臭誑<E887AD>刻<EFBFBD><E588BB>諹挽蝵宮lientIp<49>㚚<EFBFBD>憭硋<E686AD>畾?
|
||
// <20>啣銁靽萘<E99DBD>銝粹<E98A9D>霈文<E99C88>?蝛?
|
||
|
||
return hostObject;
|
||
}
|
||
|
||
@end
|