-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
233 lines (199 loc) · 5.84 KB
/
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const fs = require('fs')
const path = require('path')
const dns2 = require('dns2')
const chokidar = require('chokidar')
const traefik = require('./traefik')
const local = require('./local')
const run = require('./run')
const install = require('./install')
const uninstall = require('./uninstall')
const help = require('./help')
const env = require('./env')
const logo = require('./logo')
async function init(config) {
let server
let tResolve
try {
const primaryDNS = config.dns && config.dns.primary ? {
resolve: dns2.TCPClient({
dns: config.dns.primary,
}),
dns: config.dns.primary,
} : null
const secondaryDNS = config.dns && config.dns.secondary ? {
resolve: dns2.TCPClient({
dns: config.dns.secondary,
}),
dns: config.dns.secondary,
} : null
if (config.docker.enable) {
tResolve = await traefik.init(config.docker.refresh)
}
const lResolve = await local.init(config.local)
server = dns2.createServer({
udp: true,
handle: async (request, send, rinfo) => {
const response = dns2.Packet.createResponseFromRequest(request)
const [question] = request.questions
const { name } = question
// traefik resolve
const tName = tResolve != null ? tResolve.resolve(name) : null
const lName = lResolve.resolve(name)
if (tName) {
response.answers.push({
name,
type: dns2.Packet.TYPE.A,
class: dns2.Packet.CLASS.IN,
ttl: 300,
address: tName,
})
} else if (lName) {
response.answers.push({
name,
type: dns2.Packet.TYPE.A,
class: dns2.Packet.CLASS.IN,
ttl: 300,
address: lName,
})
} else {
const dnsResult = []
if (primaryDNS != null) {
try {
const pResult = await primaryDNS.resolve(name)
dnsResult.push(...pResult.answers)
} catch (err) {
console.error('❌ error primary dns response:', config.dns.primary, '-', err.message)
}
}
if (secondaryDNS != null && dnsResult.length < 1) {
try {
const sResult = await secondaryDNS.resolve(name)
dnsResult.push(...sResult.answers)
} catch (err) {
console.error('❌ error secondary dns response:', config.dns.secondary, '-', err.message)
}
}
response.answers.push(...dnsResult)
}
send(response)
},
})
server.on('listening', (instance) => {
console.log('⚙️ server ready...', instance.udp)
})
server.on('error', () => {
console.log('❌ error host and port already in use,', `host: ${config.host}, port: ${config.port}`)
process.exit(1)
})
await server.listen({
udp: {
address: config.host,
port: config.port,
},
})
} catch (err) {
console.log('❌ error dns server,', `error: ${err.stack || err}`)
}
return {
async close() {
try {
if (server != null) await server.close()
if (tResolve != null) await tResolve.close()
} catch (_) {
// pass
}
},
}
}
function configLoad() {
let configPath = './config.json'
if (process.pkg) {
configPath = path.join(path.dirname(process.argv[0]), 'config.json')
}
if (!fs.existsSync(configPath)) {
configPath = path.join(process.cwd(), 'config.json')
}
let config = null
if (!fs.existsSync(configPath)) {
console.error('❌ config file not found:', configPath)
} else {
try {
config = JSON.parse(fs.readFileSync(configPath).toString())
} catch (err) {
console.error('❌ config file json error:', configPath)
}
}
return { config, configPath }
}
async function main() {
let configSeed = configLoad()
let watcher = null
let server = null
if (configSeed.config == null || (configSeed.config.watch && configSeed.config.watch.enable)) {
let interval = 2000
if (configSeed.config != null && configSeed.config.watch && configSeed.config.watch.interval) {
interval = configSeed.config.watch.interval
}
watcher = chokidar.watch(configSeed.configPath, {
interval,
usePolling: true,
persistent: true,
ignoreInitial: true,
})
watcher
.on('add', () => async () => {
console.log('🔃 detect config file add, server start...')
if (server != null) {
await server.close()
server = null
}
configSeed = configLoad()
if (configSeed.config != null) {
server = await init(configSeed.config)
}
})
.on('change', async () => {
console.log('🔃 detect config file change, server restart...')
if (server != null) {
await server.close()
server = null
}
configSeed = configLoad()
if (configSeed.config != null) {
server = await init(configSeed.config)
}
})
.on('unlink', async () => {
console.log('🔃 detect config file removed, server close...')
if (server != null) {
await server.close()
server = null
}
})
}
if (configSeed.config != null) {
server = await init(configSeed.config)
}
process.on('SIGTERM', async () => {
if (watcher != null) {
await watcher.close()
}
if (server != null) {
await server.close()
server = null
}
})
}
console.log(logo)
console.log('version:', env.version, '\n')
if (process.argv[2] === '--install') {
install.install(process.argv[3] === '--force')
} else if (process.argv[2] === '--uninstall') {
uninstall.uninstall()
} else if (process.argv[2] === '--help') {
help.help()
} else if (process.argv[2] === '--run') {
run.run(true)
} else {
main()
}