🤞 Sync 2026-09-27 22:16:29

This commit is contained in:
github-actions[bot] committed 2026-09-27 22:16:29 +08:00
1 parent 8ede4a8320
commit 618f51e9dc
459 files changed
+44817 -2191

No files matched your search

@@ -0,0 +1,86 @@
<div class="netstat-about-card">
<h3 style="margin:0 0 4px;"><%:LuCI Netstat%></h3>
<p style="margin:0 0 16px;font-size:13px;color:#6b7280;">
<%:Frontend for traffic monitoring and usage charts on OpenWrt.%>
</p>
<div class="netstat-about-grid">
<div class="netstat-about-row">
<span class="netstat-about-label"><%:Developer%></span>
<strong>NoobLk</strong>
</div>
<div class="netstat-about-row">
<span class="netstat-about-label"><%:Repository%></span>
<a href="https://github.com/nooblk-98/luci-app-netstat"
target="_blank" rel="noopener noreferrer">
github.com/nooblk-98/luci-app-netstat
</a>
</div>
<div class="netstat-about-row">
<span class="netstat-about-label"><%:Contributors%></span>
<a href="https://github.com/nooblk-98/luci-app-netstat/graphs/contributors"
target="_blank" rel="noopener noreferrer">
<%:View on GitHub%>
</a>
</div>
<div class="netstat-about-row">
<span class="netstat-about-label"><%:License%></span>
<span>GPL-3.0</span>
</div>
<div class="netstat-about-row">
<span class="netstat-about-label"><%:Issues / Support%></span>
<a href="https://github.com/nooblk-98/luci-app-netstat/issues"
target="_blank" rel="noopener noreferrer">
<%:Open an issue%>
</a>
</div>
</div>
<hr style="border:none;border-top:1px solid #e5e7eb;margin:16px 0 12px;">
<p style="margin:0;font-size:12px;color:#9ca3af;text-align:center;">
<%:Made with%> ♥ <%:for the OpenWrt community%>
</p>
</div>
<style>
.netstat-about-card {
border: 1px solid #dfe3eb;
border-radius: 10px;
background: #fff;
padding: 20px 22px;
max-width: 520px;
}
.netstat-about-grid { display: flex; flex-direction: column; gap: 10px; }
.netstat-about-row {
display: flex;
align-items: baseline;
gap: 8px;
font-size: 13px;
word-break: break-word;
}
.netstat-about-label {
min-width: 110px;
flex-shrink: 0;
color: #6b7280;
font-weight: 500;
}
.netstat-about-row a {
color: #2563eb;
text-decoration: none;
}
.netstat-about-row a:hover { text-decoration: underline; }
@media (max-width: 480px) {
.netstat-about-row { flex-direction: column; gap: 2px; }
.netstat-about-label { min-width: unset; }
}
</style>
@@ -0,0 +1,126 @@
<%
local http = require "luci.http"
local dispatcher = require "luci.dispatcher"
local active_tab = http.formvalue("tab") or "usage"
local base_url = dispatcher.build_url("admin", "tools", "netstat")
local function tab_url(tab) return base_url .. "?tab=" .. tab end
local tabs_def = {
{ id="usage", label=translate("Network Usage") },
{ id="settings", label=translate("Settings") },
{ id="about", label=translate("About") },
}
%>
<style>
.netstat-tab-wrap { margin: 4px 0 14px; }
.netstat-tabmenu { display:flex; gap:4px; margin:0; padding:0; list-style:none; flex-wrap:wrap; }
.netstat-tabmenu li a {
display: flex;
align-items: center;
gap: 6px;
padding: 8px 16px;
border: 1px solid #cfd6e2;
border-bottom: none;
border-radius: 6px 6px 0 0;
background: #e6ebf2;
color: #2f3b52;
text-decoration: none;
font-size: 13px;
font-weight: 500;
transition: background 0.1s, color 0.1s;
}
.netstat-tabmenu li a:hover:not(.active-link) {
background: #d8e2f0;
}
.netstat-tabmenu li.active a,
.netstat-tabmenu li a.active-link {
background: #f7f9fc;
border-color: #cfd6e2;
border-bottom-color: #f7f9fc;
color: #3659d9;
font-weight: 600;
}
.netstat-tab-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
opacity: 0;
transition: opacity 0.15s;
flex-shrink: 0;
}
.netstat-tabmenu li.active .netstat-tab-dot { opacity: 1; }
.netstat-tab-divider {
border: none;
border-top: 1px solid #cfd6e2;
margin-top: -1px;
margin-bottom: 0;
}
<% if active_tab == "settings" then %>
.cbi-map > .cbi-section:last-of-type {
border: 1px solid #dfe3eb;
border-radius: 10px;
background: #fff;
padding: 16px 18px;
}
<% end %>
</style>
<style id="netstat-dark-style"></style>
<script>
(function () {
var isDark = localStorage.getItem('luci-theme') === 'dark'
|| document.body.classList.contains('dark-theme');
window._netstatDark = isDark;
if (!isDark) return;
document.getElementById('netstat-dark-style').textContent = [
'.netstat-tabmenu li a{background:#2a2d32!important;border-color:#4b5563!important;color:#d1d5db!important}',
'.netstat-tabmenu li.active a{background:#1a1d20!important;border-color:#4b5563!important;border-bottom-color:#1a1d20!important;color:#60a5fa!important}',
'.netstat-tab-divider{border-top-color:#4b5563!important}',
'.netstat-dashboard{background:#1a1d20!important;border-color:#374151!important;color:#f3f4f6!important}',
'.netstat-summary{background:#111315!important;border-color:#374151!important;color:#f3f4f6!important}',
'.netstat-chart-wrap{background:#1a1d20!important;border-color:#374151!important}',
'.netstat-chart-header{background:#1a1d20!important}',
'.netstat-chart-title{color:#f3f4f6!important}',
'.netstat-chart-badge{background:#1e3a5f!important;color:#93c5fd!important}',
'.netstat-summary-label{color:#9ca3af!important}',
'.netstat-summary-item{color:#f3f4f6!important}',
'.netstat-empty{color:#9ca3af!important}',
'.netstat-hint{color:#6b7280!important}',
'.netstat-about-card{background:#1a1d20!important;border-color:#374151!important;color:#f3f4f6!important}',
'.netstat-about-card h3{color:#f3f4f6!important}',
'.netstat-about-card hr{border-top-color:#374151!important}',
'.netstat-about-label{color:#9ca3af!important}',
'.netstat-about-row a{color:#60a5fa!important}',
'.cbi-map>.cbi-section:last-of-type{background:#1a1d20!important;border-color:#374151!important}'
].join('');
})();
</script>
<div class="netstat-tab-wrap">
<ul class="netstat-tabmenu">
<% for _, tab in ipairs(tabs_def) do %>
<li class="<%= active_tab == tab.id and "active" or "" %>">
<a href="<%= tab_url(tab.id) %>">
<span class="netstat-tab-dot"></span>
<%= tab.label %>
</a>
</li>
<% end %>
</ul>
<hr class="netstat-tab-divider" />
</div>
<input type="hidden" name="tab" value="<%=active_tab%>" />
@@ -0,0 +1,293 @@
<%
local sys = require "luci.sys"
local http = require "luci.http"
local uci = require "luci.model.uci".cursor()
local function to_bytes(value, unit)
local m = { KiB=1024, MiB=1048576, GiB=1073741824, TiB=1099511627776 }
return tonumber(value) * (m[unit] or 1)
end
local function format_date(date_str)
local months = {"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"}
local mo, d, y = date_str:match("(%d%d)/(%d%d)/(%d%d)")
if mo and d and y then
return string.format("%02d %s %02d", tonumber(d), months[tonumber(mo)], tonumber(y))
end
return date_str
end
local function today_label() return os.date("%d %b %y") end
local function curmonth_label() return os.date("%b '%y") end
local function ensure_today(stats)
local lbl = today_label()
for _, s in ipairs(stats) do if s.date == lbl then return stats end end
table.insert(stats, { date=lbl, rx=0, tx=0 })
return stats
end
local function ensure_curmonth(stats)
local lbl = curmonth_label()
for _, s in ipairs(stats) do if s.month == lbl then return stats end end
table.insert(stats, { month=lbl, rx=0, tx=0 })
return stats
end
local function get_interfaces()
local ifaces = {}
local p = io.popen("ls /etc/vnstat/ 2>/dev/null | grep -v '.conf$' | grep -v '.old$'")
if p then
for iface in p:lines() do
iface = iface:match("^%s*(.-)%s*$")
if iface ~= "" then table.insert(ifaces, iface) end
end
p:close()
end
table.sort(ifaces)
return ifaces
end
local function get_daily_stats(iface)
local stats = {}
local out = sys.exec("vnstat -i " .. iface .. " --days 2>/dev/null") or ""
for date, rx_v, rx_u, tx_v, tx_u in
out:gmatch("(%d%d/%d%d/%d%d)%s+([%d%.]+)%s+([KMGT]iB)%s+|%s+([%d%.]+)%s+([KMGT]iB)") do
table.insert(stats, {
date = format_date(date),
rx = to_bytes(rx_v, rx_u),
tx = to_bytes(tx_v, tx_u)
})
end
return ensure_today(stats)
end
local function get_monthly_stats(iface)
local stats = {}
local out = sys.exec("vnstat -i " .. iface .. " --months 2>/dev/null") or ""
for month, rx_v, rx_u, tx_v, tx_u in
out:gmatch("(%a+%s+'%d%d)%s+([%d%.]+)%s+([KMGT]iB)%s+|%s+([%d%.]+)%s+([KMGT]iB)") do
table.insert(stats, {
month = month,
rx = to_bytes(rx_v, rx_u),
tx = to_bytes(tx_v, tx_u)
})
end
return ensure_curmonth(stats)
end
local function fmt_bytes(v)
if v >= 1099511627776 then return string.format("%.2f TB", v/1099511627776)
elseif v >= 1073741824 then return string.format("%.2f GB", v/1073741824)
elseif v >= 1048576 then return string.format("%.2f MB", v/1048576)
elseif v >= 1024 then return string.format("%.2f KB", v/1024)
end
return tostring(v) .. " B"
end
local ifaces = get_interfaces()
local saved_iface = uci:get("netstats", "@config[0]", "prefer") or ""
local saved_mode = uci:get("netstats", "@config[0]", "mode") or "daily"
local cur_iface = http.formvalue("iface") or saved_iface or ifaces[1] or ""
local view_mode = http.formvalue("mode") or saved_mode
if view_mode ~= "daily" and view_mode ~= "monthly" then view_mode = "daily" end
local stats, title = {}, ""
if cur_iface ~= "" then
if view_mode == "monthly" then
stats = get_monthly_stats(cur_iface)
title = translate("Monthly")
else
stats = get_daily_stats(cur_iface)
title = translate("Daily")
end
end
local total_rx, total_tx = 0, 0
for _, s in ipairs(stats) do
total_rx = total_rx + (s.rx or 0)
total_tx = total_tx + (s.tx or 0)
end
%>
<link rel="stylesheet" href="/luci-static/resources/netstat/netstat-usage.css" />
<div class="netstat-dashboard">
<p style="margin:0 0 12px;font-size:13px;color:#6b7280;">
<%:Usage is displayed using the Interface and View Mode selected in the Settings tab.%>
</p>
<% if cur_iface ~= "" and #stats > 0 then %>
<div class="netstat-summary">
<div class="netstat-summary-item">
<span class="netstat-summary-dot rx"></span>
<span class="netstat-summary-label"><%:Download%>:</span>
<strong><%=fmt_bytes(total_rx)%></strong>
</div>
<div class="netstat-summary-item">
<span class="netstat-summary-dot tx"></span>
<span class="netstat-summary-label"><%:Upload%>:</span>
<strong><%=fmt_bytes(total_tx)%></strong>
</div>
<div class="netstat-summary-item netstat-summary-total">
<span class="netstat-summary-label"><%:Total%>:</span>
<strong><%=fmt_bytes(total_rx + total_tx)%></strong>
</div>
</div>
<div class="netstat-chart-wrap">
<div class="netstat-chart-header">
<h3 class="netstat-chart-title"><%=title%> <%:Usage%></h3>
<span class="netstat-chart-badge"><%=cur_iface%></span>
</div>
<canvas id="trafficChart" class="netstat-chart-canvas"></canvas>
</div>
<script src="/luci-static/resources/netstat/chart.js"></script>
<script src="/luci-static/resources/netstat/chartjs-plugin-datalabels"></script>
<script>
(function () {
var isDark = window._netstatDark || false;
var textColor = isDark ? '#f3f4f6' : '#1f2937';
var gridColor = isDark ? 'rgba(255,255,255,0.10)' : 'rgba(0,0,0,0.08)';
var labels = [
<% for i, stat in ipairs(stats) do
local lbl = view_mode == "daily" and stat.date or stat.month
%>"<%=lbl%>"<% if i < #stats then %>,<% end %>
<% end %>
];
var rxData = [
<% for i, stat in ipairs(stats) do
%><%= math.floor(stat.rx) %><% if i < #stats then %>,<% end %>
<% end %>
];
var txData = [
<% for i, stat in ipairs(stats) do
%><%= math.floor(stat.tx) %><% if i < #stats then %>,<% end %>
<% end %>
];
var BAR_H = 48;
var canvas = document.getElementById('trafficChart');
canvas.height = Math.min(Math.max(labels.length * BAR_H, 280), 700);
canvas.width = canvas.offsetWidth || canvas.parentElement.clientWidth;
var ctx = canvas.getContext('2d');
function rxGradient() {
var g = ctx.createLinearGradient(0, 0, canvas.width || 600, 0);
g.addColorStop(0, '#86efac');
g.addColorStop(1, '#15803d');
return g;
}
function fmtBytes(v) {
if (v >= 1099511627776) return (v/1099511627776).toFixed(2) + ' TB';
if (v >= 1073741824) return (v/1073741824).toFixed(2) + ' GB';
if (v >= 1048576) return (v/1048576).toFixed(2) + ' MB';
if (v >= 1024) return (v/1024).toFixed(2) + ' KB';
return v + ' B';
}
var maxVal = Math.max.apply(null, rxData.concat(txData).concat([1]));
new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [
{
label: '<%:Download (RX)%>',
data: rxData,
backgroundColor: rxData.map(function() { return rxGradient(); }),
borderRadius: 4,
borderSkipped: false,
datalabels: {
color: '#fff',
anchor: 'center',
align: 'center',
font: { size: 11, weight: '600' },
formatter: fmtBytes,
display: function(ctx) {
return maxVal > 0 && ctx.dataset.data[ctx.dataIndex] / maxVal > 0.08;
}
}
},
{
label: '<%:Upload (TX)%>',
data: txData,
backgroundColor: '#f59e0b',
borderRadius: 4,
borderSkipped: false,
datalabels: {
color: textColor,
anchor: 'end',
align: 'right',
font: { size: 11, weight: '600' },
formatter: fmtBytes,
display: function(ctx) {
return maxVal > 0 && ctx.dataset.data[ctx.dataIndex] / maxVal > 0.02;
}
}
}
]
},
options: {
indexAxis: 'y',
responsive: false,
maintainAspectRatio: false,
animation: { duration: 400 },
plugins: {
legend: {
position: 'top',
labels: {
color: textColor,
usePointStyle: true,
pointStyle: 'circle',
padding: 16
}
},
tooltip: {
callbacks: {
label: function(ctx) { return ' ' + fmtBytes(ctx.raw); }
}
}
},
scales: {
x: {
grid: { color: gridColor },
ticks: {
color: textColor,
callback: fmtBytes,
maxTicksLimit: 6
}
},
y: {
grid: { display: false },
ticks: { color: textColor, autoSkip: false }
}
}
},
plugins: [ChartDataLabels]
});
})();
</script>
<% elseif #ifaces == 0 then %>
<div class="netstat-empty">
<span class="netstat-empty-icon">&#128225;</span>
<%:No vnStat interfaces were found. Make sure vnStat has been initialized.%>
</div>
<% else %>
<div class="netstat-empty">
<span class="netstat-empty-icon">&#128202;</span>
<%:No data available for%> <strong><%=cur_iface%></strong>
<div class="netstat-hint">
<%:vnStat may still be collecting its first data. Try again in a few minutes.%>
</div>
</div>
<% end %>
</div>