1
2module("luci.controller.appfilter", package.seeall)
3local utl = require "luci.util"
4
5function index()
6	if not nixio.fs.access("/etc/config/appfilter") then
7		return
8	end
9	
10	local page
11	entry({"admin", "services", "appfilter"}, 
12	alias("admin", "services", "appfilter", "user_list"),
13		_("App Filter"), 20).dependent = true
14
15	entry({"admin", "services", "appfilter", "user_list"}, 
16		arcombine(cbi("appfilter/user_list",{hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), 
17		cbi("appfilter/dev_status", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true})),
18		_("User List"), 21).leaf=true
19
20	entry({"admin", "services", "appfilter", "base_setting"}, 
21        cbi("appfilter/base_setting"), _("Basic Settings"), 22).leaf=true
22
23	entry({"admin", "services", "appfilter", "user_setting"}, 
24		cbi("appfilter/user_setting"), _("Effective User"), 23).leaf=true
25
26	entry({"admin", "services", "appfilter", "time_setting"}, 
27		cbi("appfilter/time_setting"), _("Effective Time"), 24).leaf=true
28
29	entry({"admin", "services", "appfilter", "feature"}, 
30		cbi("appfilter/feature", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), _("App Feature"), 25).leaf=true
31
32	page = entry({"admin", "network", "user_status"}, call("user_status"), nil)
33	page.leaf = true
34
35	page = entry({"admin", "network", "dev_app_status"}, call("dev_app_status"), nil)
36	page.leaf = true
37
38	page = entry({"admin", "network", "dev_visit_list"}, call("get_dev_visit_list"), nil)
39	page.leaf = true
40
41	page = entry({"admin", "network", "feature_upgrade"}, call("handle_feature_upgrade"), nil)
42	page.leaf = true
43
44	page = entry({"admin", "network", "dev_visit_time"}, call("get_dev_visit_time"), nil)
45	page.leaf = true
46	page = entry({"admin", "network", "app_class_visit_time"}, call("get_app_class_visit_time"), nil)
47	page.leaf = true
48end
49
50function get_hostname_by_mac(dst_mac)
51    leasefile="/tmp/dhcp.leases"
52    local fd = io.open(leasefile, "r")
53	if not fd then return end
54    while true do
55        local ln = fd:read("*l")
56        if not ln then
57            break
58        end
59        local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
60        if  dst_mac == mac then
61            fd:close()
62            return name
63        end
64    end
65	fd:close()
66    return ""
67end
68
69
70function handle_feature_upgrade()
71	local fs = require "nixio.fs"
72	local http = require "luci.http"
73	local image_tmp = "/tmp/feature.cfg"
74
75	local fp
76	http.setfilehandler(
77		function(meta, chunk, eof)
78	
79			fp = io.open(image_tmp, "w")
80			
81			if fp and chunk then
82				fp:write(chunk)
83			end
84			if fp and eof then
85				fp:close()
86			end
87		end
88	)
89
90
91end
92
93function get_app_name_by_id(appid)
94	local class_fd = io.popen("find /tmp/appfilter/ -type f -name *.class |xargs cat |grep "..appid.."|awk '{print $2}'")
95	if class_fd then
96		local name = class_fd:read("*l")
97		class_fd:close()
98		return name
99	end
100	return ""
101end
102
103function cmp_func(a,b)
104	return a.latest_time > b.latest_time
105end
106
107
108function user_status()
109	local json = require "luci.jsonc"
110	luci.http.prepare_content("application/json")
111	local fd = io.open("/proc/net/af_client","r")
112	status_buf=fd:read('*a')
113	fd:close()
114	user_array=json.parse(status_buf)
115	
116	local visit_obj=utl.ubus("appfilter", "visit_list", {});
117	local user_array=visit_obj.dev_list
118	local history={}
119	for i, v in pairs(user_array) do
120		visit_array=user_array[i].visit_info
121		for j,s in pairs(visit_array) do
122			print(user_array[i].mac, user_array[i].ip,visit_array[j].appid, visit_array[j].latest_time)
123			total_time=visit_array[j].latest_time - visit_array[j].first_time;
124			history[#history+1]={
125				mac=user_array[i].mac,
126				ip=user_array[i].ip,
127				hostname=get_hostname_by_mac(user_array[i].mac),
128				appid=visit_array[j].appid,
129				appname=get_app_name_by_id(visit_array[j].appid),
130				total_num=0,
131				drop_num=0,
132				latest_action=visit_array[j].latest_action,
133				latest_time=os.date("%Y/%m/%d %H:%M:%S", visit_array[j].latest_time),
134				first_time=os.date("%Y/%m/%d %H:%M:%S", visit_array[j].first_time),
135				total_time=total_time
136			}
137		end
138	end
139	table.sort(history, cmp_func)
140	luci.http.write_json(history);
141end
142
143
144function dev_app_status()
145	local json = require "luci.jsonc"
146	luci.http.prepare_content("application/json")
147	local visit_obj=utl.ubus("appfilter", "dev_list", {});
148	luci.http.write_json(visit_obj);
149end
150
151
152function get_dev_visit_time(mac)
153	local json = require "luci.jsonc"
154	luci.http.prepare_content("application/json")
155	local fd = io.open("/proc/net/af_client","r")
156	status_buf=fd:read('*a')
157	fd:close()
158	user_array=json.parse(status_buf)
159	local req_obj = {}
160	req_obj.mac = mac;
161	local visit_obj=utl.ubus("appfilter", "dev_visit_time", req_obj);
162	local user_array=visit_obj.app_list
163	luci.http.write_json(user_array);
164end
165
166function get_app_class_visit_time(mac)
167	local json = require "luci.jsonc"
168	luci.http.prepare_content("application/json")
169	local req_obj = {}
170	req_obj.mac = mac;
171	local visit_obj=utl.ubus("appfilter", "app_class_visit_time", req_obj);
172	local class_array=visit_obj.class_list
173	luci.http.write_json(class_array);
174end
175
176
177function get_dev_visit_list(mac)
178	local json = require "luci.jsonc"
179	luci.http.prepare_content("application/json")
180	local req_obj = {}
181	req_obj.mac = mac;
182
183	local visit_obj=utl.ubus("appfilter", "visit_list", req_obj);
184	local user_array=visit_obj.dev_list
185	local history={}
186	for i, v in pairs(user_array) do
187		visit_array=user_array[i].visit_info
188		for j,s in pairs(visit_array) do
189			print(user_array[i].mac, user_array[i].ip,visit_array[j].appid, visit_array[j].latest_time)
190			total_time=visit_array[j].latest_time - visit_array[j].first_time;
191			history[#history+1]={
192				mac=user_array[i].mac,
193				ip=user_array[i].ip,
194				hostname=get_hostname_by_mac(user_array[i].mac),
195				appid=visit_array[j].appid,
196				appname=get_app_name_by_id(visit_array[j].appid),
197				total_num=0,
198				drop_num=0,
199				latest_action=visit_array[j].latest_action,
200				latest_time=os.date("%Y/%m/%d %H:%M:%S", visit_array[j].latest_time),
201				first_time=os.date("%Y/%m/%d %H:%M:%S", visit_array[j].first_time),
202				total_time=total_time
203			}
204		end
205	end
206	table.sort(history, cmp_func)
207	luci.http.write_json(history);
208end
209