Last active
October 9, 2015 15:39
-
-
Save codeout/9126cbd926c6f748b0d0 to your computer and use it in GitHub Desktop.
NodeJS script: Open a github issue to mitigate DDoS
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
# Description | |
# Open a github issue to mitigate DDoS | |
# | |
# Commands: | |
# hubot anti ddos <path> <title>\n<yaml data> - Open a github issue with template and data | |
# | |
# Author: | |
# Shintaro Kojima <goodies@codeout.net> | |
yaml = require('js-yaml') | |
GithubTemplate = require('hubot-github-templated-issues/src/github-templated-issues/github_template') | |
GithubIssue = require('hubot-github-templated-issues/src/github-templated-issues/github_issue') | |
MRT = require('node-mrt') | |
mrt = new MRT('wide') | |
BGPDump = require('node-bgpdump2') | |
module.exports = (robot) -> | |
robot.respond /anti\s?ddos\s+(\S+)+(.*)/i, (msg) -> | |
error_handler = (error) -> | |
msg.send "ERROR: #{error}" | |
msg.robot.logger.error(error) | |
try | |
data = yaml.safeLoad(msg.match.input.replace(/.*/, '')) | |
template = new GithubTemplate(error_handler) | |
# MRT を取得 | |
mrt.get (text) -> | |
msg.send text | |
, (path) -> | |
# host address をキーに, MRT から検索 (longest-match) | |
bgpdump = new BGPDump(path) | |
prefixes = bgpdump.lookup(data.src) | |
# prefix を取り出し, uniq | |
data.src = prefixes.map (e, i) -> | |
e.prefix if e | |
.filter (x, i, self) -> | |
return x && self.indexOf(x) == i; | |
# GitHub Issue をオープン | |
template.render msg.match[1], data, (error, result) -> | |
return error_handler(error) if error | |
issue = new GithubIssue(error_handler, msg.match[2], result) | |
issue.create (error, created) -> | |
return error_handler(error) if error | |
msg.send "Issue created\n#{created.html_url}" | |
catch error | |
error_handler 'Invalid YAML data' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment