|
| 1 | +var EventEmitter, Server, bitSlice, bufferify, bufferifyV4, bufferifyV6, createSocket, dns, domainify, functionify, isIPv6, lookup, parse, qnameify, resolve, response, responseBuffer, |
| 2 | + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, |
| 3 | + hasProp = {}.hasOwnProperty; |
| 4 | + |
| 5 | +EventEmitter = require`events`.EventEmitter; |
| 6 | + |
| 7 | +createSocket = require('dgram').createSocket; |
| 8 | + |
| 9 | +isIPv6 = require('net').isIPv6; |
| 10 | + |
| 11 | +dns = require('dns'); |
| 12 | + |
| 13 | +bitSlice = function(b, offset, length) { |
| 14 | + return b >>> 7 - (offset + length - 1) & ~(0xff << length); |
| 15 | +}; |
| 16 | + |
| 17 | +bufferify = function(ip) { |
| 18 | + if (isIPv6(ip)) { |
| 19 | + return bufferifyV6(ip); |
| 20 | + } else { |
| 21 | + return bufferifyV4(ip); |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +bufferifyV4 = function(ip) { |
| 26 | + var base, buf, i, result; |
| 27 | + ip = ip.split('.').map(function(n) { |
| 28 | + return parseInt(n, 10); |
| 29 | + }); |
| 30 | + result = 0; |
| 31 | + base = 1; |
| 32 | + i = ip.length - 1; |
| 33 | + while (i >= 0) { |
| 34 | + result += ip[i] * base; |
| 35 | + base *= 256; |
| 36 | + i--; |
| 37 | + } |
| 38 | + buf = Buffer.alloc(4); |
| 39 | + buf.writeUInt32BE(result); |
| 40 | + return buf; |
| 41 | +}; |
| 42 | + |
| 43 | +bufferifyV6 = function(rawIp) { |
| 44 | + var countColons, hexIp, ip; |
| 45 | + countColons = function(x) { |
| 46 | + var n; |
| 47 | + n = 0; |
| 48 | + x.replace(/:/g, function(c) { |
| 49 | + return n++; |
| 50 | + }); |
| 51 | + return n; |
| 52 | + }; |
| 53 | + ip = rawIp.replace(/\/\d{1,3}(?=%|$)/, '').replace(/%.*$/, ''); |
| 54 | + hexIp = ip.replace(/::/, function(two) { |
| 55 | + return ':' + Array(7 - countColons(ip) + 1).join(':') + ':'; |
| 56 | + }).split(':').map(function(x) { |
| 57 | + return Array(4 - x.length).fill('0').join('') + x; |
| 58 | + }).join(''); |
| 59 | + return Buffer.from(hexIp, 'hex'); |
| 60 | +}; |
| 61 | + |
| 62 | +domainify = function(qname) { |
| 63 | + var i, length, offset, parts; |
| 64 | + parts = []; |
| 65 | + i = 0; |
| 66 | + while (i < qname.length && qname[i]) { |
| 67 | + length = qname[i]; |
| 68 | + offset = i + 1; |
| 69 | + parts.push(qname.slice(offset, offset + length).toString()); |
| 70 | + i = offset + length; |
| 71 | + } |
| 72 | + return parts.join('.'); |
| 73 | +}; |
| 74 | + |
| 75 | +qnameify = function(domain) { |
| 76 | + var i, offset, qname; |
| 77 | + qname = Buffer.alloc(domain.length + 2); |
| 78 | + offset = 0; |
| 79 | + domain = domain.split('.'); |
| 80 | + i = 0; |
| 81 | + while (i < domain.length) { |
| 82 | + qname[offset] = domain[i].length; |
| 83 | + qname.write(domain[i], offset + 1, domain[i].length, 'ascii'); |
| 84 | + offset += qname[offset] + 1; |
| 85 | + i++; |
| 86 | + } |
| 87 | + qname[qname.length - 1] = 0; |
| 88 | + return qname; |
| 89 | +}; |
| 90 | + |
| 91 | +functionify = function(val) { |
| 92 | + return function(addr, callback) { |
| 93 | + return callback(null, val); |
| 94 | + }; |
| 95 | +}; |
| 96 | + |
| 97 | +parse = function(buf) { |
| 98 | + var b, header, question; |
| 99 | + header = {}; |
| 100 | + question = {}; |
| 101 | + b = buf.slice(2, 3).toString('binary', 0, 1).charCodeAt(0); |
| 102 | + header.id = buf.slice(0, 2); |
| 103 | + header.qr = bitSlice(b, 0, 1); |
| 104 | + header.opcode = bitSlice(b, 1, 4); |
| 105 | + header.aa = bitSlice(b, 5, 1); |
| 106 | + header.tc = bitSlice(b, 6, 1); |
| 107 | + header.rd = bitSlice(b, 7, 1); |
| 108 | + b = buf.slice(3, 4).toString('binary', 0, 1).charCodeAt(0); |
| 109 | + header.ra = bitSlice(b, 0, 1); |
| 110 | + header.z = bitSlice(b, 1, 3); |
| 111 | + header.rcode = bitSlice(b, 4, 4); |
| 112 | + header.qdcount = buf.slice(4, 6); |
| 113 | + header.ancount = buf.slice(6, 8); |
| 114 | + header.nscount = buf.slice(8, 10); |
| 115 | + header.arcount = buf.slice(10, 12); |
| 116 | + question.qname = buf.slice(12, buf.length - 4); |
| 117 | + question.qtype = buf.slice(buf.length - 4, buf.length - 2); |
| 118 | + question.qclass = buf.slice(buf.length - 2, buf.length); |
| 119 | + return { |
| 120 | + header: header, |
| 121 | + question: question |
| 122 | + }; |
| 123 | +}; |
| 124 | + |
| 125 | +responseBuffer = function(query) { |
| 126 | + var buf, header, i, length, offset, qname, question, rr; |
| 127 | + question = query.question; |
| 128 | + header = query.header; |
| 129 | + qname = question.qname; |
| 130 | + offset = 16 + qname.length; |
| 131 | + length = offset; |
| 132 | + i = 0; |
| 133 | + while (i < query.rr.length) { |
| 134 | + length += query.rr[i].qname.length + 10; |
| 135 | + i++; |
| 136 | + } |
| 137 | + buf = Buffer.alloc(length); |
| 138 | + header.id.copy(buf, 0, 0, 2); |
| 139 | + buf[2] = 0x00 | header.qr << 7 | header.opcode << 3 | header.aa << 2 | header.tc << 1 | header.rd; |
| 140 | + buf[3] = 0x00 | header.ra << 7 | header.z << 4 | header.rcode; |
| 141 | + buf.writeUInt16BE(header.qdcount, 4); |
| 142 | + buf.writeUInt16BE(header.ancount, 6); |
| 143 | + buf.writeUInt16BE(header.nscount, 8); |
| 144 | + buf.writeUInt16BE(header.arcount, 10); |
| 145 | + qname.copy(buf, 12); |
| 146 | + question.qtype.copy(buf, 12 + qname.length, question.qtype, 2); |
| 147 | + question.qclass.copy(buf, 12 + qname.length + 2, question.qclass, 2); |
| 148 | + i = 0; |
| 149 | + while (i < query.rr.length) { |
| 150 | + rr = query.rr[i]; |
| 151 | + rr.qname.copy(buf, offset); |
| 152 | + offset += rr.qname.length; |
| 153 | + buf.writeUInt16BE(rr.qtype, offset); |
| 154 | + buf.writeUInt16BE(rr.qclass, offset + 2); |
| 155 | + buf.writeUInt32BE(rr.ttl, offset + 4); |
| 156 | + buf.writeUInt16BE(rr.rdlength, offset + 8); |
| 157 | + buf = Buffer.concat([buf, rr.rdata]); |
| 158 | + offset += 14; |
| 159 | + i++; |
| 160 | + } |
| 161 | + return buf; |
| 162 | +}; |
| 163 | + |
| 164 | +response = function(query, ttl, to) { |
| 165 | + var header, question, rrs; |
| 166 | + response = {}; |
| 167 | + header = response.header = {}; |
| 168 | + question = response.question = {}; |
| 169 | + rrs = resolve(query.question.qname, ttl, to); |
| 170 | + header.id = query.header.id; |
| 171 | + header.ancount = rrs.length; |
| 172 | + header.qr = 1; |
| 173 | + header.opcode = 0; |
| 174 | + header.aa = 0; |
| 175 | + header.tc = 0; |
| 176 | + header.rd = 1; |
| 177 | + header.ra = 0; |
| 178 | + header.z = 0; |
| 179 | + header.rcode = 0; |
| 180 | + header.qdcount = 1; |
| 181 | + header.nscount = 0; |
| 182 | + header.arcount = 0; |
| 183 | + question.qname = query.question.qname; |
| 184 | + question.qtype = query.question.qtype; |
| 185 | + question.qclass = query.question.qclass; |
| 186 | + response.rr = rrs; |
| 187 | + return responseBuffer(response); |
| 188 | +}; |
| 189 | + |
| 190 | +resolve = function(qname, ttl, to) { |
| 191 | + var r; |
| 192 | + r = {}; |
| 193 | + r.qname = qname; |
| 194 | + r.qtype = to.length === 4 ? 1 : 28; |
| 195 | + r.qclass = 1; |
| 196 | + r.ttl = ttl; |
| 197 | + r.rdlength = to.length; |
| 198 | + r.rdata = to; |
| 199 | + return [r]; |
| 200 | +}; |
| 201 | + |
| 202 | +lookup = function(addr, callback) { |
| 203 | + if (net.isIP(addr)) { |
| 204 | + return callback(null, addr); |
| 205 | + } |
| 206 | + return dns.lookup(addr, callback); |
| 207 | +}; |
| 208 | + |
| 209 | +Server = (function(superClass) { |
| 210 | + extend(Server, superClass); |
| 211 | + |
| 212 | + function Server(proxy) { |
| 213 | + var routes; |
| 214 | + if (proxy == null) { |
| 215 | + proxy = '8.8.8.8'; |
| 216 | + } |
| 217 | + Server.__super__.constructor.apply(this, arguments); |
| 218 | + this._socket = createSocket(isIPv6(proxy) ? 'udp6' : 'udp4'); |
| 219 | + routes = []; |
| 220 | + this._socket.on('message', (function(_this) { |
| 221 | + return function(message, rinfo) { |
| 222 | + var domain, i, onerror, onproxy, query, respond, route, routeData; |
| 223 | + query = parse(message); |
| 224 | + domain = domainify(query.question.qname); |
| 225 | + routeData = { |
| 226 | + domain: domain, |
| 227 | + rinfo: rinfo |
| 228 | + }; |
| 229 | + _this.emit('resolve', routeData); |
| 230 | + respond = function(buf) { |
| 231 | + return _this._socket.send(buf, 0, buf.length, rinfo.port, rinfo.address); |
| 232 | + }; |
| 233 | + onerror = function(err) { |
| 234 | + return _this.emit('error', err); |
| 235 | + }; |
| 236 | + onproxy = function() { |
| 237 | + var sock; |
| 238 | + sock = createSocket(isIPv6(proxy) ? 'udp6' : 'udp4'); |
| 239 | + sock.send(message, 0, message.length, 53, proxy); |
| 240 | + sock.on('error', onerror); |
| 241 | + return sock.on('message', function(response) { |
| 242 | + respond(response); |
| 243 | + return sock.close(); |
| 244 | + }); |
| 245 | + }; |
| 246 | + i = 0; |
| 247 | + while (i < routes.length) { |
| 248 | + if (routes[i].pattern.test(domain)) { |
| 249 | + route = routes[i].route; |
| 250 | + break; |
| 251 | + } |
| 252 | + i++; |
| 253 | + } |
| 254 | + if (!route) { |
| 255 | + return onproxy(); |
| 256 | + } |
| 257 | + return route(routeData, function(err, to) { |
| 258 | + var toIp, ttl; |
| 259 | + if (typeof to === 'string') { |
| 260 | + toIp = to; |
| 261 | + ttl = 1; |
| 262 | + } else { |
| 263 | + toIp = to.ip; |
| 264 | + ttl = to.ttl; |
| 265 | + } |
| 266 | + if (err) { |
| 267 | + return onerror(err); |
| 268 | + } |
| 269 | + if (!toIp) { |
| 270 | + return onproxy(); |
| 271 | + } |
| 272 | + return lookup(toIp, function(err, addr) { |
| 273 | + if (err) { |
| 274 | + return onerror(err); |
| 275 | + } |
| 276 | + _this.emit('route', domain, addr); |
| 277 | + return respond(response(query, ttl, bufferify(addr))); |
| 278 | + }); |
| 279 | + }); |
| 280 | + }; |
| 281 | + })(this)); |
| 282 | + } |
| 283 | + |
| 284 | + Server.prototype.route = function(pattern, route) { |
| 285 | + if (Array.isArray(pattern)) { |
| 286 | + pattern.forEach((function(_this) { |
| 287 | + return function(item) { |
| 288 | + return _this.route(item, route); |
| 289 | + }; |
| 290 | + })(this)); |
| 291 | + return this; |
| 292 | + } |
| 293 | + if (typeof pattern === 'function') { |
| 294 | + return this.route('*', pattern); |
| 295 | + } |
| 296 | + if (typeof route === 'string') { |
| 297 | + return this.route(pattern, functionify(route)); |
| 298 | + } |
| 299 | + pattern = pattern === '*' ? /.?/ : new RegExp('^' + pattern.replace(/\./g, '\\.').replace(/\*\\\./g, '(.+)\\.') + '$', 'i'); |
| 300 | + routes.push({ |
| 301 | + pattern: pattern, |
| 302 | + route: route |
| 303 | + }); |
| 304 | + return this; |
| 305 | + }; |
| 306 | + |
| 307 | + Server.prototype.listen = function(port) { |
| 308 | + this._socket.bind(port || 53); |
| 309 | + return this; |
| 310 | + }; |
| 311 | + |
| 312 | + Server.prototype.close = function(callback) { |
| 313 | + this._socket.close(callback); |
| 314 | + return this; |
| 315 | + }; |
| 316 | + |
| 317 | + return Server; |
| 318 | + |
| 319 | +})(EventEmitter); |
0 commit comments