目录

DnsConfiguration: DNS Customization

Use Cases

In the framework of Remote Communication Kit, the kit provides a highly customizable Domain Name System (DNS) request rule service. Using the service, you can adjust DNS query behavior based on your needs. In addition, user-defined DNS server settings are supported. You can choose and configure the most suitable DNS server according to specific network environments or security requirements. Through these functions, your app not only can achieve highly efficient network management, but also can enhance network security, ensuring the stability and security of data transmission. In DnsConfiguration, you can set dnsRules and dnsOverHttps to implement these functions.

Constraints

The DNS customization capability supports phones, 2-in-1 devices, tablets, and wearables. Starting from version 5.1.1(19), it is also supported on TVs.

Example

The following uses the examples of customizing a DNS server and rewriting the DNS resolution function to illustrate how to customize DNS to achieve the best DNS performance experience.

[h2]Customizing a DNS Server

  1. Import the required modules.

    import { rcp } from '@kit.RemoteCommunicationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Create a new session object and a request. The session is used to manage subsequent network requests. In addition, you need to define a specific request in which the target URL is specified.

    const session = rcp.createSession();
    // Change the URL to the actual one based on your needs.
    const request = new rcp.Request('https://example.com');
  3. Set the DNS rule for requests. You can change the IP address and port number based on your needs.

    request.configuration = {
      dns: {
        dnsRules: [
          {
            ip: '192.168.1.1', // IP address of the DNS server.
            port: 53, // Port number of the DNS server.
          },
        ]
      }
    };
  4. Use FETCH to initiate a network request and handle the response on success or failure. The following is just an example, and the processing of success or failure should be implemented based on your service needs.

    session.fetch(request).then((response: rcp.Response) => {
      console.info(`The response is ${JSON.stringify(response)}`); // Process the success response.
      // ...
      // Close the session.
      session.close();
    }).catch((err: BusinessError) => {
      console.error(`The error code is ${err.code}, error data is ${err.data}`); // Error handling.
      // ...
      // Close the session.
      session.close();
    })

[h2]Customizing a DNS Resolution Function

  1. Import the required modules.

    import { rcp } from '@kit.RemoteCommunicationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Create a new session object and a request. The session is used to manage subsequent network requests. In addition, you need to define a specific request in which the target URL is specified.

    const session = rcp.createSession();
    // Define the request URL. (Change the value in the example based on your needs.)
    const requestURL = 'https://example.com';
    const request = new rcp.Request(requestURL);
  3. Set the DNS resolution rule. The following is only a simple example, and you need to configure the processing logic based on your service needs.

    request.configuration = {
      dns: {
        dnsRules: (host: string, port: number): rcp.IpAddress[] => {
          if (host === 'example.com') {
            return ['192.168.1.1', '192.168.1.2']; // Set this as needed.
          }
          return [];
        }
      }
    };
  4. Use FETCH to initiate a network request and handle the response on success or failure. The following is just an example, and the processing of success or failure should be implemented based on your service needs.

    session.fetch(request).then((response: rcp.Response) => {
      // Process the success response.
      console.info(`The response is ${JSON.stringify(response)}`);
      // ...
      // Close the session.
      session.close();
    }).catch((err: BusinessError) => {
      // Error handling.
      console.error(`The error code is ${err.code}, error data is ${err.data}`);
      // ...
      // Close the session.
      session.close();
    })