Skip to content

Commit

Permalink
feat: implement functions to save and load bypass user list in JSON f…
Browse files Browse the repository at this point in the history
…ormat

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Jan 27, 2025
1 parent 64c0397 commit 1ccf2ed
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
79 changes: 79 additions & 0 deletions src/bypass_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,85 @@ dump_bypass_user_list_json()
return res;
}

#define BYPASS_USER_LIST_PATH "/etc/bypass_user_list.json"

void
save_bypass_user_list()
{
FILE *fp = fopen(BYPASS_USER_LIST_PATH, "w");
if (!fp) {
debug(LOG_ERR, "Could not open file %s for writing: %s", BYPASS_USER_LIST_PATH, strerror(errno));
return;
}

s_config *config = config_get_config();
json_object *j_obj = json_object_new_object();
json_object *j_array = json_object_new_array();
char remaining_time_str[32] = {0};

LOCK_CONFIG();

t_trusted_mac *tmac = config->trustedmaclist;
while (tmac) {
snprintf(remaining_time_str, sizeof(remaining_time_str), "%d", tmac->remaining_time);
json_object *j_user = json_object_new_object();
json_object_object_add(j_user, "mac", json_object_new_string(tmac->mac));
json_object_object_add(j_user, "serial", json_object_new_string(tmac->serial ? tmac->serial : ""));
json_object_object_add(j_user, "time", json_object_new_string(remaining_time_str));
json_object_array_add(j_array, j_user);
tmac = tmac->next;
memset(remaining_time_str, 0, sizeof(remaining_time_str));
}

UNLOCK_CONFIG();

json_object_object_add(j_obj, "user", j_array);

const char *json_str = json_object_to_json_string_ext(j_obj, JSON_C_TO_STRING_PRETTY);
fprintf(fp, "%s\n", json_str);

fclose(fp);
json_object_put(j_obj);
}

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);
add_mac_from_list(mac, time_val, serial, TRUSTED_MAC);
}
}
json_object_put(j_obj);
}
}

free(line);
fclose(fp);
}

static char *
get_release_value(const char *mac, const char *value)
{
Expand Down
2 changes: 2 additions & 0 deletions src/bypass_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ bool add_bypass_user(const char *, const uint32_t , const char *);
bool remove_bypass_user(const char *);
char *dump_bypass_user_list_json();
char *query_bypass_user_status(const char *, const char *, const char *, query_choice_t choice);
void save_bypass_user_list();
void load_bypass_user_list();

#endif
9 changes: 9 additions & 0 deletions src/wdctl_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static void wdctl_add_auth_client(struct bufferevent *, const char *);
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_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 @@ -115,6 +116,7 @@ static struct wdctl_command {
{"user_list", wdctl_user_list, NULL},
{"user_info", NULL, wdctl_user_info},
{"user_auth", NULL, wdctl_user_auth},
{"save_user", wdctl_save_user, NULL},

// hotplugin event
{"hotplugin", NULL, wdctl_hotplugin_event},
Expand Down Expand Up @@ -830,6 +832,13 @@ wdctl_user_list(struct bufferevent *fd)
bufferevent_write(fd, "{}", 2);
}

static void
wdctl_save_user(struct bufferevent *fd)
{
save_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>\n");
printf(" wdctlx apfree <user_list|user_info|user_auth|save_user>\n");
printf(" wdctlx hotplugin <json_value>\n");
}

Expand All @@ -204,6 +204,7 @@ static const CommandMapping COMMAND_MAP[] = {
{"apfree", "user_list", true, false},
{"apfree", "user_info", true, true},
{"apfree", "user_auth", true, true},
{"apfree", "save_user", true, false},
{"hotplugin", "hotplugin", false, false},
{NULL, NULL, false, false}
};
Expand All @@ -215,6 +216,7 @@ static const char *TYPE_MAP[] = {
"user_list",
"user_info",
"user_auth",
"save_user",
NULL
};

Expand Down

0 comments on commit 1ccf2ed

Please sign in to comment.