@dnsquery/dns-packet

中文文档
6.1.1发布于 2022/12/03MITTypeScriptESM + CJS近 30 天下载 73.4万

用于编码/解码 DNS 包的抽象编码兼容模块

中文使用文档
文档目录6 节
  1. UDP 用户
  2. TCP、 TLS、 HTTPS
  3. API 密码
  4. 软件包
  5. 支持的记录类型
  6. 许可证

@dnsquery/dns-packet (英语)

Tests

一个 抽象编码 dNS 包编码/解码的兼容模块。 升职了 多播广播 作为单独的模块.

这是叉子 联 合 国 dns- 包装 带有打字符类型、 javascript 模块和 不使用 Node.js 原始语。 在浏览器/反应-内生性方面效果良好.

npm install @dnsquery/dns-packet

UDP 用户

import * as dnsPacket from '@dnsquery/dns-packet'
import dgram from 'dgram'

const socket = dgram.createSocket('udp4')

const buf = dnsPacket.encode({
  type: 'query',
  id: 1,
  flags: dnsPacket.RECURSION_DESIRED,
  questions: [{
    type: 'A',
    name: 'google.com'
  }]
})

socket.on('message', message => {
  console.log(dnsPacket.decode(message)) // prints out a response from google dns
})

socket.send(buf, 0, buf.length, 53, '8.8.8.8')

又见 uDP 实例。 。 。 .

TCP、 TLS、 HTTPS

虽然DNS传统上用于数据克传输,但越来越多地通过TCP传输,用于更大的响应,通常包括DNSSEC响应和加强安全的TLS或HTTPS. 见下文关于如何使用的实例 dns-packet 将 DNS 包包裹在这些协议中 :

API 密码

var buf = packets.encode(packet, [buf], [offset])

将一个DNS包编码成包含UDP有效载荷的缓冲器.

var packet = packets.decode(buf, [offset])

从包含UDP有效载荷的缓冲器中解码出一个DNS包.

var buf = packets.streamEncode(packet, [buf], [offset])

将一个DNS包编码成包含TCP有效载荷的缓冲器.

var packet = packets.streamDecode(buf, [offset])

从含有TCP有效载荷的缓冲器解码出一个DNS包.

var len = packets.encodingLength(packet)

返回编码 DNS 包需要多少字节

软件包

包裹是这样的

{
  type: 'query|response',
  id: optionalIdNumber,
  flags: optionalBitFlags,
  questions: [...],
  answers: [...],
  additionals: [...],
  authorities: [...]
}

可用的比特旗是

import {
  RECURSION_DESIRED,
  RECURSION_AVAILABLE,
  TRUNCATED_RESPONSE,
  AUTHORITATIVE_ANSWER,
  AUTHENTIC_DATA,
  CHECKING_DISABLED
} from '@dnsquery/dns-packet'

要用一面多的旗, 或者用它们一起

var flags = packet.RECURSION_DESIRED | packet.RECURSION_AVAILABLE

并检查旗帜是否使用位元

var isRecursive = message.flags & packet.RECURSION_DESIRED

问题是这样的

{
  type: 'A', // or SRV, AAAA, etc
  class: 'IN', // one of IN, CS, CH, HS, ANY. Default: IN
  name: 'google.com' // which record are you looking for
}

而一个答案,额外的,或权威 看起来像这样

{
  type: 'A', // or SRV, AAAA, etc
  class: 'IN', // one of IN, CS, CH, HS
  name: 'google.com', // which name is this record for
  ttl: optionalTimeToLiveInSeconds,
  (record specific data, see below)
}

支持的记录类型

A

{
  data: 'IPv4 address' // fx 127.0.0.1
}

AAAA

{
  data: 'IPv6 address' // fx fe80::1
}

CAA

{
  flags: 128, // octet
  tag: 'issue|issuewild|iodef',
  value: 'ca.example.net',
  issuerCritical: false
}

CNAME

{
  data: 'cname.to.another.record'
}

DNAME

{
  data: 'dname.to.another.record'
}

DNSKEY

{
  flags: 257, // 16 bits
  algorithm: 1, // octet
  key: Buffer
}

DS

{
  keyTag: 12345,
  algorithm: 8,
  digestType: 1,
  digest: Buffer
}

HINFO

{
  data: {
    cpu: 'cpu info',
    os: 'os info'
  }
}

MX

{
  preference: 10,
  exchange: 'mail.example.net'
}

NS

{
  data: nameServer
}

NSEC

{
  nextDomain: 'a.domain',
  rrtypes: ['A', 'TXT', 'RRSIG']
}

NSEC3

{
  algorithm: 1,
  flags: 0,
  iterations: 2,
  salt: Buffer,
  nextDomain: Buffer, // Hashed per RFC5155
  rrtypes: ['A', 'TXT', 'RRSIG']
}

NULL

{
  data: Buffer('any binary data')
}

OPT

EDNS0 数据 选项.

{
  type: 'OPT',
  name: '.',
  udpPayloadSize: 4096,
  flags: packet.DNSSEC_OK,
  options: [{
    // pass in any code/data for generic EDNS0 options
    code: 12,
    data: Buffer.alloc(31)
  }, {
    // Several EDNS0 options have enhanced support
    code: 'PADDING',
    length: 31,
  }, {
    code: 'CLIENT_SUBNET',
    family: 2, // 1 for IPv4, 2 for IPv6
    sourcePrefixLength: 64, // used to truncate IP address
    scopePrefixLength: 0,
    ip: 'fe80::',
  }, {
    code: 'TCP_KEEPALIVE',
    timeout: 150 // increments of 100ms.  This means 15s.
  }, {
    code: 'KEY_TAG',
    tags: [1, 2, 3],
  }]
}

二. 备选方案 PADDING, (中文(简体) ), CLIENT_SUBNET, (中文(简体) ), TCP_KEEPALIVE 财务报告和审定财务报表 KEY_TAG 加强编码。 见 选项代码.js 用于所有支持的选项代码。 如果 data 属性存在于选项中,优先。 在解码时, data 将永远被定义.

PTR

{
  data: 'points.to.another.record'
}

RP

{
  mbox: 'admin.example.com',
  txt: 'txt.example.com'
}

SSHFP

{
  algorithm: 1,
  hash: 1,
  fingerprint: 'A108C9F834354D5B37AF988141C9294822F5BC00'
}

RRSIG

{
  typeCovered: 'A',
  algorithm: 8,
  labels: 1,
  originalTTL: 3600,
  expiration: timestamp,
  inception: timestamp,
  keyTag: 12345,
  signersName: 'a.name',
  signature: Buffer
}

SOA

{
  data:
    {
      mname: domainName,
      rname: mailbox,
      serial: zoneSerial,
      refresh: refreshInterval,
      retry: retryInterval,
      expire: expireInterval,
      minimum: minimumTTL
    }
}

SRV

{
  data: {
    port: servicePort,
    target: serviceHostName,
    priority: optionalServicePriority,
    weight: optionalServiceWeight
  }
}

TXT

{
  data: 'text' || Buffer || [ Buffer || 'text' ]
}

在编码时将scalar值转换为数组并被字符串转换为UTF-8编码的缓冲. 解码时,返回值将永远是缓冲数组.

如果你需要另一个唱片类型,打开一个问题 我们将尝试添加它.

许可证

麻省理工学院

版本与发布记录

@dnsquery/dns-packet 共发布 2 个版本,首次发布于 2022/12/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.1.1latest2022/12/033 年前
6.1.02022/12/033 年前

关于 @dnsquery/dns-packet 的常见问题

@dnsquery/dns-packet 是什么?

用于编码/解码 DNS 包的抽象编码兼容模块

如何安装 @dnsquery/dns-packet?

在项目目录运行 npm install @dnsquery/dns-packet,或使用 pnpm add / yarn add / bun add。

@dnsquery/dns-packet 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dnsquery/dns-packet 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dnsquery/dns-packet 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dnsquery/dns-packet 可以商用吗?

@dnsquery/dns-packet 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dnsquery/dns-packet 的下载量有多少?

根据 npm 官方下载统计,@dnsquery/dns-packet 在 2026/10/13 – 2026/11/11 这 30 天内被下载 734,067 次,日均约 24,469 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dnsquery/dns-packet 还在维护吗?

最新版本 6.1.1 发布于 2022/12/03(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/12/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。