All Products
Search
Document Center

HTTPDNS:Write user-defined resolution functions

Last Updated:Sep 20, 2022

Introduction to the HTTPDNS resolution mechanism

The HTTPDNS resolution process consists of several stages. Software-Defined Name System (SDNS) allows you to call resolution functions that you create in Function Compute among these stages. This way, additional stages are included in the process, and the resolution process of HTTPDNS is changed to implement capabilities based on your business requirements.

Resolution mechanism

The following table describes the capabilities that the user-defined resolution functions must deliver by additional stage.

Additional stage

Capability

BEFORE_READ_CACHE

1. The function replaces the input domain name with the domain name that is actually resolved.

2. The function replaces the default cache key based on the context.

BEFORE_WRITE_CACHE

1. The function modifies the result of the recursive resolution before the result is cached.

2. The function replaces the default cache key based on the context.

BEFORE_WRITE_RESPONSE

Before the final DNS response is returned, the function modifies the response for the last time. The function is called in this stage regardless of whether the cache is hit.

Input and output of SDNS functions

HTTPDNS passes the runtime context to a Function Compute function that is defined based on your business requirements. Then, the function can process the runtime context and pass the processing result to the next stage in the HTTPDNS resolution process.

Format of input parameters

HTTPDNS passes the runtime context as input parameters to a function in Function Compute. The runtime context is an event of the function and is in the JSON format. The following table describes the fields.

Field

Description

domainName

The domain name that you want to resolve.

clientIp

The IP address of the client.

location{}

The information about the region and Internet service provider (ISP) that correspond to the IP address of the client.

hookType

The resolution stage of the current function. For information about the stage names, see the table in the "Introduction to the HTTPDNS resolution mechanism" section.

ips[]

The IP addresses to which the domain name is resolved. This field is valid in the BEFORE_WRITE_CACHE and BEFORE_WRITE_RESPONSE stages.

ttl

The time-to-live (TTL) value of the resolution result. This field is valid in the BEFORE_WRITE_CACHE and BEFORE_WRITE_RESPONSE stages.

Example of input parameters

{
  "domainName": "www.aliyun.com",
  "clientIp": "192.168.1.4",
  "location": {
    "continent": "asia",
    "country": "china",
    "isp": "bgp",  
    "city": "zhejiang"
  },
  "ips": ["192.168.1.3"],
  "ttl": 60,
  "hookType": "BEFORE_WRITE_CACHE"
}

Format of output parameters

The functions in Function Compute return the processed runtime context to HTTPDNS in the following format to facilitate resolution in HTTPDNS. All fields are optional. If a field is not returned or null is returned for a field, HTTPDNS considers that SDNS does not need to modify the field.

Field

Description

ips[]

The IP addresses that were resolved.

ttl

The TTL value of the resolution result. Unit: seconds. The value of this field must be greater than 30 and less than 3600.

extra

The extended information that is returned to the client. The value of this field can be up to 1,024 characters in length.

domainName

The domain name that is actually resolved. This field is valid only in the BEFORE_READ_CACHE stage.

cacheKey

The default cache key. The value of this field can contain lowercase letters, digits, and hyphens, and must be 1 to 32 characters in length. This field is valid in the BEFORE_READ_CACHE and BEFORE_WRITE_CACHE stages.

hookResult

Indicates whether to immediately return the result after the hook function is run. Valid values: 0 and 1. The value 1 indicates that the result is immediately returned and the next stage is not required. The value 0 indicates that the result is not immediately returned and the next stage is required.

Example of output parameters

{
    "ips": event.ips.concat(['192.168.1.2']),
    "ttl": event.ttl * 2,
    "extra": "some-thing-send-to-user"
    // ,"domainName": "www.alibabacloud.com" This field is valid only in the BEFORE_READ_CACHE stage.
    // ,"cacheKey": "cache-key-001" This field is valid only in the BEFORE_READ_CACHE and BEFORE_WRITE_CACHE stages.
  }

Capabilities of SDNS

SDNS allows you to call resolution functions that you create in Function Compute at specified stages of the HTTPDNS resolution process. SDNS also provides the following capabilities:

  1. Obtain the information about the region and ISP that correspond to the IP address of the client.

  2. Modify the DNS resolution result and the TTL value.

  3. Add custom output and return the custom output together with the resolution result.

Function demo

The programming language of the demo is Node.js 6 or Node.js 8.

'use strict';

exports.handler = function (event, context, callback) {
  // Parse the input parameter.
  event = JSON.parse(event.toString());

  // The first parameter of the callback function is null, and the second parameter is the output parameters. The output parameters are automatically converted into a JSON string.
  callback(null, {
    ips: event.ips.concat(['192.168.1.1']),
    ttl: event.ttl * 2,
    extra: "some-thing-send-to-user"
  });
};

Guidelines for the use of user-defined resolution functions in Function Compute

Notice

  • Do not configure an HTTP trigger for a user-defined resolution function. If you configure an HTTP trigger for a user-defined resolution function, HTTPDNS cannot reference the function.

  • Do not configure a trigger for a user-defined resolution function because the function is dedicated to HTTPDNS.

  • If an error occurs when your function is called or the duration for which the function is called exceeds 2 seconds, HTTPDNS returns ips=[].

  • If the field that is returned by the function is invalid, the field is discarded.

  1. Log on to the  Function Compute console.

  2. Create a service.

  3. Create a function.

  4. Write the function code.

  5. Publish a service.

  6. Log on to the EMAS console to configure SDNS.

Perform a test in the Function Compute console

To test a function, perform the following steps:

  1. Select the function that you want to test and go to the Code tab.

  2. Choose Test Function > Configure Test Parameters. In the Configure Test Parameters dialog box, configure the Event Name parameter, paste the JSON file that is provided in the Example of input parameters section, and then click OK.

  3. Click Test Function to check whether the output parameters of the function meet your business requirements.