forked from OstlerDev/PopcornTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.js
executable file
·163 lines (146 loc) · 4.3 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var logger = require('./logger');
const DOMAIN_ATV = "trailers.apple.com";
const IP_DNS = "8.8.8.8"; // *********** Googles Default DNS ***********
const ATV_UPDATE = ['mesu.apple.com', 'appldnld.apple.com', 'appldnld.apple.com.edgesuite.net'];
function resolveDNSDomain(msg) {
var domain = [];
var index = 12;
var offset;
while (offset = msg.readUInt8(index++)) {
var sub = "";
for (var i = 0; i < offset; i++) {
try{
sub += String.fromCharCode(msg.readUInt8(index++));
} catch(e) {
logger.error(e);
}
}
sub && domain.push(sub)
}
return domain.join(".");
}
var _domain = "";
var _ip = "";
function resolveDNSIp(msg) {
if (msg.readUInt16LE(2) != 0x8081) return -1;
var domain = [];
var index = 12;
var offset;
while (offset = msg.readUInt8(index++)) {
var sub = "";
for (var i = 0; i < offset; i++) {
sub += String.fromCharCode(msg.readUInt8(index++));
}
sub && domain.push(sub);
}
_domain = domain.join(".");
index += 4;
while (msg.readUInt32BE(index + 2) != 0x00010001) {
index += 10;
var rdLength = msg.readUInt16BE(index);
index += (rdLength + 2);
if (index >= msg.length) return -1;
}
if (msg.readUInt16BE(index + 10) != 4) return -1;
index += 12;
_ip = msg.readUInt8(index) + "."
+msg.readUInt8(index + 1) + "."
+msg.readUInt8(index + 2) + "."
+msg.readUInt8(index + 3);
return index;
}
function dot2num(dot) {
var d = dot.split('.');
return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
}
function getMsg(tag, domain, ip) {
var msg = { size: 0, msg: ""};
var offset = 0;
msg.size = 128;
msg.msg = new Buffer(msg.size);
msg.msg.writeUInt16BE(tag, offset);
offset += 2;
msg.msg.writeUInt16BE(0x8180, offset);
offset += 2;
msg.msg.writeUInt32BE(0x00010001, offset);
offset += 4;
msg.msg.writeUInt32BE(0x00000000, offset);
offset += 4;
//write domain
var d = domain.split('.');
for (var i = 0; i < d.length; i++) {
var length = d[i].length;
msg.msg.writeUInt8(length, offset++);
msg.msg.write(d[i], offset, length);
offset += length;
}
msg.msg.writeUInt8(0, offset++);
msg.msg.writeUInt32BE(0x00010001, offset);
offset += 4;
msg.msg.writeUInt16BE(0xC00C, offset);
offset += 2;
msg.msg.writeUInt32BE(0x00010001, offset);
offset += 4;
msg.msg.writeUInt32BE(0x00000C82, offset);
offset += 4;
msg.msg.writeUInt16BE(0x0004, offset);
offset += 2;
msg.msg.writeUInt32BE(ip, offset);
offset += 4;
msg.size = offset;
return msg;
}
var dns;
function startDnsProxy(localIp) {
var dgram = require("dgram");
dns = dgram.createSocket("udp4", function(msg, rinfo) {
var domain = resolveDNSDomain(msg);
var server = this;
var address = rinfo.address;
var port = rinfo.port;
if (domain == DOMAIN_ATV) {
var tag = msg.readUInt16BE(0);
var ip = dot2num(localIp);
var newMsg = getMsg(tag, domain, ip);
logger.Debug(DOMAIN_ATV + " change to " + localIp);
server.send(newMsg.msg, 0, newMsg.size, port, address);
return;
} else if(domain == ATV_UPDATE[0] || domain == ATV_UPDATE[1] || domain == ATV_UPDATE[2]){
var tag = msg.readUInt16BE(0);
var ip = dot2num('127.0.0.1');
var newMsg = getMsg(tag, domain, ip);
logger.DNS('Blocking ATV Update Attempt');
logger.Debug(DOMAIN_ATV + " change to " + '127.0.0.1');
server.send(newMsg.msg, 0, newMsg.size, port, address);
return;
} else {
logger.DNS(domain);
}
dgram.createSocket("udp4", function(msg, rinfo) {
server.send(msg, 0, rinfo.size, port, address);
this.close();
}).send(msg, 0, rinfo.size, 53, IP_DNS);
});
dns.bind(53, localIp);
dns.on('error', function(err){
if (err.code == 'EADDRINUSE'){
logger.error('========= FATAL ERROR =========');
logger.error('Cannot bind to Port 53. Please make sure you are not running a DNS server on your machine!');
logger.error('Error Code: ' + err.code);
logger.error('===============================');
process.exit();
}
logger.error('========= FATAL ERROR =========');
logger.error('Cannot start DNS Server. Please make sure you are using the correct IP!');
logger.error('Error Code: ' + err.code);
logger.error('===============================');
process.exit();
})
logger.DNS("DnsProxy binding on " + localIp + ":53");
}
function stop(){
logger.DNS('Stopping DNSProxy');
dns.close();
}
exports.startDnsProxy = startDnsProxy;
exports.stop = stop;