-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.js
114 lines (101 loc) · 3.45 KB
/
dns.js
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 express = require('express');
const dns2 = require("dns2");
const { Packet } = require("dns2");
const bodyParser = require('body-parser');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
// In-memory store for registered subdomains
const registeredSubdomains = {};
// Middleware to parse JSON bodies
app.use(bodyParser.json());
// Endpoint to handle registration
app.post('/register', (req, res) => {
const token = req.headers['authorization'];
console.log('Received registration request:', req.body);
// Verify Bearer token
console.log('Verifying token:', token);
if (!token || token !== `Bearer ${process.env.BEARER_TOKEN}`) {
console.error('Unauthorized request, invalid token');
return res.status(403).json({ error: 'Unauthorized' });
}
const { name, host } = req.body;
// Validate input
if (!name || !host) {
console.error('Invalid input, missing name or host:', { name, host });
return res.status(400).json({ error: 'Missing name or host in the request body.' });
}
// Save the registration in memory
registeredSubdomains[name.toLowerCase()] = host;
console.log(`Registered subdomain: ${name} with host: ${host}`);
return res.status(200).json({ message: `Successfully registered ${name}` });
});
// Create DNS server
const dnsServer = dns2.createServer({
udp: true,
tcp: true,
doh: {
ssl: false,
},
handle: (request, send) => {
const response = Packet.createResponseFromRequest(request);
const [question] = request.questions;
const name = question.name.toLowerCase();
const parts = name.split('.');
parts.pop();
parts.pop();
const subdomain = parts.join('.');
if (parts.length === 0) {
console.log('Detected a wildcard subdomain query for:', subdomain);
response.answers.push({
type: Packet.TYPE.A,
name: name,
address: process.env.HOSTIP,
class: Packet.CLASS.IN,
ttl: 3600,
});
}
// Check if subdomain ends with 'dns'
if (subdomain.endsWith('dns')) {
console.log('Subdomain ends with "dns", adding entry:', subdomain);
response.answers.push({
type: Packet.TYPE.A,
name: name,
address: process.env.HOSTIP,
class: Packet.CLASS.IN,
ttl: 3600,
});
}
// Check for registered exact match
const filtered = Object.entries(registeredSubdomains).filter(a=>subdomain.endsWith(a[0]));
console.log({registeredSubdomains})
if(filtered.length) {
const host = filtered[0][1];
response.answers.push({
type: Packet.TYPE.A,
name: name,
address: host,
class: Packet.CLASS.IN,
ttl: 3600,
});
}
console.log(response)
// Send response
send(response);
console.log('DNS response sent for:', name);
},
});
// Start the DNS server
dnsServer.on("close", () => {
console.log("DNS server closed");
});
dnsServer.listen({
udp: 53,
tcp: 53,
}, () => {
console.log('DNS server listening on 53');
});
// Start the webhook server
app.listen(PORT, () => {
console.log(`Webhook registration server running on http://localhost:${PORT}`);
});