@push.rocks/smartdns 6.2.0 → 6.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/npmextra.json +7 -4
  2. package/package.json +8 -5
  3. package/readme.md +265 -5
package/npmextra.json CHANGED
@@ -9,14 +9,17 @@
9
9
  "npmPackagename": "@push.rocks/smartdns",
10
10
  "license": "MIT",
11
11
  "keywords": [
12
- "DNS",
13
12
  "TypeScript",
13
+ "DNS",
14
+ "DNS records",
15
+ "DNS resolution",
16
+ "DNS management",
17
+ "DNSSEC",
14
18
  "Node.js",
15
19
  "Google DNS",
16
20
  "Cloudflare",
17
- "DNS records",
18
- "DNS resolution",
19
- "DNSSEC"
21
+ "UDP DNS",
22
+ "HTTPS DNS"
20
23
  ]
21
24
  }
22
25
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@push.rocks/smartdns",
3
- "version": "6.2.0",
3
+ "version": "6.2.1",
4
4
  "private": false,
5
5
  "description": "A TypeScript library for smart DNS methods, supporting various DNS records and providers.",
6
6
  "exports": {
@@ -13,14 +13,17 @@
13
13
  "url": "https://code.foss.global/push.rocks/smartdns.git"
14
14
  },
15
15
  "keywords": [
16
- "DNS",
17
16
  "TypeScript",
17
+ "DNS",
18
+ "DNS records",
19
+ "DNS resolution",
20
+ "DNS management",
21
+ "DNSSEC",
18
22
  "Node.js",
19
23
  "Google DNS",
20
24
  "Cloudflare",
21
- "DNS records",
22
- "DNS resolution",
23
- "DNSSEC"
25
+ "UDP DNS",
26
+ "HTTPS DNS"
24
27
  ],
25
28
  "author": "Lossless GmbH",
26
29
  "license": "MIT",
package/readme.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # @push.rocks/smartdns
2
-
3
- smart dns methods written in TypeScript
2
+ A TypeScript library for smart DNS methods, supporting various DNS records and providers.
4
3
 
5
4
  ## Install
6
5
 
@@ -16,7 +15,7 @@ Or with `yarn`:
16
15
  yarn add @push.rocks/smartdns
17
16
  ```
18
17
 
19
- Make sure you have a TypeScript environment setup to utilize the library effectively.
18
+ Make sure you have a TypeScript environment set up to utilize the library effectively.
20
19
 
21
20
  ## Usage
22
21
 
@@ -39,6 +38,8 @@ Often, the need arises to fetch various DNS records for a domain. `@push.rocks/s
39
38
  To fetch an "A" record for a domain:
40
39
 
41
40
  ```typescript
41
+ import { Smartdns } from '@push.rocks/smartdns';
42
+
42
43
  const dnsManager = new Smartdns({});
43
44
  const aRecords = await dnsManager.getRecordsA('example.com');
44
45
  console.log(aRecords);
@@ -53,6 +54,15 @@ const aaaaRecords = await dnsManager.getRecordsAAAA('example.com');
53
54
  console.log(aaaaRecords);
54
55
  ```
55
56
 
57
+ #### Fetching TXT Records
58
+
59
+ For "TXT" records:
60
+
61
+ ```typescript
62
+ const txtRecords = await dnsManager.getRecordsTxt('example.com');
63
+ console.log(txtRecords);
64
+ ```
65
+
56
66
  ### Advanced DNS Management
57
67
 
58
68
  Beyond simple queries, `@push.rocks/smartdns` offers functionalities suitable for more complex DNS management scenarios.
@@ -94,6 +104,258 @@ if (featureFlags['NewFeature']) {
94
104
  }
95
105
  ```
96
106
 
107
+ ### DNS Server Implementation
108
+
109
+ To implement a DNS server, `@push.rocks/smartdns` includes classes and methods to set up a UDP and HTTPS DNS server supporting DNSSEC.
110
+
111
+ #### Basic DNS Server Example
112
+
113
+ Here's a basic example of a UDP/HTTPS DNS server:
114
+
115
+ ```typescript
116
+ import { DnsServer } from '@push.rocks/smartdns';
117
+
118
+ const dnsServer = new DnsServer({
119
+ httpsKey: 'path/to/key.pem',
120
+ httpsCert: 'path/to/cert.pem',
121
+ httpsPort: 443,
122
+ udpPort: 53,
123
+ dnssecZone: 'example.com',
124
+ });
125
+
126
+ dnsServer.registerHandler('*.example.com', ['A'], (question) => ({
127
+ name: question.name,
128
+ type: 'A',
129
+ class: 'IN',
130
+ ttl: 300,
131
+ data: '127.0.0.1',
132
+ }));
133
+
134
+ dnsServer.start().then(() => console.log('DNS Server started'));
135
+ ```
136
+
137
+ ### DNSSEC Support
138
+
139
+ `@push.rocks/smartdns` provides support for DNSSEC, including the generation, signing, and validation of DNS records.
140
+
141
+ #### DNSSEC Configuration
142
+
143
+ To configure DNSSEC for your DNS server:
144
+
145
+ ```typescript
146
+ import { DnsServer } from '@push.rocks/smartdns';
147
+
148
+ const dnsServer = new DnsServer({
149
+ httpsKey: 'path/to/key.pem',
150
+ httpsCert: 'path/to/cert.pem',
151
+ httpsPort: 443,
152
+ udpPort: 53,
153
+ dnssecZone: 'example.com',
154
+ });
155
+
156
+ dnsServer.registerHandler('*.example.com', ['A'], (question) => ({
157
+ name: question.name,
158
+ type: 'A',
159
+ class: 'IN',
160
+ ttl: 300,
161
+ data: '127.0.0.1',
162
+ }));
163
+
164
+ dnsServer.start().then(() => console.log('DNS Server with DNSSEC started'));
165
+ ```
166
+
167
+ This setup ensures that DNS records are signed and can be verified for authenticity.
168
+
169
+ ### Handling DNS Queries Over Different Protocols
170
+
171
+ The library supports handling DNS queries over UDP and HTTPS.
172
+
173
+ #### Handling UDP Queries
174
+
175
+ UDP is the traditional means of DNS query transport.
176
+
177
+ ```typescript
178
+ import { DnsServer } from '@push.rocks/smartdns';
179
+ import dgram from 'dgram';
180
+
181
+ dnsServer.registerHandler('*.example.com', ['A'], (question) => ({
182
+ name: question.name,
183
+ type: 'A',
184
+ class: 'IN',
185
+ ttl: 300,
186
+ data: '127.0.0.1',
187
+ }));
188
+
189
+ dnsServer.start().then(() => {
190
+ console.log('UDP DNS Server started on port', dnsServer.getOptions().udpPort);
191
+ });
192
+
193
+ const client = dgram.createSocket('udp4');
194
+
195
+ client.on('message', (msg, rinfo) => {
196
+ console.log(`Received ${msg} from ${rinfo.address}:${rinfo.port}`);
197
+ });
198
+
199
+ client.send(Buffer.from('example DNS query'), dnsServer.getOptions().udpPort, 'localhost');
200
+ ```
201
+
202
+ #### Handling HTTPS Queries
203
+
204
+ DNS over HTTPS (DoH) is increasingly adopted for privacy and security.
205
+
206
+ ```typescript
207
+ import { DnsServer } from '@push.rocks/smartdns';
208
+ import https from 'https';
209
+ import fs from 'fs';
210
+
211
+ const dnsServer = new DnsServer({
212
+ httpsKey: fs.readFileSync('path/to/key.pem'),
213
+ httpsCert: fs.readFileSync('path/to/cert.pem'),
214
+ httpsPort: 443,
215
+ udpPort: 53,
216
+ dnssecZone: 'example.com',
217
+ });
218
+
219
+ dnsServer.registerHandler('*.example.com', ['A'], (question) => ({
220
+ name: question.name,
221
+ type: 'A',
222
+ class: 'IN',
223
+ ttl: 300,
224
+ data: '127.0.0.1',
225
+ }));
226
+
227
+ dnsServer.start().then(() => console.log('HTTPS DNS Server started'));
228
+
229
+ const client = https.request({
230
+ hostname: 'localhost',
231
+ port: 443,
232
+ path: '/dns-query',
233
+ method: 'POST',
234
+ headers: {
235
+ 'Content-Type': 'application/dns-message'
236
+ }
237
+ }, (res) => {
238
+ res.on('data', (d) => {
239
+ process.stdout.write(d);
240
+ });
241
+ });
242
+
243
+ client.on('error', (e) => {
244
+ console.error(e);
245
+ });
246
+
247
+ client.write(Buffer.from('example DNS query'));
248
+ client.end();
249
+ ```
250
+
251
+ ### Testing
252
+
253
+ To ensure that the DNS server behaves as expected, it is important to write tests for various scenarios.
254
+
255
+ #### DNS Server Tests
256
+
257
+ Here is an example of how to test the DNS server with TAP:
258
+
259
+ ```typescript
260
+ import { expect, tap } from '@push.rocks/tapbundle';
261
+
262
+ import { DnsServer } from '@push.rocks/smartdns';
263
+
264
+ let dnsServer: DnsServer;
265
+
266
+ tap.test('should create an instance of DnsServer', async () => {
267
+ dnsServer = new DnsServer({
268
+ httpsKey: 'path/to/key.pem',
269
+ httpsCert: 'path/to/cert.pem',
270
+ httpsPort: 443,
271
+ udpPort: 53,
272
+ dnssecZone: 'example.com',
273
+ });
274
+ expect(dnsServer).toBeInstanceOf(DnsServer);
275
+ });
276
+
277
+ tap.test('should start the server', async () => {
278
+ await dnsServer.start();
279
+ expect(dnsServer.isRunning()).toBeTrue();
280
+ });
281
+
282
+ tap.test('should add a DNS handler', async () => {
283
+ dnsServer.registerHandler('*.example.com', ['A'], (question) => ({
284
+ name: question.name,
285
+ type: 'A',
286
+ class: 'IN',
287
+ ttl: 300,
288
+ data: '127.0.0.1',
289
+ }));
290
+
291
+ const response = dnsServer.processDnsRequest({
292
+ type: 'query',
293
+ id: 1,
294
+ flags: 0,
295
+ questions: [
296
+ {
297
+ name: 'test.example.com',
298
+ type: 'A',
299
+ class: 'IN',
300
+ },
301
+ ],
302
+ answers: [],
303
+ });
304
+
305
+ expect(response.answers[0]).toEqual({
306
+ name: 'test.example.com',
307
+ type: 'A',
308
+ class: 'IN',
309
+ ttl: 300,
310
+ data: '127.0.0.1',
311
+ });
312
+ });
313
+
314
+ tap.test('should query the server over HTTP', async () => {
315
+ // Assuming fetch or any HTTP client is available
316
+ const query = dnsPacket.encode({
317
+ type: 'query',
318
+ id: 2,
319
+ flags: dnsPacket.RECURSION_DESIRED,
320
+ questions: [
321
+ {
322
+ name: 'test.example.com',
323
+ type: 'A',
324
+ class: 'IN',
325
+ },
326
+ ],
327
+ });
328
+
329
+ const response = await fetch('https://localhost:443/dns-query', {
330
+ method: 'POST',
331
+ body: query,
332
+ headers: {
333
+ 'Content-Type': 'application/dns-message',
334
+ }
335
+ });
336
+
337
+ expect(response.status).toEqual(200);
338
+
339
+ const responseData = await response.arrayBuffer();
340
+ const dnsResponse = dnsPacket.decode(Buffer.from(responseData));
341
+
342
+ expect(dnsResponse.answers[0]).toEqual({
343
+ name: 'test.example.com',
344
+ type: 'A',
345
+ class: 'IN',
346
+ ttl: 300,
347
+ data: '127.0.0.1',
348
+ });
349
+ });
350
+
351
+ tap.test('should stop the server', async () => {
352
+ await dnsServer.stop();
353
+ expect(dnsServer.isRunning()).toBeFalse();
354
+ });
355
+
356
+ await tap.start();
357
+ ```
358
+
97
359
  ### Conclusion
98
360
 
99
361
  `@push.rocks/smartdns` offers a versatile set of tools for DNS querying and management, tailored for applications at any scale. The examples provided illustrate the library's potential use cases, highlighting its applicability in various scenarios from basic lookups to facilitating complex application features through DNS.
@@ -102,8 +364,6 @@ For the full spectrum of functionalities, including detailed method documentatio
102
364
 
103
365
  Remember, DNS changes might take time to propagate worldwide, and the utility methods provided by `@push.rocks/smartdns` for checking record availability will be invaluable in managing these changes seamlessly.
104
366
 
105
-
106
-
107
367
  ## License and Legal Information
108
368
 
109
369
  This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.