commit fa7e3058e6431e212e4b83ca0f93e48b569b231a
parent 910f0fbd29768287d188a90e0c5ff30c8c641bd0
Author: Frederic Cambus <fred@statdns.com>
Date: Thu, 4 Oct 2018 23:35:42 +0200
Add the 'jsonip' endpoint, and link against jansson
Diffstat:
3 files changed, 52 insertions(+), 0 deletions(-)
diff --git a/conf/build.conf b/conf/build.conf
@@ -12,6 +12,7 @@
cflags=-Wall -Wmissing-declarations -Wshadow
cflags=-Wstrict-prototypes -Wmissing-prototypes
cflags=-Wpointer-arith -Wcast-qual -Wsign-compare
+ldflags=-L/usr/local/lib -ljansson
cxxflags=-Wall -Wmissing-declarations -Wshadow
cxxflags=-Wpointer-arith -Wcast-qual -Wsign-compare
diff --git a/conf/telize.conf b/conf/telize.conf
@@ -12,4 +12,5 @@ domain * {
accesslog telize.log
static /ip ip
+ static /jsonip jsonip
}
diff --git a/src/jsonip.c b/src/jsonip.c
@@ -0,0 +1,50 @@
+/*****************************************************************************/
+/* */
+/* Telize 2.0.0 */
+/* Copyright (c) 2013-2018, Frederic Cambus */
+/* https://www.telize.com */
+/* */
+/* Created: 2013-08-15 */
+/* Last Updated: 2018-10-04 */
+/* */
+/* 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 jsonip(struct http_request *);
+
+int
+jsonip(struct http_request *req)
+{
+ const char *visitor_ip;
+ char *answer, *ip, addr[INET6_ADDRSTRLEN];
+ json_t *output = json_object();
+
+ 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));
+ answer = json_dumps(output, JSON_INDENT(3));
+
+ http_response_header(req, "content-type", "application/json");
+ http_response(req, 200, answer, strlen(answer));
+
+ return (KORE_RESULT_OK);
+}