-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
34 lines (29 loc) · 1 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
const app = require('express')()
const { make } = require('./')
const USAGE = require('./usage')
var SH_USAGE = `echo '${USAGE}'`
.replace(/firewall-my-system/g, 'firewallmy.systems')
.replace(' FIREWALL PORTSPEC', '/FIREWALL/PORTSPEC')
.replace(' iptables t22 t80 u5000', '/iptables/t22/t80/u5000/')
SH_USAGE += "\n\necho 'want your own copy? https://www.npmjs.com/package/firewall-my-systems'\n"
app.get('/', (req, res) => {
// pls don't re-indent this -- it looks nice ;)
res.type('text/plain').send(SH_USAGE)
})
// matches /foo/(anything or nothing)
// notably does *not* match
app.get(/(\w+)\/(.*)/, (req, res) => {
// always text/plain
res.type('text/plain')
const portString = req.params[1] || ""
const firewall = make(req.params[0], portString)
if (!firewall) {
res.status(404)
.send(`echo 'Error: Unknown firewall "${req.params[0]}"'\n` + SH_USAGE)
return
}
res.send(firewall.build())
})
const PORT = process.env.PORT || 8080
console.log('PORT =', PORT)
app.listen(PORT)