Commit f63199a5 authored by Marvin W's avatar Marvin W
Browse files

Add initial director

parent 44eece5e
/usr/local/share/director:
file.directory
/usr/local/share/director/main.js:
file.managed:
- source: salt://freifunk-mgmt/files/director/main.js
- require:
- file: /usr/local/share/director
/etc/systemd/system/director.service:
file.managed:
- source: salt://freifunk-mgmt/files/director/director.service
- require:
- file: /usr/local/share/director/main.js
service_director:
service.running:
- name: director
- enable: True
- require:
- pkg: nodejs
- watch:
- file: /usr/local/share/director/main.js
- file: /etc/systemd/system/director.service
[Unit]
Description=Helps nodes to select the right domain.
[Service]
WorkingDirectory=/usr/local/share/director
ExecStart=/usr/bin/nodejs main.js
User=nobody
[Install]
WantedBy=multi-user.target
const http = require("http")
const fs = require("fs")
const firstByte = (s) => ((parseInt(s,16) >> 8) & 0xff).toString(16)
const secondByte = (s) => (parseInt(s,16) & 0xff).toString(16)
const prefixByte = (s) => (0x100+parseInt(s,16)).toString(16).substr(-2)
const prefixPair = (s) => (0x10000+parseInt(s,16)).toString(16).substr(-4)
const server = http.createServer(async (req, res) => {
const result = {
node_information: {
domain: {
name: undefined,
switch_time: -1
},
ip: undefined,
id: undefined,
region: undefined
}
}
result.node_information.ip = req.headers["x-forwarded-for"]
if (result.node_information.ip && result.node_information.ip.split(":").length == 8) {
const ip = result.node_information.ip.split(":")
result.node_information.id = prefixByte((parseInt(firstByte(ip[4]),16)^2).toString(16))+prefixByte(secondByte(ip[4]))+prefixByte(firstByte(ip[5]))+prefixByte(secondByte(ip[6]))+prefixPair(ip[7])
}
if (result.node_information.id) {
const nodeinfo = await new Promise((resolve, reject) => {
fs.readFile("/var/www/html/data/extended-nodeinfo.json", (err, data) => {
try {
if (err) reject(err)
else resolve(JSON.parse(data))
} catch (e) {
reject(e)
}
})
})
if (nodeinfo && nodeinfo[result.node_information.id]) {
result.node_information.region = nodeinfo[result.node_information.id].meta.region
}
}
if (result.node_information.region) {
switch (result.node_information.region) {
case "saarbruecken": result.node_information.domain.name = "sb"; break
case "st-wendel": result.node_information.domain.name = "wnd"; break
case "merzig-wadern": result.node_information.domain.name = "mzg"; break
case "neunkirchen": result.node_information.domain.name = "nk"; break
case "saarlouis": result.node_information.domain.name = "sls"; break
case "saarpfalz": result.node_information.domain.name = "hom"; break
}
}
res.writeHead(200)
res.end(JSON.stringify(result))
})
server.on('clientError', (err, socket) => {
if (err.code === 'ECONNRESET' || !socket.writable) return
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n')
})
server.listen(8055);
......@@ -88,6 +88,11 @@ server {
proxy_pass http://127.0.0.1:4000/;
}
location /director/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8055/;
}
location ~ ^/data/(freifunk-api|api-.*|extended-nodeinfo|regions)\.json {
root /var/www/html;
add_header 'Access-Control-Allow-Origin' '*';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment