up ddns-go 增加日志功能

This commit is contained in:
Pdboy Sir 2024-10-09 21:05:54 +08:00 committed by GitHub
parent 55a1d4ded4
commit 7db343c032
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 202 additions and 3 deletions

View File

@ -69,7 +69,23 @@ start_instance() {
procd_open_instance
get_tz
[[ -z "$SET_TZ" ]] || procd_set_param env TZ="$SET_TZ"
procd_set_param command ${PROG} -l :$port -c "$CONF" ${param}
procd_set_param command /bin/sh -c "
${PROG} -l :$port -c \"$CONF\" ${param} >> /var/log/ddns-go.log 2>&1 &
DDNS_GO_PID=\$!
{
while true; do
LOG_SIZE=\$(ls -l /var/log/ddns-go.log | awk '{print int(\$5/1024)}')
if [ \$LOG_SIZE -gt 100 ]; then
tail -n 100 /var/log/ddns-go.log > /var/log/ddns-go.log.tmp
mv /var/log/ddns-go.log.tmp /var/log/ddns-go.log
fi
sleep 3600
done
} &
LOG_MANAGEMENT_PID=\$!
trap \"kill -TERM \$DDNS_GO_PID; kill -TERM \$LOG_MANAGEMENT_PID; exit\" SIGINT SIGTERM EXIT
wait \$DDNS_GO_PID
"
[[ "x$logger" == x1 ]] && procd_set_param stderr 1
procd_set_param respawn
procd_close_instance

View File

@ -12,8 +12,11 @@ function index()
e.dependent=false
e.acl_depends={ "luci-app-ddns-go" }
entry({"admin", "services", "ddns-go", "setting"}, cbi("ddns-go"), _("Base Setting"), 20).leaf=true
entry({"admin", "services", "ddns-go", "ddns-go"}, template("ddns-go"), _("DDNS-GO Control panel"), 30).leaf = true
entry({"admin", "services", "ddns-go", "ddns-go"}, template("ddns-go/ddns-go"), _("DDNS-GO Control panel"), 30).leaf = true
entry({"admin", "services", "ddnsgo_status"}, call("act_status"))
entry({"admin", "services", "ddns-go", "log"}, template("ddns-go/ddns-go_log"), _("Log"), 40).leaf = true
entry({"admin", "services", "ddns-go", "fetch_log"}, call("fetch_log"), nil).leaf = true
entry({"admin", "services", "ddns-go", "clear_log"}, call("clear_log")).leaf = true
end
function act_status()
@ -23,3 +26,20 @@ function act_status()
luci.http.prepare_content("application/json")
luci.http.write_json(e)
end
function fetch_log()
local fs = require "nixio.fs"
local log_file = "/var/log/ddns-go.log"
local log_content = fs.readfile(log_file) or "No Log."
luci.http.write(log_content)
end
function clear_log()
local fs = require "nixio.fs"
local log_file = "/var/log/ddns-go.log"
local f = io.open(log_file, "w")
if f then
f:close()
luci.http.status(204, "No Content")
else
luci.http.status(500, "Internal Server Error")
end
end

View File

@ -6,7 +6,7 @@ m = Map("ddns-go")
m.title = translate("DDNS-GO")
m.description = translate("DDNS-GO automatically obtains your public IPv4 or IPv6 address and resolves it to the corresponding domain name service.")..translate("</br>For specific usage, see:")..translate("<a href=\'https://github.com/sirpdboy/luci-app-ddns-go.git' target=\'_blank\'>GitHub @sirpdboy/luci-app-ddns-go </a>")
m:section(SimpleSection).template = "ddns-go_status"
m:section(SimpleSection).template = "ddns-go/ddns-go_status"
s = m:section(TypedSection, "basic", translate("Global Settings"))
s.addremove = false

View File

@ -0,0 +1,34 @@
<%#
Copyright 2008-2024 by sirpdboy <herboy2008@gmail.com>
https://github.com/sirpdboy/luci-app-ddns-go
Licensed to the public under the Apache License 2.0.
-%>
<%
local running = luci.sys.exec("pidof ddns-go | awk -F ' ' '{print $1}'")
%>
<%+header%>
<div class="cbi-map">
<% if tonumber(running) ~= nil then %>
<iframe id="ddnsgo" style="width: 100%; min-height: 580px; border: none; border-radius: .375rem;box-shadow: rgba(0, 0, 0, 0.75) 0px 0px 15px -5px;"><br/><em style=\"color:red\">
<p ><em><%:The DDNS-GO service is not running.%></em></p>
</iframe>
</div>
<script type="text/javascript">
document.getElementById("ddnsgo").src = window.location.protocol + "//" + window.location.hostname + ":<%=luci.model.uci.cursor():get_first("ddns-go", "basic", "port") %>";
document.getElementById("ddnsgo").height = document.documentElement.clientHeight;
window.onresize = function(){
document.getElementById("ddnsgo").height = document.documentElement.clientHeight;
}
</script>
<% else %>
<style>.running{text-align: center;} .running>h1{font-size: 25px; color: #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xMC42NjctMTM0LjIyOS0yOS43ODFsNDIxLjU0Ny00MjEuNTQ3YzE5LjA3MiA0MC43ODkgMjkuNzgxIDg2LjI3MiAyOS43ODEgMTM0LjIyOS0wLjA0MyAxNzUuMTQ3LTE0MS45OTUgMzE3LjA5OS0zMTcuMDk5IDMxNy4wOTl6Ij48L3BhdGg+Cjwvc3ZnPgo=" height="100">
<h1><font color="color:red"><%:The DDNS-GO service is not running.%></font></h1>
<p><%:Please enable the DDNS-GO service%></p>
</div>
<% end -%>
</div>
<%+footer%>

View File

@ -0,0 +1,90 @@
<%+header%>
<div>
<input class="btn cbi-button-action" type="button" onclick="clearLog()" value="清空日志">
<input class="btn cbi-button-action" type="button" onclick="toggleRefresh()" value="停止刷新">
</div>
<pre id="logContent">
<%
local fs = require "nixio.fs"
local log_file_path = "/var/log/ddns-go.log"
local raw_log_content = fs.readfile(log_file_path) or "No Log."
local log_lines = {}
for line in raw_log_content:gmatch("[^\r\n]+") do
table.insert(log_lines, line)
end
for i=1, math.floor(#log_lines / 2) do
log_lines[i], log_lines[#log_lines - i + 1] = log_lines[#log_lines - i + 1], log_lines[i]
end
local log_content = table.concat(log_lines, "\n")
%>
<%=log_content%>
</pre>
<%+footer%>
<style>
#logContent {
background-color: #ffffff;
border: 1px solid #ddd;
padding: 15px;
padding-top: 0px;
margin-top: 5px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
white-space: pre-wrap;
height: 100vh;
overflow-y: auto;
}
</style>
<script>
var refreshInterval = null;
function fetchLog() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/cgi-bin/luci/admin/services/ddns-go/fetch_log', true);
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
var logLines = xhr.responseText.split("\n").reverse().join("\n");
document.getElementById('logContent').textContent = logLines;
} else {
document.getElementById('logContent').textContent = 'Failed to load log.';
}
};
xhr.onerror = function () {
document.getElementById('logContent').textContent = 'Error loading log.';
};
xhr.send();
}
function clearLog() {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/cgi-bin/luci/admin/services/ddns-go/clear_log', true);
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
fetchLog(); // 刷新日志内容
} else {
alert('Failed to clear log.');
}
};
xhr.onerror = function () {
alert('Error clearing log.');
};
xhr.send();
}
function toggleRefresh() {
var refreshButton = document.querySelector("input[onclick='toggleRefresh()']");
if (refreshInterval) {
clearInterval(refreshInterval);
refreshInterval = null;
refreshButton.value = '开始刷新';
} else {
fetchLog(); // 立即获取一次日志
refreshInterval = setInterval(fetchLog, 1000);
refreshButton.value = '停止刷新';
}
}
// 页面加载完成后开始刷新日志
document.addEventListener('DOMContentLoaded', function () {
toggleRefresh();
});
</script>

View File

@ -0,0 +1,33 @@
<%
protocol="http://"
%>
<script type="text/javascript">//<![CDATA[
XHR.poll(3, '<%=url([[admin]], [[services]], [[ddnsgo_status]])%>', null,
function(x, data) {
var tb = document.getElementById('ddnsgo_status');
if (data && tb)
{
if (data.running)
{
var URL = "<%=protocol%>" + window.location.hostname +":<%=luci.model.uci.cursor():get_first("ddns-go", "basic", "port") %>";
tb.innerHTML = '<em><b style=\"color:green\"><%:The DDNS-GO service is running.%></b></em>';
tb.innerHTML +='<em> <br/><br/>' + ' <b > <%:Click the new page to open ddns-go%> </em>';
tb.innerHTML += "<input class=\"cbi-button cbi-button-reload \" type=\"button\" value=\" "+ URL+ "\" onclick=\"window.open('"+ URL+ "/')\"/>";
}
else
{
tb.innerHTML = '<em style=\"color:red\"><%:The DDNS-GO service is not running.%></em><br/>';
}
}
}
);
//]]></script>
<style>.mar-10 {margin-left: 50px; margin-right: 10px;}</style>
<fieldset class="cbi-section">
<legend><%:DDNS-GO Status%></legend>
<p id="ddnsgo_status">
<em><%:Collecting data...%></em>
</p>
</fieldset>

View File

@ -16,6 +16,9 @@ msgstr "运行状态"
msgid "DDNS-GO Control panel"
msgstr "DDNS-GO操作台"
msgid "Log"
msgstr "日志"
msgid "The DDNS-GO service is running."
msgstr "DDNS-GO服务已启动"

View File

@ -16,6 +16,9 @@ msgstr "运行状态"
msgid "DDNS-GO Control panel"
msgstr "DDNS-GO操作台"
msgid "Log"
msgstr "日志"
msgid "The DDNS-GO service is running."
msgstr "DDNS-GO服务已启动"