diff --git a/luci-app-zapret/htdocs/luci-static/resources/view/zapret/dmnlog.js b/luci-app-zapret/htdocs/luci-static/resources/view/zapret/dmnlog.js index 75fe77d..99c98f2 100644 --- a/luci-app-zapret/htdocs/luci-static/resources/view/zapret/dmnlog.js +++ b/luci-app-zapret/htdocs/luci-static/resources/view/zapret/dmnlog.js @@ -8,15 +8,17 @@ 'require view.zapret.tools as tools'; return view.extend({ - retrieveLog: async function() { - return Promise.all([ - L.resolveDefault(fs.stat('/bin/cat'), null), - fs.exec('/usr/bin/find', [ '/tmp', '-maxdepth', '1', '-type', 'f', '-name', tools.appName+'+*.log' ]), - uci.load(tools.appName), - ]).then(function(status_array) { - var filereader = status_array[0] ? status_array[0].path : null; - var log_data = status_array[1]; // stdout: multiline text - if (log_data.code != 0) { + POLL: new tools.POLLER( { } ), + + retrieveLog: async function() + { + return tools.promiseAllDict({ + filereader : L.resolveDefault(fs.stat('/bin/cat'), null), + log_data : fs.exec('/usr/bin/find', [ '/tmp', '-maxdepth', '1', '-type', 'f', '-name', tools.appName+'+*.log' ]), + }).then( (data) => { + var filereader = data.filereader ? data.filereader.path : null; + var log_data = data.log_data; // stdout: multiline text + if (log_data?.code === undefined || log_data.code != 0) { ui.addNotification(null, E('p', _('Unable to get log files') + '(code = ' + log_data.code + ') : retrieveLog()')); return null; } @@ -68,17 +70,20 @@ return view.extend({ ))); return null; }); - }).catch(function(e) { + }).catch( (e) => { const [, lineno, colno] = e.stack.match(/(\d+):(\d+)/); ui.addNotification(null, E('p', _('Unable to execute or read contents') + ': %s [ lineno: %s | %s | %s | %s ]'.format( e.message, lineno, 'retrieveLog', 'uci.'+tools.appName ))); return null; + }).finally( () => { + this.POLL.running = false; }); }, - pollLog: async function() { + pollLog: async function() + { let logdate_len = -2; let logdata; for (let txt_id = 0; txt_id < 10; txt_id++) { @@ -111,26 +116,28 @@ return view.extend({ } }, - load: async function() { - poll.add(this.pollLog.bind(this)); - return await this.retrieveLog(); + load: function() + { + return tools.baseLoad(this, (data) => { + tools.load_feat_env(); + this.svc_info = data.svc_info; + return this.retrieveLog(); + }); }, - render: function(logdata) { - if (!logdata) { - return; - } + render: function(logdata) + { if (typeof(logdata) === 'string') { return E('div', {}, [ E('p', {'class': 'cbi-title-field'}, [ logdata ]), ]); } - if (!Array.isArray(logdata)) { + if (!logdata || !Array.isArray(logdata)) { ui.addNotification(null, E('p', _('Unable to get log files') + ' : render()')); return; } var h2 = E('div', {'class' : 'cbi-title-section'}, [ - E('h2', {'class': 'cbi-title-field'}, [ tools.AppName + ' - ' + _('Log Viewer') ]), + E('h2', {'class': 'cbi-title-field'}, [ ]), ]); var tabs = E('div', {}, E('div')); @@ -193,8 +200,11 @@ return view.extend({ tabs.firstElementChild.appendChild(tab); } ui.tabs.initTabGroup(tabs.firstElementChild.childNodes); - //this.pollFn = L.bind(this.handleScanRefresh, this); - //poll.add(this.pollFn); + + this.POLL.mode = 1; + this.POLL.init( this.pollLog.bind(this), 1000 ); // interval 1000 ms + this.POLL.start(); + return E('div', { }, [ h2, tabs ]); }, diff --git a/luci-app-zapret/htdocs/luci-static/resources/view/zapret/settings.js b/luci-app-zapret/htdocs/luci-static/resources/view/zapret/settings.js index 3575c55..343f3b4 100644 --- a/luci-app-zapret/htdocs/luci-static/resources/view/zapret/settings.js +++ b/luci-app-zapret/htdocs/luci-static/resources/view/zapret/settings.js @@ -90,6 +90,43 @@ return view.extend({ o.rmempty = false; o.default = 0; + let current_size = uci.get(tools.appName, 'config', 'DAEMON_LOG_SIZE_MAX') || '0'; + let has_valid_value = false; + let size_list = [ 500, 1000, 1500, 2000, 2500, 3000, 4000, 5000, 7000 ]; + if (current_size && current_size != '0') { + try { + current_size = parseInt(current_size, 10); + if (!isNaN(current_size) && current_size > 0) { + has_valid_value = true; + if (!size_list.includes(current_size)) { + size_list.push(current_size); + size_list.sort((a, b) => a - b); + } + } + } catch(e) { + has_valid_value = false; + } + } + o = s.taboption(tabname, form.ListValue, 'DAEMON_LOG_SIZE_MAX', _('DAEMON_LOG_SIZE_MAX')); + o.rmempty = false; + if (!has_valid_value) { + o.value('', ''); + o.default = ''; + } + for (let idx = 0; idx < size_list.length; idx++) { + let fsize = size_list[idx]; + o.value('' + fsize, fsize + ' KB'); + if (has_valid_value && fsize === current_size) { + o.default = '' + fsize; + } + } + o.validate = function(section_id, value) { + if (!value || value === '') { + return _('Please select maximum log size'); + } + return true; + }; + /* NFQWS_OPT_DESYNC tab */ tabname = 'nfqws_params'; diff --git a/zapret/comfunc.sh b/zapret/comfunc.sh index 1dc9289..b9762c8 100755 --- a/zapret/comfunc.sh +++ b/zapret/comfunc.sh @@ -170,17 +170,25 @@ function merge_cfg_with_def_values function remove_cron_task_logs { - if [ -f "$CRONTAB_FILE" ]; then - sed -i "/-name '$ZAPRET_CFG_NAME+\*.log' -size +/d" "$CRONTAB_FILE" + [ ! -f $CRONTAB_FILE ] && return 0 + if grep -q -e "-name '$ZAPRET_CFG_NAME+\*\.log' -size " $CRONTAB_FILE; then + sed -i "/-name '$ZAPRET_CFG_NAME+\*.log' -size /d" $CRONTAB_FILE + #/etc/init.d/cron restart 2> /dev/null fi } function insert_cron_task_logs { - [ ! -f "$CRONTAB_FILE" ] && touch "$CRONTAB_FILE" - [ ! -f "$CRONTAB_FILE" ] && return 1 - if ! grep -q -e "-name '$ZAPRET_CFG_NAME+\*\.log' -size \+" "$CRONTAB_FILE"; then - echo "*/2 * * * * /usr/bin/find /tmp -maxdepth 1 -type f -name '$ZAPRET_CFG_NAME+*.log' -size +2600k -exec rm -f {} \;" >> "$CRONTAB_FILE" + local daemon_log_size_max=${1:-2000} + [ ! -f $CRONTAB_FILE ] && touch $CRONTAB_FILE + [ ! -f $CRONTAB_FILE ] && return 1 + if ! grep -q -e "-name '$ZAPRET_CFG_NAME+\*\.log' -size " $CRONTAB_FILE; then + case "$daemon_log_size_max" in + ''|'0'|*[!0-9]*) + daemon_log_size_max=2000 + ;; + esac + echo "*/1 * * * * /usr/bin/find /tmp -maxdepth 1 -type f -name '$ZAPRET_CFG_NAME+*.log' -size +${daemon_log_size_max}k -exec rm -f {} \;" >> $CRONTAB_FILE /etc/init.d/cron restart 2> /dev/null fi return 0 @@ -188,7 +196,8 @@ function insert_cron_task_logs function init_before_start { - local DAEMON_LOG_ENABLE=$1 + local daemon_log_enable=$1 + local daemon_log_size_max=${2:-2000} local HOSTLIST_FN="$ZAPRET_BASE/ipset/zapret-hosts-user.txt" [ ! -f "$HOSTLIST_FN" ] && touch "$HOSTLIST_FN" chmod 644 $ZAPRET_BASE/ipset/*.txt @@ -198,8 +207,8 @@ function init_before_start rm -f $ZAPRET_BASE/init.d/openwrt/custom.d/*.apk* rm -f /tmp/$ZAPRET_CFG_NAME+*.log #*/ - if [ "$DAEMON_LOG_ENABLE" = "1" ]; then - insert_cron_task_logs + if [ "$daemon_log_enable" = "1" ]; then + insert_cron_task_logs "$daemon_log_size_max" else remove_cron_task_logs fi diff --git a/zapret/config.default b/zapret/config.default index 1d7f0b5..c89785d 100644 --- a/zapret/config.default +++ b/zapret/config.default @@ -155,5 +155,5 @@ FILTER_TTL_EXPIRED_ICMP=1 DAEMON_LOG_ENABLE=0 - +DAEMON_LOG_SIZE_MAX=2000 DAEMON_LOG_FILE="/tmp/zapret+++.log" diff --git a/zapret/def-cfg.sh b/zapret/def-cfg.sh index 09c1020..336da52 100755 --- a/zapret/def-cfg.sh +++ b/zapret/def-cfg.sh @@ -19,6 +19,7 @@ function set_cfg_reset_values set $cfgname.config.DISABLE_CUSTOM='1' set $cfgname.config.WS_USER='daemon' set $cfgname.config.DAEMON_LOG_ENABLE='0' + set $cfgname.config.DAEMON_LOG_SIZE_MAX='2000' set $cfgname.config.DAEMON_LOG_FILE='/tmp/zapret+++.log' # autohostlist options set $cfgname.config.AUTOHOSTLIST_RETRANS_THRESHOLD='3' diff --git a/zapret/init.d.sh b/zapret/init.d.sh index 27a5438..4a91491 100755 --- a/zapret/init.d.sh +++ b/zapret/init.d.sh @@ -70,18 +70,18 @@ function boot fi fi fi - init_before_start "$DAEMON_LOG_ENABLE" + init_before_start "$DAEMON_LOG_ENABLE" "$DAEMON_LOG_SIZE_MAX" /bin/sh /etc/rc.common $ZAPRET_ORIG_INITD start "$@" } function start { - init_before_start "$DAEMON_LOG_ENABLE" + init_before_start "$DAEMON_LOG_ENABLE" "$DAEMON_LOG_SIZE_MAX" /bin/sh /etc/rc.common $ZAPRET_ORIG_INITD start "$@" } function restart { - init_before_start "$DAEMON_LOG_ENABLE" + init_before_start "$DAEMON_LOG_ENABLE" "$DAEMON_LOG_SIZE_MAX" /bin/sh /etc/rc.common $ZAPRET_ORIG_INITD restart "$@" } diff --git a/zapret/sync_config.sh b/zapret/sync_config.sh index 2fa37f5..7e94a6e 100755 --- a/zapret/sync_config.sh +++ b/zapret/sync_config.sh @@ -93,6 +93,7 @@ sync_param MODE_FILTER sync_param DISABLE_CUSTOM sync_param WS_USER str sync_param DAEMON_LOG_ENABLE +sync_param DAEMON_LOG_SIZE_MAX sync_param DAEMON_LOG_FILE str sync_param AUTOHOSTLIST_RETRANS_THRESHOLD