Skip to content

Commit

Permalink
Add dev online status
Browse files Browse the repository at this point in the history
  • Loading branch information
destan19 committed Mar 22, 2021
1 parent 227c5de commit af168f6
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 69 deletions.
73 changes: 7 additions & 66 deletions luci-app-oaf/luasrc/view/admin_network/user_status.htm
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,6 @@
}-->
</style>
<script type="text/javascript">//<![CDATA[

/*
XHR.poll(10, '<%=url('admin/network/user_status')%>', null,
function(x, st)
{
var tb = document.getElementById('user_status_table');
var str=JSON.stringify(st);
if (st && tb)
{
while(tb.rows.length > 1)
tb.deleteRow(1);
for(var i = 0; i < st.length; i++ )
{
var action_status=""
if(st[i].latest_action == 1)
action_status="已过滤"
else
action_status="未过滤"
var hostname=""
if(st[i].hostname == "" || st[i].hostname == "*"){
hostname="?";
}
else{
hostname=st[i].hostname;
}
var tr = tb.insertRow(-1);
//tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
tr.insertCell(-1).innerHTML = st[i].appname;
tr.insertCell(-1).innerHTML = hostname;
tr.insertCell(-1).innerHTML = "<a href='<%=url('admin/network/appfilter/')%>"+st[i].mac+"'>"+st[i].mac+"</a>";
tr.insertCell(-1).innerHTML = st[i].first_time;
var hour=parseInt(st[i].total_time / 3600);
var seconds=st[i].total_time % 3600;
var min=parseInt(seconds / 60)
var total_time_str;
if (st[i].latest_action == 1)
total_time_str="-"
else {
if (hour > 0)
total_time_str=hour + "小时" + min + "分"
else{
if (min == 0)
min = 1;
total_time_str=min + "分"
}
}
tr.insertCell(-1).innerHTML = total_time_str;
tr.insertCell(-1).innerHTML = action_status;
}
}
}
);
*/

XHR.poll(5, '<%=url('admin/network/dev_app_status')%>', null,
function(x, st)
{
Expand All @@ -105,6 +49,7 @@
}
var tr = tb.insertRow(-1);
//tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
tr.insertCell(-1).innerHTML = i + 1;
tr.insertCell(-1).innerHTML = hostname;
tr.insertCell(-1).innerHTML = "<a href='<%=url('admin/network/appfilter/')%>"+devlist[i].mac+"'>"+devlist[i].mac+"</a>";
tr.insertCell(-1).innerHTML = devlist[i].ip;
Expand All @@ -119,6 +64,10 @@
app_list_str="--"
}
tr.insertCell(-1).innerHTML = app_list_str;
if (devlist[i].online == 1)
tr.insertCell(-1).innerHTML = "在线";
else
tr.insertCell(-1).innerHTML = "离线";
}
}
}
Expand All @@ -130,21 +79,13 @@
<fieldset class="cbi-section">
<legend><%:终端列表%></legend>
<table class="imagetable" id="user_status_table">
<!--
<tr>
<th ><%:App%></th>
<th ><%:主机名%></th>
<th ><%:mac地址%></th>
<th><%:开始时间%></th>
<th><%:访问时长%></th>
<th><%:过滤状态%></th>
</tr>
-->
<tr>
<th ><%:编号%></th>
<th ><%:主机名%></th>
<th ><%:mac地址%></th>
<th ><%:ip地址%></th>
<th><%:常用APP(TOP5)%></th>
<th><%:在线状态%></th>
</tr>
<tr>
<td colspan="8"><em><br /><%:Collecting data...%></em></td>
Expand Down
1 change: 1 addition & 0 deletions open-app-filter/files/appfilter.init
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ service_triggers()

stop_service(){
killall -9 oafd
rmmod oaf
}

start_service(){
Expand Down
56 changes: 56 additions & 0 deletions open-app-filter/src/appfilter_ubus.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,16 @@ appfilter_handle_dev_list(struct ubus_context *ctx, struct ubus_object *obj,
struct json_object * root_obj = json_object_new_object();

struct json_object * dev_array = json_object_new_array();
int count = 0;
for (i = 0;i < MAX_DEV_NODE_HASH_SIZE; i++){

dev_node_t *node = dev_hash_table[i];
while(node){
if (node->online == 0){

node = node->next;
continue;
}
struct json_object * dev_obj = json_object_new_object();
struct json_object * app_array = json_object_new_array();
app_visit_time_info_t top5_app_list[5];
Expand All @@ -286,12 +292,62 @@ appfilter_handle_dev_list(struct ubus_context *ctx, struct ubus_object *obj,
char hostname[32] = {0};
get_hostname_by_mac(node->mac, hostname);
json_object_object_add(dev_obj, "ip", json_object_new_string(node->ip));

json_object_object_add(dev_obj, "online", json_object_new_int(1));
json_object_object_add(dev_obj, "hostname", json_object_new_string(hostname));
json_object_object_add(dev_obj, "latest_app", json_object_new_string("test"));
json_object_array_add(dev_array, dev_obj);

node = node->next;
count++;
if (count >= MAX_SUPPORT_DEV_NUM)
goto END;
}
}
for (i = 0;i < MAX_DEV_NODE_HASH_SIZE; i++){
dev_node_t *node = dev_hash_table[i];
while(node){
if (node->online != 0){

node = node->next;
continue;
}
struct json_object * dev_obj = json_object_new_object();
struct json_object * app_array = json_object_new_array();
app_visit_time_info_t top5_app_list[5];
memset(top5_app_list, 0x0, sizeof(top5_app_list));
update_top5_app(node, top5_app_list);

for (j = 0; j < 5; j++){
if (top5_app_list[j].app_id == 0)
break;
struct json_object * app_obj = json_object_new_object();
json_object_object_add(app_obj, "id", json_object_new_int(top5_app_list[j].app_id));
json_object_object_add(app_obj, "name", json_object_new_string(get_app_name_by_id(top5_app_list[j].app_id)));
json_object_array_add(app_array, app_obj);
}

json_object_object_add(dev_obj, "applist", app_array);
json_object_object_add(dev_obj, "mac", json_object_new_string(node->mac));
char hostname[32] = {0};
get_hostname_by_mac(node->mac, hostname);
json_object_object_add(dev_obj, "ip", json_object_new_string(node->ip));

json_object_object_add(dev_obj, "online", json_object_new_int(0));
json_object_object_add(dev_obj, "hostname", json_object_new_string(hostname));
json_object_object_add(dev_obj, "latest_app", json_object_new_string("test"));
json_object_array_add(dev_array, dev_obj);
node = node->next;
count++;
if (count >= MAX_SUPPORT_DEV_NUM)
goto END;
}
}


END:


json_object_object_add(root_obj, "devlist", dev_array);
blob_buf_init(&b, 0);
blobmsg_add_object(&b, root_obj);
Expand Down
77 changes: 74 additions & 3 deletions open-app-filter/src/appfilter_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ unsigned int hash_mac(unsigned char *mac)
else
return mac[0] & (MAX_DEV_NODE_HASH_SIZE - 1);
}
int get_timestamp(void){
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
return cur_time.tv_sec;
}

int hash_appid(int appid){
return appid % (MAX_VISIT_HASH_SIZE - 1);
Expand All @@ -62,6 +67,8 @@ void init_dev_node_htable(){
}
}



dev_node_t *add_dev_node(char *mac){
unsigned int hash = 0;
if (g_cur_user_num >= MAX_SUPPORT_USER_NUM){
Expand All @@ -77,7 +84,8 @@ dev_node_t *add_dev_node(char *mac){
if (!node)
return NULL;
strncpy(node->mac, mac, sizeof(node->mac));

node->online = 1;
node->online_time = get_timestamp();
if (dev_hash_table[hash] == NULL)
dev_hash_table[hash] = node;
else{
Expand Down Expand Up @@ -154,20 +162,83 @@ void update_dev_hostname(void){
fclose(fp);
}

void clean_dev_online_status(void){
int i;
for (i = 0;i < MAX_DEV_NODE_HASH_SIZE; i++){
dev_node_t *node = dev_hash_table[i];
while(node){
node->online = 0;
node->offline_time = get_timestamp();
node = node->next;
}
}
}

/*
Id Mac Ip
1 10:bf:48:37:0c:94 192.168.66.244
*/
void check_dev_expire(void){
char line_buf[256] = {0};
char mac_buf[32] = {0};
char ip_buf[32] = {0};
printf("check dev expire...\n");
FILE *fp = fopen("/proc/net/af_client", "r");
if (!fp){
printf("open dev file....failed\n");
return;
}
while(fgets(line_buf, sizeof(line_buf), fp)){
if (strlen(line_buf) <= 16)
continue;
sscanf(line_buf, "%*s %s %s", mac_buf, ip_buf);
printf("mac = %s, ip = %s\n", mac_buf, ip_buf);
dev_node_t *node = find_dev_node(mac_buf);
if (!node)
continue;
node->online = 1;
printf("node is online, mac = %s\n" , node->mac);
}
fclose(fp);

}
void dump_dev_list(void){
int i, j;
int count = 0;
clean_dev_online_status();
update_dev_hostname();
check_dev_expire();
FILE *fp = fopen(OAF_DEV_LIST_FILE, "w");
if (!fp){
return;
}

fprintf(fp, "%-4s %-20s %-20s %-32s\n", "Id", "Mac Addr", "Ip Addr", "Hostname");
fprintf(fp, "%-4s %-20s %-20s %-32s %-8s\n", "Id", "Mac Addr", "Ip Addr", "Hostname", "Online");
for (i = 0;i < MAX_DEV_NODE_HASH_SIZE; i++){
dev_node_t *node = dev_hash_table[i];
while(node){
fprintf(fp, "%-4d %-20s %-20s %-32s\n", i + 1, node->mac, node->ip, node->hostname);
if (node->online != 0){
fprintf(fp, "%-4d %-20s %-20s %-32s %-8d\n",
i + 1, node->mac, node->ip, node->hostname, node->online);
count++;
}
if (count >= MAX_SUPPORT_DEV_NUM){
goto EXIT;
}
node = node->next;
}
}

for (i = 0;i < MAX_DEV_NODE_HASH_SIZE; i++){
dev_node_t *node = dev_hash_table[i];
while(node){
if (node->online == 0){
fprintf(fp, "%-4d %-20s %-20s %-32s %-8d\n",
i + 1, node->mac, node->ip, node->hostname, node->online);
}
if (count >= MAX_SUPPORT_DEV_NUM)
goto EXIT;
node = node->next;
}
}
Expand Down
5 changes: 5 additions & 0 deletions open-app-filter/src/appfilter_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#define MAX_VISITLIST_DUMP_NUM 16
#define MAX_APP_TYPE 16
#define MAX_APP_ID_NUM 128
#define MAX_SUPPORT_DEV_NUM 64

//extern dev_node_t *dev_hash_table[MAX_DEV_NODE_HASH_SIZE];

/*
Expand Down Expand Up @@ -71,6 +73,9 @@ typedef struct dev_node{
char mac[MAX_MAC_LEN];
char ip[MAX_IP_LEN];
char hostname[MAX_HOSTNAME_SIZE];
int online;
int offline_time;
int online_time;
visit_info_t *visit_htable[MAX_VISIT_HASH_SIZE];
visit_stat_t stat[MAX_APP_TYPE][MAX_APP_ID_NUM];
struct dev_node *next;
Expand Down

0 comments on commit af168f6

Please sign in to comment.