-
Notifications
You must be signed in to change notification settings - Fork 0
/
appid.js
328 lines (280 loc) · 9.32 KB
/
appid.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
popoverParserOptions = {"trigger": "click"}
$('#parser').popover(popoverParserOptions)
function valueEnd(ctx) {
const value = ctx.options.typed ? inferType(ctx.value) : ctx.value
ctx.entry.push(ctx.reviver(value, ctx.row, ctx.col))
ctx.value = ''
ctx.col++
}
function entryEnd(ctx) {
ctx.output.push(ctx.entry)
ctx.entry = []
ctx.row++
ctx.col = 1
}
function inferType(value) {
const isNumber = /.\./
switch (true) {
case value === 'true':
case value === 'false':
return value === 'true'
case isNumber.test(value):
return parseFloat(value)
case isFinite(value):
return parseInt(value)
default:
return value
}
}
function parse(csv, options, reviver = v => v) {
const ctx = Object.create(null)
ctx.options = options || {}
ctx.reviver = reviver
ctx.value = ''
ctx.entry = []
ctx.output = []
ctx.col = 1
ctx.row = 1
const lexer = /"|,|\r\n|\n|\r|[^",\r\n]+/y
const isNewline = /^(\r\n|\n|\r)$/
let matches = []
let match = ''
let state = 0
while ((matches = lexer.exec(csv)) !== null) {
match = matches[0]
switch (state) {
case 0: // start of entry
switch (true) {
case match === '"':
state = 3
break
case match === ',':
state = 0
valueEnd(ctx)
break
case isNewline.test(match):
state = 0
valueEnd(ctx)
entryEnd(ctx)
break
default:
ctx.value += match
state = 2
break
}
break
case 2: // un-delimited input
switch (true) {
case match === ',':
state = 0
valueEnd(ctx)
break
case isNewline.test(match):
state = 0
valueEnd(ctx)
entryEnd(ctx)
break
default:
state = 4
throw Error(`CSVError: Illegal state [row:${ctx.row}, col:${ctx.col}]`)
}
break
case 3: // delimited input
switch (true) {
case match === '"':
state = 4
break
default:
state = 3
ctx.value += match
break
}
break
case 4: // escaped or closing delimiter
switch (true) {
case match === '"':
state = 3
ctx.value += match
break
case match === ',':
state = 0
valueEnd(ctx)
break
case isNewline.test(match):
state = 0
valueEnd(ctx)
entryEnd(ctx)
break
default:
throw Error(`CSVError: Illegal state [row:${ctx.row}, col:${ctx.col}]`)
}
break
}
}
// flush the last value
if (ctx.entry.length !== 0) {
valueEnd(ctx)
entryEnd(ctx)
}
return ctx.output
}
function csv2text(csv) {
var parsed;
parsed = parse(csv)
return parsed
}
var csvArray;
var applicationsMap;
var fr = new FileReader();
fr.onload = function () {
csvArray = (csv2text(fr.result));
//console.log(csvArray)
}
document.getElementById('inputfile')
.addEventListener('change', function () {
fr.readAsText(this.files[0]);
//you can read the output in fr.result
})
function parseReport(csvArray) {
var ruleHeader;
var appHeader;
for (var j = 0; j < csvArray[0].length; j++) {
if (csvArray[0][j] == "Rule") {
ruleHeader = j
}
if (csvArray[0][j] == "Application") {
appHeader = j
}
}
var app;
const appMap = new Map();
var blacklist = document.getElementById("blacklist").value.split(" ");
const blacklistSet = new Set(blacklist);
//skip header i=1
for (var i = 1; i < csvArray.length; i++) {
app = csvArray[i][appHeader]
rule = csvArray[i][ruleHeader]
if (blacklistSet.has(app)) {
continue
}
if (appMap.has(rule)) {
appArray = appMap.get(rule).add(app)
}
else {
const appSet = new Set();
appSet.add(app)
appMap.set(rule, appSet)
}
}
// appMap now has map of rule -> [app1, app2..]
applicationsMap = appMap
}
var RuleBase;
var fr2 = new FileReader();
fr2.onload = function () {
var csvRulebase = (csv2text(fr2.result));
//console.log(csvArray)
RuleBase = csvRulebase
}
document.getElementById('rulebase')
.addEventListener('change', function () {
fr2.readAsText(this.files[0]);
//you can read the output in fr.result
})
document.getElementById("submitbutton").addEventListener("click", createSetCommands);
document.getElementById("copybutton").addEventListener("click", copyText);
function copyText() {
// Get the text field
var copyText = document.getElementById("output");
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.value);
}
function createSetCommands() {
parseReport(csvArray);
if (RuleBase == null || applicationsMap == null) {
//todo
return
}
var nameHeader;
var applicationColumn;
var LocationColumn;
var device_group = document.getElementById("dg").value;
var rulebase_stored = document.getElementById("rulebase_store").value;
//get columns for parsing
for (var j = 0; j < RuleBase[0].length; j++) {
if (RuleBase[0][j] == "Name") {
nameHeader = j
}
if (RuleBase[0][j] == "Application") {
applicationColumn = j
}
if (RuleBase[0][j] == "Location") {
LocationColumn = j
}
}
var rulesObject = {}
var rulenamesArray = [];
//skip header i=1
for (var i = 1; i < RuleBase.length; i++) {
//filter rules not in device group!
if (RuleBase[i][LocationColumn] != device_group){
continue;
}
//add Rule names to rulenamesArray
rulenamesArray.push(RuleBase[i][nameHeader])
rulesObject[RuleBase[i][nameHeader]] = {
Application: RuleBase[i][applicationColumn]
}
}
const rulenamesSet = new Set(rulenamesArray);
var output = "";
output += `set shared tag "Appid-Old"\n`
output += `set shared tag "Appid-New"\n`
for (let [rulename, applications] of applicationsMap) {
//console.log(key + " = " + value);
applications = Array.from(applications);
if (rulename.startsWith("[Disabled]")) {
continue
}
if (!(rulenamesSet.has(rulename))) {
continue
}
var appid;
appid = rulesObject[rulename]["Application"]
if (appid != "any") {
continue
}
var rule_suffix;
rule_suffix = document.getElementById("suffix").value
if (rulename.endsWith(rule_suffix)) {
continue
}
var new_rulename;
var max_length = 63 - rule_suffix.length;
new_rulename = rulename.slice(0, max_length) + rule_suffix
var new_rule = 1;
if (rulenamesSet.has(new_rulename)) {
new_rule = 0;
appid_old = rulesObject[new_rulename]["Application"].split(";")
applications.push(...appid_old)
}
applications = applications.join(" ")
var copy_command = `copy device-group ${device_group} ${rulebase_stored} security rules "${rulename}" to "${new_rulename}`.repeat(new_rule)
var move_command = `move device-group ${device_group} ${rulebase_stored} security rules "${new_rulename}" before "${rulename}"`.repeat(new_rule)
var removeany_command = `delete device-group ${device_group} ${rulebase_stored} security rules "${new_rulename}" application any`
var addApps_command = `set device-group ${device_group} ${rulebase_stored} security rules "${new_rulename}" application [ ${applications} ]`
var addtag1 = `set device-group ${device_group} ${rulebase_stored} security rules "${rulename}" tag "Appid-Old"`
var addtag2 = `set device-group ${device_group} ${rulebase_stored} security rules "${new_rulename}" tag "Appid-New"`
output += `${copy_command}\n`
output += `${move_command}\n`
output += `${removeany_command}\n`
output += `${addApps_command}\n`
output += `${addtag1}\n`
output += `${addtag2}\n`
}
const element = document.getElementById("output")
element.innerHTML = `${output}`
}