updated haproxy state
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2023-04-15 13:51:27 +02:00
parent e770dcee17
commit c1dddc6fb4

View File

@ -19,13 +19,13 @@ done
#include <maxminddb.h> #include <maxminddb.h>
#include <jansson.h> #include <jansson.h>
static MMDB_s mmdb; static MMDB_s* mmdb;
const int STR_MAX_LEN = 255;
static int load_geoip(lua_State *L) { static int load_geoip(lua_State *L) {
const char *filename = luaL_checkstring(L, 1); const char *filename = luaL_checkstring(L, 1);
int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb); int status;
mmdb = malloc(sizeof (struct MMDB_s));
status = MMDB_open(filename, MMDB_MODE_MMAP, mmdb);
if (status != 0) { if (status != 0) {
printf("Error loading geoip database\n"); printf("Error loading geoip database\n");
exit(1); exit(1);
@ -37,7 +37,7 @@ static int load_geoip(lua_State *L) {
int get_geoip_info(const char* ip_address, MMDB_lookup_result_s* result) { int get_geoip_info(const char* ip_address, MMDB_lookup_result_s* result) {
int gai_error, mmdb_error; int gai_error, mmdb_error;
(*result) = MMDB_lookup_string(&mmdb, ip_address, &gai_error, &mmdb_error); (*result) = MMDB_lookup_string(mmdb, ip_address, &gai_error, &mmdb_error);
if (0 != gai_error) { if (0 != gai_error) {
fprintf(stderr, "\n Error from getaddrinfo for %s - %s\n\n", ip_address, gai_strerror(gai_error)); fprintf(stderr, "\n Error from getaddrinfo for %s - %s\n\n", ip_address, gai_strerror(gai_error));
@ -51,25 +51,22 @@ int get_geoip_info(const char* ip_address, MMDB_lookup_result_s* result) {
return 0; return 0;
} }
int filter_value(MMDB_lookup_result_s* result, int filter_value(
MMDB_entry_data_list_s** entry_data_list, bool found_entry,
MMDB_entry_s* entry,
MMDB_entry_data_list_s* entry_data_list,
MMDB_entry_data_s* entry_data, MMDB_entry_data_s* entry_data,
const char* query) { const char* query) {
int status = MMDB_get_entry_data_list(&result->entry, entry_data_list); int status;
MMDB_get_entry_data_list(entry, &entry_data_list);
MMDB_free_entry_data_list(entry_data_list);
if (result->found_entry) { if (found_entry) {
if (strcmp(query, "country") == 0) { if (strcmp(query, "country") == 0) {
status = MMDB_get_value(&result->entry, entry_data, status = MMDB_get_value(entry, entry_data, "country", "iso_code", NULL);
"country",
"iso_code",
NULL);
return status; return status;
} else if (strcmp(query, "city") == 0){ } else if (strcmp(query, "city") == 0){
status = MMDB_get_value(&result->entry, entry_data, status = MMDB_get_value(entry, entry_data, "city", "names", "en", NULL);
"city",
"names",
"en",
NULL);
return status; return status;
} }
} }
@ -78,18 +75,21 @@ int filter_value(MMDB_lookup_result_s* result,
static int get_country(lua_State *L) { static int get_country(lua_State *L) {
const char *ip_address = luaL_checkstring(L, 1); const char *ip_address = luaL_checkstring(L, 1);
char country[STR_MAX_LEN]; char* country = malloc(4);
int status;
MMDB_entry_data_list_s *entry_data_list = NULL; MMDB_entry_data_list_s *entry_data_list = malloc(sizeof (struct MMDB_entry_data_list_s));
MMDB_entry_data_s entry_data; MMDB_entry_data_s* entry_data = malloc(sizeof (struct MMDB_entry_data_s));
MMDB_lookup_result_s result; MMDB_lookup_result_s* result = malloc(sizeof (struct MMDB_lookup_result_s));
get_geoip_info(ip_address, &result); status = get_geoip_info(ip_address, result);
int status = filter_value(&result, &entry_data_list, &entry_data, "country"); status = filter_value(result->found_entry, &result->entry, entry_data_list, entry_data, "country");
free(result);
result = NULL;
if (status == 0) { if (status == 0) {
sprintf(country, "%.*s", entry_data.data_size,entry_data.utf8_string); sprintf(country, "%.*s", entry_data->data_size, entry_data->utf8_string);
} else { } else {
strcpy(country, "not found"); strcpy(country, "not found");
} }
@ -100,23 +100,33 @@ static int get_country(lua_State *L) {
static int get_city(lua_State *L) { static int get_city(lua_State *L) {
const char *ip_address = luaL_checkstring(L, 1); const char *ip_address = luaL_checkstring(L, 1);
char city[STR_MAX_LEN]; char* city = malloc(50);
int status;
MMDB_entry_data_list_s *entry_data_list = NULL; MMDB_entry_data_list_s *entry_data_list = malloc(sizeof (struct MMDB_entry_data_list_s));
MMDB_entry_data_s entry_data; MMDB_entry_data_s* entry_data = malloc(sizeof (struct MMDB_entry_data_s));
MMDB_lookup_result_s result; MMDB_lookup_result_s* result = malloc(sizeof (struct MMDB_lookup_result_s));
get_geoip_info(ip_address, &result); status = get_geoip_info(ip_address, result);
int status = filter_value(&result, &entry_data_list, &entry_data, "city"); status = filter_value(result->found_entry, &result->entry, entry_data_list, entry_data, "city");
free(result);
result = NULL;
if (status == 0) { if (status == 0) {
sprintf(city, "%.*s", entry_data.data_size, entry_data.utf8_string); sprintf(city, "%.*s", entry_data->data_size, entry_data->utf8_string);
} else { } else {
strcpy(city, "not found"); strcpy(city, "not found");
} }
free(entry_data);
entry_data = NULL;
lua_pushstring(L, city); lua_pushstring(L, city);
free(city);
city = NULL;
return 1; return 1;
} }