-
Notifications
You must be signed in to change notification settings - Fork 40
/
dns-proxy.js
executable file
·151 lines (124 loc) · 4.17 KB
/
dns-proxy.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
#!/usr/bin/env node
const fs = require('fs')
const rc = require('rc')
const dgram = require('dgram')
const packet = require('native-dns-packet')
const wildcard = require('wildcard2')
const util = require('./util.js')
const defaults = {
port: 53,
host: '127.0.0.1',
logging: 'dnsproxy:query,dnsproxy:info',
nameservers: [
'1.1.1.1',
'1.0.0.1'
],
servers: {},
domains: {
xdev: '127.0.0.1'
},
hosts: {
devlocal: '127.0.0.1'
},
fallback_timeout: 350,
reload_config: true
}
let config = rc('dnsproxy', defaults)
process.env.DEBUG_FD = process.env.DEBUG_FD || 1
process.env.DEBUG = process.env.DEBUG || config.logging
const d = process.env.DEBUG.split(',')
d.push('dnsproxy:error')
process.env.DEBUG = d.join(',')
const loginfo = require('debug')('dnsproxy:info')
const logdebug = require('debug')('dnsproxy:debug')
const logquery = require('debug')('dnsproxy:query')
const logerror = require('debug')('dnsproxy:error')
if (config.reload_config === true && typeof config.config !== 'undefined') {
var configFile = config.config
fs.watchFile(configFile, function (curr, prev) {
loginfo('config file changed, reloading config options')
try {
config = rc('dnsproxy', defaults)
} catch (e) {
logerror('error reloading configuration')
logerror(e)
}
})
}
logdebug('options: %j', config)
const server = dgram.createSocket('udp4')
server.on('listening', function () {
loginfo('we are up and listening at %s on %s', config.host, config.port)
})
server.on('error', function (err) {
logerror('udp socket error')
logerror(err)
})
server.on('message', function (message, rinfo) {
let returner = false
let nameserver = config.nameservers[0]
const query = packet.parse(message)
const domain = query.question[0].name
const type = query.question[0].type
logdebug('query: %j', query)
Object.keys(config.hosts).forEach(function (h) {
if (domain === h) {
let answer = config.hosts[h]
if (typeof config.hosts[config.hosts[h]] !== 'undefined') {
answer = config.hosts[config.hosts[h]]
}
logquery('type: host, domain: %s, answer: %s, source: %s:%s, size: %d', domain, config.hosts[h], rinfo.address, rinfo.port, rinfo.size)
const res = util.createAnswer(query, answer)
server.send(res, 0, res.length, rinfo.port, rinfo.address)
returner = true
}
})
if (returner) {
return
}
Object.keys(config.domains).forEach(function (s) {
const sLen = s.length
const dLen = domain.length
if ((domain.indexOf(s) >= 0 && domain.indexOf(s) === (dLen - sLen)) || wildcard(domain, s)) {
let answer = config.domains[s]
if (typeof config.domains[config.domains[s]] !== 'undefined') {
answer = config.domains[config.domains[s]]
}
logquery('type: server, domain: %s, answer: %s, source: %s:%s, size: %d', domain, config.domains[s], rinfo.address, rinfo.port, rinfo.size)
const res = util.createAnswer(query, answer)
server.send(res, 0, res.length, rinfo.port, rinfo.address)
returner = true
}
})
if (returner) {
return
}
Object.keys(config.servers).forEach(function (s) {
if (domain.indexOf(s) !== -1) {
nameserver = config.servers[s]
}
})
const nameParts = nameserver.split(':')
nameserver = nameParts[0]
const port = nameParts[1] || 53
let fallback
(function queryns (message, nameserver) {
const sock = dgram.createSocket('udp4')
sock.send(message, 0, message.length, port, nameserver, function () {
fallback = setTimeout(function () {
queryns(message, config.nameservers[0])
}, config.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, source: %s:%s, size: %d', nameserver, domain, util.records[type] || 'unknown', util.listAnswer(response), rinfo.address, rinfo.port, rinfo.size)
server.send(response, 0, response.length, rinfo.port, rinfo.address)
sock.close()
})
}(message, nameserver))
})
server.bind(config.port, config.host)