File: get_ndpi_applications.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (170 lines) | stat: -rw-r--r-- 4,756 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
--
-- (C) 2013-22 - ntop.org
--

dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local protos_utils = require("protos_utils")
local page_utils = require("page_utils")

local json = require("dkjson")
sendHTTPContentTypeHeader('text/html')


-- Table parameters
local currentPage  = _GET["currentPage"]
local perPage      = _GET["perPage"]
local sortColumn   = _GET["sortColumn"]
local sortOrder    = _GET["sortOrder"]
local proto_filter = _GET["l7proto"]
local category_filter = _GET["category"]

local sortPrefs = "ndpi_application_category"
local custom_protos = protos_utils.parseProtosTxt()
local proto_to_num_rules = {}
local applications = interface.getnDPIProtocols()

for proto, rules in pairs(custom_protos) do
   proto_to_num_rules[proto] = #rules
end

local function makeApplicationHostsList(appname)
   local hosts_list = {}

   for _, rule in ipairs(custom_protos[appname] or {}) do
      hosts_list[#hosts_list + 1] = rule.value
   end

   return table.concat(hosts_list, ",")
end

if((sortColumn == nil) or (sortColumn == "column_"))then
   sortColumn = getDefaultTableSort(sortPrefs)
else
   if((sortColumn ~= "column_")
    and (sortColumn ~= "")) then
      tablePreferences("sort_"..sortPrefs,sortColumn)
   end
end

if(sortOrder == nil) then
   sortOrder = getDefaultTableSortOrder(sortPrefs)
else
   if((sortColumn ~= "column_")
    and (sortColumn ~= "")) then
      tablePreferences("sort_order_"..sortPrefs,sortOrder)
   end
end

if(currentPage == nil) then
   currentPage = 1
else
   currentPage = tonumber(currentPage)
end

if(perPage == nil) then
   perPage = getDefaultTableSize()
else
   perPage = tonumber(perPage)
   tablePreferences("rows_number", perPage)
end

interface.select(ifname)

if category_filter ~= nil and starts(category_filter, "cat_") then
   category_filter = split(category_filter, "cat_")[2]
end

local to_skip = (currentPage-1) * perPage

if(sortOrder == "desc") then sOrder = rev_insensitive else sOrder = asc_insensitive end

local sorter = {}
local num_apps = 0
for app_name, app_id in pairs(applications) do
   if app_name == "Unknown" then
      -- prevent the Unknown protocol from being re-assigned
      -- nDPI bug?
      goto continue
   end

   local cat = ntop.getnDPIProtoCategory(tonumber(app_id))

   if not isEmptyString(proto_filter) then
     if tostring(app_id) ~= proto_filter then
       goto continue
     end
   end

   if not isEmptyString(category_filter) then
      if tostring(cat.id) ~= category_filter then
       goto continue
      end
   end

   applications[app_name] = {app_name = app_name, app_id = app_id, cat = cat}
   num_apps = num_apps + 1

   if sortColumn == "column_" or sortColumn == "column_ndpi_application" then
      sorter[app_name] = app_name
   elseif sortColumn == "column_ndpi_application_category" then
      sorter[app_name] = cat["name"]
   elseif sortColumn == "column_num_hosts" then
      sorter[app_name] = proto_to_num_rules[app_name] or 0
   end

   ::continue::
end

local categories = interface.getnDPICategories()

local res_formatted = {}

if not isEmptyString(proto_filter) then
   num_apps = 1
end

local cur_num = 0
for app, _ in pairsByValues(sorter, sOrder) do
   app = applications[app]

   cur_num = cur_num + 1
   if cur_num <= to_skip then
      goto continue
   elseif cur_num > to_skip + perPage then
      break
   end

   local record = {}
   record["column_ndpi_application_id"] = tostring(app["app_id"])
   record["column_ndpi_application_category_id"] = tostring(app["cat"]["id"])
   record["column_ndpi_application"] = app["app_name"]
   record["column_num_hosts"] = proto_to_num_rules[app["app_name"]] or 0
   record["column_application_hosts"] = makeApplicationHostsList(app["app_name"])
   record["column_is_custom"] = ntop.isCustomApplication(tonumber(app["app_id"]))

   cat_select_dropdown = '<select class="form-select" style="width:320px;" name="proto_' .. app["app_id"] .. '">'
   local current_id = tostring(app["cat"]["id"])

   for cat_name, cat_id in pairsByKeys(categories, asc_insensitive) do
      cat_select_dropdown = cat_select_dropdown .. [[<option value="cat_]] ..cat_id .. [["]] ..
         ternary(cat_id == current_id, " selected", "") .. [[>]] ..
         getCategoryLabel(cat_name) .. [[</option>]]
   end
   cat_select_dropdown = cat_select_dropdown .. "</select>"

   record["column_ndpi_application_category"] = cat_select_dropdown

   res_formatted[#res_formatted + 1] = record
   ::continue::
end

local result = {}
result["perPage"] = perPage
result["currentPage"] = currentPage
result["totalRows"] = num_apps
result["data"] = res_formatted
result["sort"] = {{sortColumn, sortOrder}}

print(json.encode(result, nil))