瀏覽代碼

增加更新反馈

rufengsuixing 5 年之前
父節點
當前提交
0197bdbcd3

+ 28 - 1
luasrc/controller/AdGuardHome.lua

@@ -1,12 +1,16 @@
 module("luci.controller.AdGuardHome",package.seeall)
+io     = require "io"
+fs=require"nixio.fs"
 function index()
-if not nixio.fs.access("/etc/config/AdGuardHome")then
+if not fs.access("/etc/config/AdGuardHome")then
 return
 end
 	entry({"admin","services","AdGuardHome"},firstchild(),_("AdGuard Home"),30).dependent=true
 	entry({"admin","services","AdGuardHome","general"},cbi("AdGuardHome"),_("Base Setting"),1)
     entry({"admin","services","AdGuardHome","log"},form("AdGuardHomelog"),_("Log"),2)
     entry({"admin","services","AdGuardHome","status"},call("act_status")).leaf=true
+	entry({"admin", "services", "AdGuardHome", "check"}, call("check_update"))
+	entry({"admin", "services", "AdGuardHome", "doupdate"}, call("do_update"))
 end 
 
 function act_status()
@@ -14,4 +18,27 @@ function act_status()
   e.running=luci.sys.call("pgrep -f AdGuardHome/AdGuardHome >/dev/null")==0
   luci.http.prepare_content("application/json")
   luci.http.write_json(e)
+end
+function do_update()
+luci.sys.exec("sh /usr/share/AdGuardHome/update_core.sh &")
+luci.http.prepare_content("application/json")
+luci.http.write('')
+end
+function check_update()
+luci.http.prepare_content("text/plain; charset=utf-8")
+if fs.access("/var/run/update_core") then
+	a=luci.sys.exec("sed -i -e '{w /tmp/tmp.txt' -e 'd}' /tmp/AdGuardHome_update.log && cat /tmp/tmp.txt && rm /tmp/tmp.txt") 
+	luci.http.write(a)
+else
+	if fs.access("/tmp/AdGuardHome_update.log") then
+		a=luci.sys.exec("cat /tmp/AdGuardHome_update.log && rm /tmp/AdGuardHome_update.log")
+		if (a~="") then
+			luci.http.write(a)
+		else
+			luci.http.write("tingzhitongbu")
+		end
+	else
+		luci.http.write("tingzhitongbu")
+	end
+end
 end

+ 6 - 6
luasrc/model/cbi/AdGuardHome.lua

@@ -7,7 +7,10 @@ local configpath=uci:get("AdGuardHome","AdGuardHome","configpath")
 if (configpath==nil) then
 configpath="/etc/AdGuardHome.yaml"
 end
-local binpath="/usr/bin/AdGuardHome/AdGuardHome"
+local binpath=uci:get("AdGuardHome","AdGuardHome","binpath")
+if (configpath==nil) then
+configpath="/usr/bin/AdGuardHome/AdGuardHome"
+end
 local httpport=luci.sys.exec("awk '/bind_port:/{printf($2)}' "..configpath.." 2>nul")
 mp = Map("AdGuardHome", translate("AdGuard Home"))
 mp.description = translate("免费和开源,功能强大的全网络广告和跟踪程序拦截DNS服务器")
@@ -35,12 +38,9 @@ o.inputtitle=translate("更新核心版本")
 if (e==nil) then
 e="not found"
 end
+o.template = "AdGuardHome/AdGuardHome_check"
 o.description=string.format(translate("目前运行主程序版本").."<strong><font color=\"green\">: %s </font></strong>",e)
-o.inputstyle="reload"
-o.write=function()
-luci.sys.exec("bash /usr/share/AdGuardHome/update_core.sh 2>&1")
-luci.http.redirect(luci.dispatcher.build_url("admin","services","AdGuardHome"))
-end
+
 ---- port warning not safe
 local port=luci.sys.exec("awk '/  port:/{printf($2)}' "..configpath.." 2>nul")
 ---- Redirect

+ 40 - 0
luasrc/view/AdGuardHome/AdGuardHome_check.htm

@@ -0,0 +1,40 @@
+<%+cbi/valueheader%>
+<script type="text/javascript">//<![CDATA[
+function check(btn)
+	{
+		btn.disabled = true;
+		btn.value    = '<%:Check...%>';
+		var lv = document.getElementById('cbid.logview.1.conf');
+		lv.style.display="block"
+		lv.width=document.body.clientWidth
+XHR.get('<%=url([[admin]], [[services]], [[AdGuardHome]], [[doupdate]])%>',null,function(x, data){}
+		);
+
+XHR.poll(3, '<%=url([[admin]], [[services]], [[AdGuardHome]], [[check]])%>', null,
+	function(x, data) {
+		var lv = document.getElementById('cbid.logview.1.conf');
+        console.debug(x)
+		if (x.responseText && lv) {
+			
+			if (x.responseText=="tingzhitongbu")
+			{
+				for(j = 0,len=this.XHR._q.length; j < len; j++) {
+				if (this.XHR._q[j].url == '<%=url([[admin]], [[services]], [[AdGuardHome]], [[check]])%>')
+				{
+				this.XHR._q.pop();
+				btn.disabled = false;
+				btn.value    = '已更新';
+				return;}
+				}			
+			}
+			lv.innerHTML += x.responseText; 
+		}
+	}
+);}
+//]]>
+</script>
+		<input class="cbi-button cbi-button-apply" name="cbid.AdGuardHome.AdGuardHome.restart" id="cbid.AdGuardHome.AdGuardHome.restart" value="更新核心版本" onclick="return check(this)"/>
+		<textarea id="cbid.logview.1.conf" class="cbi-input-textarea" style="width: 100%;display:none" data-update="change" name="vbid.logview.1.conf" rows="5" cols="70" readonly="readonly" > </textarea>
+
+	
+<%+cbi/valuefooter%>

+ 0 - 4
root/etc/config/AdGuardHome

@@ -8,9 +8,5 @@ config AdGuardHome 'AdGuardHome'
 	option workdir '/usr/bin/AdGuardHome'
 	option logfile '/tmp/AdGuardHome.log'
 	option verbose '0'
-	option lessspace '1'
 	option binpath '/usr/bin/AdGuardHome/AdGuardHome'
-	list old_redirect 'none'
-	list old_port '5553'
-	list old_enabled '1'
 

+ 17 - 8
root/usr/share/AdGuardHome/update_core.sh

@@ -1,13 +1,16 @@
+#!/bin/bash
+PATH="/usr/sbin:/usr/bin:/sbin:/bin"
 binpath=$(uci get AdGuardHome.AdGuardHome.binpath)
+touch /var/run/update_core
 if [ -z "$binpath" ]; then
 uci get AdGuardHome.AdGuardHome.binpath="/tmp/AdGuardHome/AdGuardHome"
 binpath="/tmp/AdGuardHome/AdGuardHome"
 fi
 mkdir -p ${binpath%/*}
-/usr/bin/AdGuardHome
+
 function check_if_already_running(){
 	running_tasks="$(ps |grep "AdGuardHome" |grep "update_core" |grep -v "grep" |awk '{print $1}' |wc -l)"
-	[ "${running_tasks}" -gt "2" ] && echo -e "\nA task is already running." >>/tmp/AdGuardHome_update.log && exit 2
+	[ "${running_tasks}" -gt "2" ] && echo -e "\nA task is already running." >>/tmp/AdGuardHome_update.log && rm /var/run/update_core && exit 2
 }
 
 function clean_log(){
@@ -18,18 +21,19 @@ function check_latest_version(){
 	latest_ver="$(wget -O- https://api.github.com/repos/AdguardTeam/AdGuardHome/releases/latest 2>/dev/null|grep -E 'tag_name' |grep -E 'v[0-9.]+' -o 2>/dev/null)"
 	[ -z "${latest_ver}" ] && echo -e "\nFailed to check latest version, please try again later." >>/tmp/AdGuardHome_update.log && exit 1
 	now_ver="$($binpath  --check-config 2>&1| grep -E 'v[0-9.]+' -o)"
-	if [ "${latest_ver}" != "${now_ver}" ]; then
+	if [ "${latest_ver}"x != "${now_ver}"x ]; then
 		clean_log
 		echo -e "Local version: ${now_ver}., cloud version: ${latest_ver}." >>/tmp/AdGuardHome_update.log
-		update_core
+		doupdate_core
 	else
 			echo -e "\nLocal version: ${now_ver}, cloud version: ${latest_ver}." >>/tmp/AdGuardHome_update.log
 			echo -e "You're already using the latest version." >>/tmp/AdGuardHome_update.log
+			rm /var/run/update_core
 			exit 3
 	fi
 }
 
-function update_core(){
+function doupdate_core(){
 	echo -e "Updating core..." >>/tmp/AdGuardHome_update.log
 	mkdir -p "/tmp/AdGuardHome/update" >/dev/null 2>&1
 	rm -rf /tmp/AdGuardHome/update/* >/dev/null 2>&1
@@ -58,6 +62,7 @@ function update_core(){
 	;;
 	*)
 	echo -e "error not support $Archt" >>/tmp/AdGuardHome_update.log
+	rm /var/run/update_core
 	exit 1
 	;;
 	esac
@@ -67,6 +72,7 @@ function update_core(){
 	if [ ! -e "/tmp/AdGuardHome/update/AdGuardHome" ]; then
 		echo -e "Failed to download core." >>/tmp/AdGuardHome_update.log
 		rm -rf "/tmp/AdGuardHome/update" >/dev/null 2>&1
+		rm /var/run/update_core
 		exit 1
 	else
 		if [ "$(uci get AdGuardHome.AdGuardHome.lessspace)"x != "1"x ]; then
@@ -78,7 +84,8 @@ function update_core(){
 				if [ "$?" == "0" ]; then
 					uci set AdGuardHome.AdGuardHome.lessspace="1"
 				else
-					echo cp failed
+					echo "cp failed" >>/tmp/AdGuardHome_update.log
+					rm /var/run/update_core
 					exit 1
 				fi
 			fi
@@ -86,7 +93,8 @@ function update_core(){
 		    /etc/init.d/AdGuardHome stop
 			cp -f /tmp/AdGuardHome/update/AdGuardHome/AdGuardHome "$binpath"
 			if [ "$?" != "0" ]; then
-				echo cp failed
+				echo "cp failed" >>/tmp/AdGuardHome_update.log
+				rm /var/run/update_core
 				exit 1
 			fi
 		fi
@@ -94,8 +102,9 @@ function update_core(){
 		/etc/init.d/AdGuardHome restart
 	fi
 	rm -rf "/tmp/AdGuardHome/update" >/dev/null 2>&1
-	echo -e "Succeeded in updating core." >/tmp/AdGuardHome_update.log
+	echo -e "Succeeded in updating core." >>/tmp/AdGuardHome_update.log
 	echo -e "Local version: ${now_ver}, cloud version: ${latest_ver}.\n" >>/tmp/AdGuardHome_update.log
+	rm /var/run/update_core
 }
 
 function main(){