-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsdns.js
74 lines (68 loc) · 2.43 KB
/
tsdns.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
"use strict";
var net = require('net');
var sqlite3 = require('sqlite3').verbose();
const url = require('url');
var db = new sqlite3.Database('./tsdns.sqlite');
var sockets = [];
var tsdns = net.createServer(function (socket) {
sockets.push(socket);
var writeEnd = function(message) {
socket.write(message, function() {
socket.end();
});
};
var freeTimeout = setTimeout(function() {
writeEnd('404');
}, 60000);
socket.on('data', function(data) {
var domain = data.toString().replace(/\r|\n/g, '');
var ds = [];
ds = domain.split('.');
ds.unshift(domain);
var success = false;
for(var i=0,len=ds.length; i<len;i++){
if(i == 0){
db.all("SELECT * FROM zones WHERE zone=?",ds[0], function(err, rows){
if( err ){
console.log(err);
}else{
if( rows.length ){
return writeEnd(rows[0].target)
}
}
});
ds.shift();
}
else if(success){
return writeEnd('404')
}
else{
var i_d = ds;
i_d[i - 1] = i_d[i - 1].replace(i_d[i - 1], "*");
domain = i_d.join(".");
db.all("SELECT * FROM zones WHERE zone=?",domain, function(err, rows){
if( err ){
console.log(err);
}else{
if( rows[0] != undefined){
success = true;
return writeEnd(rows[0].target);
}
}
});
}
}
});
socket.on('close', function() {
for (var i in sockets) {
if (sockets[i] === socket) {
sockets.splice(i, 1);
}
}
})
socket.on('error', function(error) {});
});
tsdns.on('close', function() {
db.close();
})
module.exports = tsdns;