-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (45 loc) · 1.38 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
const http = require('http')
const fs = require('fs')
const SOCKET = process.argv.length > 2 ? process.argv[2] : './http.sock'
try {
fs.unlinkSync(SOCKET)
} catch (err) {}
const oui = JSON.parse(fs.readFileSync('./oui.json'))
const server = http.createServer((req, res) => {
let date = new Date().toLocaleString()
console.log(`${date} ${req.method} ${req.url}`)
let m = null
if (req.method == 'GET' && (m = req.url.match(/^\/([0-9a-f:,]+)/i))) {
let prefixs = m[1]
.split(',')
.map((mac) => mac.replace(/:/g, '').toUpperCase().substring(0, 6))
.filter((mac) => mac.length == 6)
let result = prefixs
.map((prefix) => {
if (!oui[prefix]) {
return null
}
let [provider, addr1, addr2, country] = oui[prefix].split('\n')
return { provider, country, prefix }
})
.filter((x) => x)
if (!result.length) {
res.writeHead(404, { 'Content-Type': 'text/plain' })
return res.end(`404 - not found`)
}
if (prefixs.length == 1) {
result = result[0]
}
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(result))
return res.end()
}
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('404 - File not found')
})
const shutdown = () => {
server.close()
}
server.listen(SOCKET)
process.on('exit', shutdown)
process.on('SIGINT', shutdown)