All Products
Search
Document Center

HTTPDNS:Write custom resolution function code

Last Updated:Dec 05, 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) lets you 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.

Stage Name

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 as needed.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 to the event input parameter object of an FC function. The context is passed as a JSON object with a fixed structure.

The fields are described as follows:

Field

Subfield

Description

domainName

The domain name that you want to resolve.

clientIp

The public egress IP address of the client. Hereinafter referred to as the client IP.

location

continent

The continent where the client IP is located:

  • africa: Africa

  • asia: Asia

  • north_america: North America

  • south_america: South America

  • europe: Europe

  • oceania: Oceania

  • antarctica: Antarctica

region

The country or region code (ISO 3166-1 alpha-2) for the client IP. The code is in lowercase. For more information, see Country or region codes.

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 function runs in the following phases:

  • BEFORE_READ_CACHE

  • BEFORE_WRITE_CACHE

  • BEFORE_WRITE_RESPONSE

ips[]

The list of IP addresses from the parsing result is available only 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 is being resolved.
  "clientIp": "192.168.1.4", // The client IP.
  "location": {
    "continent": "asia", // The continent where the client IP is located.
    "region": "cn", // The country or region where the client IP is located.
    "isp": "bgp", // The carrier line for the client IP.
    "province": "zhejiang" // The province where the client IP is located.
  },
  "ips": ["192.168.1.3"], // A list of IP addresses from the resolution result. This field is not available in the BEFORE_READ_CACHE stage.
  "ttl": 60, // The TTL of the resolution result. This field is not available in the BEFORE_READ_CACHE stage.
  "hookType": "BEFORE_WRITE_CACHE", // The execution stage of the function.
  "parameters":{ // The parameter object carried in the request.
    "param1":"p1",   // Corresponds to the "sdns-param1=p1" parameter in the URL.
    "param2":"p2"
   }
}

Format of output parameters

An FC function must return the processed result to HTTPDNS in the following format. All fields are optional. If a field is not returned or its value is null, HTTPDNS assumes that the SDNS function does not need to modify that 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

Replaces the domain name to be 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.

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: Continue execution (default behavior).

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

Only

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

This example uses the nodejs6 or nodejs8 runtime.

'use strict';
exports.handler = (event, context, callback) => {
  // Format the input parameters into an object.
  const eventObj = JSON.parse(event.toString());
  const {
    location, // Region
    ips, // The resolution result returned by the authoritative DNS server.
    ttl, // The original TTL.
  } = eventObj;

  if (location.province === 'zhejiang' && location.isp === 'chinanet') {
    // When a China Telecom user in Zhejiang accesses the domain name, return the specified IP address.
    callback(null, {
      ips: ips.concat(['1.1.X.X']), // Add the specified IP address to the returned IP address array.
      ttl: event.ttl * 2, // Modify the TTL.
      extra: "", // Additional parameters to be sent to the client.
    });
  } else {
    // Default return.
    callback(null, {
      ips,
      ttl,
      extra: "",
    });
  }
};

For more information about how to create a service in Function Compute, see Configure custom DNS resolution using Function Compute.