Skip to content

Instantly share code, notes, and snippets.

@common-nighthawk
Created October 21, 2016 01:50
Show Gist options
  • Save common-nighthawk/7ae1c143096e01b4d0fe036d9087b5d8 to your computer and use it in GitHub Desktop.
Save common-nighthawk/7ae1c143096e01b4d0fe036d9087b5d8 to your computer and use it in GitHub Desktop.
'use strict';
exports.handler = (event, context, callback) => {
const execSync = require('child_process').execSync;
var http = require('http');
var ip = event["headers"]["X-Forwarded-For"].split(",")[0];
http.get({ host: 'freegeoip.net', path: '/json/' + ip }, function(res) {
res.on("data", function(chunk) {
var city = JSON.parse(chunk.toString())["city"].toLowerCase();
var state = JSON.parse(chunk.toString())["region_name"].toLowerCase();
execSync(cmd(ip, city, state));
});
});
function cmd(ip, city, state) {
return "curl -X PUT -d '" + data(ip, city, state) + "' '" + url() + "'"
}
function url() {
var datetime = new Date().toISOString().split("-").join("").split("T").join("").split(":").slice(0, -1).join("");
return "https://my-url.firebaseio.com/datetimes/" + datetime + ".json"
}
function data(ip, city, state) {
return JSON.stringify({"ip": ip, "city": city, "state": state})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment