Skip to content

Commit 142ab76

Browse files
committed
chore: age encryption support
1 parent c7826ea commit 142ab76

19 files changed

Lines changed: 1167 additions & 201 deletions

File tree

luci-app-openclash/luasrc/controller/openclash.lua

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ function index()
9595
entry({"admin", "services", "openclash", "config_file_save"}, call("action_config_file_save"))
9696
entry({"admin", "services", "openclash", "upload_config"}, call("action_upload_config"))
9797
entry({"admin", "services", "openclash", "add_subscription"}, call("action_add_subscription"))
98+
entry({"admin", "services", "openclash", "generate_age_key"}, call("action_generate_age_key"))
99+
entry({"admin", "services", "openclash", "add_age_config"}, call("action_add_age_config"))
98100
entry({"admin", "services", "openclash", "upload_overwrite"}, call("action_upload_overwrite"))
99101
entry({"admin", "services", "openclash", "overwrite_subscribe_info"}, call("action_overwrite_subscribe_info"))
100102
entry({"admin", "services", "openclash", "overwrite_file_list"}, call("action_overwrite_file_list"))
@@ -3343,20 +3345,19 @@ function action_add_subscription()
33433345
local keyword = luci.http.formvalue("keyword") or ""
33443346
local ex_keyword = luci.http.formvalue("ex_keyword") or ""
33453347
local de_ex_keyword = luci.http.formvalue("de_ex_keyword") or ""
3346-
33473348
luci.http.prepare_content("application/json")
33483349

3349-
if not name or not address then
3350+
if not name then
33503351
luci.http.write_json({
33513352
status = "error",
3352-
message = "Missing name or address parameter"
3353+
message = "Missing name parameter"
33533354
})
33543355
return
33553356
end
33563357

33573358
local is_valid_url = false
33583359

3359-
if sub_convert == "1" then
3360+
if address and address ~= "" and sub_convert == "1" then
33603361
local prefixed_http_pattern = "^[^,%s]+,https?://.+"
33613362
local encoded_prefixed_http_pattern = "^[^%%%s]+%%2[Cc]https?%%3[Aa]%%2[Ff]%%2[Ff].+"
33623363

@@ -3391,10 +3392,12 @@ function action_add_subscription()
33913392
is_valid_url = true
33923393
end
33933394
end
3394-
else
3395+
elseif address and address ~= "" then
33953396
if string.find(address, "^https?://") and not string.find(address, "\n") and not string.find(address, "|") then
33963397
is_valid_url = true
33973398
end
3399+
else
3400+
is_valid_url = true
33983401
end
33993402

34003403
if not is_valid_url then
@@ -3452,7 +3455,11 @@ function action_add_subscription()
34523455

34533456
if section_id then
34543457
uci:set("openclash", section_id, "name", name)
3455-
uci:set("openclash", section_id, "address", normalized_address)
3458+
if normalized_address and normalized_address ~= "" then
3459+
uci:set("openclash", section_id, "address", normalized_address)
3460+
else
3461+
uci:delete("openclash", section_id, "address")
3462+
end
34563463
uci:set("openclash", section_id, "sub_ua", sub_ua)
34573464
uci:set("openclash", section_id, "sub_convert", sub_convert)
34583465
if sub_convert == "1" then
@@ -4000,6 +4007,15 @@ function action_get_subscribe_data()
40004007
end
40014008
end)
40024009

4010+
uci:foreach("openclash", "config_age_secret", function(a)
4011+
if a.name == filename then
4012+
if a.secret then data.config_age_secret = a.secret end
4013+
if a.public then data.config_age_public = a.public end
4014+
if a.algo then data.config_age_algo = a.algo end
4015+
return false
4016+
end
4017+
end)
4018+
40034019
luci.http.prepare_content("application/json")
40044020
luci.http.write_json(data)
40054021
end
@@ -4013,3 +4029,71 @@ function action_get_subscribe_info_data()
40134029
luci.http.prepare_content("application/json")
40144030
luci.http.write_json(get_sub_url(filename))
40154031
end
4032+
4033+
function action_generate_age_key()
4034+
local algo = luci.http.formvalue("algo") or "keygen"
4035+
local cmd = string.format("%s age %s", meta_core_path, (algo == "pq" and "keygen-pq" or "keygen"))
4036+
local out = luci.sys.exec(cmd .. " 2>/dev/null")
4037+
local secret = out:match("(AGE%-SECRET%-KEY%-%S+)")
4038+
local public = out:match("# public key: ([^\n\r]+)")
4039+
luci.http.prepare_content("application/json")
4040+
if not secret then
4041+
luci.http.write_json({status = "error", message = "Failed to generate age key", output = out})
4042+
return
4043+
end
4044+
luci.http.write_json({status = "success", secret = secret, public = public})
4045+
end
4046+
4047+
function action_add_age_config()
4048+
local name = luci.http.formvalue("name")
4049+
local age_secret = luci.http.formvalue("age_secret") or ""
4050+
local age_public = luci.http.formvalue("age_public") or ""
4051+
local age_algo = luci.http.formvalue("age_algo") or ""
4052+
4053+
luci.http.prepare_content("application/json")
4054+
4055+
if not name or name == "" then
4056+
luci.http.write_json({status = "error", message = "Missing name parameter"})
4057+
return
4058+
end
4059+
4060+
local age_section_id = nil
4061+
uci:foreach("openclash", "config_age_secret", function(s)
4062+
if s.name == name then
4063+
age_section_id = s['.name']
4064+
return false
4065+
end
4066+
end)
4067+
4068+
if not age_section_id and (age_secret ~= "" or age_public ~= "" or age_algo ~= "") then
4069+
age_section_id = uci:add("openclash", "config_age_secret")
4070+
if age_section_id then
4071+
uci:set("openclash", age_section_id, "name", name)
4072+
end
4073+
end
4074+
4075+
if age_section_id then
4076+
if (age_secret == "" and age_public == "") then
4077+
uci:delete("openclash", age_section_id)
4078+
else
4079+
if age_secret and age_secret ~= "" then
4080+
uci:set("openclash", age_section_id, "secret", age_secret)
4081+
else
4082+
uci:delete("openclash", age_section_id, "secret")
4083+
end
4084+
if age_public and age_public ~= "" then
4085+
uci:set("openclash", age_section_id, "public", age_public)
4086+
else
4087+
uci:delete("openclash", age_section_id, "public")
4088+
end
4089+
if age_algo and age_algo ~= "" then
4090+
uci:set("openclash", age_section_id, "algo", age_algo)
4091+
else
4092+
uci:delete("openclash", age_section_id, "algo")
4093+
end
4094+
end
4095+
uci:commit("openclash")
4096+
end
4097+
4098+
luci.http.write_json({status = "success"})
4099+
end

luci-app-openclash/luasrc/model/cbi/openclash/config-overwrite.lua

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,14 @@ custom_fallback_filter.wrap = "off"
205205
custom_fallback_filter:depends("custom_fallback_filter", "1")
206206

207207
function custom_fallback_filter.cfgvalue(self, section)
208-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_fallback_filter.yaml") or ""
208+
return fs.readfile("/etc/openclash/custom/openclash_custom_fallback_filter.yaml") or ""
209209
end
210210
function custom_fallback_filter.write(self, section, value)
211211
if value then
212212
value = value:gsub("\r\n?", "\n")
213-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_fallback_filter.yaml")
213+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_fallback_filter.yaml")
214214
if value ~= old_value then
215-
NXFS.writefile("/etc/openclash/custom/openclash_custom_fallback_filter.yaml", value)
215+
fs.writefile("/etc/openclash/custom/openclash_custom_fallback_filter.yaml", value)
216216
end
217217
end
218218
end
@@ -237,14 +237,14 @@ custom_fake_black.wrap = "off"
237237
custom_fake_black:depends("custom_fakeip_filter", "1")
238238

239239
function custom_fake_black.cfgvalue(self, section)
240-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_fake_filter.list") or ""
240+
return fs.readfile("/etc/openclash/custom/openclash_custom_fake_filter.list") or ""
241241
end
242242
function custom_fake_black.write(self, section, value)
243243
if value then
244244
value = value:gsub("\r\n?", "\n")
245-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_fake_filter.list")
245+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_fake_filter.list")
246246
if value ~= old_value then
247-
NXFS.writefile("/etc/openclash/custom/openclash_custom_fake_filter.list", value)
247+
fs.writefile("/etc/openclash/custom/openclash_custom_fake_filter.list", value)
248248
end
249249
end
250250
end
@@ -261,14 +261,14 @@ custom_domain_dns_policy.wrap = "off"
261261
custom_domain_dns_policy:depends("custom_name_policy", "1")
262262

263263
function custom_domain_dns_policy.cfgvalue(self, section)
264-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_domain_dns_policy.list") or ""
264+
return fs.readfile("/etc/openclash/custom/openclash_custom_domain_dns_policy.list") or ""
265265
end
266266
function custom_domain_dns_policy.write(self, section, value)
267267
if value then
268268
value = value:gsub("\r\n?", "\n")
269-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_domain_dns_policy.list")
269+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_domain_dns_policy.list")
270270
if value ~= old_value then
271-
NXFS.writefile("/etc/openclash/custom/openclash_custom_domain_dns_policy.list", value)
271+
fs.writefile("/etc/openclash/custom/openclash_custom_domain_dns_policy.list", value)
272272
end
273273
end
274274
end
@@ -284,14 +284,14 @@ custom_proxy_server_dns_policy.wrap = "off"
284284
custom_proxy_server_dns_policy:depends("custom_proxy_server_policy", "1")
285285

286286
function custom_proxy_server_dns_policy.cfgvalue(self, section)
287-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_proxy_server_dns_policy.list") or ""
287+
return fs.readfile("/etc/openclash/custom/openclash_custom_proxy_server_dns_policy.list") or ""
288288
end
289289
function custom_proxy_server_dns_policy.write(self, section, value)
290290
if value then
291291
value = value:gsub("\r\n?", "\n")
292-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_proxy_server_dns_policy.list")
292+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_proxy_server_dns_policy.list")
293293
if value ~= old_value then
294-
NXFS.writefile("/etc/openclash/custom/openclash_custom_proxy_server_dns_policy.list", value)
294+
fs.writefile("/etc/openclash/custom/openclash_custom_proxy_server_dns_policy.list", value)
295295
end
296296
end
297297
end
@@ -307,14 +307,14 @@ custom_hosts.wrap = "off"
307307
custom_hosts:depends("custom_host", "1")
308308

309309
function custom_hosts.cfgvalue(self, section)
310-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_hosts.list") or ""
310+
return fs.readfile("/etc/openclash/custom/openclash_custom_hosts.list") or ""
311311
end
312312
function custom_hosts.write(self, section, value)
313313
if value then
314314
value = value:gsub("\r\n?", "\n")
315-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_hosts.list")
315+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_hosts.list")
316316
if value ~= old_value then
317-
NXFS.writefile("/etc/openclash/custom/openclash_custom_hosts.list", value)
317+
fs.writefile("/etc/openclash/custom/openclash_custom_hosts.list", value)
318318
end
319319
end
320320
end
@@ -374,14 +374,14 @@ sniffer_custom.rows = 20
374374
sniffer_custom.wrap = "off"
375375

376376
function sniffer_custom.cfgvalue(self, section)
377-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_sniffer.yaml") or ""
377+
return fs.readfile("/etc/openclash/custom/openclash_custom_sniffer.yaml") or ""
378378
end
379379
function sniffer_custom.write(self, section, value)
380380
if value then
381381
value = value:gsub("\r\n?", "\n")
382-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_sniffer.yaml")
382+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_sniffer.yaml")
383383
if value ~= old_value then
384-
NXFS.writefile("/etc/openclash/custom/openclash_custom_sniffer.yaml", value)
384+
fs.writefile("/etc/openclash/custom/openclash_custom_sniffer.yaml", value)
385385
end
386386
end
387387
end
@@ -466,14 +466,14 @@ custom_rules.rows = 20
466466
custom_rules.wrap = "off"
467467

468468
function custom_rules.cfgvalue(self, section)
469-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_rules.list") or ""
469+
return fs.readfile("/etc/openclash/custom/openclash_custom_rules.list") or ""
470470
end
471471
function custom_rules.write(self, section, value)
472472
if value then
473473
value = value:gsub("\r\n?", "\n")
474-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_rules.list")
474+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_rules.list")
475475
if value ~= old_value then
476-
NXFS.writefile("/etc/openclash/custom/openclash_custom_rules.list", value)
476+
fs.writefile("/etc/openclash/custom/openclash_custom_rules.list", value)
477477
end
478478
end
479479
end
@@ -486,14 +486,14 @@ custom_rules_2.rows = 20
486486
custom_rules_2.wrap = "off"
487487

488488
function custom_rules_2.cfgvalue(self, section)
489-
return NXFS.readfile("/etc/openclash/custom/openclash_custom_rules_2.list") or ""
489+
return fs.readfile("/etc/openclash/custom/openclash_custom_rules_2.list") or ""
490490
end
491491
function custom_rules_2.write(self, section, value)
492492
if value then
493493
value = value:gsub("\r\n?", "\n")
494-
local old_value = NXFS.readfile("/etc/openclash/custom/openclash_custom_rules_2.list")
494+
local old_value = fs.readfile("/etc/openclash/custom/openclash_custom_rules_2.list")
495495
if value ~= old_value then
496-
NXFS.writefile("/etc/openclash/custom/openclash_custom_rules_2.list", value)
496+
fs.writefile("/etc/openclash/custom/openclash_custom_rules_2.list", value)
497497
end
498498
end
499499
end

0 commit comments

Comments
 (0)