Skip to content

Instantly share code, notes, and snippets.

@APTy
Created January 19, 2016 18:08
Show Gist options
  • Save APTy/1106c6fb7bd692422c38 to your computer and use it in GitHub Desktop.
Save APTy/1106c6fb7bd692422c38 to your computer and use it in GitHub Desktop.
Image to get IP Address
var fs = require('fs');
var server = require('http').createServer();
var httpPng;
const HTTP_STATUS_OK = 200;
const PNG_FILE_NAME = 'http.png';
const PNG_HEADER = {'Content-Type': 'image/png'};
// Load the file into memory
fs.readFile(PNG_FILE_NAME, function(err, data) {
if (err) throw err;
httpPng = data;
});
server.on('request', function(req, res) {
// Save the requester's address
fs.appendFile('ips.txt', req.client.address().address + '\n');
// Serve the image
if (req.url.indexOf(PNG_FILE_NAME) !== -1) {
res.writeHead(HTTP_STATUS_OK, PNG_HEADER);
res.write(httpPng);
}
// End the response
res.end();
});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment