Repository navigation
Expand file tree
/
Copy pathmock_dns_https.js
More file actions
114 lines (105 loc) · 3.51 KB
/
Copy pathmock_dns_https.js
File metadata and controls
114 lines (105 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const dgram = require("dgram");
const fs = require("fs");
const https = require("https");
const path = require("path");
const mockHost = process.env.APP_MOCK_HOST || "mock.example.com";
const mockAddress = process.env.APP_MOCK_ADDRESS;
const upstreamDns = process.env.APP_UPSTREAM_DNS;
const certificateDirectory = process.env.APP_CERT_DIR || process.cwd();
if (!mockAddress || !upstreamDns) {
console.error("APP_MOCK_ADDRESS and APP_UPSTREAM_DNS are required");
process.exit(1);
}
function readQuestion(message) {
let offset = 12;
const labels = [];
while (offset < message.length) {
const length = message[offset++];
if (length === 0) {
break;
}
labels.push(message.subarray(offset, offset + length).toString("ascii"));
offset += length;
}
return {
name: labels.join(".").toLowerCase(),
type: message.readUInt16BE(offset),
questionEnd: offset + 4,
};
}
function createAResponse(query, questionEnd) {
const response = Buffer.alloc(questionEnd + 16);
query.copy(response, 0, 0, questionEnd);
response.writeUInt16BE(query.readUInt16BE(2) | 0x8080, 2);
response.writeUInt16BE(1, 6);
response.writeUInt16BE(0, 8);
response.writeUInt16BE(0, 10);
let offset = questionEnd;
response.writeUInt16BE(0xc00c, offset);
offset += 2;
response.writeUInt16BE(1, offset);
offset += 2;
response.writeUInt16BE(1, offset);
offset += 2;
response.writeUInt32BE(30, offset);
offset += 4;
response.writeUInt16BE(4, offset);
offset += 2;
mockAddress.split(".").forEach((part, index) => {
response[offset + index] = Number(part);
});
return response;
}
function createEmptyResponse(query, questionEnd) {
const response = Buffer.alloc(questionEnd);
query.copy(response, 0, 0, questionEnd);
response.writeUInt16BE(query.readUInt16BE(2) | 0x8080, 2);
response.writeUInt16BE(0, 6);
response.writeUInt16BE(0, 8);
response.writeUInt16BE(0, 10);
return response;
}
const dnsServer = dgram.createSocket("udp4");
dnsServer.on("message", (message, remote) => {
const question = readQuestion(message);
if (question.name === mockHost) {
const response = question.type === 1
? createAResponse(message, question.questionEnd)
: createEmptyResponse(message, question.questionEnd);
dnsServer.send(response, remote.port, remote.address);
console.log(`DNS ${question.name} type=${question.type} -> `
+ (question.type === 1 ? mockAddress : "NOERROR/NODATA"));
return;
}
const upstream = dgram.createSocket("udp4");
upstream.once("message", (response) => {
dnsServer.send(response, remote.port, remote.address);
upstream.close();
});
upstream.once("error", () => upstream.close());
upstream.send(message, 53, upstreamDns);
});
dnsServer.bind(53, "0.0.0.0", () => {
console.log("DNS server listening on UDP/53");
});
const httpsServer = https.createServer({
key: fs.readFileSync(path.join(certificateDirectory, "server.key")),
cert: fs.readFileSync(path.join(certificateDirectory, "server.pem")),
requestCert: true,
rejectUnauthorized: false,
}, (request, response) => {
console.log(`HTTPS ${request.method} ${request.url} `
+ `UA=${request.headers["user-agent"]}`);
const body = JSON.stringify({
schema_version: 1,
description: "example json"
});
response.writeHead(200, {"Content-Type": "application/json"});
response.end(body);
});
httpsServer.on("tlsClientError", (error) => {
console.log(`TLS_ERROR ${error.message}`);
});
httpsServer.listen(443, "0.0.0.0", () => {
console.log("HTTPS server listening on TCP/443");
});