-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
497 lines (439 loc) · 20.6 KB
/
index.html
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<title>pfSense FW Rules Manager - CSV/Google Sheet</title>
<style>
#menuBar {
width: 100%;
text-align: center;
}
#menuBar td {
width: 50%;
}
#menuBar input {
width: 100%;
}
table.wideTable {
width: 100%;
border: 1px solid black;
border-collapse: collapse;
}
table table {
border-collapse: collapse;
}
table.wideTable th {
white-space: nowrap;
padding: 5px;
text-align: right;
border: 1px solid black;
border-collapse: collapse;
}
table.wideTable td {
width: 100%;
padding: 5px;
border: 1px solid black;
border-collapse: collapse;
}
textarea,
input[type='text'] {
background-color: #ffffdd;
width: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
border: 1px solid black;
padding: 5px;
}
select {
background-color: #ffffdd;
border: 1px solid black;
padding: 5px;
}
textarea {
height: 200px;
}
.center {
text-align: center;
}
.toggleRow {
/* background-color: black; */
display: none;
}
/* .toggleRow td * {
visibility: hidden;
display: none;
} */
table.show_fromSource_xmlFile #tableRow_fromSource_xmlFile.toggleRow,
table.show_fromSource_xmlInput #tableRow_fromSource_xmlInput.toggleRow,
table.show_destinationType_googleSheet #tableRow_destinationType_googleSheet.toggleRow,
table.show_destinationType_googleSheet.show_googleSheetType_existing #tableRow_googleSheetType_existing.toggleRow,
table.show_fromSource_googleSheet #tableRow_fromSource_googleSheet.toggleRow,
table.show_fromSource_csvFile #tableRow_fromSource_csvFile.toggleRow,
table.show_fromSource_csvInput #tableRow_fromSource_csvInput.toggleRow {
/* background-color: inherit; */
display: table-row;
}
/* table.show_fromSource_xmlFile #tableRow_fromSource_xmlFile.toggleRow td *,
table.show_fromSource_xmlInput #tableRow_fromSource_xmlInput.toggleRow td *,
table.show_destinationType_googleSheet #tableRow_destinationType_googleSheet.toggleRow td *,
table.show_destinationType_googleSheet.show_googleSheetType_existing #tableRow_googleSheetType_existing.toggleRow td *,
table.show_fromSource_googleSheet #tableRow_fromSource_googleSheet.toggleRow td *,
table.show_fromSource_csvFile #tableRow_fromSource_csvFile.toggleRow td *,
table.show_fromSource_csvInput #tableRow_fromSource_csvInput.toggleRow td * {
visibility: inherit;
display: inherit;
} */
table td.log table {
font-size: 8pt;
font-family: 'Courier New', Courier, monospace;
border: 1px solid black;
background-color: #dddddd;
width: 100%;
border-collapse: collapse;
}
table td.log table + table
{
margin-top: 20px;
}
table td.log table td {
width: 0;
white-space: nowrap;
}
table td.log table td+td {
width: 100%;
}
#XMLTo,
#toXML {
display: none;
}
body.show_section_XMLTo #XMLTo,
body.show_section_toXML #toXML {
display: inherit;
}
.bold
{
font-weight: bold;
}
.error {
color: red;
}
input[type='button'],
input[type='submit']
{
padding: 5px;
background-color: #ddffdd;
border: 1px solid black;
cursor: pointer;
}
</style>
<script type="text/javascript">
// show a section and hide the rest
function showSection(button)
{
var body = document.body;
var showClassPrefix = "show_section_";
var showClass = showClassPrefix + button.name;
// hide all the other sections
body.className = body.className.replace(new RegExp(showClassPrefix + "[^\\s]+", "g"), "");
// show this section
body.classList.add(showClass);
}
// show a table row, hide the rest
function showRow(radioInput)
{
// find the closest table
var parentTable = radioInput.closest("table");
// prefix for other rows in the same group
var showClassPrefix = "show_" + radioInput.name + "_";
// the class we want to add to the table to show
var showClass = showClassPrefix + radioInput.value;
// hide all the other rows
parentTable.className = parentTable.className.replace(new RegExp(showClassPrefix + "[^\\s]+", "g"), "");
// show this row
parentTable.classList.add(showClass);
}
// add a log row
function addToLog(table, message, isError)
{
var row = table.insertRow(-1);
if(isError) row.classList.add("error");
row.insertCell(-1).innerText = (new Date()).toLocaleString();
row.insertCell(-1).innerHTML = message;
}
function copyTextarea(a)
{
navigator.clipboard.writeText(a.parentElement.querySelector("textarea").value);
}
function disableEnableFormElements(formObject, disable)
{
var elements = formObject.elements;
for(var i = 0, numElements = elements.length; i < numElements; ++i)
{
elements[i].disabled = disable;
}
}
// process submission for XML to ...
function submit_XMLTo(formObject)
{
// get the elements
var formElements = formObject.elements;
// validate we have a valid source
switch(formElements.fromSource.value)
{
case "xmlFile": if(!formElements.xmlFile.value) {alert("ERROR: Please select a file."); return;}; break;
case "xmlInput": if(!formElements.xmlText.value) {alert("ERROR: Please enter the XML."); return;}; break;
default: alert("ERROR: Please select a from."); return;
}
// validate we have a valid destination
if(!formElements.destinationType.value)
{
alert("ERROR: Please select a destination type.");
return;
}
else if(formElements.destinationType.value == "googleSheet")
{
if(!formElements.googleSheetType.value)
{
alert("ERROR: Please select a Google Sheet type.");
return;
}
else if(formElements.googleSheetType.value == "existing" && !formElements.existingSheetURL.value.trim().match(/^https:\/\/.+?google.com.+?\/[-\w]{25,}\//))
{
alert("ERROR: Please enter a valid Google Sheet URL.");
return;
}
}
var logTable = document.createElement("table");
formObject.querySelector(".log").appendChild(logTable);
addToLog(logTable, "working...");
// send the data to GAS and do it
google.script.run.withSuccessHandler(ret =>
{
if(!ret.success)
{
addToLog(logTable, "ERROR: " + ret.message, true);
}
else
{
addToLog(logTable, "...done");
if(formElements.destinationType.value == "googleSheet")
{
addToLog(logTable, '<a href="' + ret.data + '" target="_blank">' + ret.data + '</a>');
}
else
{
var logHTML = '<textarea disabled="true">' + ret.data + '</textarea>';
logHTML += '<br />';
logHTML += '<br />';
logHTML += '<a href="#" onclick="copyTextarea(this); return false">copy to clipboard</a>';
addToLog(logTable, logHTML);
}
}
}).importXML(formObject);
}
function updateSheets(sheetURLInput)
{
var formObject = sheetURLInput.closest("form");
var sheetNamesHolders = formObject.querySelector("#sheetNamesHolders");
var logTable = document.createElement("table");
formObject.querySelector(".log").appendChild(logTable);
var value = sheetURLInput.value.trim();
sheetNamesHolders.innerHTML = "";
if(!value.match(/^https:\/\/.+?google.com.+?\/([-\w]{25,})\//))
{
alert("ERROR: Please enter a valid Google Sheet URL.");
return;
}
disableEnableFormElements(formObject, true);
addToLog(logTable, "getting sheet names...");
google.script.run.withSuccessHandler(ret =>
{
if(!ret.success)
{
addToLog(logTable, "ERROR: " + ret.message, true);
}
else
{
var selectHTML = '<select name="sheetName"><option disabled selected value> -- select a sheet -- </option>';
for(var i = 0, numSheets = ret.names.length; i < numSheets; ++i)
{
selectHTML += '<option value="' + ret.names[i] + '">' + ret.names[i] + '</option>';
}
selectHTML += '</select>'
sheetNamesHolders.innerHTML = selectHTML;
addToLog(logTable, "...done");
}
disableEnableFormElements(formObject, false);
}).getSheetNames(value)
}
function submit_toXML(formObject)
{
var formElements = formObject.elements;
switch(formElements.fromSource.value)
{
case "googleSheet": if(!formElements.sheetName || !formElements.sheetName.value) {alert("ERROR: Please select a sheet."); return;}; break;
case "csvFile": if(!formElements.csvFile.value) {alert("ERROR: Please select a file."); return;}; break;
case "csvInput": if(!formElements.csvText.value) {alert("ERROR: Please enter the XML."); return;}; break;
default: alert("ERROR: Please select a from."); return;
}
var logTable = document.createElement("table");
formObject.querySelector(".log").appendChild(logTable);
addToLog(logTable, "working...");
// send the data to GAS and do it
google.script.run.withSuccessHandler(ret =>
{
if(!ret.success)
{
addToLog(logTable, "ERROR: " + ret.message, true);
}
else
{
addToLog(logTable, "...done");
var logHTML = '<textarea disabled="true">' + ret.xml + '</textarea>';
logHTML += '<br />';
logHTML += '<br />';
logHTML += '<a href="#" onclick="copyTextarea(this); return false">copy to clipboard</a>';
addToLog(logTable, logHTML);
}
}).exportXML(formObject);
}
</script>
</head>
<body>
<h1>pfSense FW Rules Manager - CSV/Google Sheet</h1>
<p>This web-app will let you:</p>
<ul>
<li>convert an XML export of pfSense's firewall rules into a CSV or Google Sheet spreadhseet</li>
<li>convert a CSV or Google Sheet spreadsheet of firewall rules into an XML file that pfSense can import</li>
</ul>
<p class="bold error">
Please read through <a href="https://github.com/imthenachoman/pfSense-Firewall-Rules-Manager" target="_blank">https://github.com/imthenachoman/pfSense-Firewall-Rules-Manager</a> for more details, including disclaimer/warnings.
</p>
<p>
This site looks ugly. Front-end is not my thing. If you want to make it prettier, please submit a pull request or <a target="_blank" href="https://github.com/imthenachoman/pfSense-Firewall-Rules-Manager#contact-support-help">contact me</a>.
</p>
<h2>Main Event</h2>
<table id="menuBar">
<tr>
<td><input type="button" value="XML to CSV/Google Sheet" name="XMLTo" onclick="showSection(this)" /></td>
<td><input type="button" value="CSV/Google Sheet to XML" name="toXML" onclick="showSection(this)" /></td>
</tr>
</table>
<div id="XMLTo">
<h3>XML to CSV/Google Sheet</h2>
<form onsubmit="submit_XMLTo(this); return false">
<table class="wideTable">
<tr>
<th>from</th>
<td>
<input type="radio" onchange="showRow(this)" name="fromSource" value="xmlFile" id="XMLTo_xmlFile" /><label for="XMLTo_xmlFile">XML file upload</label><br />
<input type="radio" onchange="showRow(this)" name="fromSource" value="xmlInput" id="XMLTo_xmlInput" /><label for="XMLTo_xmlInput">XML input</label><br />
</td>
</tr>
<tr id="tableRow_fromSource_xmlFile" class="toggleRow">
<th>select XML file</th>
<td>
<input name="xmlFile" type="file" />
</td>
</tr>
<tr id="tableRow_fromSource_xmlInput" class="toggleRow">
<th>enter XML</th>
<td>
<textarea name="xmlText"></textarea>
</td>
</tr>
<tr>
<th>destination type</th>
<td>
<input type="radio" onchange="showRow(this)" name="destinationType" value="googleSheet" id="XMLTo_googleSheet" /><label for="XMLTo_googleSheet">Google Sheet</label><br />
<input type="radio" onchange="showRow(this)" name="destinationType" value="csv" id="XMLTo_csv" /><label for="XMLTo_csv">CSV</label><br />
</td>
</tr>
<tr id="tableRow_destinationType_googleSheet" class="toggleRow">
<th>Google Sheet type</th>
<td>
<input type="radio" onchange="showRow(this)" name="googleSheetType" value="new" id="XMLTo_new" /><label for="XMLTo_new">new</label><br />
<input type="radio" onchange="showRow(this)" name="googleSheetType" value="existing" id="XMLTo_existing" /><label for="XMLTo_existing">existing</label><br />
</td>
</tr>
<tr id="tableRow_googleSheetType_existing" class="toggleRow">
<th>existing Google Sheet sheet URL</th>
<td>
<input type="text" name="existingSheetURL" />
</td>
</tr>
<tr>
<th>submit</th>
<td>
<input type="submit" value="do it" />
</td>
</tr>
<tr>
<th>log</th>
<td class="log"></td>
</tr>
</table>
</form>
</div>
<div id="toXML">
<h3>CSV/Google Sheet to XML</h2>
<p class="bold error">use with caution</p>
<ul class="bold error">
<li>save a backup copy</li>
<li>see <a target="_blank" href="https://github.com/imthenachoman/pfSense-Firewall-Rules-Manager#disclaimer-and-warnings">disclaimer and warnings</a></li>
</ul>
<form onsubmit="submit_toXML(this); return false">
<table class="wideTable">
<tr>
<th>from</th>
<td>
<input type="radio" onchange="showRow(this)" name="fromSource" value="googleSheet" id="toXML_googleSheet" /><label for="toXML_googleSheet">Google Sheet</label><br />
<input type="radio" onchange="showRow(this)" name="fromSource" value="csvFile" id="toXML_csvFile" /><label for="toXML_csvFile">CSV file upload</label><br />
<input type="radio" onchange="showRow(this)" name="fromSource" value="csvInput" id="toXML_csvInput" /><label for="toXML_csvInput">CSV input</label><br />
</td>
</tr>
<tr id="tableRow_fromSource_googleSheet" class="toggleRow">
<th>Google Sheet</th>
<td>
<table>
<tr>
<th>URL</th>
<td>
<input type="text" name="sheetURL" onchange="updateSheets(this)" />
</td>
</tr>
<tr>
<th>sheet</th>
<td id="sheetNamesHolders"></td>
</tr>
</table>
</td>
</tr>
<tr id="tableRow_fromSource_csvFile" class="toggleRow">
<th>select CSV file</th>
<td>
<input name="csvFile" type="file" />
</td>
</tr>
<tr id="tableRow_fromSource_csvInput" class="toggleRow">
<th>enter CSV</th>
<td>
<textarea name="csvText"></textarea>
</td>
</tr>
<tr>
<th>submit</th>
<td>
<input type="submit" value="do it" />
</td>
</tr>
<tr>
<th>log</th>
<td class="log"></td>
</tr>
</table>
</form>
</div>
</body>
</html>