All Products
Search
Document Center

HTTPDNS:Write user-defined resolution functions

Last Updated:Feb 14, 2025

This topic describes the input and output parameters of a user-defined resolution function and how to write a user-defined resolution function.

Resolution mechanism of compute policies for a user-defined resolution function

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

运行机制

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

Additional stage

Description

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. The precessed results are merged back into 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 object of the function and is in the JSON format.

The following table describes the fields.

Field

Subfield

Description

domainName

The domain name that you want to resolve.

clientIp

The IP address of the client.

location

continent

The continent to which the IP address of the client belongs.

country

The country to which the IP address of the client belongs. This field is valid only on the IP address of the client in the Chinese mainland.

isp

The Internet service provider (ISP) to which the IP address of the client belongs. This field is valid only on the IP address of the client in the Chinese mainland. Valid values:

cmcc: China Mobile.

unicom: China Unicom.

chinanet: China Telecom.

bgp: BGP line.

unknown: unknown line.

province

The province to which the IP address of the client belongs. This field is valid only on the IP address of the client in the Chinese mainland.

hookType

The resolution stage of the current function. Valid values:

BEFORE_READ_CACHE

BEFORE_WRITE_CACHE

BEFORE_WRITE_RESPONSE

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.

parameters{}

The user-defined SDNS parameter object.

queryType

The resolution type. The value 1 indicates an A record, which is an IPv4 address. The value 28 indicates an AAAA record, which is an IPv6 address.

Example of input parameters

{
  "domainName": "www.aliyun.com", // The domain name that you want to resolve.
  "clientIp": "192.168.1.4", // The IP address of the client.
  "location": {
    "continent": "asia", // The continent to which the IP address of the client belongs.
    "country": "china", // The country to which the IP address of the client belongs.
    "isp": "bgp", // The ISP to which the IP address of the client belongs. 
    "province": "zhejiang" // The province to which the IP address of the client belongs.
  },
  "ips": ["192.168.1.3"], // The IP addresses to which the domain name is resolved. This field is invalid in the BEFORE_READ_CACHE stage.
  "ttl": 60, // The TTL value of the resolution result. This field is invalid in the BEFORE_READ_CACHE stage.
  "hookType": "BEFORE_WRITE_CACHE", // The resolution stage of the function.
  "parameters":{ // The parameter objects during the resolution.
    "param1":"p1", // The sdns-param1=p1 parameter in the URL.
    "param2":"p2"
   }
}

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 to which the domain name is 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: The result is not immediately returned and the next stage is required. This is the default value.

  • 1: The result is immediately returned and the next stage is not required.

This field is valid in the BEFORE_READ_CACHE and BEFORE_WRITE_CACHE stages.

Sample output parameter

{
    "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.
 }

Function capabilities

You can call resolution functions that you create in Function Compute at specified stages of the HTTPDNS resolution process. You can also use 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 = (event, context, callback) => {
  // Format the input parameters as objects.
  const eventObj = JSON.parse(event.toString());
  const {
    location, // The region.
    ips, // The resolution result returned.
    ttl, // The original TTL.
  } = eventObj;

  if (location.province === 'zhejiang' && location.isp === 'chinanet') {
    // When a user of China Telecom in Zhejiang province accesses the domain name, the specified IP address is returned.
    callback(null, {
      ips: ips.concat(['1.1.1.1']), // Add the specified IP address to the returned array of IP addresses.
      ttl: event.ttl * 2, // Modify the TTL.
      extra: "", // Additional parameters carried to the client.
    });
  } else {
    // Returned by default.
    callback(null, {
      ips,
      ttl,
      extra: "",
    });
  }
};

For more information about how to create a Function Compute service, see Add a Function Compute policy.