-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
serain
committed
Sep 1, 2018
1 parent
77c4a13
commit 190bc60
Showing
19 changed files
with
423 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Router } from 'express' | ||
import { check, validationResult } from 'express-validator/check' | ||
import cors from 'cors' | ||
import * as iptables from '../utils/iptables' | ||
import { resolve } from 'url'; | ||
|
||
const router = Router() | ||
|
||
function runIPTables (command, port, address) { | ||
return new Promise((resolve, reject) => { | ||
iptables.execute({ | ||
table: iptables.Table.NAT, | ||
command: command, | ||
chain: iptables.Chain.PREROUTING, | ||
target: iptables.Target.REDIRECT, | ||
fromPort: port, | ||
toPort: 1, | ||
srcAddress: address | ||
}).then(status => { | ||
resolve(status) | ||
}) | ||
}) | ||
} | ||
|
||
router.post('/', cors(), [ | ||
check('block').optional().isBoolean(), | ||
check('port').optional().isInt({min: 1, max: 65535}) | ||
], function (req, res, next) { | ||
const errors = validationResult(req) | ||
|
||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }) | ||
} | ||
|
||
console.log('dref: POST IPTables\n' + JSON.stringify(req.body, null, 4)) | ||
|
||
// Get IP address | ||
const ipv4Match = req.ip.match(/::ffff:(\d{0,3}.\d{0,3}.\d{0,3}.\d{0,3})/) | ||
if (!ipv4Match) { | ||
console.log(`source IP ${req.ip} doesn't appear to be IPv4, can't manipulate iptables and fast-rebind not available`) | ||
return res.status(400).send() | ||
} | ||
const ipv4 = ipv4Match[1] | ||
|
||
// Block for 10 seconds or unblock | ||
if (req.body.block) { | ||
runIPTables(iptables.Command.INSERT, req.body.port, ipv4).then(status => { | ||
// unblock after 10 seconds max (fail-safe if client forgets to unblock) | ||
setTimeout(function () { | ||
runIPTables(iptables.Command.DELETE, req.body.port, ipv4) | ||
}, global.config.general.iptablesTimeout) | ||
|
||
if (status) { | ||
return res.status(204).send() | ||
} | ||
return res.status(400).send() | ||
}) | ||
} else { | ||
runIPTables(iptables.Command.DELETE, req.body.port, ipv4).then(status => { | ||
if (status) return res.status(204).send() | ||
return res.status(400).send() | ||
}) | ||
} | ||
}) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import {spawn} from 'child_process' | ||
|
||
export const Command = Object.freeze({ | ||
APPEND: '-A', | ||
CHECK: '-C', | ||
DELETE: '-D', | ||
INSERT: '-I' | ||
}) | ||
|
||
export const Target = Object.freeze({ | ||
DROP: 'DROP', | ||
REDIRECT: 'REDIRECT', | ||
REJECT: 'REJECT' | ||
}) | ||
|
||
export const Table = Object.freeze({ | ||
FILTER: 'filter', | ||
NAT: 'nat' | ||
}) | ||
|
||
export const Chain = Object.freeze({ | ||
INPUT: 'INPUT', | ||
PREROUTING: 'PREROUTING' | ||
}) | ||
|
||
function getRule ({table, command, chain, target, fromPort, toPort, srcAddress}) { | ||
fromPort = fromPort || null | ||
toPort = toPort || null | ||
srcAddress = srcAddress || null | ||
|
||
let args = [] | ||
args = args.concat(['-t', table]) | ||
args = args.concat([command, chain]) | ||
args = args.concat(['-p', 'tcp']) | ||
|
||
if (srcAddress) args = args.concat(['--src', srcAddress]) | ||
if (fromPort) args = args.concat(['--dport', fromPort]) | ||
|
||
args = args.concat(['-j', target]) | ||
|
||
if (target == Target.REJECT) args = args.concat(['--reject-with', 'tcp-reset']) | ||
if (toPort) args = args.concat(['--to-port', toPort]) | ||
|
||
return args | ||
} | ||
|
||
function checkRuleExists (args) { | ||
// returns true if the rule with args alreadys exists | ||
return new Promise((resolve, reject) => { | ||
const check = spawn('iptables', args) | ||
|
||
check.on('close', (code) => { | ||
if (code === 1) return resolve(false) | ||
return resolve(true) | ||
}) | ||
}) | ||
} | ||
|
||
export function execute ({table, command, chain, target, fromPort, toPort, srcAddress} = {}) { | ||
return new Promise((resolve, reject) => { | ||
fromPort = fromPort || null | ||
toPort = toPort || null | ||
srcAddress = srcAddress || null | ||
|
||
checkRuleExists(getRule({ | ||
table: table, | ||
command: Command.CHECK, | ||
chain: chain, | ||
target: target, | ||
fromPort: fromPort, | ||
toPort: toPort, | ||
srcAddress: srcAddress | ||
})).then((exists) => { | ||
if (([Command.APPEND, Command.INSERT].includes(command) && exists) || (command === Command.DELETE && !exists)) { | ||
console.log(`ignoring execute(${table}, ${command}, ${chain}, ${target}, ${fromPort}, ${toPort}, ${srcAddress})`) | ||
resolve(true) | ||
} | ||
|
||
const iptables = spawn('iptables', getRule({ | ||
table: table, | ||
command: command, | ||
chain: chain, | ||
target: target, | ||
fromPort: fromPort, | ||
toPort: toPort, | ||
srcAddress: srcAddress | ||
})) | ||
|
||
iptables.on('close', (code) => { | ||
if (code === 0) { | ||
console.log(`success execute(${table}, ${command}, ${chain}, ${target}, ${fromPort}, ${toPort}, ${srcAddress})`) | ||
resolve(true) | ||
} else { | ||
console.log(`fail execute(${table}, ${command}, ${chain}, ${target}, ${fromPort}, ${toPort}, ${srcAddress})`) | ||
resolve(false) | ||
} | ||
}) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.