tencent cloud

Feedback

Last updated: 2024-10-21 17:43:47

    Setting basic business information

    Type definition

    Note:
    [Disused from v1.7.0] The SDK log reporting capability is configured in the console.
    /**
    Encryption method
    **/
    typedef enum {
    HttpDnsEncryptTypeDES = 0, // DES encryption
    HttpDnsEncryptTypeAES = 1, // AES encryption
    HttpDnsEncryptTypeHTTPS = 2 // HTTPS encryption
    } HttpDnsEncryptType;
    
    /**
    IP address type
    **/
    typedef enum {
    HttpDnsAddressTypeAuto = 0, // Automatically detected by the SDK
    HttpDnsAddressTypeIPv4 = 1, // Only IPv4 is supported
    HttpDnsAddressTypeIPv6 = 2, // Only IPv6 is supported
    HttpDnsAddressTypeDual = 3, // Both protocols are supported
    } HttpDnsAddressType;
    
    /**
    Configuration structure
    You can get the following authentication information after activating the service in the HTTPDNS console (https://console.tencentcloud.com/httpdns/configure).
    **/
    typedef struct DnsConfigStruct {
    NSString* appId; // Application ID, which is optional and can be obtained after you apply for SDK integration in the HTTPDNS console. It is used for data reporting
    int dnsId; // Authorization ID, which you can directly view in the HTTPDNS console after applying
    NSString* 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 authentication
    NSString* token; // Encryption token, which you must pass in for the HTTPS encryption method
    NSString* 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 and HTTPS protocols are `119.29.29.98` and `119.29.29.99` respectively
    BOOL debug; // Specify whether to enable debug logging. `YES`: Enable; `NO`: Disable. We recommend you enable it during joint testing and disable it before launch
    int timeout; // Timeout period in milliseconds, which is optional. If you set it to `0`, the default value `2000ms` will be used
    HttpDnsEncryptType encryptType; // Set the encryption method
    HttpDnsAddressType 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 address
    BOOL 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 returned
    NSUInteger retryTimesBeforeSwitchServer; // Number of retries before the IP switch, which is optional and 3 by default
    NSUInteger minutesBeforeSwitchToMain; // Interval for switching back to the primary IP, which is optional and 10 minutes by default
    BOOL enableReport; // [Disused from v1.7.0] The SDK log reporting capability is configured in the console.
    } DnsConfig;

    API declaration

    /**
    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.
    */
    - (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.
    */
    - (void) WGSetExpiredIPEnabled:(BOOL)enable;
    
    /**
    * Set whether to enable the feature of local persistent cache. It is disabled by default.
    */
    - (void) WGSetPersistCacheIPEnabled:(BOOL)enable;
    Note:
    The HTTPDNS SDK provides multiple DNS optimization options, which can be combined for optimal DNS performance.
    You can configure (void) WGSetExpiredIPEnabled:(true)enable; and (void) WGSetPersistCacheIPEnabled:(true)enable; to achieve optimistic DNS cache.
    The persistent cache feature is used to improve cache hit rate and shorten the time to the first meaningful paint. This feature stores the last DNS result locally and preferentially reads the local cache when an app is launched.
    If the cached DNS result has expired (TTL expired), since optimistic DNS cache allows the return of IP addresses in expired DNS results, the SDK returns the IP addresses in the expired DNS result (assuming that the IP addresses are available in most cases) and initiates an async request to update the cache.
    When optimistic DNS cache is enabled, cache cannot be hit (there is no cache) for the first DNS request of a domain, so 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;.
    If the server IP addresses are changed frequently, be sure to enable automatic cache update via (void) WGSetKeepAliveDomains:(NSArray *)domains; and DNS prefetch via (void) WGSetPreResolvedDomains:(NSArray *)domains; to ensure the accuracy of DNS results.

    Sample code

    Sample API call:
    In an Objective-C project:
    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];
    In a Swift project:
    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; 2: HTTPS
    ]);

    DNS APIs

    The following APIs are used to get IPs, and you only need to import the header file to call the corresponding API. For batch query, a maximum of 8 domain names are supported at each time.
    Sync APIs
    Single query API: WGGetHostByName:
    Batch query API (with one IP returned): WGGetHostsByNames:
    Batch query API (with all IPs returned): WGGetAllHostsByNames:
    Note:
    For a sync API, execution will block. Therefore, we recommended you call a sync API in a child thread or use an async API.
    Async APIs
    Single query API: WGGetHostByNameAsync:returnIps:
    Batch query API (with one IP returned): WGGetHostsByNamesAsync:returnIps:
    Batch query API (with all IPs returned): WGGetAllHostsByNamesAsync:returnIps:
    The returned address is in the following format:
    Single query: The single query API will return 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:
    Under IPv4, only the IPv4 address will be returned; that is, the response format will be [ipv4, 0] .
    Under IPv6, only the IPv6 address will be returned; that is, the response format will be [0, ipv6] .
    Under a dual stack network, the resolved IPv4 and IPv6 addresses (if they exist) will be returned; that is, the response format will be [ipv4, ipv6] .
    If DNS query fails, [0, 0] will be returned; in this case, you only need to call the WGGetHostByName API again.
    Batch query (with one IP returned) : The batch query API will return an 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:
    Under IPv4, only the IPv4 address will be returned; that is, the response format will be {"queryDomain" : [ipv4, 0]}.
    Under IPv6, only the IPv6 address will be returned; that is, the response format will be {"queryDomain" : [0, ipv6]}.
    Under a dual stack network, the resolved IPv4 and IPv6 addresses (if they exist) will be returned; that is, the response format will be {"queryDomain" : [ipv4, ipv6]}.
    If DNS query fails, {"queryDomain" : [0, 0]} will be returned; in this case, you only need to call the WGGetHostByNames API again.
    Batch query (with all IPs returned) : The batch query API will return an 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": [ ]}}
    How to improve IPv6 usage:
    When you make a URL request by using an IPv6 address, you need to enclose the IPv6 address in square brackets, such as http://[64:ff9b::b6fe:7475]/.
    If the IPv6 address is 0, you can directly use the IPv4 address for connection.
    If the IPv4 address is 0, you can directly use the IPv6 address for connection.
    If neither the IPv4 address nor the IPv6 address is 0, the client can determine which address to use first for connection; if the client fails to connect to the preferred address, it should switch to the other one.
    When you integrate HTTPDNS through the SDK, if HTTPDNS does not find a DNS query result, the domain will be resolved by local DNS, and the result provided by local DNS will be returned.

    Sync DNS APIs

    API name

    WGGetHostByName and WGGetHostsByNames

    API declaration

    /**
    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;

    Sample code

    Sample API call:
    // Query a single domain
    NSArray *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 domains
    NSDictionary *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 APIs

    API name

    WGGetHostByNameAsync and WGGetHostsByNamesAsync

    API declaration

    /**
    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;

    Sample code

    Note:
    You can select any call method based on your business needs.
    Example 1
    Example 2
    Wait for the entire DNS query process to complete, get the result, and make a connection.
    Strengths: It is guaranteed that each request can get the returned result for subsequent connection operations.
    Shortcomings: Processing through the async API is slightly more complex than that through the sync API.
    // 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 connection
    if (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 connection
    NSArray *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
    }
    }
    }];
    You can directly get the cached result with no need to wait. If there is no cache, result will be nil
    Strengths: Businesses that have demanding requirements for the DNS time don't need to wait and can directly get the cached result to perform subsequent connection operations, unlike the sync API where the DNS query may take more than 100 milliseconds.
    Shortcomings: The result of the first request will be nil, 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
    }
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support