Skip to content

Commit

Permalink
luci-app-banip: optimized external file loading
Browse files Browse the repository at this point in the history
* optimize loading of external config/feed files

Signed-off-by: Dirk Brenken <[email protected]>
  • Loading branch information
dibdot committed Jul 26, 2024
1 parent f71e9cb commit 7154efa
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ return view.extend({
/*
general settings tab
*/
o = s.taboption('general', form.DummyValue, '_sub');
o.rawhtml = true;
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>'
+ '<hr style="width: 200px; height: 1px;" />';

o = s.taboption('general', form.Flag, 'ban_enabled', _('Enabled'), _('Enable the banIP service.'));
o.rmempty = false;

Expand Down Expand Up @@ -339,7 +344,8 @@ return view.extend({
*/
o = s.taboption('advanced', form.DummyValue, '_sub');
o.rawhtml = true;
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>'
+ '<hr style="width: 200px; height: 1px;" />';

o = s.taboption('advanced', form.ListValue, 'ban_nicelimit', _('Nice Level'), _('The selected priority will be used for banIP background processing.'));
o.value('-20', _('Highest Priority'));
Expand Down Expand Up @@ -408,7 +414,8 @@ return view.extend({
*/
o = s.taboption('adv_chain', form.DummyValue, '_sub');
o.rawhtml = true;
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>'
+ '<hr style="width: 200px; height: 1px;" />';

o = s.taboption('adv_chain', form.ListValue, 'ban_nftpriority', _('Chain Priority'), _('Set the nft chain priority within the banIP table, lower values means higher priority.'));
o.value('10');
Expand Down Expand Up @@ -485,7 +492,8 @@ return view.extend({
*/
o = s.taboption('adv_set', form.DummyValue, '_sub');
o.rawhtml = true;
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>'
+ '<hr style="width: 200px; height: 1px;" />';

o = s.taboption('adv_set', form.ListValue, 'ban_nftpolicy', _('Set Policy'), _('Set the nft policy for banIP-related Sets.'));
o.value('memory', _('memory'));
Expand Down Expand Up @@ -513,22 +521,23 @@ return view.extend({
o.rmempty = true;

let feed, feeds, descr;
if (result[0]) {
try {
feeds = JSON.parse(result[0]);
} catch (e) {
feeds = "";
ui.addNotification(null, E('p', _('Unable to parse the custom feed file!')), 'error');
if (result && Object.keys(result).length) {
if (result[0]) {
try {
feeds = JSON.parse(result[0]);
} catch (e) {
ui.addNotification(null, E('p', _('Unable to parse the custom feed file!')), 'error');
}
}
} else if (result[1]) {
try {
feeds = JSON.parse(result[1]);
} catch (e) {
feeds = "";
ui.addNotification(null, E('p', _('Unable to parse the default feed file!')), 'error');
if (result[1] && (!feeds || (feeds && !Object.keys(feeds).length))) {
try {
feeds = JSON.parse(result[1]);
} catch (e) {
ui.addNotification(null, E('p', _('Unable to parse the default feed file!')), 'error');
}
}
}
if (feeds) {
if (feeds && Object.keys(feeds).length) {
o = s.taboption('adv_set', form.MultiValue, 'ban_blockinput', _('WAN-Input Chain'), _('Limit certain feeds to the WAN-Input chain.'));
o.value('allowlist', _('local allowlist'));
o.value('blocklist', _('local blocklist'));
Expand Down Expand Up @@ -565,7 +574,8 @@ return view.extend({
*/
o = s.taboption('adv_log', form.DummyValue, '_sub');
o.rawhtml = true;
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>'
+ '<hr style="width: 200px; height: 1px;" />';

o = s.taboption('adv_log', form.ListValue, 'ban_nftloglevel', _('NFT Log Level'), _('Set the syslog level for NFT logging.'));
o.value('emerg', _('emerg'));
Expand Down Expand Up @@ -645,7 +655,8 @@ return view.extend({
*/
o = s.taboption('adv_email', form.DummyValue, '_sub');
o.rawhtml = true;
o.default = '<em style="color:#37c;font-weight:bold;">' + _('To enable email notifications, set up the \'msmtp\' package and specify a vaild E-Mail receiver address.') + '</em>';
o.default = '<em style="color:#37c;font-weight:bold;">' + _('To enable email notifications, set up the \'msmtp\' package and specify a vaild E-Mail receiver address.') + '</em>'
+ '<hr style="width: 200px; height: 1px;" />';

o = s.taboption('adv_email', form.Flag, 'ban_mailnotification', _('E-Mail Notification'), _('Receive E-Mail notifications with every banIP run.'));
o.rmempty = true;
Expand All @@ -672,9 +683,11 @@ return view.extend({
*/
o = s.taboption('feeds', form.DummyValue, '_sub');
o.rawhtml = true;
o.default = '<em style="color:#37c;font-weight:bold;">' + _('External Blocklist Feeds') + '</em>';
o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service reload to take effect.') + '</em>'
+ '<hr style="width: 200px; height: 1px;" />'
+ '<em style="color:#37c;font-weight:bold;">' + _('External Blocklist Feeds') + '</em>';

if (feeds) {
if (feeds && Object.keys(feeds).length) {
o = s.taboption('feeds', form.MultiValue, 'ban_feed', _('Blocklist Feed'));
for (let i = 0; i < Object.keys(feeds).length; i++) {
feed = Object.keys(feeds)[i].trim();
Expand All @@ -686,26 +699,27 @@ return view.extend({
}

let err, ccode, rir, country, countries = [];
if (result[2]) {
if (result && Object.keys(result[2]).length) {
countries = result[2].trim().split('\n');

o = s.taboption('feeds', form.MultiValue, 'ban_country', _('Countries') + ' (<abbr title="Regional Internet Registries">RIR</abbr>)');
for (let i = 0; i < countries.length; i++) {
try {
ccode = countries[i].match(/^(\w+)\t/)[1].trim();
rir = countries[i].match(/^\w+\t(\w+)\t/)[1].trim();
country = countries[i].match(/^\w+\t\w+\t(.*$)/)[1].trim();
o.value(ccode, country + ' (' + rir + ')');
} catch (e) {
countries[i] = "";
if (!err) {
ui.addNotification(null, E('p', _('Unable to parse the countries file!')), 'error');
if (countries && countries.length) {
o = s.taboption('feeds', form.MultiValue, 'ban_country', _('Countries') + ' (<abbr title="Regional Internet Registries">RIR</abbr>)');
for (let i = 0; i < countries.length; i++) {
try {
ccode = countries[i].match(/^(\w+)\t/)[1].trim();
rir = countries[i].match(/^\w+\t(\w+)\t/)[1].trim();
country = countries[i].match(/^\w+\t\w+\t(.*$)/)[1].trim();
o.value(ccode, country + ' (' + rir + ')');
} catch (e) {
countries[i] = "";
if (!err) {
ui.addNotification(null, E('p', _('Unable to parse the countries file!')), 'error');
}
err = e;
}
err = e;
}
o.optional = true;
o.rmempty = true;
}
o.optional = true;
o.rmempty = true;
}

o = s.taboption('feeds', form.MultiValue, 'ban_region', _('Regional Internet Registry'));
Expand All @@ -726,8 +740,8 @@ return view.extend({
o.rawhtml = true;
o.default = '<hr style="width: 200px; height: 1px;" /><em style="color:#37c;font-weight:bold;">' + _('External Allowlist Feeds') + '</em>';

o = s.taboption('feeds', form.DynamicList, 'ban_allowurl', _('Allowlist Feed URLs'));
if (countries) {
if (countries && countries.length) {
o = s.taboption('feeds', form.DynamicList, 'ban_allowurl', _('Allowlist Feed URLs'));
for (let i = 0; i < countries.length; i++) {
try {
ccode = countries[i].match(/^(\w+)\t/)[1].trim();
Expand All @@ -739,17 +753,17 @@ return view.extend({
countries[i] = "";
}
}
}
o.optional = true;
o.rmempty = true;
o.validate = function (section_id, value) {
if (!value) {
o.optional = true;
o.rmempty = true;
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;
}
if (!value.match(/^(http:\/\/|https:\/\/)[A-Za-z0-9\/\.\-_\?\&\+=:~#]+$/)) {
return _('Protocol/URL format not supported');
}
return true;
}

o = s.taboption('feeds', form.DummyValue, '_feeds');
Expand Down
Loading

0 comments on commit 7154efa

Please sign in to comment.