mirror of
https://github.com/remittor/zapret-openwrt.git
synced 2026-03-29 01:07:55 +03:00
updater: Replace wget to curl and other fixes
This commit is contained in:
@@ -32,11 +32,13 @@ return baseclass.extend({
|
||||
setStage: function(stage, btn_flag = true) {
|
||||
if (stage == 0) {
|
||||
this.btn_action.textContent = _('Check for updates');
|
||||
this.btn_action.classList.remove('hidden');
|
||||
} else
|
||||
if (stage == 1) {
|
||||
this.btn_action.textContent = _('Update packages');
|
||||
this.btn_action.classList.remove('hidden');
|
||||
} else {
|
||||
this.btn_action.textContent = _('');
|
||||
this.btn_action.classList.add('hidden');
|
||||
}
|
||||
if (stage > 1 && typeof(this.btn_action) == 'object') {
|
||||
this.setBtnMode(1);
|
||||
@@ -102,10 +104,12 @@ return baseclass.extend({
|
||||
}
|
||||
let rpc_opt = { timeout: 5*1000 }
|
||||
//rpc_opt.uid = 0; // run under root
|
||||
const logFile = '/tmp/zapret_pkg_install.log';
|
||||
const pidFile = '/tmp/zapret_pkg_install.pid';
|
||||
const x_filename = 'zapret_pkg_install';
|
||||
const pidFile = '/tmp/' + x_filename + '.pid';
|
||||
const logFile = '/tmp/' + x_filename + '.log';
|
||||
const errFile = '/tmp/' + x_filename + '.err';
|
||||
try {
|
||||
await fs.exec('/bin/busybox', [ 'rm', '-f', '/tmp/zapret_pkg_install.*' ], null, rpc_opt);
|
||||
await fs.exec('/bin/busybox', [ 'rm', '-f', '/tmp/' + x_filename + '.*' ], null, rpc_opt);
|
||||
this.appendLog('Install log cleared.');
|
||||
} catch (e) {
|
||||
this.appendLog('ERROR: Failed to clear log file');
|
||||
@@ -114,7 +118,7 @@ return baseclass.extend({
|
||||
}
|
||||
//console.log(`Start ${fn_update_pkg_sh}...`);
|
||||
try {
|
||||
let opt = [ logFile, pidFile, fn_update_pkg_sh ];
|
||||
let opt = [ x_filename, fn_update_pkg_sh ];
|
||||
//opt.push('-t'); // only for testing
|
||||
opt.push(...opt_list);
|
||||
await fs.exec('/opt/zapret/script-exec.sh', opt, null, rpc_opt);
|
||||
@@ -157,21 +161,28 @@ return baseclass.extend({
|
||||
if (pid > 0 && !alive) {
|
||||
clearInterval(timer);
|
||||
this.appendLog('\nProcess finished.');
|
||||
let err_code = -1;
|
||||
try {
|
||||
let err_data = await fs.exec('/bin/cat', [ errFile ], null, rpc_opt);
|
||||
err_code = parseInt(err_data.stdout.trim(), 10);
|
||||
} catch (e) {
|
||||
// nothing
|
||||
}
|
||||
let log = res.stdout;
|
||||
let code = log.match(/^RESULT:\s*\(([^)]+)\)\s+.+$/m);
|
||||
if (code && code[1] == '+') {
|
||||
this.setStage(999);
|
||||
this.stage = 999;
|
||||
this.btn_action.textContent = _('OK');
|
||||
this.btn_action.disabled = false;
|
||||
this.btn_cancel.disabled = true;
|
||||
return 0;
|
||||
}
|
||||
this.appendLog('ERROR: Install updates failed!');
|
||||
this.appendLog('ERROR: Install updates failed with error ' + err_code);
|
||||
this.setStage(999);
|
||||
}
|
||||
} catch (e) {
|
||||
clearInterval(timer);
|
||||
this.appendLog('ERROR: reading log: ' + e.message);
|
||||
this.appendLog('ERROR: installUpdates: ' + e.message);
|
||||
this.setStage(999);
|
||||
}
|
||||
}, 500);
|
||||
|
||||
@@ -90,14 +90,28 @@ function get_run_on_boot_option
|
||||
fi
|
||||
}
|
||||
|
||||
function get_distrib_param
|
||||
{
|
||||
local parname=$1
|
||||
local value="__unknown__"
|
||||
if [ -f /etc/openwrt_release ]; then
|
||||
while IFS='=' read -r key val; do
|
||||
val="${val#\'}"
|
||||
val="${val%\'}"
|
||||
val="${val#\"}"
|
||||
val="${val%\"}"
|
||||
if [ "$key" = "$parname" ]; then
|
||||
value="$val"
|
||||
break
|
||||
fi
|
||||
done < /etc/openwrt_release
|
||||
fi
|
||||
printf '%s' "$value"
|
||||
}
|
||||
|
||||
function get_cpu_arch
|
||||
{
|
||||
if [ -f /etc/openwrt_release ]; then
|
||||
. /etc/openwrt_release
|
||||
printf '%s' "$DISTRIB_ARCH"
|
||||
else
|
||||
printf 'unknown'
|
||||
fi
|
||||
get_distrib_param DISTRIB_ARCH
|
||||
}
|
||||
|
||||
function restore_ipset_txt
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
#!/bin/sh
|
||||
# Copyright (c) 2024 remittor
|
||||
LOG_FILE=$1
|
||||
PID_FILE=$2
|
||||
shift 2
|
||||
PID_FILE=/tmp/$1.pid
|
||||
LOG_FILE=/tmp/$1.log
|
||||
ERR_FILE=/tmp/$1.err
|
||||
shift 1
|
||||
: > $LOG_FILE
|
||||
: > $ERR_FILE
|
||||
(
|
||||
exec </dev/null >/dev/null 2>&1
|
||||
"$@" >> $LOG_FILE 2>&1
|
||||
echo $? > "$ERR_FILE"
|
||||
sleep 1
|
||||
) &
|
||||
echo $! > $PID_FILE
|
||||
exit 0
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
. /opt/zapret/comfunc.sh
|
||||
. /usr/share/libubox/jshn.sh
|
||||
. /etc/openwrt_release
|
||||
|
||||
opt_check=
|
||||
opt_prerelease=
|
||||
@@ -35,9 +36,9 @@ fi
|
||||
ZAP_CPU_ARCH=$(get_cpu_arch)
|
||||
ZAP_REL_URL="https://raw.githubusercontent.com/remittor/zapret-openwrt/gh-pages/releases/releases_zap1_$ZAP_CPU_ARCH.json"
|
||||
|
||||
WGET_TIMEOUT=5
|
||||
WGET_HEADER1="Accept: application/json"
|
||||
WGET_HEADER2="Cache-Control: no-cache"
|
||||
CURL_TIMEOUT=5
|
||||
CURL_HEADER1="Accept: application/json"
|
||||
CURL_HEADER2="Cache-Control: no-cache"
|
||||
|
||||
REL_JSON=
|
||||
REL_ACTUAL_TAG=
|
||||
@@ -48,13 +49,6 @@ ZAP_OUT=
|
||||
ZAP_ERR=
|
||||
ZAP_PKG_URL=
|
||||
|
||||
function download_json
|
||||
{
|
||||
local url="$1"
|
||||
wget -q -T "$WGET_TIMEOUT" --header="$WGET_HEADER1" --header="$WGET_HEADER2" -O - "$url"
|
||||
return $?
|
||||
}
|
||||
|
||||
if command -v apk >/dev/null; then
|
||||
PKG_MGR=apk
|
||||
ZAP_PKG_EXT=apk
|
||||
@@ -68,6 +62,13 @@ fi
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------
|
||||
|
||||
function download_json
|
||||
{
|
||||
local url="$1"
|
||||
curl -s -L --max-time $CURL_TIMEOUT -H "$CURL_HEADER1" -H "$CURL_HEADER2" "$url" 2>/dev/null
|
||||
return $?
|
||||
}
|
||||
|
||||
function get_pkg_version
|
||||
{
|
||||
local pkg_name="$1"
|
||||
@@ -148,7 +149,6 @@ function download_releases_info
|
||||
{
|
||||
local txt txtlen txtlines generated_at
|
||||
REL_JSON=
|
||||
echo "CPU arch: $ZAP_CPU_ARCH"
|
||||
echo "Download releases info..."
|
||||
txt=$(download_json $ZAP_REL_URL)
|
||||
txtlen=${#txt}
|
||||
@@ -229,13 +229,32 @@ function get_actual_release
|
||||
|
||||
if [ "$opt_check" != "true" -a "$opt_update" = "" ]; then
|
||||
echo 'ERROR: Incorrect arguments'
|
||||
return 1
|
||||
return 4
|
||||
fi
|
||||
|
||||
if [ "$opt_update" = "@" ]; then
|
||||
opt_check="true"
|
||||
fi
|
||||
|
||||
#echo "DISTRIB_ID: $DISTRIB_ID"
|
||||
echo "DISTRIB_RELEASE: $DISTRIB_RELEASE"
|
||||
echo "DISTRIB_DESCRIPTION:" $(get_distrib_param DISTRIB_DESCRIPTION)
|
||||
echo "DISTRIB_ARCH:" $(get_distrib_param DISTRIB_ARCH)
|
||||
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
echo "ERROR: package \"curl\" not installed!"
|
||||
return 10
|
||||
fi
|
||||
CURL_INFO=$( curl -V )
|
||||
if ! echo "$CURL_INFO" | grep -q 'https'; then
|
||||
echo "------- package curl"
|
||||
echo "$CURL_INFO"
|
||||
echo "-------"
|
||||
echo "ERROR: package \"curl\" not supported HTTPS protocol!"
|
||||
echo "NOTE: Please install package \"curl-ssl\""
|
||||
return 11
|
||||
fi
|
||||
|
||||
if [ "$opt_check" = "true" ]; then
|
||||
download_releases_info
|
||||
ZAP_ERR=$?
|
||||
@@ -317,7 +336,8 @@ if [ "$opt_update" != "" ]; then
|
||||
fi
|
||||
ZAP_PKG_DIR=/tmp/zapret_pkg
|
||||
rm -rf $ZAP_PKG_DIR
|
||||
ZAP_PKG_SIZE=$( wget --spider -T $WGET_TIMEOUT --header="$WGET_HEADER2" -S "$ZAP_PKG_URL" 2>&1 | grep -i 'Content-Length:' | tail -n1 | awk '{print $2}' | tr -d '\r' )
|
||||
ZAP_PKG_HDRS=$( curl -s -I -L --max-time $CURL_TIMEOUT -H "$CURL_HEADER2" "$ZAP_PKG_URL" )
|
||||
ZAP_PKG_SIZE=$( echo "$ZAP_PKG_HDRS" | grep -i 'content-length: ' | tail -n1 | awk '{print $2}' | tr -d '\r' )
|
||||
echo "Downloded ZIP-file size = $ZAP_PKG_SIZE bytes"
|
||||
[ "$ZAP_PKG_SIZE" = "" ] || [[ $ZAP_PKG_SIZE -lt 256 ]] && {
|
||||
echo "ERROR: incorrect package size!"
|
||||
@@ -326,14 +346,14 @@ if [ "$opt_update" != "" ]; then
|
||||
mkdir $ZAP_PKG_DIR
|
||||
ZAP_PKG_FN="$ZAP_PKG_DIR/${ZAP_PKG_URL##*/}"
|
||||
echo "Download ZIP-file..."
|
||||
wget -q -T 15 --header="$WGET_HEADER2" -O "$ZAP_PKG_FN" "$ZAP_PKG_URL"
|
||||
curl -s -L --max-time 15 -H "$CURL_HEADER2" "$ZAP_PKG_URL" -o "$ZAP_PKG_FN"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "ERROR: cannot download package!"
|
||||
return 215
|
||||
fi
|
||||
ZAP_PKG_SZ=$( wc -c < "$ZAP_PKG_FN" )
|
||||
if [ "$ZAP_PKG_SZ" != "$ZAP_PKG_SIZE" ]; then
|
||||
echo "ERROR: downloaded package is incorrect!"
|
||||
echo "ERROR: downloaded package is incorrect! (size = $ZAP_PKG_SZ)"
|
||||
return 216
|
||||
fi
|
||||
unzip -q "$ZAP_PKG_FN" -d $ZAP_PKG_DIR
|
||||
@@ -384,5 +404,4 @@ if [ "$opt_update" != "" ]; then
|
||||
return 247
|
||||
fi
|
||||
echo "RESULT: (+) Packages from $ZAP_PKG_ZIP_NAME successfully installed!"
|
||||
sleep 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user