/**Encryption method**/typedef enum {HttpDnsEncryptTypeDES = 0, // DES encryptionHttpDnsEncryptTypeAES = 1, // AES encryptionHttpDnsEncryptTypeHTTPS = 2 // HTTPS encryption, this method is not supported on the international site} HttpDnsEncryptType;/**IP address type**/typedef enum {HttpDnsAddressTypeAuto = 0, // Automatically detected by the SDKHttpDnsAddressTypeIPv4 = 1, // Only IPv4 is supportedHttpDnsAddressTypeIPv6 = 2, // Only IPv6 is supportedHttpDnsAddressTypeDual = 3, // Both protocols are supported} HttpDnsAddressType;/**Configuration structureYou can get the following authentication information after activating the service in the HTTPDNS console (https://console.tencentcloud.com/httpdns/configure).**/typedef struct DnsConfigStruct {int dnsId; // Authorization ID, which you can directly view in the HTTPDNS console after applyingNSString* dnsKey; // Encryption key, which you must pass in for the AES or DES encryption method and can view in the HTTPDNS console after applying. It is used for DNS authenticationNSString* dnsIp; // [For v1.8.0 and later, the IP is scheduled inside the SDK and you do not need to set it] HTTPDNS server IP. The service addresses for the HTTP protocols is `119.29.29.98`BOOL debug; // Specify whether to enable debug logging. `YES`: Enable; `NO`: Disable. We recommend you enable it during joint testing and disable it before launchint timeout; // Timeout period in milliseconds, which is optional. If you set it to `0`, the default value `2000ms` will be usedHttpDnsEncryptType encryptType; // Set the encryption methodHttpDnsAddressType addressType; // Specify the type of the returned IP address, which is `HttpDnsAddressTypeAuto` by default (automatically detected by the SDK)NSString* routeIp; // The ECS (EDNS-Client-Subnet) value of the DNS request. By default, the HTTPDNS server will query the client's egress IP in order to query the IP for the DNS split zone. You can specify the split zone's IP address by passing in an IPv4/IPv6 addressBOOL httpOnly;// This parameter is optional. It specifies whether to return only the DNS query result provided by HTTPDNS. `false` (default): When HTTPDNS query fails, the DNS query result provided by the local DNS will be returned. `true`: Only the DNS query result provided by HTTPDNS will be returnedNSUInteger retryTimesBeforeSwitchServer; // Number of retries before the IP switch, which is optional and 3 by defaultNSUInteger minutesBeforeSwitchToMain; // Interval for switching back to the primary IP, which is optional and 10 minutes by defaultBOOL enableReport; // [Disused from v1.7.0] The SDK log reporting capability is configured in the console.} DnsConfig;
/**Set the basic business information (for Tencent Cloud services)@param config Business configuration structure@return YES: Set successfully. NO: Failed to set.*/- (BOOL) initConfig:(DnsConfig *)config;/*** It is configured through `Dictionary` as for the `DnsConfig` structure. It is used for compatibility with Swift projects so that the `DnsConfig` type can be recognized in Swift projects** @param config Configuration* @return YES: Set successfully. NO: Failed to set.*/- (BOOL) initConfigWithDictionary:(NSDictionary *)config;/**// Domains for DNS prefetch. You can configure up to eight domains. DNS prefetch is triggered only during initialization.* Sample code: [[MSDKDns sharedInstance] WGSetPreResolvedDomains:@[@"dnspod.com", @"dnspod.cn"]];* @param domains Domain array*/- (void) WGSetPreResolvedDomains:(NSArray *)domains;/*** Domains for automatic cache update. You can configure up to eight domains.* Sample code: [[MSDKDns sharedInstance] WGSetKeepAliveDomains:@[@"dnspod.com", @"dnspod.cn"]];* @param domains Domain array*/- (void) WGSetKeepAliveDomains:(NSArray *)domains;/*** Set whether to enable IP address ranking. To enable it, configure the port number corresponding to the domain so that HTTPDNS dynamically ranks the resolved IP addresses and ensures that the first resolved IP address is optimal.* Sample code: [[MSDKDns sharedInstance] WGSetIPRankData:@{@"www.dnspod.com":@443}];*/- (void) WGSetIPRankData:(NSDictionary *)IPRankData;/*** Set whether to return the IP addresses in expired DNS records of a domain. This feature is disabled by default.* When this parameter is set to `true`, the SDK directly returns the cached DNS result or `0` if there is no cache, and initiates an async DNS request to update the cache if there is no cached result or the cache has expired. Since the async API always returns the unexpired DNS result in the callback, the async API is unavailable when this parameter is set to `true`. In this case, we recommend you use the sync API.* Note: After enabling this feature, the cache will not be cleared when the network environment changes. Choose to use it based on your own business situation.*/- (void) WGSetExpiredIPEnabled:(BOOL)enable;/*** Set whether to enable the feature of local persistent cache. It is disabled by default.*/- (void) WGSetPersistCacheIPEnabled:(BOOL)enable;
(void) WGSetExpiredIPEnabled:(true)enable; and (void) WGSetPersistCacheIPEnabled:(true)enable; to achieve optimistic DNS cache.0;0 is returned, and an async DNS request is initiated to update the cache. Therefore, after enabling optimistic DNS cache, you need to ensure that local DNS will work if no cache is hit. For important domains, we recommend you enable DNS prefetch via (void) WGSetPreResolvedDomains:(NSArray *)domains;.DnsConfig *config = new DnsConfig();config->dnsIp = @"HTTPDNS server IP";config->dnsId = DNS authorization ID;config->dnsKey = @"Encryption key";config->encryptType = HttpDnsEncryptTypeDES;config->debug = YES;config->timeout = 2000;[[MSDKDns sharedInstance] initConfig: config];
let msdkDns = MSDKDns.sharedInstance() as? MSDKDns;msdkDns?.initConfig(with: ["dnsIp": "HTTPDNS server IP","dnsId": "DNS authorization ID","dnsKey": "Encryption key","encryptType": 0, // 0: DES; 1: AES]);
NSArray with a fixed length of 2, where the first value is the IPv4 address, and the second value is the IPv6 address. The response format is as described below:[ipv4, 0] .[0, ipv6] .[ipv4, ipv6] .[0, 0] will be returned; in this case, you only need to call the WGGetHostByName API again.NSDictionary, where key is the queried domain, and value is NSArray with a fixed length of 2. The first value is the IPv4 address, and the second value is the IPv6 address. The response format is as described below:{"queryDomain" : [ipv4, 0]}.{"queryDomain" : [0, ipv6]}.{"queryDomain" : [ipv4, ipv6]}.{"queryDomain" : [0, 0]} will be returned; in this case, you only need to call the WGGetHostByNames API again.NSDictionary, where key is the queried domain, and value is an NSDictionary containing two keys (ipv4, ipv6) with corresponding NSArray objects as values indicating all queried IPv4/IPv6 result IPs. The response format is as follows:
{"queryDomain" : { "ipv4": [], "ipv6": [ ]}}http://[64:ff9b::b6fe:7475]/./**Sync DNS API (general)@param domain Domain@return Array of found IPs. The `[0,0]` array will be returned if timeout (1s) occurs or no IPs are found*/- (NSArray *) WGGetHostByName:(NSString *) domain;/**Batch sync DNS API (general)@param domains Domain array@return Dictionary of found IPs*/- (NSDictionary *) WGGetHostsByNames:(NSArray *) domains;
// Query a single domainNSArray *ipsArray = [[MSDKDns sharedInstance] WGGetHostByName: @"qq.com"];if (ipsArray && ipsArray.count > 1) {NSString *ipv4 = ipsArray[0];NSString *ipv6 = ipsArray[1];if (![ipv6 isEqualToString:@"0"]) {// TODO: When making a URL connection by using an IPv6 address, be sure to enclose the IPv6 address in square brackets, such as `http://[64:ff9b::b6fe:7475]/`} else if (![ipv4 isEqualToString:@"0"]){// Connect by using the IPv4 address} else {// `0,0` will be returned if an exception occurs. In this case, we recommend you retry once}}// Batch query domainsNSDictionary *ipsDict = [[MSDKDns sharedInstance] WGGetHostsByNames: @[@"qq.com", @"dnspod.cn"]];NSArray *ips = [ipsDict objectForKey: @"qq.com"];if (ips && ips.count > 1) {NSString *ipv4 = ips[0];NSString *ipv6 = ips[1];if (![ipv6 isEqualToString:@"0"]) {// TODO: When making a URL connection by using an IPv6 address, be sure to enclose the IPv6 address in square brackets, such as `http://[64:ff9b::b6fe:7475]/`} else if (![ipv4 isEqualToString:@"0"]){// Connect by using the IPv4 address} else {// `0,0` will be returned if an exception occurs. In this case, we recommend you retry once}}
/**Async DNS API (general)@param domain Domain@param handler Array of returned IPs. The `[0,0]` array will be returned if timeout (1s) occurs or no IPs are found*/- (void) WGGetHostByNameAsync:(NSString *) domain returnIps:(void (^)(NSArray *ipsArray))handler;/**Batch async DNS API (general)@param domains Domain array@param handler Dictionary of returned IPs. {"queryDomain" : [0, 0] ...} will be returned if timeout (1s) occurs or no IPs are found*/- (void) WGGetHostsByNamesAsync:(NSArray *) domains returnIps:(void (^)(NSDictionary * ipsDictionary))handler;
// Query a single domain[[MSDKDns sharedInstance] WGGetHostByNameAsync:@"qq.com" returnIps:^(NSArray *ipsArray) {// Wait for the end of the complete DNS query process, get the result, and make a connectionif (ipsArray && ipsArray.count > 1) {NSString *ipv4 = ipsArray[0];NSString *ipv6 = ipsArray[1];if (![ipv6 isEqualToString:@"0"]) {// Suggestion: Use an IPv6 address first if it exists// TODO: When making a URL connection by using an IPv6 address, be sure to enclose the IPv6 address in square brackets, such as `http://[64:ff9b::b6fe:7475]/`} else if (![ipv4 isEqualToString:@"0"]){// Connect by using the IPv4 address} else {// `0,0` will be returned if an exception occurs. In this case, we recommend you retry once}}}];// Batch query domains[[MSDKDns sharedInstance] WGGetHostsByNamesAsync:@[@"qq.com", @"dnspod.cn"] returnIps:^(NSDictionary *ipsDict) {// Wait for the end of the complete DNS query process, get the result, and make a connectionNSArray *ips = [ipsDict objectForKey: @"qq.com"];if (ips && ips.count > 1) {NSString *ipv4 = ips[0];NSString *ipv6 = ips[1];if (![ipv6 isEqualToString:@"0"]) {// Suggestion: Use an IPv6 address first if it exists// TODO: When making a URL connection by using an IPv6 address, be sure to enclose the IPv6 address in square brackets, such as `http://[64:ff9b::b6fe:7475]/`} else if (![ipv4 isEqualToString:@"0"]){// Connect by using the IPv4 address} else {// `0,0` will be returned if an exception occurs. In this case, we recommend you retry once}}}];
result will be nilnil, and you need to add the processing logic.__block NSArray* result;[[MSDKDns sharedInstance] WGGetHostByNameAsync:domain returnIps:^(NSArray *ipsArray) {result = ipsArray;}];// You can directly get the cached result with no need to wait. If there is no cache, `result` will be `nil`if (result) {// Get the cached result and make a connection} else {// There is no cache for this request, and you can follow the original logic}
Feedback