Skip to content

Commit

Permalink
feat: add restore user functionality and improve JSON loading in bypa…
Browse files Browse the repository at this point in the history
…ss user list

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Jan 27, 2025
1 parent 1ccf2ed commit 0119193
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
65 changes: 44 additions & 21 deletions src/bypass_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,39 +414,62 @@ save_bypass_user_list()
void
load_bypass_user_list()
{
s_config *config = config_get_config();
FILE *fp = fopen(BYPASS_USER_LIST_PATH, "r");
if (!fp) {
debug(LOG_ERR, "Could not open file %s for reading: %s", BYPASS_USER_LIST_PATH, strerror(errno));
return;
}

char *line = NULL;
size_t len = 0;
ssize_t read;
json_object *j_obj = NULL;
json_object *j_array = NULL;

while ((read = getline(&line, &len, fp)) != -1) {
j_obj = json_tokener_parse(line);
if (j_obj) {
j_array = json_object_object_get(j_obj, "user");
if (j_array) {
for (size_t i = 0; i < json_object_array_length(j_array); i++) {
json_object *j_user = json_object_array_get_idx(j_array, i);
const char *mac = json_object_get_string(json_object_object_get(j_user, "mac"));
const char *serial = json_object_get_string(json_object_object_get(j_user, "serial"));
const char *time_str = json_object_get_string(json_object_object_get(j_user, "time"));
uint32_t time_val = atoi(time_str);
// Get file size
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);

// Read entire file
char *file_content = safe_malloc(file_size + 1);
size_t bytes_read = fread(file_content, 1, file_size, fp);
fclose(fp);

if (bytes_read != (size_t)file_size) {
debug(LOG_ERR, "Failed to read entire file: expected %ld bytes but got %zu", file_size, bytes_read);
free(file_content);
return;
}
file_content[file_size] = '\0';

// Parse JSON
json_object *j_obj = json_tokener_parse(file_content);
free(file_content);

if (!j_obj) {
debug(LOG_ERR, "Failed to parse JSON content");
return;
}

json_object *j_array = json_object_object_get(j_obj, "user");
if (j_array && json_object_is_type(j_array, json_type_array)) {
for (size_t i = 0; i < json_object_array_length(j_array); i++) {
json_object *j_user = json_object_array_get_idx(j_array, i);
if (!j_user) continue;

json_object *j_mac = json_object_object_get(j_user, "mac");
json_object *j_serial = json_object_object_get(j_user, "serial");
json_object *j_time = json_object_object_get(j_user, "time");

if (j_mac && j_serial && j_time) {
const char *mac = json_object_get_string(j_mac);
const char *serial = json_object_get_string(j_serial);
const char *time_str = json_object_get_string(j_time);

if (mac && serial && time_str) {
uint32_t time_val = (uint32_t)strtoul(time_str, NULL, 10);
add_mac_from_list(mac, time_val, serial, TRUSTED_MAC);
}
}
json_object_put(j_obj);
}
}

free(line);
fclose(fp);
json_object_put(j_obj);
}

static char *
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

#ifndef _VERSION_
#define _VERSION_
#define VERSION "8.01.2325"
#define VERSION "8.01.2327"
#endif
9 changes: 9 additions & 0 deletions src/wdctl_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static void wdctl_user_list(struct bufferevent *);
static void wdctl_user_info(struct bufferevent *, const char *);
static void wdctl_user_auth(struct bufferevent *, const char *);
static void wdctl_save_user(struct bufferevent *);
static void wdctl_restore_user(struct bufferevent *);
static void wdctl_add_anti_nat_permit_device(struct bufferevent *, const char *mac);
static void wdctl_del_anti_nat_permit_device(struct bufferevent *, const char *mac);

Expand Down Expand Up @@ -117,6 +118,7 @@ static struct wdctl_command {
{"user_info", NULL, wdctl_user_info},
{"user_auth", NULL, wdctl_user_auth},
{"save_user", wdctl_save_user, NULL},
{"restore_user", wdctl_restore_user, NULL},

// hotplugin event
{"hotplugin", NULL, wdctl_hotplugin_event},
Expand Down Expand Up @@ -839,6 +841,13 @@ wdctl_save_user(struct bufferevent *fd)
bufferevent_write(fd, "Yes", 3);
}

static void
wdctl_restore_user(struct bufferevent *fd)
{
load_bypass_user_list();
bufferevent_write(fd, "Yes", 3);
}

static void
wdctl_user_info(struct bufferevent *fd, const char *args)
{
Expand Down
4 changes: 3 additions & 1 deletion src/wdctlx.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ static void display_help() {
printf(" wdctlx reset <value>\n");
printf(" wdctlx status [client|auth|wifidogx]\n");
printf(" wdctlx refresh\n");
printf(" wdctlx apfree <user_list|user_info|user_auth|save_user>\n");
printf(" wdctlx apfree <user_list|user_info|user_auth|save_user|restore_user>\n");
printf(" wdctlx hotplugin <json_value>\n");
}

Expand All @@ -205,6 +205,7 @@ static const CommandMapping COMMAND_MAP[] = {
{"apfree", "user_info", true, true},
{"apfree", "user_auth", true, true},
{"apfree", "save_user", true, false},
{"apfree", "restore_user", true, false},
{"hotplugin", "hotplugin", false, false},
{NULL, NULL, false, false}
};
Expand All @@ -217,6 +218,7 @@ static const char *TYPE_MAP[] = {
"user_info",
"user_auth",
"save_user",
"restore_user",
NULL
};

Expand Down

0 comments on commit 0119193

Please sign in to comment.