telize

High performance JSON IP and GeoIP REST API (IP Geolocation)
Log | Files | Refs | README | LICENSE

commit a500a52ea3f3d068cdfb6f00a1470d57f92c1c96
parent 437137cdd77d2f71a378bdd91d3392fc95ae5a2f
Author: Frederic Cambus <fred@statdns.com>
Date:   Fri,  5 Oct 2018 08:56:48 +0200

Add an initial 'location' endpoint, for now it's the 'jsonip' one duplicated

Diffstat:
Mconf/telize.conf | 6++++++
Asrc/location.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/conf/telize.conf b/conf/telize.conf @@ -18,4 +18,10 @@ domain * { static /jsonip jsonip params qs:get /jsonip { validate callback v_callback + } + + static /location location + params qs:get /location { + validate callback v_callback + } } diff --git a/src/location.c b/src/location.c @@ -0,0 +1,58 @@ +/*****************************************************************************/ +/* */ +/* Telize 2.0.0 */ +/* Copyright (c) 2013-2018, Frederic Cambus */ +/* https://www.telize.com */ +/* */ +/* Created: 2013-08-15 */ +/* Last Updated: 2018-10-05 */ +/* */ +/* Telize is released under the BSD 2-Clause license. */ +/* See LICENSE file for details. */ +/* */ +/*****************************************************************************/ + +#include <sys/socket.h> + +#include <kore/kore.h> +#include <kore/http.h> + +#include <jansson.h> + +int location(struct http_request *); + +int +location(struct http_request *req) +{ + const char *visitor_ip; + char *answer, *callback, *json, *ip, addr[INET6_ADDRSTRLEN]; + json_t *output = json_object(); + + http_populate_get(req); + + if (req->owner->addrtype == AF_INET) { + inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv4.sin_addr), addr, sizeof(addr)); + } else { + inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv6.sin6_addr), addr, sizeof(addr)); + } + + if (http_request_header(req, "X-Forwarded-For", &visitor_ip)) { + ip = kore_strdup(visitor_ip); + } else { + ip = addr; + } + + json_object_set_new(output, "ip", json_string(ip)); + json = json_dumps(output, JSON_INDENT(3)); + + if (http_argument_get_string(req, "callback", &callback)) { + asprintf(&answer, "%s(%s);", callback, json); + } else { + answer = json; + } + + http_response_header(req, "content-type", "application/json; charset=utf-8"); + http_response(req, 200, answer, strlen(answer)); + + return (KORE_RESULT_OK); +}