-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_server.js
84 lines (73 loc) · 1.77 KB
/
dns_server.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
/**
* Created by wikeLi on 2017/5/27.
*/
var fs = require('fs');
var path = require('path');
var dns = require('native-dns');
var http = require('http');
var express = require('express');
String.prototype.endsWith = function(s) {
return this.length >= s.length && this.substr(this.length - s.length) == s;
};
var startDns = function(example_port, callback) {
var server = dns.createServer();
server.on('request', function(request, response) {
var found = false;
for (var q = 0; q < request.question.length; q++)
{
var name = request.question[q].name;
if (name.endsWith("game1.tjbtn.net"))
{
response.answer.push(dns.A({
name:name,
address:'192.168.1.101',
port:example_port,
ttl:600
}));
found = true;
}
}
if (found)
{
response.send();
}
});
server.on('error', function(err, buff, req, res) {
console.log(JSON.stringify(err));
});
server.on('listening', function() {
console.log("DNS server started on port 53");
if (callback)
{
callback();
}
});
server.serve(53);
return server;
};
var startHttp = function(serverPort) {
var app = express();
var server = http.createServer(app);
app.get('/', function(req, res, next) {
res.send('Hello from ' + req.headers.host);
});
server.listen(serverPort, 'game1.tn.net');
console.log('https server started on port ' + serverPort);
return server;
};
var server_port = parseInt(process.argv[2] ||80);
var httpsServer;
var dnsServer = startDns(server_port, function() {
httpsServer = startHttp(server_port)
});
process.on('SIGINT', function() {
console.log("shutting down");
if (httpsServer)
{
httpsServer.close();
}
if (dnsServer)
{
dnsServer.close();
}
});