'use strict'; 'require baseclass'; 'require fs'; 'require rpc'; 'require ui'; 'require uci'; document.head.append(E('style', {'type': 'text/css'}, ` .label-status { display: inline; margin: 0 2px 0 0 !important; padding: 2px 4px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; font-weight: bold; color: #fff !important; } .starting { background-color: #9c994c !important; } .running { background-color: #2ea256 !important; } .updating { background-color: #1e82ff !important; } .stopped { background-color: #8a8a8a !important; } .error { background-color: #ff4e54 !important; } `)); return baseclass.extend({ appName : 'zapret', execPath : '/etc/init.d/zapret', syncCfgPath : '/opt/zapret/sync_config.sh', hostsUserFN : '/opt/zapret/ipset/zapret-hosts-user.txt', hostsUserExcludeFN: '/opt/zapret/ipset/zapret-hosts-user-exclude.txt', iplstExcludeFN : '/opt/zapret/ipset/zapret-ip-exclude.txt', iplstUserFN : '/opt/zapret/ipset/zapret-ip-user.txt', iplstUserExcludeFN: '/opt/zapret/ipset/zapret-ip-user-exclude.txt', custFileMax : 4, custFileTemplate : '/opt/zapret/ipset/cust%s.txt', autoHostListFN : '/opt/zapret/ipset/zapret-hosts-auto.txt', autoHostListDbgFN : '/opt/zapret/ipset/zapret-hosts-auto-debug.log', infoLabelRunning : '' + _('Running') + '', infoLabelStarting : '' + _('Starting') + '', infoLabelStopped : '' + _('Stopped') + '', infoLabelDisabled : '' + _('Disabled') + '', infoLabelError : '' + _('Error') + '', infoLabelUpdating : '' + _('Updating') + '', statusDict: { error : { code: 0, name: _('Error') , label: this.infoLabelError }, disabled : { code: 1, name: _('Disabled') , label: this.infoLabelDisabled }, stopped : { code: 2, name: _('Stopped') , label: this.infoLabelStopped }, starting : { code: 3, name: _('Starting') , label: this.infoLabelStarting }, running : { code: 4, name: _('Running') , label: this.infoLabelRunning }, }, callInitState: rpc.declare({ object: 'luci', method: 'getInitList', params: [ 'name' ], expect: { '': {} } }), callInitAction: rpc.declare({ object: 'luci', method: 'setInitAction', params: [ 'name', 'action' ], expect: { result: false } }), getInitState: function(name) { return this.callInitState(name).then(res => { if (res) { return res[name].enabled ? true : false; } else { throw _('Command failed'); } }).catch(e => { ui.addNotification(null, E('p', _('Failed to get %s init status: %s').format(name, e))); }); }, handleServiceAction: function(name, action) { return this.callInitAction(name, action).then(success => { if (!success) { throw _('Command failed'); } return true; }).catch(e => { ui.addNotification(null, E('p', _('Service action failed "%s %s": %s').format(name, action, e))); }); }, normalizeValue: function(v) { return (v && typeof(v) === 'string') ? v.trim().replace(/\r?\n/g, '') : v; }, decode_pkg_list: function(pkg_list) { let pkg_dict = { }; let lines = pkg_list.trim().split('\n'); for (let i = 0; i < lines.length; i++) { let line = lines[i].trim(); if (line.length >= 4) { let word_list = line.split(' - '); let name = word_list[0].trim(); let ver = word_list[1].trim(); pkg_dict[name] = ver; } } return pkg_dict; }, get_pid_list: function(proc_list) { let plist = [ ]; let lines = proc_list.trim().split('\n'); for (let i = 0; i < lines.length; i++) { let line = lines[i].trim(); if (line.length > 5) { let word_list = line.split(/\s+/); let pid = word_list[0]; let isnum = /^\d+$/.test(pid); if (isnum) { plist.push(parseInt(pid)); } } } return plist; }, decode_svc_info: function(svc_autorun, svc_info, proc_list, cfg) { let result = { "autorun": svc_autorun, "dmn": { inited: false, total: 0, running: 0, working: 0, }, "status": this.statusDict.error, }; if (svc_info.code != 0) { return -1; } if (proc_list.code != 0) { return -2; } let plist = this.get_pid_list(proc_list.stdout); let jdata = JSON.parse(svc_info.stdout); if (typeof(jdata) !== 'object') { return -3; } if (typeof(jdata.zapret) == 'object') { result.dmn.inited = true; let dmn_list = jdata.zapret.instances; if (typeof(dmn_list) == 'object') { for (const [dmn_name, daemon] of Object.entries(dmn_list)) { result.dmn.total += 1; if (daemon.running) { result.dmn.running += 1; } if (daemon.pid !== undefined && daemon.pid != null) { if (plist.includes(daemon.pid)) { result.dmn.working += 1; } } } } } //console.log('SVC_DAEMONS: ' + result.dmn.working + ' / ' + result.dmn.total); if (result.dmn.total == 0) { result.status = (!svc_autorun) ? this.statusDict.disabled : this.statusDict.stopped; } else { result.status = (result.dmn.inited) ? this.statusDict.started : this.statusDict.running; } return result; }, makeStatusString: function(svcinfo, fwtype, bllist_preset) { let svc_autorun = _('Unknown'); let svc_daemons = _('Unknown'); if (typeof(svcinfo) == 'object') { svc_autorun = (svcinfo.autorun) ? _('Enabled') : _('Disabled'); if (!svcinfo.dmn.inited) { svc_daemons = _('Stopped'); } else { svc_daemons = (!svcinfo.dmn.working) ? _('Starting') : _('Running'); svc_daemons += ' [' + svcinfo.dmn.working + '/' + svcinfo.dmn.total + ']'; } } let update_mode = _('user entries only'); let td_name_width = 40; let td_name_style = `style="width: ${td_name_width}%; min-width:${td_name_width}%; max-width:${td_name_width}%;"`; let out = `
| ${_('Service autorun status')}: | ${svc_autorun} |
| ${_('Service daemons status')}: | ${svc_daemons} |
| ${_('FW type')}: | ${fwtype} |
| ${_('Blacklist update mode')}: | ${update_mode} |