refactor: deduplicate ucode helpers, providers runtime and frontend icons

This commit is contained in:
Dushnilin
2026-09-12 20:37:47 +03:00
parent 1697f34a8b
commit c17658eb40
44 changed files with 608 additions and 1765 deletions
+4 -30
View File
@@ -5,28 +5,9 @@ let common = require("core.common");
let as_string = common.as_string;
let read_json_file = common.read_json_file;
function read_stdin() {
let input = fs.open("/dev/stdin", "r");
if (!input)
return "";
let data = input.read("all");
input.close();
return data == null ? "" : data;
}
function read_stdin_json() {
let data = read_stdin();
try {
return json(data);
}
catch (e) {
return null;
}
}
function write_json(value) {
print(sprintf("%J", value), "\n");
}
let read_stdin = common.read_stdin;
let read_stdin_json = common.read_stdin_json;
let write_json = common.write_json;
function stdin_contains_ci(needle) {
needle = lc(as_string(needle));
@@ -45,14 +26,7 @@ function stdin_first_line_last_field() {
print(as_string(fields[length(fields) - 1]), "\n");
}
function file_first_line(path) {
let data = fs.readfile(path);
if (data == null)
exit(1);
let newline = index(data, "\n");
print(newline >= 0 ? substr(data, 0, newline) : data, "\n");
}
let file_first_line = common.print_file_first_line;
function json_file_field(path, key, fallback) {
let value = read_json_file(path);
+3 -20
View File
@@ -200,23 +200,8 @@ function write_state_file(path, value) {
return true;
}
function file_first_line(path) {
let data = fs.readfile(path);
if (data == null)
exit(1);
let newline = index(data, "\n");
print(newline >= 0 ? substr(data, 0, newline) : data, "\n");
}
function file_first_line_value(path) {
let data = fs.readfile(path);
if (data == null)
return "";
let newline = index(data, "\n");
return newline >= 0 ? substr(data, 0, newline) : data;
}
let file_first_line = common.print_file_first_line;
let file_first_line_value = common.file_first_line;
function arg_bool(value) {
value = lc(as_string(value));
@@ -300,9 +285,7 @@ function file_last_nonblank_line_value(path, fallback, max_chars) {
return text_first_chars(result, max_chars);
}
function section_name(section) {
return as_string(object_or_empty(section)[".name"]);
}
let section_name = common.section_name;
function file_exists_value(path) {
return fs.stat(as_string(path)) != null;
+2 -30
View File
@@ -645,33 +645,7 @@ function mwan3_is_active() {
command_success_from_args([ "/etc/init.d/mwan3", "enabled" ]);
}
function hex_digit_value(value) {
let pos = index("0123456789abcdef", lc(as_string(value)));
return pos >= 0 ? pos : null;
}
function parse_number(value) {
value = lc(trim(as_string(value)));
if (value == "")
return null;
if (substr(value, 0, 2) == "0x") {
value = substr(value, 2);
if (value == "")
return null;
let result = 0;
for (let i = 0; i < length(value); i++) {
let digit = hex_digit_value(substr(value, i, 1));
if (digit == null)
return null;
result = result * 16 + digit;
}
return result;
}
return match(value, /^[0-9]+$/) == null ? null : int(value);
}
let parse_number = common.parse_number;
function subscription_parser() {
if (subscription_parser_module == null)
@@ -764,9 +738,7 @@ function sections_by_type(type_name) {
return result;
}
function section_name(section) {
return option(section, ".name", "");
}
let section_name = common.section_name;
function section_enabled(section) {
return bool_option(section, "enabled", true);
+60
View File
@@ -599,8 +599,68 @@ function get_mixed_port() {
return info ? info.port : 4534;
}
function hex_digit_value(value) {
let pos = index("0123456789abcdef", lc(as_string(value)));
return pos >= 0 ? pos : null;
}
function parse_number(value) {
value = lc(trim(as_string(value)));
if (value == "")
return null;
if (substr(value, 0, 2) == "0x") {
value = substr(value, 2);
if (value == "")
return null;
let result = 0;
for (let i = 0; i < length(value); i++) {
let digit = hex_digit_value(substr(value, i, 1));
if (digit == null)
return null;
result = result * 16 + digit;
}
return result;
}
return match(value, /^[0-9]+$/) == null ? null : int(value, 10);
}
function file_first_line(path) {
let data = fs.readfile(as_string(path));
if (data == null)
return "";
let newline = index(data, "\n");
return trim(newline >= 0 ? substr(data, 0, newline) : data);
}
function print_file_first_line(path) {
let data = fs.readfile(as_string(path));
if (data == null)
exit(1);
let newline = index(data, "\n");
print(newline >= 0 ? substr(data, 0, newline) : data, "\n");
}
function section_name(section) {
return as_string(object_or_empty(section)[".name"]);
}
function log_message(message, level, tag) {
level = as_string(level || "info");
tag = as_string(tag || "tachyon");
command_success_from_args([ "logger", "-t", tag, "[" + level + "] " + as_string(message) ]);
}
return {
as_string,
hex_digit_value,
parse_number,
file_first_line,
print_file_first_line,
section_name,
log_message,
read_json_file,
read_stdin,
read_stdin_json,
+4 -13
View File
@@ -1553,19 +1553,10 @@ function render_tachyon_logs() {
}
function file_first_line(path, fallback) {
let data = fs.readfile(path);
let result = "";
if (data != null) {
let lines = split(as_string(data), "\n");
if (length(lines) > 0)
result = str_remove_suffix(as_string(lines[0]), "\r");
}
if (result == "")
result = as_string(fallback);
print_line(result);
let line = common.file_first_line(path);
if (line == "")
line = as_string(fallback);
print_line(line);
}
function js_var_string_value(path, var_name) {
+1 -27
View File
@@ -1742,33 +1742,7 @@ function nft_create_runtime_output_rules(table, localv4_set, common_set, port_se
);
}
function hex_digit_value(value) {
let pos = index("0123456789abcdef", lc(as_string(value)));
return pos >= 0 ? pos : null;
}
function parse_mark_number(value) {
value = lc(trim(as_string(value)));
if (value == "")
return null;
if (substr(value, 0, 2) == "0x") {
value = substr(value, 2);
if (value == "")
return null;
let result = 0;
for (let i = 0; i < length(value); i++) {
let digit = hex_digit_value(substr(value, i, 1));
if (digit == null)
return null;
result = result * 16 + digit;
}
return result;
}
return match(value, /^[0-9]+$/) == null ? null : int(value);
}
let parse_mark_number = common.parse_number;
function nft_provider_mark_hex(route_mark_base, index) {
let base = parse_mark_number(route_mark_base);
@@ -38,19 +38,9 @@ function command_exists(name) {
return command_success_from_args([ "command", "-v", name ]);
}
function log_message(message, level) {
level = as_string(level || "info");
command_success_from_args([ "logger", "-t", "tachyon", "[" + level + "] " + as_string(message) ]);
}
function bool_option(section, key, fallback) {
let value = object_or_empty(section)[key];
return value == null ? !!fallback : bool_value(value);
}
function section_name(section) {
return as_string(object_or_empty(section)[".name"]);
}
let log_message = common.log_message;
let bool_option = common.bool_option;
let section_name = common.section_name;
function uci_sections(type_name) {
return uci_core.section_objects(CONFIG_NAME, as_string(type_name));
@@ -185,13 +175,7 @@ function runtime_pid_running(pid) {
return match(pid, /^[0-9]+$/) != null && command_success_from_args([ "kill", "-0", pid ]);
}
function file_first_line(path) {
let data = fs.readfile(as_string(path));
if (data == null)
return "";
let newline = index(data, "\n");
return trim(newline >= 0 ? substr(data, 0, newline) : data);
}
let file_first_line = common.file_first_line;
function kill_pidfile_process(path, signal) {
let pid = file_first_line(path);
@@ -36,19 +36,9 @@ function module_success(args) {
return command_success(module_command(args));
}
function log_message(message, level) {
level = as_string(level || "info");
command_success_from_args([ "logger", "-t", "tachyon", "[" + level + "] " + as_string(message) ]);
}
function bool_option(section, key, fallback) {
let value = object_or_empty(section)[key];
return value == null ? !!fallback : bool_value(value);
}
function section_name(section) {
return as_string(object_or_empty(section)[".name"]);
}
let log_message = common.log_message;
let bool_option = common.bool_option;
let section_name = common.section_name;
function uci_sections(type_name) {
return uci_core.section_objects(CONFIG_NAME, as_string(type_name));
@@ -94,28 +84,7 @@ function enabled_sections(cfg) {
return result;
}
function hex_digit_value(value) {
let pos = index("0123456789abcdef", lc(as_string(value)));
return pos >= 0 ? pos : null;
}
function parse_number(value) {
value = lc(trim(as_string(value)));
if (value == "")
return null;
if (substr(value, 0, 2) == "0x") {
value = substr(value, 2);
let result = 0;
for (let i = 0; i < length(value); i++) {
let digit = hex_digit_value(substr(value, i, 1));
if (digit == null)
return null;
result = result * 16 + digit;
}
return result;
}
return match(value, /^[0-9]+$/) == null ? null : int(value);
}
let parse_number = common.parse_number;
function route_mark_value(cfg, index_value) {
return parse_number(cfg.route_mark_base) + int(index_value);
@@ -185,13 +154,7 @@ function runtime_pid_running(pid) {
return match(pid, /^[0-9]+$/) != null && command_success_from_args([ "kill", "-0", pid ]);
}
function file_first_line(path) {
let data = fs.readfile(as_string(path));
if (data == null)
return "";
let newline = index(data, "\n");
return trim(newline >= 0 ? substr(data, 0, newline) : data);
}
let file_first_line = common.file_first_line;
function kill_pidfile_process(path, signal) {
let pid = file_first_line(path);
@@ -22,61 +22,15 @@ function log_message(msg, level) {
system("logger -t " + MODE + " -p " + (level == "fatal" ? "user.err" : level == "warn" ? "user.warning" : "user.info") + " '" + msg + "'");
}
function command_from_args(args) {
let parts = [];
for (let arg in args)
push(parts, common.shell_quote(as_string(arg)));
return join(" ", parts);
}
function command_output(cmd) {
let output = trim(popen(cmd, "r") || "");
return output;
}
function command_output_from_args(args) {
return command_output(command_from_args(args));
}
function command_success_from_args(args) {
return system(command_from_args(args)) == 0;
}
function option(section, key, fallback) {
let val = section[key];
if (val == null)
return fallback || "";
if (type(val) == "array")
return join(" ", val);
return as_string(val);
}
function bool_option(section, key, fallback) {
let val = section[key];
if (val == null)
return fallback || false;
return bool_value(val);
}
function list_option(section, key) {
let val = section[key];
if (val == null)
return [];
if (type(val) == "array")
return val;
return split(trim(as_string(val)), " \t\n\r");
}
function section_name(section) {
return as_string(section[".name"] || "");
}
function read_json_file(path) {
let data = fs.readfile(path);
if (data == null || data == "")
return null;
return json(data);
}
let command_from_args = common.command_from_args;
let command_output = common.command_output;
let command_output_from_args = common.command_output_from_args;
let command_success_from_args = common.command_success_from_args;
let option = common.option;
let bool_option = common.bool_option;
let list_option = common.list_option;
let section_name = common.section_name;
let read_json_file = common.read_json_file;
function yaml_quote(value) {
value = as_string(value);
+1 -27
View File
@@ -10,33 +10,7 @@ let bool_value = common.bool_value;
const CONFIG_NAME = getenv("TACHYON_CONFIG_NAME") || "tachyon";
function hex_digit_value(value) {
let pos = index("0123456789abcdef", lc(as_string(value)));
return pos >= 0 ? pos : null;
}
function parse_number(value) {
value = lc(trim(as_string(value)));
if (value == "")
return null;
if (substr(value, 0, 2) == "0x") {
value = substr(value, 2);
if (value == "")
return null;
let result = 0;
for (let i = 0; i < length(value); i++) {
let digit = hex_digit_value(substr(value, i, 1));
if (digit == null)
return null;
result = result * 16 + digit;
}
return result;
}
return match(value, /^[0-9]+$/) == null ? null : int(value);
}
let parse_number = common.parse_number;
function rule_rows() {
let rows = [];
+10 -65
View File
@@ -22,71 +22,16 @@ function log_message(msg, level) {
system("logger -t " + MODE + " -p " + (level == "fatal" ? "user.err" : level == "warn" ? "user.warning" : "user.info") + " '" + msg + "'");
}
function command_from_args(args) {
let parts = [];
for (let arg in args)
push(parts, common.shell_quote(as_string(arg)));
return join(" ", parts);
}
function command_output(cmd) {
let output = trim(popen(cmd, "r") || "");
return output;
}
function command_output_from_args(args) {
return command_output(command_from_args(args));
}
function command_success_from_args(args) {
return system(command_from_args(args)) == 0;
}
function option(section, key, fallback) {
let val = section[key];
if (val == null)
return fallback || "";
if (type(val) == "array")
return join(" ", val);
return as_string(val);
}
function bool_option(section, key, fallback) {
let val = section[key];
if (val == null)
return fallback || false;
return bool_value(val);
}
function list_option(section, key) {
let val = section[key];
if (val == null)
return [];
if (type(val) == "array")
return val;
return split(trim(as_string(val)), " \t\n\r");
}
function section_name(section) {
return as_string(section[".name"] || "");
}
function read_json_file(path) {
let data = fs.readfile(path);
if (data == null || data == "")
return null;
return json(data);
}
function write_json_file(path, data) {
let tmp = path + ".tmp";
let f = fs.open(tmp, "w");
if (!f)
return false;
f.write(sprintf("%J", data));
f.close();
return rename(tmp, path) == 0;
}
let command_from_args = common.command_from_args;
let command_output = common.command_output;
let command_output_from_args = common.command_output_from_args;
let command_success_from_args = common.command_success_from_args;
let option = common.option;
let bool_option = common.bool_option;
let list_option = common.list_option;
let section_name = common.section_name;
let read_json_file = common.read_json_file;
let write_json_file = common.write_json_file;
function lan_mac_address() {
for (let iface in ["br-lan", "lan", "eth0", "eth1"]) {
+3 -14
View File
@@ -15,11 +15,7 @@ let write_json = common.write_json;
const CONFIG_NAME = getenv("TACHYON_CONFIG_NAME") || "tachyon";
function ascii_lower(value) {
let upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let lower = "abcdefghijklmnopqrstuvwxyz";
return replace(as_string(value), /[A-Z]/g, function(ch) {
return substr(lower, index(upper, ch), 1);
});
return lc(as_string(value));
}
function run(command) {
@@ -262,14 +258,7 @@ function mtproto_base_secret_from_value(value) {
print(result, "\n");
}
function hex_digit_value(value) {
value = ord(lc(as_string(value)));
if (value >= 48 && value <= 57)
return value - 48;
if (value >= 97 && value <= 102)
return value - 87;
return -1;
}
let hex_digit_value = common.hex_digit_value;
function hex_to_string_value(value) {
value = as_string(value);
@@ -280,7 +269,7 @@ function hex_to_string_value(value) {
for (let i = 0; i < length(value); i += 2) {
let high = hex_digit_value(substr(value, i, 1));
let low = hex_digit_value(substr(value, i + 1, 1));
if (high < 0 || low < 0)
if (high == null || low == null)
return null;
result += chr(high * 16 + low);
}
+2 -12
View File
@@ -85,15 +85,7 @@ function uci_exists(path) {
}
let object_or_empty = common.object_or_empty;
function file_first_line(path) {
let data = fs.readfile(path);
if (data == null)
exit(1);
let newline = index(data, "\n");
print(newline >= 0 ? substr(data, 0, newline) : data, "\n");
}
let file_first_line = common.print_file_first_line;
function read_state_value(path, needle) {
let data = fs.readfile(path);
@@ -704,9 +696,7 @@ function bool_option_value(section, key, fallback) {
return bool_option(section, key, fallback) ? "1" : "0";
}
function section_name(section) {
return as_string(object_or_empty(section)[".name"]);
}
let section_name = common.section_name;
function str_startswith(value, prefix) {
value = as_string(value);
@@ -27,9 +27,7 @@ function settings() {
return common.object_or_empty(uci_core.get_all(CONFIG_NAME, "settings"));
}
function ensure_dir(path) {
return command_success_from_args([ "mkdir", "-p", path ]);
}
let ensure_dir = common.ensure_dir;
function write_state(path, value) {
if (!ensure_dir(RUNTIME_STATE_DIR))
@@ -345,12 +343,7 @@ function worker() {
}
}
function file_first_line(path) {
let data = fs.readfile(path);
if (data == null)
return "";
return trim(split(as_string(data), "\n")[0]);
}
let file_first_line = common.file_first_line;
function process_running(pid) {
return match(as_string(pid), /^[0-9]+$/) != null && command_success_from_args([ "kill", "-0", pid ]);
+2 -11
View File
@@ -23,17 +23,8 @@ const PRIORITY_PID_FILE = getenv("TACHYON_PRIORITY_PID_FILE") || RUNTIME_STATE_D
const PRIORITY_UC = getenv("TACHYON_PRIORITY_UC") || LIB_DIR + "/singbox/priority.uc";
const DIAGNOSTICS_UC = getenv("TACHYON_DIAGNOSTICS_UC") || LIB_DIR + "/diagnostics/runtime.uc";
function ensure_dir(path) {
return command_success_from_args([ "mkdir", "-p", path ]);
}
function file_first_line(path) {
let data = fs.readfile(as_string(path));
if (data == null)
return "";
let newline = index(data, "\n");
return trim(newline >= 0 ? substr(data, 0, newline) : data);
}
let ensure_dir = common.ensure_dir;
let file_first_line = common.file_first_line;
function log_message(message, level) {
level = as_string(level || "info");
+1 -7
View File
@@ -85,13 +85,7 @@ function remove_files(paths) {
remove_file(path);
}
function file_first_line(path) {
let data = fs.readfile(as_string(path));
if (data == null)
return "";
let newline = index(data, "\n");
return trim(newline >= 0 ? substr(data, 0, newline) : data);
}
let file_first_line = common.file_first_line;
function arg_bool(value) {
value = lc(as_string(value));
@@ -62,14 +62,7 @@ function parse_source_entry(entry) {
};
}
function file_first_line(path) {
let data = fs.readfile(path);
if (data == null)
return "";
let newline = index(data, "\n");
return newline >= 0 ? substr(data, 0, newline) : data;
}
let file_first_line = common.file_first_line;
function valid_metadata_object(value) {
return type(value) == "object" && object_key_count(value) > 1;
+3 -20
View File
@@ -89,23 +89,8 @@ function write_stdout_json(value) {
print(sprintf("%J", value), "\n");
}
function file_first_line(path) {
let data = fs.readfile(path);
if (data == null)
exit(1);
let newline = index(data, "\n");
print(newline >= 0 ? substr(data, 0, newline) : data, "\n");
}
function file_first_line_value(path) {
let data = fs.readfile(path);
if (data == null)
return "";
let newline = index(data, "\n");
return newline >= 0 ? substr(data, 0, newline) : data;
}
let file_first_line = common.print_file_first_line;
let file_first_line_value = common.file_first_line;
function file_has_exact_line(path, needle) {
let data = fs.readfile(path);
@@ -249,9 +234,7 @@ function append_word_once(words, value) {
return words + (words != "" ? " " : "") + value;
}
function section_name(section) {
return as_string(object_or_empty(section)[".name"]);
}
let section_name = common.section_name;
function source_id(section, index) {
return as_string(section) + "-subscription-" + as_string(index);