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.
dnsRules (DNS rule configuration)
DnsServers: specifies custom DNS servers to provide the resolution service.
StaticDnsRules: adds static DNS rules when the default DNS cannot resolve some domain names. After static DNS rules are added, if the hostname matches, the specified address is preferentially used.
DynamicDnsRule: adds dynamic DNS rules. DynamicDnsRules is a function that returns an IP address based on the hostname and port number. If this function is used, the IP address returned by the function is used preferentially.
If multiple static or dynamic DNS rules are configured for a single domain name, Remote Communication Kit will cycle through the servers based on the defined rules.
dnsOverHttps
DnsOverHttpsConfiguration: configures DNS over HTTPS (DOH). The encrypted HTTPS protocol is used for DNS resolution requests to prevent the DNS resolution requests of users in the original DNS protocol from being eavesdropped or modified, protecting user privacy. If this parameter is set, DNS-resolved addresses will be used by priority.
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.
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.
Import the required modules.
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit'; 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'); 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.
},
]
}
}; 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();
}) Import the required modules.
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit'; 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);
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 [];
}
}
}; 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();
})