Permalink
Browse files

now supports running as a connect/express plugin.

  • Loading branch information...
1 parent 1cdf1ba commit 6596518b07e7d1140f01a951f7c546dd86c326d7 Joachim Kainz committed Jan 2, 2012
Showing with 51 additions and 49 deletions.
  1. +6 −13 README.md
  2. +2 −2 bin/404reporter
  3. +8 −0 examples/express.js
  4. +30 −32 lib/404project.js
  5. +5 −2 package.json
View
@@ -14,25 +14,18 @@ Usage
---------------------
```
-var express = require('express');
-
-// the user id is usually a number, like 1234567
-var myUserID='[Your ISC ID]';
-var myKey='[Your Authentication Key]';
-var reporter = require('404project').create(myUserID, myKey);
+var express = require("express");
+var reporter = require("404project");
+var myUserID = process.argv[2] || "[Your ISC ID]";
+var myKey = process.argv[3] || "[Your Authentication Key]";
var app = module.exports = express.createServer();
-app.get(function(request, response) {
- response.statusCode = 404;
- response.end("<h1>not found</h1>");
- reporter.report404(request);
-});
-
+app.use(reporter(myUserID, myKey));
app.listen(3000);
```
There is also a command-line tool:
```
-bin/404reporter 636742678 aaaaaaaaaaaabbcccccccccccceeeeeeeeffffff "http://www.jolira.com/__phpamdin" 125.64.23.180 jakarta
+bin/404reporter 036742670 aaaaaaaaaaaabbcccccccccccceeeeeeeeffffff "http://www.jolira.com/__phpamdin" 125.64.23.180 jakarta
```
View
@@ -16,7 +16,7 @@ else {
var ip = process.argv[5];
var ua = process.argv[6];
var ts = process.argv[7];
- var reporter = _404.create(userID, key);
+ var reporter = _404(userID, key);
- reporter.report404(url, ip, ua, ts);
+ reporter(url, ip, ua, ts);
}
View
@@ -0,0 +1,8 @@
+var express = require("express");
+var reporter = require("..");
+var myUserID = process.argv[2] || "[Your ISC ID]";
+var myKey = process.argv[3] || "[Your Authentication Key]";
+var app = module.exports = express.createServer();
+
+app.use(reporter(myUserID, myKey));
+app.listen(3e3);
View
@@ -11,9 +11,7 @@
* $sMyUserID - in right-column under "Logged in as:"
* $sMyKey - in section https://isc.sans.edu/myinfo.html#report
* labeled "Your Authentication Key:"
- */
- /*jshint devel:true, node:true, indent:2, maxerr:50 */
-(function () {
+ *//*jshint devel:true, node:true, indent:2, maxerr:50 */(function() {
"use strict";
var https = require("https");
function formatYmd(ts) {
@@ -28,49 +26,49 @@
var second = ts.getUTCSeconds();
return hour + ":" + minute + ":" + second;
}
- function Reporter(userID, key) {
- this.userID = userID;
- this.key = key;
- }
- Reporter.prototype.report404 = function () {
- var url;
- var ip;
- var ua;
- var ts;
- if (arguments.length === 1) {
- var request = arguments[0];
- url = request.url;
- ip = request.connection.remoteAddress;
- ua = request.headers["user-agent"];
- ts = new Date();
- } else {
- url = arguments[0];
- ip = arguments[1];
- ua = arguments[2];
- ts = arguments[3] || new Date();
- }
+ function submit(userID, key, url, ip, ua, ts) {
var req = https.request({
host: "isc.sans.edu",
port: 443,
- path: "/weblogs/404project.html?id=" + this.userID + "&version=2",
+ path: "/weblogs/404project.html?id=" + userID + "&version=2",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
- }, function (res) {
+ }, function(res) {
res.setEncoding("utf8");
- res.on("data", function (chunk) {
+ res.on("data", function(chunk) {
// console.log(chunk);
});
});
- var data = this.userID + "\x00" + this.key + "\x00" + url + "\x00" + ip +
- "\x00" + ua + "\x00" + formatYmd(ts) + "\x00" + formatHms(ts);
+ var data = userID + "\0" + key + "\0" + url + "\0" + ip + "\0" + ua + "\0" + formatYmd(ts) + "\0" + formatHms(ts);
var buf = new Buffer(data);
var payload = buf.toString("base64");
req.write("DATA=" + payload);
req.end();
- };
- module.exports.create = function (userID, key) {
- return new Reporter(userID, key);
+ }
+ module.exports = function(userID, key) {
+ return function() {
+ if (!arguments[0].url) {
+ return submit(userID, key, arguments[0], arguments[1], arguments[2], arguments[3] || new Date);
+ }
+ var request = arguments[0];
+ var response = arguments[1];
+ if (!response) {
+ return submit(userID, key, request.url, request.connection.remoteAddress, request.headers["user-agent"], new Date);
+ }
+ var next = arguments[2];
+ var end = response.end;
+ response.end = function(chunk, encoding) {
+ response.end = end;
+ response.end(chunk, encoding);
+ if (response.statusCode === 404) {
+ submit(userID, key, request.url, request.connection.remoteAddress, request.headers["user-agent"], new Date);
+ }
+ };
+ if (next) {
+ return next();
+ }
+ };
};
})();
View
@@ -1,8 +1,8 @@
{
"name": "404project",
- "version": "0.0.3",
+ "version": "0.0.4",
"description": "Report 404 errors to http://www.dshield.org/tools/404project.html",
- "keywords": ["security", "http", "https"],
+ "keywords": ["security", "http", "https", "connect", "express"],
"repository": "git://github.com/jolira/node-404project.git",
"author": "Joachim Kainz <jfk@jolira.com> (http://www.jolira.com)",
"repository": "git@github.com:jolira/node-404project.git",
@@ -13,6 +13,9 @@
"bin": {
"404reporter": "./bin/404reporter"
},
+ "devDependencies": {
+ "express": ">= 2.5.3"
+ },
"scripts": {
"prepublish": "npm prune"
},

0 comments on commit 6596518

Please sign in to comment.