-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
129 lines (102 loc) · 3.48 KB
/
index.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
var PORT = process.env.PORT || 53
var EXTERNAL_DNS = process.env.EXTERNAL_DNS || '8.8.8.8,8.8.4.4'
var opts = require('rc')('dnsproxy', {
host: '0.0.0.0',
logging: 'dnsproxy:query',
domains: {
'.docker': '127.0.0.1',
'.rancher': '192.168.99.100'
},
fallback_timeout: 350
})
if (!opts.port) opts.port = PORT
if (!opts.host) opts.host = '0.0.0.0'
if (!opts.nameservers) opts.nameservers = EXTERNAL_DNS.split(',')
if (!opts.servers) opts.servers = {}
if (!opts.domains) opts.domains = {}
if (!opts.hosts) opts.hosts = {}
process.env.DEBUG_FD = process.env.DEBUG_FD || 1
process.env.DEBUG = process.env.DEBUG || opts.logging
var d = process.env.DEBUG.split(',')
d.push('dnsproxy:error')
process.env.DEBUG = d.join(',')
var dgram = require('dgram')
var packet = require('native-dns-packet')
var util = require('./util.js')
var logdebug = require('debug')('dnsproxy:debug')
var logquery = require('debug')('dnsproxy:query')
var logerror = require('debug')('dnsproxy:error')
logdebug('options: %j', opts)
var server = dgram.createSocket('udp4')
server.on('listening', () => {
var address = server.address();
console.log(`dns server listening ${address.address}:${address.port}`);
});
server.on('error', function (err) {
logerror('Server Error: %s', err)
})
server.on('message', function (message, rinfo) {
var nameserver = opts.nameservers[0]
var returner = false
var query = packet.parse(message)
var domain = query.question[0].name
var type = query.question[0].type
logdebug('query: %j', query)
Object.keys(opts.hosts).forEach(function (h) {
if (domain === h) {
var answer = opts.hosts[h]
if (typeof opts.hosts[opts.hosts[h]] !== 'undefined') {
answer = opts.hosts[opts.hosts[h]]
}
logquery('type: host, domain: %s, answer: %s', domain, opts.hosts[h])
var res = util.createAnswer(query, answer)
server.send(res, 0, res.length, rinfo.port, rinfo.address)
returner = true
}
})
if (returner) {
return
}
Object.keys(opts.domains).forEach(function (s) {
var sLen = s.length
var dLen = domain.length
if (domain.indexOf(s) >= 0 && domain.indexOf(s) === (dLen - sLen)) {
var answer = opts.domains[s]
if (typeof opts.domains[opts.domains[s]] !== 'undefined') {
answer = opts.domains[opts.domains[s]]
}
logquery('type: server, domain: %s, answer: %s', domain, opts.domains[s])
var res = util.createAnswer(query, answer)
server.send(res, 0, res.length, rinfo.port, rinfo.address)
returner = true
}
})
if (returner) {
return
}
Object.keys(opts.servers).forEach(function (s) {
if (domain.indexOf(s) !== -1) {
nameserver = opts.servers[s]
}
})
var fallback
(function queryns (message, nameserver) {
var sock = dgram.createSocket('udp4')
sock.send(message, 0, message.length, 53, nameserver, function () {
fallback = setTimeout(function () {
queryns(message, opts.nameservers[0])
}, opts.fallback_timeout)
})
sock.on('error', function (err) {
logerror('Socket Error: %s', err)
process.exit(5)
})
sock.on('message', function (response) {
clearTimeout(fallback)
logquery('type: primary, nameserver: %s, query: %s, type: %s, answer: %s', nameserver, domain, util.records[type] || 'unknown', util.listAnswer(response))
server.send(response, 0, response.length, rinfo.port, rinfo.address)
sock.close()
})
}(message, nameserver))
})
server.bind(opts.port, opts.host)