small-packages/luci-app-mosdns/htdocs/luci-static/resources/view/mosdns/update.js
2026-05-10 09:48:30 +08:00

137 lines
3.7 KiB
JavaScript

'use strict';
'require form';
'require fs';
'require ui';
'require view';
'require rpc';
const callStartUpdate = rpc.declare({
object: 'luci.mosdns',
method: 'start_update',
expect: { '': {} }
});
const callGetUpdateLog = rpc.declare({
object: 'luci.mosdns',
method: 'get_update_log',
expect: { '': {} }
});
return view.extend({
handleUpdate() {
const logTextarea = E('textarea', {
'class': 'cbi-input-textarea',
'readonly': 'readonly',
'style': 'width: 100%; height: 300px; font-family: monospace; font-size: 12px; margin-top: 10px;',
'placeholder': _('Starting update...')
});
const closeButton = E('button', {
'class': 'btn',
'style': 'display: none;',
'click': ui.hideModal
}, _('Close'));
ui.showModal(_('Updating Database...'), [
E('p', { 'class': 'spinning' }, _('Please wait, this may take a few moments...')),
logTextarea,
E('div', { 'class': 'right' }, [ closeButton ])
]);
const pollLog = () => {
return callGetUpdateLog().then(res => {
if (res && res.log) {
logTextarea.value = res.log;
logTextarea.scrollTop = logTextarea.scrollHeight;
if (res.log.match(/UPDATE_FINISHED/)) {
ui.hideModal();
ui.addNotification(null, E('p', _('Update success')), 'info');
return false;
}
if (res.log.match(/Another update is already in progress/)) {
ui.hideModal();
ui.addNotification(null, E('p', _('Another update is already in progress.')), 'warn');
return false;
}
}
return true;
});
};
return callStartUpdate().then(res => {
if (res.success) {
const interval = window.setInterval(() => {
pollLog().then(continuePolling => {
if (!continuePolling) {
window.clearInterval(interval);
}
});
}, 1000);
} else {
ui.hideModal();
ui.addNotification(null, E('p', res.error || _('Failed to start update.')), 'error');
}
}).catch(e => {
ui.hideModal();
ui.addNotification(null, E('p', _('Update failed: %s').format(e.message)), 'error');
});
},
render() {
let m, s, o;
m = new form.Map('mosdns', _('Update GeoIP & GeoSite databases'),
_('Automatically update GeoIP and GeoSite databases as well as ad filtering rules through scheduled tasks.'));
s = m.section(form.TypedSection);
s.anonymous = true;
o = s.option(form.Flag, 'geo_auto_update', _('Enable Auto Database Update'));
o.rmempty = false;
o = s.option(form.ListValue, 'geo_update_week_time', _('Update Cycle'));
o.value('*', _('Every Day'));
o.value('1', _('Every Monday'));
o.value('2', _('Every Tuesday'));
o.value('3', _('Every Wednesday'));
o.value('4', _('Every Thursday'));
o.value('5', _('Every Friday'));
o.value('6', _('Every Saturday'));
o.value('0', _('Every Sunday'));
o.default = 3;
o = s.option(form.ListValue, 'geo_update_day_time', _('Update Time'));
for (let t = 0; t < 24; t++) {
o.value(t, t + ':00');
}
o.default = 3;
o = s.option(form.ListValue, 'geoip_type', _('GeoIP Type'),
_('Little: only include Mainland China and Private IP addresses.') +
'<br>' +
_('Full: includes all Countries and Private IP addresses.')
);
o.value('geoip', _('Full'));
o.value('geoip-only-cn-private', _('Little'));
o.rmempty = false;
o.default = 'geoip-only-cn-private';
o = s.option(form.Value, 'github_proxy', _('GitHub Proxy'),
_('Update data files with GitHub Proxy, leave blank to disable proxy downloads.'));
o.value('https://gh-proxy.com', _('https://gh-proxy.com'));
o.rmempty = true;
o.default = '';
o = s.option(form.Button, '_udpate', null,
_('Check And Update GeoData.'));
o.title = _('Database Update');
o.inputtitle = _('Check And Update');
o.inputstyle = 'apply';
o.onclick = () => this.handleUpdate();
return m.render();
}
});