Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.