Created
October 21, 2016 01:50
-
-
Save common-nighthawk/7ae1c143096e01b4d0fe036d9087b5d8 to your computer and use it in GitHub Desktop.
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
'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