luci-app-ssr-plus: allow custom IP/CIDR rules for Mihomo Clash node

This commit is contained in:
coolsnowwolf committed 2026-05-03 23:43:27 +08:00
1 parent 827f1d2cfd
commit 4b8378481b
8 files changed
+713 -15

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@ LUCI_TITLE:=luci-app-ssr-plus
LUCI_PKGARCH:=all
PKG_NAME:=luci-app-ssr-plus
PKG_VERSION:=192
PKG_RELEASE:=7
PKG_RELEASE:=8
PKG_CONFIG_DEPENDS:= \
CONFIG_PACKAGE_$(PKG_NAME)_Iptables_Transparent_Proxy \
@@ -5,6 +5,8 @@ require "nixio"
require "nixio.fs"
require "luci.util"
require "luci.template"
require "luci.sys"
local datatypes = require "luci.cbi.datatypes"
local json = require "luci.jsonc"
local uci = require "luci.model.uci".cursor()
@@ -23,6 +25,68 @@ local SUPPORTED_GEO_COMPONENTS = {
v2ray_geo = true
}
local function trim(value)
return tostring(value or ""):gsub("^%s+", ""):gsub("%s+$", "")
end
local function sanitize_mac(value)
value = trim(value):upper():gsub("-", ":")
if value:match("^%x%x:%x%x:%x%x:%x%x:%x%x:%x%x$") then
return value
end
return ""
end
local function normalize_client_ip(value)
value = trim(value)
if value == "" then
return ""
end
if datatypes.cidr4(value) or datatypes.ip4addr(value) then
return value
end
return ""
end
local function collect_lan_clients()
local clients = {}
local seen = {}
luci.sys.net.host_hints(function(mac, ipv4, _, name)
local ip = trim(ipv4)
local norm_mac = sanitize_mac(mac)
if ip ~= "" and not seen[ip] then
seen[ip] = true
clients[#clients + 1] = {
ip = ip,
mac = norm_mac,
name = trim(name) ~= "" and trim(name) or ip
}
end
end)
table.sort(clients, function(a, b)
return tostring(a.name or a.ip) < tostring(b.name or b.ip)
end)
return clients
end
local function read_clash_client_rules()
local rules = {}
uci:foreach("shadowsocksr", "clash_client_group", function(section)
rules[#rules + 1] = {
id = section[".name"],
enabled = section.enabled == "1",
remarks = section.remarks or "",
ip_addr = section.ip_addr or "",
client_mac = section.client_mac or "",
policy_group = section.policy_group or ""
}
end)
return rules
end
local function normalize_ping_ms(value, scale)
local num = tonumber(value)
if not num or num <= 0 then
@@ -418,6 +482,8 @@ function index()
entry({"admin", "services", "shadowsocksr", "clash_switch"}, call("clash_switch")).leaf = true
entry({"admin", "services", "shadowsocksr", "clash_refresh"}, call("clash_refresh")).leaf = true
entry({"admin", "services", "shadowsocksr", "clash_reset_defaults"}, call("clash_reset_defaults")).leaf = true
entry({"admin", "services", "shadowsocksr", "clash_client_policies"}, call("clash_client_policies")).leaf = true
entry({"admin", "services", "shadowsocksr", "clash_client_rule_save"}, call("clash_client_rule_save")).leaf = true
--[[Backup]]
entry({"admin", "services", "shadowsocksr", "backup"}, call("create_backup")).leaf = true
end
@@ -765,6 +831,111 @@ function clash_reset_defaults()
})
end
function clash_client_policies()
local sid = luci.http.formvalue("sid")
local active_sid = resolve_active_clash_sid(sid)
local groups = {}
if active_sid then
local raw = clash_api_request(active_sid, "GET", "/proxies")
if raw then
groups = parse_clash_groups(raw.body)
end
end
local policies = {}
local seen = {}
for _, group in ipairs(groups or {}) do
if group.name and not seen[group.name] then
seen[group.name] = true
policies[#policies + 1] = {
name = group.name,
label = group.name,
type = group.type or ""
}
end
for _, proxy_name in ipairs(group.all or {}) do
if proxy_name and proxy_name ~= "" and not seen[proxy_name] then
seen[proxy_name] = true
policies[#policies + 1] = {
name = proxy_name,
label = proxy_name,
type = "proxy"
}
end
end
end
table.sort(policies, function(a, b)
return tostring(a.label or a.name) < tostring(b.label or b.name)
end)
luci.http.prepare_content("application/json")
luci.http.write_json({
active = active_sid ~= nil,
sid = active_sid,
clients = collect_lan_clients(),
rules = read_clash_client_rules(),
policies = policies
})
end
function clash_client_rule_save()
local sid = luci.http.formvalue("sid")
local rows = {}
local max_rows = tonumber(luci.http.formvalue("count") or "0") or 0
for index = 1, math.min(max_rows, 256) do
local prefix = string.format("rule_%d_", index)
local ip_addr = normalize_client_ip(luci.http.formvalue(prefix .. "ip_addr"))
local enabled = luci.http.formvalue(prefix .. "enabled") == "1" and "1" or "0"
local remarks = trim(luci.http.formvalue(prefix .. "remarks"))
local policy_group = trim(luci.http.formvalue(prefix .. "policy_group"))
local client_mac = sanitize_mac(luci.http.formvalue(prefix .. "client_mac"))
if ip_addr ~= "" and policy_group ~= "" then
rows[#rows + 1] = {
enabled = enabled,
remarks = remarks,
ip_addr = ip_addr,
client_mac = client_mac,
policy_group = policy_group
}
end
end
uci:delete_all("shadowsocksr", "clash_client_group")
for _, row in ipairs(rows) do
local rid = uci:add("shadowsocksr", "clash_client_group")
if rid then
uci:set("shadowsocksr", rid, "enabled", row.enabled)
uci:set("shadowsocksr", rid, "remarks", row.remarks)
uci:set("shadowsocksr", rid, "ip_addr", row.ip_addr)
uci:set("shadowsocksr", rid, "client_mac", row.client_mac)
uci:set("shadowsocksr", rid, "policy_group", row.policy_group)
end
end
uci:save("shadowsocksr")
uci:commit("shadowsocksr")
local active_sid = resolve_active_clash_sid(sid)
local current_sid = uci:get_first("shadowsocksr", "global", "global_server")
local reapplied = false
if sid and sid ~= "" and uci:get("shadowsocksr", sid) == "servers"
and uci:get("shadowsocksr", sid, "type") == "clash"
and current_sid == sid then
luci.sys.call("/etc/init.d/shadowsocksr restart >/dev/null 2>&1 &")
reapplied = true
end
luci.http.prepare_content("application/json")
luci.http.write_json({
success = true,
count = #rows,
reapplied = reapplied,
rules = read_clash_client_rules()
})
end
function act_ping()
local e = {}
local domain = luci.http.formvalue("domain")
@@ -44,7 +44,8 @@ local current_sid = self.map:get(section, "global_server") or "nil"
left: 50%;
transform: translate(-50%, -50%);
width: min(900px, calc(100vw - 32px));
max-height: min(720px, calc(100vh - 80px));
min-height: min(570px, calc(100vh - 48px));
max-height: min(645px, calc(100vh - 48px));
overflow: auto;
background: var(--ssr-clash-modal-bg);
color: var(--ssr-clash-modal-fg);
@@ -98,6 +99,118 @@ local current_sid = self.map:get(section, "global_server") or "nil"
color: var(--ssr-clash-modal-muted);
}
#ssr-clash-client-rules {
display: none;
}
#ssr-clash-client-rules table {
width: 100%;
border-collapse: collapse;
overflow: visible;
}
#ssr-clash-client-rules th,
#ssr-clash-client-rules td {
padding: 8px 6px;
vertical-align: middle;
overflow: visible;
position: relative;
}
#ssr-clash-client-rules th {
color: var(--ssr-clash-modal-muted);
font-weight: 600;
text-align: left;
}
#ssr-clash-client-rules .cbi-input-select,
#ssr-clash-client-rules .cbi-input-text,
#ssr-clash-client-rules select,
#ssr-clash-client-rules input[type="text"] {
width: 100%;
background: var(--ssr-clash-field-bg) !important;
color: var(--ssr-clash-field-fg) !important;
border: 1px solid var(--ssr-clash-field-border) !important;
box-shadow: none;
box-sizing: border-box;
}
#ssr-clash-client-rules .ssr-client-input-wrap {
position: relative;
z-index: 50;
}
#ssr-clash-client-rules .ssr-client-suggest {
display: none;
position: absolute;
left: 0;
right: 0;
top: calc(100% + 4px);
z-index: 2005;
max-height: 240px;
overflow: auto;
background: var(--ssr-clash-field-bg);
color: var(--ssr-clash-field-fg);
border: 1px solid var(--ssr-clash-field-border);
border-radius: 8px;
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.16);
min-width: 100%;
}
#ssr-clash-client-rules .ssr-client-suggest.is-open {
display: block;
}
#ssr-clash-client-rules tr:has(.ssr-client-suggest.is-open),
#ssr-clash-client-rules td:has(.ssr-client-suggest.is-open) {
z-index: 60;
}
#ssr-clash-client-rules .ssr-client-suggest-item {
padding: 10px 12px;
cursor: pointer;
line-height: 1.35;
word-break: break-all;
}
#ssr-clash-client-rules .ssr-client-suggest-item:hover,
#ssr-clash-client-rules .ssr-client-suggest-item.active {
background: rgba(31, 111, 235, 0.12);
}
#ssr-clash-client-rules option {
background: var(--ssr-clash-field-bg);
color: var(--ssr-clash-field-fg);
}
#ssr-clash-client-rules .ssr-clash-rules-actions {
display: flex;
gap: 8px;
align-items: center;
justify-content: space-between;
margin: 0 0 10px;
flex-wrap: wrap;
}
#ssr-clash-client-rules .ssr-clash-rules-actions .left,
#ssr-clash-client-rules .ssr-clash-rules-actions .right {
display: flex;
gap: 8px;
align-items: center;
flex-wrap: wrap;
}
#ssr-clash-client-rules .ssr-clash-rules-note {
color: var(--ssr-clash-modal-muted);
font-size: 13px;
margin: 0 0 10px;
}
#ssr-clash-client-rules .ssr-clash-rules-empty {
color: var(--ssr-clash-modal-muted);
padding: 12px 0;
}
#ssr-clash-modal-groups .cbi-input-select,
#ssr-clash-modal-groups select {
background: var(--ssr-clash-field-bg) !important;
@@ -132,6 +245,7 @@ local current_sid = self.map:get(section, "global_server") or "nil"
#ssr-clash-modal {
width: calc(100vw - 16px);
max-height: calc(100vh - 24px);
min-height: min(420px, calc(100vh - 24px));
padding: 10px 12px 12px;
}
@@ -202,6 +316,10 @@ local current_sid = self.map:get(section, "global_server") or "nil"
(function() {
var currentPanelSid = '<%=current_sid%>';
var currentEditorSid = '';
var clientPolicies = [];
var lanClients = [];
var clientRules = [];
var clashNodes = {
<%
for sid, _ in pairs(clash_nodes) do
@@ -241,6 +359,155 @@ local current_sid = self.map:get(section, "global_server") or "nil"
return dict[name] || name;
}
function normalizeList(value) {
return Array.isArray(value) ? value : [];
}
function renderClientRuleRows() {
var body = document.getElementById('ssr-clash-client-rules-body');
if (!body) {
return;
}
body.innerHTML = '';
if (!clientRules.length) {
var row = document.createElement('tr');
var cell = document.createElement('td');
cell.colSpan = 5;
cell.className = 'ssr-clash-rules-empty';
cell.textContent = '<%:No custom client proxy rules yet.%>';
row.appendChild(cell);
body.appendChild(row);
return;
}
clientRules.forEach(function(rule, index) {
var row = document.createElement('tr');
var enabledCell = document.createElement('td');
var enabled = document.createElement('input');
enabled.type = 'checkbox';
enabled.checked = !!rule.enabled;
enabled.onchange = function() { rule.enabled = enabled.checked; };
enabledCell.appendChild(enabled);
var clientCell = document.createElement('td');
var clientWrap = document.createElement('div');
clientWrap.className = 'ssr-client-input-wrap';
var clientInput = document.createElement('input');
clientInput.type = 'text';
clientInput.className = 'cbi-input-text';
clientInput.value = rule.ip_addr || '';
var suggest = document.createElement('div');
suggest.className = 'ssr-client-suggest';
function setClientByValue(value) {
rule.ip_addr = value;
var matched = lanClients.find(function(client) {
return client.ip === value;
});
rule.client_mac = matched ? (matched.mac || '') : '';
if (!remarkInput.value && matched) {
rule.remarks = matched.name || matched.ip;
remarkInput.value = rule.remarks;
}
}
function renderSuggestions(keyword) {
var text = (keyword || '').toLowerCase();
suggest.innerHTML = '';
var matchedClients = lanClients.filter(function(client) {
var label = (client.name + ' ' + client.ip + ' ' + (client.mac || '')).toLowerCase();
return !text || label.indexOf(text) >= 0;
});
if (!matchedClients.length) {
suggest.classList.remove('is-open');
return;
}
matchedClients.forEach(function(client) {
var item = document.createElement('div');
item.className = 'ssr-client-suggest-item';
item.textContent = client.name + ' (' + client.ip + ')';
item.onmousedown = function(ev) {
ev.preventDefault();
clientInput.value = client.ip;
setClientByValue(client.ip);
suggest.classList.remove('is-open');
};
suggest.appendChild(item);
});
suggest.classList.add('is-open');
}
var remarksCell = document.createElement('td');
var remarkInput = document.createElement('input');
remarkInput.type = 'text';
remarkInput.className = 'cbi-input-text';
remarkInput.value = rule.remarks || '';
clientInput.onfocus = function() {
renderSuggestions(clientInput.value);
};
clientInput.oninput = function() {
setClientByValue(clientInput.value);
renderSuggestions(clientInput.value);
};
clientInput.onblur = function() {
window.setTimeout(function() {
suggest.classList.remove('is-open');
}, 120);
};
clientWrap.appendChild(clientInput);
clientWrap.appendChild(suggest);
clientCell.appendChild(clientWrap);
var policyCell = document.createElement('td');
var policySelect = document.createElement('select');
policySelect.className = 'cbi-input-select';
clientPolicies.forEach(function(policy) {
var option = document.createElement('option');
option.value = policy.name;
option.textContent = translateClashName(policy.label || policy.name);
if (policy.name === rule.policy_group) {
option.selected = true;
}
policySelect.appendChild(option);
});
policySelect.onchange = function() {
rule.policy_group = policySelect.value;
};
policyCell.appendChild(policySelect);
remarkInput.oninput = function() {
rule.remarks = remarkInput.value;
};
remarksCell.appendChild(remarkInput);
var actionCell = document.createElement('td');
var removeBtn = document.createElement('input');
removeBtn.type = 'button';
removeBtn.className = 'btn cbi-button cbi-button-remove';
removeBtn.value = '<%:Delete%>';
removeBtn.onclick = function() {
clientRules.splice(index, 1);
renderClientRuleRows();
};
actionCell.appendChild(removeBtn);
row.appendChild(enabledCell);
row.appendChild(clientCell);
row.appendChild(policyCell);
row.appendChild(remarksCell);
row.appendChild(actionCell);
body.appendChild(row);
});
setPanelStatus('<%:Ready.%>', '#249400');
}
function renderGroups(groups) {
var groupsEl = document.getElementById('ssr-clash-modal-groups');
if (!groupsEl) {
@@ -300,12 +567,111 @@ local current_sid = self.map:get(section, "global_server") or "nil"
});
}
function addClientRule() {
var firstClient = lanClients[0] || { ip: '', mac: '', name: '' };
var firstPolicy = clientPolicies[0] || { name: '', label: '' };
clientRules.push({
enabled: true,
ip_addr: firstClient.ip || '',
client_mac: firstClient.mac || '',
remarks: firstClient.name || firstClient.ip || '',
policy_group: firstPolicy.name || ''
});
renderClientRuleRows();
return false;
}
function saveClientRules(btn) {
if (!currentEditorSid) {
return false;
}
var original = btn.value;
btn.disabled = true;
btn.value = '<%:Saving...%>';
var payload = {
sid: currentEditorSid,
count: String(clientRules.length)
};
clientRules.forEach(function(rule, idx) {
var index = idx + 1;
payload['rule_' + index + '_enabled'] = rule.enabled ? '1' : '0';
payload['rule_' + index + '_ip_addr'] = rule.ip_addr || '';
payload['rule_' + index + '_client_mac'] = rule.client_mac || '';
payload['rule_' + index + '_remarks'] = rule.remarks || '';
payload['rule_' + index + '_policy_group'] = rule.policy_group || '';
});
XHR.post('<%=luci.dispatcher.build_url("admin", "services", "shadowsocksr", "clash_client_rule_save")%>', payload, function(x, result) {
btn.disabled = false;
btn.value = original;
if (result && result.success) {
clientRules = normalizeList(result.rules);
renderClientRuleRows();
setPanelStatus(result.reapplied ? '<%:Custom client proxy rules saved and applied.%>' : '<%:Custom client proxy rules saved.%>', '#249400');
} else {
setPanelStatus('<%:Failed to save custom client proxy rules.%>', '#ff0000');
}
});
return false;
}
function openClientRulesPanel(sid) {
currentEditorSid = sid;
setPanelStatus('<%:Loading...%>', '#666');
var groupsEl = document.getElementById('ssr-clash-modal-groups');
var rulesEl = document.getElementById('ssr-clash-client-rules');
var refreshBtn = document.getElementById('ssr-clash-modal-refresh');
if (groupsEl) groupsEl.style.display = 'none';
if (rulesEl) rulesEl.style.display = 'block';
if (refreshBtn) refreshBtn.style.display = 'none';
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "shadowsocksr", "clash_client_policies")%>', {
sid: sid
}, function(x, result) {
if (!result || !result.active) {
setPanelStatus('<%:This Clash total node is not active. Apply this node first.%>', '#ff7700');
clientPolicies = [];
lanClients = [];
clientRules = [];
renderClientRuleRows();
return;
}
clientPolicies = normalizeList(result.policies);
lanClients = normalizeList(result.clients);
clientRules = normalizeList(result.rules);
renderClientRuleRows();
setPanelStatus('<%:Ready.%>', '#249400');
});
return false;
}
window.ssrClashAddClientRule = function() {
return addClientRule();
};
window.ssrClashSaveClientRules = function(btn) {
return saveClientRules(btn);
};
window.ssrClashOpenClientRulesPanel = function(sid) {
openPanel(sid);
return openClientRulesPanel(sid);
};
function loadGroups(sid) {
setPanelStatus('<%:Loading...%>', '#666');
var groupsEl = document.getElementById('ssr-clash-modal-groups');
var rulesEl = document.getElementById('ssr-clash-client-rules');
var refreshBtn = document.getElementById('ssr-clash-modal-refresh');
if (groupsEl) groupsEl.style.display = 'block';
if (rulesEl) rulesEl.style.display = 'none';
if (refreshBtn) refreshBtn.style.display = '';
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "shadowsocksr", "clash_groups")%>', {
sid: sid
}, function(x, result) {
var groupsEl = document.getElementById('ssr-clash-modal-groups');
if (result && result.sid) {
currentPanelSid = result.sid;
}
@@ -357,22 +723,22 @@ local current_sid = self.map:get(section, "global_server") or "nil"
var select = document.querySelector('select[name$=".global_server"]');
var wrap = document.getElementById('ssr-clash-main-panel-wrap');
var row = wrap ? wrap.closest('.cbi-value') : null;
var refreshBtn = document.getElementById('ssr-clash-main-refresh');
var customRulesBtn = document.getElementById('ssr-clash-main-client-rules');
var resetBtn = document.getElementById('ssr-clash-main-reset-defaults');
var panelBtn = document.getElementById('ssr-clash-main-open');
if (!select || !row || !refreshBtn || !resetBtn || !panelBtn) {
if (!select || !row || !customRulesBtn || !resetBtn || !panelBtn) {
return;
}
var sid = select.value;
if (sid && clashNodes[sid]) {
row.style.display = '';
refreshBtn.setAttribute('data-sid', sid);
customRulesBtn.setAttribute('data-sid', sid);
resetBtn.setAttribute('data-sid', sid);
panelBtn.setAttribute('data-sid', sid);
} else {
row.style.display = 'none';
refreshBtn.setAttribute('data-sid', '');
customRulesBtn.setAttribute('data-sid', '');
resetBtn.setAttribute('data-sid', '');
panelBtn.setAttribute('data-sid', '');
}
@@ -380,10 +746,12 @@ local current_sid = self.map:get(section, "global_server") or "nil"
window.setTimeout(function() {
var select = document.querySelector('select[name$=".global_server"]');
var refreshBtn = document.getElementById('ssr-clash-main-refresh');
var customRulesBtn = document.getElementById('ssr-clash-main-client-rules');
var resetBtn = document.getElementById('ssr-clash-main-reset-defaults');
var panelBtn = document.getElementById('ssr-clash-main-open');
var modalRefreshBtn = document.getElementById('ssr-clash-modal-refresh');
var modalRulesAddBtn = document.getElementById('ssr-clash-rules-add');
var modalRulesSaveBtn = document.getElementById('ssr-clash-rules-save');
var modalCloseBtn = document.getElementById('ssr-clash-modal-close');
var modalMask = document.getElementById('ssr-clash-modal-mask');
var wrap = document.getElementById('ssr-clash-main-panel-wrap');
@@ -397,11 +765,11 @@ local current_sid = self.map:get(section, "global_server") or "nil"
select.addEventListener('change', updateClashPanelMain);
}
if (refreshBtn) {
refreshBtn.onclick = function() {
if (customRulesBtn) {
customRulesBtn.onclick = function() {
var sid = this.getAttribute('data-sid');
if (!sid) return false;
return ssrClashRefreshSubscription(this, sid);
return window.ssrClashOpenClientRulesPanel(sid);
};
}
@@ -444,10 +812,10 @@ local current_sid = self.map:get(section, "global_server") or "nil"
<input
type="button"
id="ssr-clash-main-refresh"
id="ssr-clash-main-client-rules"
class="btn cbi-button"
value="<%:Reload YAML%>"
title="<%:Reload YAML%>"
value="<%:Custom Client Proxies Rules%>"
title="<%:Custom Client Proxies Rules%>"
data-sid="<%=current_sid%>" />
<input
type="button"
@@ -476,6 +844,29 @@ local current_sid = self.map:get(section, "global_server") or "nil"
</div>
<input type="button" id="ssr-clash-modal-close" class="btn cbi-button" value="<%:Close%>" />
</div>
<div id="ssr-clash-client-rules">
<div class="ssr-clash-rules-actions">
<div class="left">
<input type="button" id="ssr-clash-rules-add" class="btn cbi-button" value="<%:Add%>" onclick="return window.ssrClashAddClientRule();" />
</div>
<div class="right">
<input type="button" id="ssr-clash-rules-save" class="btn cbi-button cbi-button-apply" value="<%:Save%>" onclick="return window.ssrClashSaveClientRules(this);" />
</div>
</div>
<div class="ssr-clash-rules-note"><%:Define per-client Mihomo proxy targets. Rules are injected as SRC-IP-CIDR entries before the original Clash rules.%> <%:Client supports manual IP or IP/CIDR input.%></div>
<table>
<thead>
<tr>
<th style="width:1%"><%:Enable%></th>
<th style="width:28%"><%:Client%></th>
<th style="width:28%"><%:Policy%></th>
<th><%:Remarks%></th>
<th style="width:1%"><%:Actions%></th>
</tr>
</thead>
<tbody id="ssr-clash-client-rules-body"></tbody>
</table>
</div>
<div id="ssr-clash-modal-groups"></div>
</div>
</div>
@@ -1770,6 +1770,9 @@ msgstr ""
msgid "Reload YAML"
msgstr ""
msgid "Custom Client Proxies Rules"
msgstr ""
msgid "Reloading YAML..."
msgstr ""
@@ -1797,6 +1800,36 @@ msgstr ""
msgid "Reset Defaults Failed!"
msgstr ""
msgid "No custom client proxy rules yet."
msgstr ""
msgid "Client"
msgstr ""
msgid "Policy"
msgstr ""
msgid "Remarks"
msgstr ""
msgid "Saving..."
msgstr ""
msgid "Custom client proxy rules saved and applied."
msgstr ""
msgid "Custom client proxy rules saved."
msgstr ""
msgid "Failed to save custom client proxy rules."
msgstr ""
msgid "Define per-client Mihomo proxy targets. Rules are injected as SRC-IP-CIDR entries before the original Clash rules."
msgstr ""
msgid "Client supports manual IP or IP/CIDR input."
msgstr ""
#: applications/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/servers.lua:134
msgid "Regular update (Hour)"
msgstr ""
+33
View File
@@ -1935,6 +1935,9 @@ msgstr "正在更新,请稍候..."
msgid "Reload YAML"
msgstr "重载 YAML"
msgid "Custom Client Proxies Rules"
msgstr "自定义客户端代理规则"
msgid "Reloading YAML..."
msgstr "正在重载 YAML..."
@@ -1962,6 +1965,36 @@ msgstr "已清空默认状态!"
msgid "Reset Defaults Failed!"
msgstr "重置默认值失败!"
msgid "No custom client proxy rules yet."
msgstr "暂无自定义客户端代理规则。"
msgid "Client"
msgstr "客户端"
msgid "Policy"
msgstr "策略"
msgid "Remarks"
msgstr "备注"
msgid "Saving..."
msgstr "保存中..."
msgid "Custom client proxy rules saved and applied."
msgstr "自定义客户端代理规则已保存并生效。"
msgid "Custom client proxy rules saved."
msgstr "自定义客户端代理规则已保存。"
msgid "Failed to save custom client proxy rules."
msgstr "保存自定义客户端代理规则失败。"
msgid "Define per-client Mihomo proxy targets. Rules are injected as SRC-IP-CIDR entries before the original Clash rules."
msgstr "为每个客户端指定 Mihomo 代理目标。规则会以 SRC-IP-CIDR 形式注入到原始 Clash 规则之前。"
msgid "Client supports manual IP or IP/CIDR input."
msgstr "客户端支持手动输入 IP 或 IP/CIDR。"
#: applications/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/servers.lua:134
msgid "Regular update (Hour)"
msgstr "定时更新(小时)"
@@ -360,6 +360,14 @@ prepare_clash_runtime_config() {
if [ "$stripped_script_rules" -gt 0 ] 2>/dev/null; then
echolog "Clash YAML 兼容修复:共移除 $stripped_script_rules 条不兼容的 SCRIPT 规则。"
fi
local client_policy_stats=""
local client_rules=0
client_policy_stats="$(/usr/bin/lua "$CLASH_YAML_HELPER" append_client_policy_rules "$runtime_file" 2>/dev/null)" || true
client_rules="$(printf '%s\n' "$client_policy_stats" | sed -n 's/.*client_rules=\([0-9][0-9]*\).*/\1/p' | tail -n1)"
[ -n "$client_rules" ] || client_rules=0
if [ "$client_rules" -gt 0 ] 2>/dev/null; then
echolog "Clash YAML 自定义客户端代理规则已注入:$client_rules 条。"
fi
# Persist Mihomo's store-selected cache across service restarts.
if [ -f "$workdir/cache.db" ] && [ ! -L "$workdir/cache.db" ]; then
@@ -263,6 +263,59 @@ local function merge(raw_path, overlay_path, output_path)
return true
end
local function append_client_policy_rules(runtime_path)
local doc, err = load_yaml(runtime_path)
if not doc then
io.stderr:write(err or "parse_failed", "\n")
return false
end
local custom_rules = {}
uci:foreach("shadowsocksr", "clash_client_group", function(section)
if tostring(section.enabled or "0") == "1" then
local ip_addr = tostring(section.ip_addr or "")
local policy_group = tostring(section.policy_group or "")
if ip_addr ~= "" and policy_group ~= "" then
if not ip_addr:find("/", 1, true) then
ip_addr = ip_addr .. "/32"
end
custom_rules[#custom_rules + 1] = string.format("SRC-IP-CIDR,%s,%s", ip_addr, policy_group)
end
end
end)
if #custom_rules == 0 then
io.stdout:write("client_rules=0\n")
return true
end
local existing_rules = {}
for _, rule in ipairs(doc.rules or {}) do
local text = tostring(rule or "")
if not text:match("^SRC%-IP%-CIDR,") then
existing_rules[#existing_rules + 1] = rule
end
end
doc.rules = {}
for _, rule in ipairs(custom_rules) do
doc.rules[#doc.rules + 1] = rule
end
for _, rule in ipairs(existing_rules) do
doc.rules[#doc.rules + 1] = rule
end
local ok, rendered = pcall(lyaml.dump, { doc })
if not ok or not rendered then
io.stderr:write("dump_failed\n")
return false
end
write_file(runtime_path, rendered)
io.stdout:write(string.format("client_rules=%d\n", #custom_rules))
return true
end
local function bool_enabled(value)
return value == "1" or value == 1 or value == true or value == "true"
end
@@ -408,9 +461,11 @@ elseif action == "prepare" then
os.exit(prepare(arg[2], arg[3]) and 0 or 1)
elseif action == "merge" then
os.exit(merge(arg[2], arg[3], arg[4]) and 0 or 1)
elseif action == "append_client_policy_rules" then
os.exit(append_client_policy_rules(arg[2]) and 0 or 1)
elseif action == "tuic" then
os.exit(generate_tuic_runtime(arg[2], arg[3], arg[4], arg[5], arg[6]) and 0 or 1)
else
io.stderr:write("usage: clash_yaml.lua validate <yaml> | filter <yaml> <words> | prepare <input> <output> | merge <raw> <overlay> <output> | tuic <sid> <output> <local_port> [socks_port] [mode]\n")
io.stderr:write("usage: clash_yaml.lua validate <yaml> | filter <yaml> <words> | prepare <input> <output> | merge <raw> <overlay> <output> | append_client_policy_rules <runtime_yaml> | tuic <sid> <output> <local_port> [socks_port] [mode]\n")
os.exit(1)
end
@@ -59,3 +59,10 @@ config server_global
config global_xray_fragment
option fragment '0'
option noise '0'
config clash_client_group
option enabled '0'
option remarks ''
option ip_addr ''
option client_mac ''
option policy_group ''