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
|
'use strict';
'require view';
'require form';
'require fs';
'require ui';
/*
include custom CSS
*/
document.querySelector('head').appendChild(E('link', {
'rel': 'stylesheet',
'type': 'text/css',
'href': L.resource('view/banip/custom.css')
}));
/*
observe DOM changes
*/
const observer = new MutationObserver(function (mutations) {
if (mutations) {
const inputs = document.querySelectorAll('input');
inputs.forEach(function (input) {
input.setAttribute('autocomplete', 'off')
input.setAttribute('autocorrect', 'off')
input.setAttribute('autocapitalize', 'off')
input.setAttribute('spellcheck', false)
})
const labels = document.querySelectorAll('label[for^="widget.cbid.json"][for$="name"]');
labels.forEach(function (label) {
label.setAttribute("style", "font-weight: bold !important; color: #595 !important;");
})
L.resolveDefault(fs.stat('/etc/banip/banip.custom.feeds'), '').then(function (stat) {
const buttons = document.querySelectorAll('#btnClear, #btnCreate, #btnSave, #btnUpload, #btnDownload');
if (buttons[1] && buttons[2] && stat.size === 0) {
buttons[1].removeAttribute('disabled');
buttons[2].removeAttribute('disabled');
} else if (buttons[0] && buttons[3] && buttons[4] && stat.size > 0) {
buttons[0].removeAttribute('disabled');
buttons[3].removeAttribute('disabled');
buttons[4].removeAttribute('disabled');
}
});
}
});
const targetNode = document.getElementById('view');
const observerConfig = {
childList: true,
subtree: true,
attributes: false,
characterData: false
};
observer.observe(targetNode, observerConfig);
/*
button handling
*/
function handleEdit(ev) {
if (ev === 'upload') {
return ui.uploadFile('/etc/banip/banip.custom.feeds').then(function () {
L.resolveDefault(fs.read_direct('/etc/banip/banip.custom.feeds', 'json'), "").then(function (data) {
if (data) {
let dataLength = Object.keys(data).length || 0;
if (dataLength > 0) {
for (let i = 0; i < dataLength; i++) {
let feed = Object.keys(data)[i];
let descr = data[feed].descr;
if (feed && descr) {
continue;
}
fs.write('/etc/banip/banip.custom.feeds', null).then(function () {
ui.addNotification(null, E('p', _('Upload of the custom feed file failed.')), 'error');
});
return;
}
} else {
fs.write('/etc/banip/banip.custom.feeds', null).then(function () {
ui.addNotification(null, E('p', _('Upload of the custom feed file failed.')), 'error');
});
return;
}
location.reload();
} else {
fs.write('/etc/banip/banip.custom.feeds', null).then(function () {
ui.addNotification(null, E('p', _('Upload of the custom feed file failed.')), 'error');
});
}
});
}).catch(function () { });
}
if (ev === 'download') {
return fs.read_direct('/etc/banip/banip.custom.feeds', 'blob').then(function (blob) {
let url = window.URL.createObjectURL(blob),
date = new Date(),
name = 'banip.custom.feeds_%04d-%02d-%02d.json'.format(date.getFullYear(), date.getMonth() + 1, date.getDate()),
link = E('a', { 'style': 'display:none', 'href': url, 'download': name });
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
}).catch(function () { });
}
if (ev === 'create') {
return fs.read_direct('/etc/banip/banip.feeds', 'json').then(function (content) {
fs.write('/etc/banip/banip.custom.feeds', JSON.stringify(content)).then(function () {
location.reload();
});
});
}
if (ev === 'clear') {
return fs.write('/etc/banip/banip.custom.feeds', null).then(function () {
location.reload();
});
}
if (ev === 'save') {
const invalid = document.querySelectorAll('.cbi-input-invalid');
if (invalid.length > 0) {
document.body.scrollTop = document.documentElement.scrollTop = 0;
return ui.addNotification(null, E('p', _('Invalid input values, unable to save modifications.')), 'error');
}
}
let sumSubElements = [], exportJson;
const nodeKeys = document.querySelectorAll('[id^="widget.cbid.json"][id$="name"]');
for (let i = 0; i < nodeKeys.length; i++) {
let subElements = {};
let elements = document.querySelectorAll('[id^="widget.cbid.json.' + nodeKeys[i].id.split('.')[3] + '\."]');
for (const element of elements) {
let key = element.id.split('.')[4];
let value = element.value || "";
if (value === "") {
continue;
}
switch (key) {
case 'url_4':
subElements.url_4 = value;
break;
case 'rule_4':
subElements.rule_4 = value;
break;
case 'url_6':
subElements.url_6 = value;
break;
case 'rule_6':
subElements.rule_6 = value;
break;
case 'descr':
subElements.descr = value;
break;
case 'flag':
subElements.flag = value;
break;
}
}
if (nodeKeys[i].value !== "" && subElements.descr !== "") {
sumSubElements.push(nodeKeys[i].value, subElements);
}
}
if (sumSubElements.length > 0) {
exportJson = JSON.stringify(sumSubElements).replace(/^\[/, '{\n').replace(/\}]$/, '\n\t}\n}\n').replace(/,{"/g, ':{\n\t"').replace(/"},"/g, '"\n\t},\n"').replace(/","/g, '",\n\t"');
}
return fs.write('/etc/banip/banip.custom.feeds', exportJson).then(function () {
location.reload();
});
}
return view.extend({
load: function () {
return L.resolveDefault(fs.read_direct('/etc/banip/banip.custom.feeds', 'json'), "");
},
render: function (data) {
let m, s, o, feed, url_4, url_6, rule_4, rule_6, descr, flag;
m = new form.JSONMap(data, _('Custom Feed Editor'), _('With this editor you can upload your local custom feed file or fill up an initial one (a 1:1 copy of the version shipped with the package). \
The file is located at \'/etc/banip/banip.custom.feeds\'. \
Then you can edit this file, delete entries, add new ones or make a local backup. To go back to the maintainers version just empty the custom feed file again (do not delete it!).'));
for (let i = 0; i < Object.keys(m.data.data).length; i++) {
feed = Object.keys(m.data.data)[i];
url_4 = m.data.data[feed].url_4;
rule_4 = m.data.data[feed].rule_4;
url_6 = m.data.data[feed].url_6;
rule_6 = m.data.data[feed].rule_6;
descr = m.data.data[feed].descr;
flag = m.data.data[feed].flag;
s = m.section(form.TypedSection, feed, null);
s.addremove = true;
s.anonymous = true;
o = s.option(form.Value, 'name', _('Feed Name'));
o.ucioption = '.name';
o.datatype = 'and(minlength(3),maxlength(15))';
o.validate = function (section_id, value) {
if (!value) {
return _('Empty field not allowed');
}
if (!value.match(/^[a-z0-9]+$/)) {
return _('Invalid characters');
}
return true;
}
o = s.option(form.Value, 'url_4', _('URLv4'));
o.validate = function (section_id, value) {
if (!value) {
return true;
}
if (!value.match(/^(http:\/\/|https:\/\/)[A-Za-z0-9\/\.\-\?\&\+_@%=:~#]+$/)) {
return _('Protocol/URL format not supported');
}
return true;
}
o = s.option(form.Value, 'rule_4', _('Rulev4'));
o = s.option(form.Value, 'url_6', _('URLv6'));
o.validate = function (section_id, value) {
if (!value) {
return true;
}
if (!value.match(/^(http:\/\/|https:\/\/)[A-Za-z0-9\/\.\-\?\&\+_@%=:~#]+$/)) {
return _('Protocol/URL format not supported');
}
return true;
}
o = s.option(form.Value, 'rule_6', _('Rulev6'));
o = s.option(form.Value, 'descr', _('Description'));
o.datatype = 'and(minlength(3),maxlength(30))';
o.validate = function (section_id, value) {
if (!value) {
return _('Empty field not allowed');
}
return true;
}
o = s.option(form.Value, 'flag', _('Flag'));
o.datatype = 'and(minlength(2),maxlength(2))';
o.validate = function (section_id, value) {
if (!value) {
return true;
}
if (!value.match(/^gz$/)) {
return _('Flag not supported');
}
return true;
}
}
s = m.section(form.NamedSection, 'global');
s.render = L.bind(function () {
return E('div', { class: 'right' }, [
E('button', {
'class': 'btn cbi-button cbi-button-action',
'id': 'btnDownload',
'disabled': 'disabled',
'click': ui.createHandlerFn(this, function () {
return handleEdit('download');
})
}, [_('Download Custom Feeds')]),
'\xa0\xa0\xa0',
E('button', {
'class': 'btn cbi-button cbi-button-action',
'id': 'btnUpload',
'disabled': 'disabled',
'click': ui.createHandlerFn(this, function () {
return handleEdit('upload');
})
}, [_('Upload Custom Feeds')]),
'\xa0\xa0\xa0\xa0\xa0\xa0',
E('button', {
'class': 'btn cbi-button cbi-button-action important',
'id': 'btnCreate',
'disabled': 'disabled',
'click': ui.createHandlerFn(this, function () {
return handleEdit('create');
})
}, [_('Fill Custom Feeds')]),
'\xa0\xa0\xa0',
E('button', {
'class': 'btn cbi-button cbi-button-negative important',
'id': 'btnClear',
'disabled': 'disabled',
'click': ui.createHandlerFn(this, function () {
return handleEdit('clear');
})
}, [_('Clear Custom Feeds')]),
'\xa0\xa0\xa0',
E('button', {
'class': 'btn cbi-button cbi-button-positive important',
'id': 'btnSave',
'disabled': 'disabled',
'click': ui.createHandlerFn(this, function () {
return handleEdit('save');
})
}, [_('Save Custom Feeds')])
])
});
return m.render();
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});
|