Рефакторинг кода: дедупликация ucode, исправления shellcheck и глобальное улучшение install.sh
This commit is contained in:
@@ -27,17 +27,20 @@ let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_exists = common.command_exists;
|
||||
let command_from_args = common.command_from_args;
|
||||
let write_json = common.write_json;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
function arg_bool(value) {
|
||||
value = lc(as_string(value));
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_env(assignments) {
|
||||
let parts = [];
|
||||
@@ -46,42 +49,15 @@ function command_env(assignments) {
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status("(" + command + ") >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_success(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_exists(name) {
|
||||
return command_success_from_args([ "command", "-v", name ]);
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function write_file(path, value) {
|
||||
return fs.writefile(as_string(path), as_string(value)) != null;
|
||||
|
||||
@@ -2,45 +2,20 @@
|
||||
|
||||
let fs = require("fs");
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let write_json = common.write_json;
|
||||
let read_json_file = common.read_json_file;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let read_stdin = common.read_stdin;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let read_stdin_json = common.read_stdin_json;
|
||||
|
||||
|
||||
function read_json_file(path) {
|
||||
let data = fs.readfile(path);
|
||||
if (data == null)
|
||||
return null;
|
||||
|
||||
try {
|
||||
return json(data);
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
function stdin_contains_ci(needle) {
|
||||
needle = lc(as_string(needle));
|
||||
@@ -84,13 +59,7 @@ function object_get_default(key, fallback) {
|
||||
print(as_string(fallback), "\n");
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function str_contains(haystack, needle) {
|
||||
return index(as_string(haystack), as_string(needle)) >= 0;
|
||||
|
||||
@@ -55,6 +55,18 @@ const SB_SERVICE_MIXED_INBOUND_PORT = getenv("SB_SERVICE_MIXED_INBOUND_PORT") ||
|
||||
const SB_VARIANT_STATE_FILE = getenv("SB_VARIANT_STATE_FILE") || "/etc/tachyon/sing-box-variant";
|
||||
const GITHUB_RAW_URL = getenv("GITHUB_RAW_URL") || "https://raw.githubusercontent.com/itdoginfo/allow-domains/main";
|
||||
const BUILTIN_SUBNET_URLS = {
|
||||
|
||||
let option = common.option;
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let bool_option = common.bool_option;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let write_json = common.write_json;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let command_output = common.command_output;
|
||||
|
||||
twitter: getenv("SUBNETS_TWITTER") || GITHUB_RAW_URL + "/Subnets/IPv4/twitter.lst",
|
||||
meta: getenv("SUBNETS_META") || GITHUB_RAW_URL + "/Subnets/IPv4/meta.lst",
|
||||
discord: getenv("SUBNETS_DISCORD") || GITHUB_RAW_URL + "/Subnets/IPv4/discord.lst",
|
||||
@@ -88,14 +100,6 @@ let shell_quote = common.shell_quote;
|
||||
let read_json_file = common.read_json_file;
|
||||
let read_stdin = common.read_stdin;
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_env(assignments) {
|
||||
let parts = [];
|
||||
@@ -106,18 +110,6 @@ function command_env(assignments) {
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function file_md5(path) {
|
||||
path = as_string(path);
|
||||
@@ -128,22 +120,12 @@ function file_md5(path) {
|
||||
return length(fields) > 0 ? as_string(fields[0]) : "";
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status(command + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return system(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function now_seconds() {
|
||||
return int(clock()[0]);
|
||||
@@ -160,9 +142,6 @@ function log_message(message, level) {
|
||||
|
||||
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function json_text(value) {
|
||||
return sprintf("%J", value) + "\n";
|
||||
@@ -226,9 +205,6 @@ function arg_number(value) {
|
||||
return int(value);
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function text_first_chars(value, max_chars) {
|
||||
value = as_string(value);
|
||||
@@ -252,26 +228,8 @@ function file_last_nonblank_line_value(path, fallback, max_chars) {
|
||||
return text_first_chars(result, max_chars);
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return fallback;
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
function bool_option(section, key, fallback) {
|
||||
let value = object_or_empty(section)[key];
|
||||
return value == null ? !!fallback : arg_bool(value);
|
||||
}
|
||||
|
||||
function section_name(section) {
|
||||
return as_string(object_or_empty(section)[".name"]);
|
||||
|
||||
@@ -9,9 +9,10 @@ const PUNYCODE_INITIAL_BIAS = 72;
|
||||
const PUNYCODE_INITIAL_N = 128;
|
||||
const PUNYCODE_DELIMITER = "-";
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
|
||||
|
||||
|
||||
function ascii_lower(value) {
|
||||
let upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
@@ -36,6 +36,9 @@ const APPLIED_MIGRATIONS_OPTION = "applied_migrations";
|
||||
const SERVER_COUNTRY_METHOD_FLAG_EMOJI = "flag_emoji";
|
||||
const SERVER_COUNTRY_METHOD_COUNTRY_IS = "country_is";
|
||||
const CHILD_ITEM_TYPES = [
|
||||
|
||||
let command_output = common.command_output;
|
||||
|
||||
"subscription_url",
|
||||
"section_interface",
|
||||
"urltest"
|
||||
@@ -43,18 +46,6 @@ const CHILD_ITEM_TYPES = [
|
||||
|
||||
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
|
||||
return replace(as_string(data), /[\r\n]+$/g, "");
|
||||
}
|
||||
|
||||
function run(command) {
|
||||
return system(command) == 0;
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
let ip = require("core.ip");
|
||||
let domain_config = require("config.domain");
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
|
||||
|
||||
|
||||
function strip_list_comment(line) {
|
||||
line = replace(as_string(line), /[[:space:]]*\/\/.*$/, "");
|
||||
|
||||
@@ -34,6 +34,10 @@ let command_output_from_args = common.command_output_from_args;
|
||||
let command_trimmed_output_from_args = common.command_trimmed_output_from_args;
|
||||
let command_exists = common.command_exists;
|
||||
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
|
||||
|
||||
function string_starts_with(value, prefix) {
|
||||
value = as_string(value);
|
||||
prefix = as_string(prefix);
|
||||
@@ -73,13 +77,7 @@ function list_has_value(values, needle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function contains(values, needle) {
|
||||
for (let value in array_or_empty(values))
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#!/usr/bin/env ucode
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
|
||||
|
||||
|
||||
function str_last_index(value, needle) {
|
||||
value = as_string(value);
|
||||
|
||||
@@ -6,32 +6,17 @@ let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
let command_exists = common.command_exists;
|
||||
let command_from_args = common.command_from_args;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
|
||||
function command_success(args) {
|
||||
return system(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_output(args) {
|
||||
let pipe = fs.popen(command_from_args(args), "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_exists(name) {
|
||||
return command_success([ "command", "-v", name ]);
|
||||
}
|
||||
|
||||
function apk_installed(package_name) {
|
||||
return command_exists("apk") && command_success([ "apk", "info", "-e", as_string(package_name) ]);
|
||||
|
||||
@@ -64,6 +64,21 @@ let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
let read_json_file = common.read_json_file;
|
||||
|
||||
let option = common.option;
|
||||
let list_option = common.list_option;
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let bool_option = common.bool_option;
|
||||
let read_stdin = common.read_stdin;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_exists = common.command_exists;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let write_json = common.write_json;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
function arg_number(value) {
|
||||
value = as_string(value);
|
||||
return value == "" || match(value, /[^0-9-]/) != null ? 0 : int(value, 10);
|
||||
@@ -74,21 +89,12 @@ function arg_bool(value) {
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function normalize_status(status) {
|
||||
status = int(status);
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
return normalize_status(system(command));
|
||||
}
|
||||
|
||||
function command_capture(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
@@ -100,26 +106,13 @@ function command_capture(command) {
|
||||
return { status, output: data == null ? "" : as_string(data) };
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let result = command_capture(command);
|
||||
return result.status == 0 ? result.output : "";
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status(command + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args) + " 2>/dev/null");
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_success(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_exists(name) {
|
||||
return command_success_from_args([ "command", "-v", as_string(name) ]);
|
||||
}
|
||||
|
||||
function module_args(module_path, args) {
|
||||
let result = [ "ucode", "-L", LIB_DIR, module_path ];
|
||||
@@ -187,18 +180,7 @@ function status_success(args, input) {
|
||||
return status_capture(args, input).status == 0;
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function read_stdin() {
|
||||
let input = fs.open("/dev/stdin", "r");
|
||||
if (!input)
|
||||
return "";
|
||||
let data = input.read("all");
|
||||
input.close();
|
||||
return data == null ? "" : as_string(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -211,37 +193,10 @@ function parse_json_or_null(value) {
|
||||
}
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return as_string(fallback);
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
function list_option(section, key) {
|
||||
let value = object_or_empty(section)[key];
|
||||
if (type(value) == "array")
|
||||
return value;
|
||||
if (as_string(value) != "")
|
||||
return [ as_string(value) ];
|
||||
return [];
|
||||
}
|
||||
|
||||
function bool_option(section, key, fallback) {
|
||||
return arg_bool(option(section, key, fallback ? "1" : "0"));
|
||||
}
|
||||
|
||||
function settings() {
|
||||
return object_or_empty(uci_core.get_all(CONFIG_NAME, "settings"));
|
||||
|
||||
@@ -3,9 +3,17 @@
|
||||
let fs = require("fs");
|
||||
let core_ip = require("core.ip");
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let write_json = common.write_json;
|
||||
let read_json_file = common.read_json_file;
|
||||
let write_json_file = common.write_json_file;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let read_stdin = common.read_stdin;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let read_stdin_json = common.read_stdin_json;
|
||||
|
||||
|
||||
|
||||
function url_encode(value) {
|
||||
value = as_string(value);
|
||||
@@ -23,45 +31,14 @@ function url_encode(value) {
|
||||
print("\n");
|
||||
}
|
||||
|
||||
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() {
|
||||
try {
|
||||
return json(read_stdin());
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function proxy_response_is_retryable_error() {
|
||||
let response = read_stdin();
|
||||
return index(response, "<html") == 0 || index(response, "403 Forbidden") >= 0;
|
||||
}
|
||||
|
||||
function read_json_file(path) {
|
||||
let data = fs.readfile(path);
|
||||
if (data == null)
|
||||
return null;
|
||||
|
||||
try {
|
||||
return json(data);
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function write_json_file(path, value) {
|
||||
return fs.writefile(path, sprintf("%J", value) + "\n");
|
||||
}
|
||||
|
||||
function parse_json_object(value) {
|
||||
try {
|
||||
@@ -605,13 +582,7 @@ function object_value(object, key) {
|
||||
return type(object) == "object" && object[key] != null ? as_string(object[key]) : "";
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function str_startswith(value, prefix) {
|
||||
value = as_string(value);
|
||||
@@ -942,9 +913,6 @@ function print_line(value) {
|
||||
print(value, "\n");
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function stdin_json() {
|
||||
let value = read_stdin_json();
|
||||
|
||||
@@ -15,26 +15,23 @@ let list_option = common.list_option;
|
||||
let bool_option = common.bool_option;
|
||||
let as_string = common.as_string;
|
||||
|
||||
let write_compact_string_array = common.write_compact_string_array;
|
||||
let run_args = common.run_args;
|
||||
let read_stdin = common.read_stdin;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let csv_to_json_array = common.csv_to_json_array;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let write_json = common.write_json;
|
||||
let option = common.option;
|
||||
|
||||
|
||||
function arg_bool(value) {
|
||||
value = lc(as_string(value));
|
||||
return value == "1" || value == "true" || value == "yes";
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return fallback;
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
function uci_section(section_name) {
|
||||
return object_or_empty(uci_core.get_all(CONFIG_NAME, section_name));
|
||||
@@ -48,28 +45,8 @@ function uci_settings() {
|
||||
return uci_section("settings");
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function write_compact_string_array(values) {
|
||||
print("[");
|
||||
for (let i = 0; i < length(values); i++) {
|
||||
if (i > 0)
|
||||
print(",");
|
||||
print(sprintf("%J", as_string(values[i])));
|
||||
}
|
||||
print("]\n");
|
||||
}
|
||||
|
||||
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 write_text_file(path, text) {
|
||||
let result = fs.writefile(path, as_string(text));
|
||||
@@ -98,35 +75,12 @@ function unlink_file(path) {
|
||||
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function run_args(args) {
|
||||
return system(command_from_args(args)) == 0;
|
||||
}
|
||||
|
||||
function run_args_quiet(args) {
|
||||
return system(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
let pipe = fs.popen(command_from_args(args), "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function log_debug(message) {
|
||||
run_args([ "logger", "-t", "tachyon", "[debug] " + as_string(message) ]);
|
||||
@@ -175,15 +129,6 @@ function text_list_to_csv(value, separator_mode) {
|
||||
print_csv(text_list_values(value, separator_mode));
|
||||
}
|
||||
|
||||
function csv_to_json_array(value) {
|
||||
value = as_string(value);
|
||||
if (value == "") {
|
||||
print("[]\n");
|
||||
return;
|
||||
}
|
||||
|
||||
write_compact_string_array(split(value, ","));
|
||||
}
|
||||
|
||||
function csv_list_contains(value, needle) {
|
||||
needle = as_string(needle);
|
||||
|
||||
@@ -25,84 +25,43 @@ let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
let read_json_file = common.read_json_file;
|
||||
|
||||
let option = common.option;
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let bool_option = common.bool_option;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_exists = common.command_exists;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let write_json = common.write_json;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
function bool_value(value) {
|
||||
value = lc(as_string(value));
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status(command + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return system(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
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 object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return fallback;
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
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"]);
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
#!/usr/bin/env ucode
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let write_json = common.write_json;
|
||||
|
||||
|
||||
|
||||
function bool_arg(value) {
|
||||
value = as_string(value);
|
||||
return value == "1" || value == "true";
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function normalize_strategy_whitespace(value) {
|
||||
value = replace(as_string(value), /[\t\r\n]/g, " ");
|
||||
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
let fs = require("fs");
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let read_stdin = common.read_stdin;
|
||||
|
||||
|
||||
|
||||
function read_stdin() {
|
||||
let data = fs.readfile("/dev/stdin");
|
||||
return data == null ? "" : data;
|
||||
}
|
||||
|
||||
function strip_trailing_cr(line) {
|
||||
line = as_string(line);
|
||||
|
||||
@@ -15,54 +15,34 @@ let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
let read_json_file = common.read_json_file;
|
||||
|
||||
let option = common.option;
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let bool_option = common.bool_option;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_exists = common.command_exists;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let write_json = common.write_json;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
function bool_value(value) {
|
||||
value = lc(as_string(value));
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status(command + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return system(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_exists(name) {
|
||||
return command_success_from_args([ "command", "-v", name ]);
|
||||
}
|
||||
|
||||
function module_command(args) {
|
||||
let command_args = [ "ucode", "-L", LIB_DIR ];
|
||||
@@ -80,30 +60,9 @@ function log_message(message, level) {
|
||||
command_success_from_args([ "logger", "-t", "tachyon", "[" + level + "] " + as_string(message) ]);
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return fallback;
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
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"]);
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#!/usr/bin/env ucode
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let write_json = common.write_json;
|
||||
|
||||
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function normalize_strategy_whitespace(value) {
|
||||
value = replace(as_string(value), /[\t\r\n]/g, " ");
|
||||
|
||||
@@ -5,18 +5,12 @@ let uci_core = require("core.uci");
|
||||
|
||||
const CONFIG_NAME = getenv("TACHYON_CONFIG_NAME") || "tachyon";
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let read_stdin = common.read_stdin;
|
||||
|
||||
|
||||
|
||||
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 hex_digit_value(value) {
|
||||
let pos = index("0123456789abcdef", lc(as_string(value)));
|
||||
|
||||
@@ -2,9 +2,14 @@
|
||||
|
||||
let fs = require("fs");
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let write_json = common.write_json;
|
||||
let read_json_file = common.read_json_file;
|
||||
let read_stdin = common.read_stdin;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
|
||||
|
||||
|
||||
function int_arg(value) {
|
||||
value = as_string(value);
|
||||
@@ -16,35 +21,9 @@ function bool_arg(value) {
|
||||
return value == "1" || value == "true";
|
||||
}
|
||||
|
||||
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_json_file(path) {
|
||||
let data = fs.readfile(path);
|
||||
if (data == null)
|
||||
return null;
|
||||
|
||||
try {
|
||||
return json(data);
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function first_field(line) {
|
||||
let fields = split(trim(as_string(line)), /[ \t\r\n]+/);
|
||||
|
||||
@@ -4,9 +4,14 @@ let fs = require("fs");
|
||||
let ip = require("core.ip");
|
||||
let domain_config = require("config.domain");
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let read_json_file = common.read_json_file;
|
||||
let write_json_file = common.write_json_file;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
|
||||
|
||||
|
||||
function json_decode_text(text) {
|
||||
try {
|
||||
@@ -17,10 +22,6 @@ function json_decode_text(text) {
|
||||
}
|
||||
}
|
||||
|
||||
function read_json_file(path) {
|
||||
let data = fs.readfile(path);
|
||||
return data == null ? null : json_decode_text(data);
|
||||
}
|
||||
|
||||
function write_text_file(path, text) {
|
||||
let result = fs.writefile(path, text);
|
||||
@@ -31,13 +32,7 @@ function write_text_file(path, text) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function write_json_file(path, value) {
|
||||
return write_text_file(path, sprintf("%J", value) + "\n");
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function array_from_value(value) {
|
||||
if (value == null)
|
||||
@@ -47,9 +42,6 @@ function array_from_value(value) {
|
||||
return [ value ];
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function trim_string(value) {
|
||||
let text = as_string(value);
|
||||
|
||||
@@ -12,6 +12,12 @@ let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
let read_json_file = common.read_json_file;
|
||||
|
||||
let read_stdin = common.read_stdin;
|
||||
let write_json = common.write_json;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let read_stdin_json = common.read_stdin_json;
|
||||
|
||||
|
||||
function ascii_lower(value) {
|
||||
let upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
let lower = "abcdefghijklmnopqrstuvwxyz";
|
||||
@@ -20,9 +26,6 @@ function ascii_lower(value) {
|
||||
});
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function run(command) {
|
||||
return system(command) == 0;
|
||||
@@ -105,23 +108,7 @@ function server_set_option(section, option, value) {
|
||||
|
||||
|
||||
|
||||
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() {
|
||||
try {
|
||||
return json(read_stdin());
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function stdin_first_nonempty_line() {
|
||||
for (let line in split(read_stdin(), "\n")) {
|
||||
@@ -143,9 +130,6 @@ function shell_single_quote(value) {
|
||||
print("'", replace(value, /'/g, "'\\''"), "'\n");
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function array_append_string(value) {
|
||||
let result = array_or_empty(read_stdin_json());
|
||||
|
||||
@@ -8,6 +8,15 @@ let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
let option = common.option;
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
function constant_value(name, fallback) {
|
||||
let value = constants[name];
|
||||
return value == null ? as_string(fallback) : as_string(value);
|
||||
@@ -38,21 +47,12 @@ function shell_assignment(name, value) {
|
||||
print(as_string(name), "=", shell_quote(value), "\n");
|
||||
}
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function normalize_status(status) {
|
||||
status = int(status);
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
return normalize_status(system(command));
|
||||
}
|
||||
|
||||
function command_capture(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
@@ -64,18 +64,8 @@ function command_capture(command) {
|
||||
return { status, output: data == null ? "" : as_string(data) };
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let result = command_capture(command);
|
||||
return result.status == 0 ? result.output : "";
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args) + " 2>/dev/null");
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_status(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_status_from_args(args) {
|
||||
return command_status(command_from_args(args));
|
||||
@@ -118,20 +108,7 @@ function bool_text(value) {
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return as_string(fallback);
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
function file_exists(path) {
|
||||
return fs.stat(as_string(path)) != null;
|
||||
|
||||
@@ -9,6 +9,16 @@ let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
let read_json_file = common.read_json_file;
|
||||
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let write_json = common.write_json;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
function constant_value(name, fallback) {
|
||||
let value = constants[name];
|
||||
return value == null ? as_string(fallback) : as_string(value);
|
||||
@@ -124,21 +134,12 @@ let startup_config_fingerprint = "";
|
||||
|
||||
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function normalize_status(status) {
|
||||
status = int(status);
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
return normalize_status(system(command));
|
||||
}
|
||||
|
||||
function command_capture(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
@@ -150,16 +151,9 @@ function command_capture(command) {
|
||||
return { status, output: data == null ? "" : as_string(data) };
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let result = command_capture(command);
|
||||
return result.status == 0 ? result.output : "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status(command + " >/dev/null 2>&1") == 0;
|
||||
@@ -169,13 +163,7 @@ function command_status_from_args(args) {
|
||||
return command_status(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args) + " 2>/dev/null");
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_status(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function external_config_fingerprint() {
|
||||
let data = fs.readfile(CONFIG_FILE);
|
||||
@@ -208,13 +196,7 @@ function bool_text(value) {
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function string_array_contains(values, needle) {
|
||||
needle = as_string(needle);
|
||||
|
||||
@@ -7,6 +7,10 @@ let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
|
||||
|
||||
function env(name, fallback) {
|
||||
let value = getenv(name);
|
||||
return value == null ? as_string(fallback) : as_string(value);
|
||||
@@ -23,21 +27,12 @@ const SING_BOX_CRONET = env("TACHYON_SING_BOX_CRONET", "/usr/lib/libcronet.so");
|
||||
const SING_BOX_MANAGED_MARKER = env("SB_MANAGED_SERVICE_MARKER", "Tachyon managed sing-box service for binary variants");
|
||||
const PACKAGE_TEST_MODE = env("TACHYON_PACKAGE_TEST_MODE", "") != "";
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function normalize_status(status) {
|
||||
status = int(status);
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return normalize_status(system(command_from_args(args) + " >/dev/null 2>&1")) == 0;
|
||||
}
|
||||
|
||||
function path_exists(path) {
|
||||
return fs.stat(as_string(path)) != null;
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
let fs = require("fs");
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
|
||||
|
||||
|
||||
function arg_bool(value) {
|
||||
return value === true || value == "true" || value == "1" || value == 1;
|
||||
|
||||
@@ -20,24 +20,19 @@ let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
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;
|
||||
}
|
||||
let list_option = common.list_option;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let bool_option = common.bool_option;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
let command_trimmed_output_from_args = common.command_trimmed_output_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let read_stdin = common.read_stdin;
|
||||
let read_stdin_json = common.read_stdin_json;
|
||||
let option = common.option;
|
||||
|
||||
|
||||
|
||||
function read_stdin_json() {
|
||||
let data = read_stdin();
|
||||
try {
|
||||
return json(data);
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function write_text_file(path, text) {
|
||||
let result = fs.writefile(path, as_string(text));
|
||||
@@ -56,35 +51,9 @@ function unlink_file(path) {
|
||||
}
|
||||
}
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
let pipe = fs.popen(command_from_args(args), "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_trimmed_output_from_args(args) {
|
||||
return replace(command_output_from_args(args), /[\r\n]+$/g, "");
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return system(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function uci_get(path) {
|
||||
return uci_core.get(path);
|
||||
@@ -94,9 +63,6 @@ function uci_exists(path) {
|
||||
return uci_core.exists(path);
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function file_first_line(path) {
|
||||
let data = fs.readfile(path);
|
||||
@@ -578,34 +544,8 @@ function arg_bool(value) {
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return fallback;
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
function list_option(section, key, fallback) {
|
||||
let value = object_or_empty(section)[key];
|
||||
if (type(value) == "array")
|
||||
return value;
|
||||
|
||||
value = trim(as_string(value));
|
||||
if (value != "")
|
||||
return split(value, /[ \t\r\n]+/);
|
||||
|
||||
fallback = as_string(fallback);
|
||||
return fallback == "" ? [] : [ fallback ];
|
||||
}
|
||||
|
||||
function bool_option(section, key, fallback) {
|
||||
return arg_bool(option(section, key, fallback ? "1" : "0"));
|
||||
}
|
||||
|
||||
function bool_value(value) {
|
||||
return arg_bool(value) ? "1" : "0";
|
||||
|
||||
@@ -11,22 +11,14 @@ const OFFSET_FILE = "/var/run/tachyon_telegram_offset";
|
||||
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
|
||||
|
||||
function command_from_args(args) {
|
||||
let result = [];
|
||||
for (let arg in args)
|
||||
push(result, shell_quote(arg));
|
||||
return join(" ", result);
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_status(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_capture(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
|
||||
@@ -39,14 +39,16 @@ let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
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;
|
||||
}
|
||||
let write_json = common.write_json;
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let read_stdin = common.read_stdin;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
|
||||
function parse_json_or_null(value) {
|
||||
try {
|
||||
@@ -57,22 +59,11 @@ function parse_json_or_null(value) {
|
||||
}
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function json_text(value) {
|
||||
return sprintf("%J", value) + "\n";
|
||||
}
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_env(assignments) {
|
||||
let parts = [];
|
||||
@@ -83,35 +74,13 @@ function command_env(assignments) {
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status(command + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_success(command_from_args(args));
|
||||
}
|
||||
|
||||
function module_success(module_path, args) {
|
||||
let command_args = [ "ucode", "-L", LIB_DIR, module_path ];
|
||||
@@ -201,9 +170,6 @@ function unsigned_number(value) {
|
||||
return int(value);
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function path_basename(path) {
|
||||
let parts = split(as_string(path), "/");
|
||||
|
||||
@@ -37,6 +37,10 @@ const BYEDPI_LISTEN_ADDRESS = "127.0.0.1";
|
||||
const BYEDPI_PORT_BASE = 1080;
|
||||
|
||||
const RESERVED_TAGS = {
|
||||
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
|
||||
[DNS_SERVER_TAG]: true,
|
||||
[FAKEIP_DNS_SERVER_TAG]: true,
|
||||
[BOOTSTRAP_DNS_SERVER_TAG]: true,
|
||||
@@ -51,9 +55,6 @@ const RESERVED_TAGS = {
|
||||
[BYPASS_OUTBOUND_TAG]: true
|
||||
};
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
|
||||
function tag(base, postfix) {
|
||||
let candidate = as_string(base) + "-" + as_string(postfix);
|
||||
|
||||
@@ -12,24 +12,11 @@ const COUNTRY_IS_BATCH_SIZE = 100;
|
||||
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
|
||||
|
||||
function command_output_from_args(args) {
|
||||
let pipe = fs.popen(command_from_args(args) + " 2>/dev/null", "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function remove_file(path) {
|
||||
if (as_string(path) != "")
|
||||
|
||||
@@ -18,30 +18,15 @@ const CHECK_DOMAIN = "example.com";
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
function command_from_args(args) {
|
||||
let result = [];
|
||||
for (let arg in args)
|
||||
push(result, shell_quote(arg));
|
||||
return join(" ", result);
|
||||
}
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
|
||||
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_status(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
let pipe = fs.popen(command_from_args(args), "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
return status == 0 && data != null ? as_string(data) : "";
|
||||
}
|
||||
|
||||
function settings() {
|
||||
return common.object_or_empty(uci_core.get_all(CONFIG_NAME, "settings"));
|
||||
|
||||
@@ -18,37 +18,17 @@ const DIAGNOSTICS_UC = getenv("TACHYON_DIAGNOSTICS_UC") || LIB_DIR + "/diagnosti
|
||||
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return command_status(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function ensure_dir(path) {
|
||||
return command_success_from_args([ "mkdir", "-p", path ]);
|
||||
|
||||
@@ -7,9 +7,9 @@ let runtime_constants = require("singbox.constants");
|
||||
let option = common.option;
|
||||
let bool_option = common.bool_option;
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
let as_string = common.as_string;
|
||||
|
||||
|
||||
|
||||
function bool_value(value) {
|
||||
return value === true || value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
|
||||
@@ -6,6 +6,10 @@ const SRS_SUPERCELL_URL = "https://raw.githubusercontent.com/ushan0v/sing-box-su
|
||||
const SRS_GITHUB_URL = "https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo/geosite/github.srs";
|
||||
|
||||
const COMMUNITY_SERVICES = {
|
||||
|
||||
let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
|
||||
russia_inside: true,
|
||||
russia_outside: true,
|
||||
ukraine_inside: true,
|
||||
@@ -35,9 +39,6 @@ const COMMUNITY_SERVICES = {
|
||||
github: true
|
||||
};
|
||||
|
||||
function as_string(value) {
|
||||
return value == null ? "" : "" + value;
|
||||
}
|
||||
|
||||
function is_community(name) {
|
||||
return COMMUNITY_SERVICES[as_string(name)] === true;
|
||||
|
||||
@@ -41,12 +41,18 @@ const SB_MANAGED_SERVICE_MARKER = getenv("SB_MANAGED_SERVICE_MARKER") || "Tachyo
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
return join(" ", parts);
|
||||
}
|
||||
let option = common.option;
|
||||
let command_status = common.command_status;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let bool_option = common.bool_option;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_exists = common.command_exists;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let command_output = common.command_output;
|
||||
|
||||
|
||||
|
||||
function command_env(assignments) {
|
||||
let parts = [];
|
||||
@@ -55,38 +61,14 @@ function command_env(assignments) {
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
function command_output_from_args(args) {
|
||||
return command_output(command_from_args(args));
|
||||
}
|
||||
|
||||
function command_status(command) {
|
||||
let status = int(system(command));
|
||||
return status > 255 ? int(status / 256) : status;
|
||||
}
|
||||
|
||||
function command_success(command) {
|
||||
return command_status(command + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_success_from_args(args) {
|
||||
return system(command_from_args(args) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_exists(name) {
|
||||
return command_success_from_args([ "command", "-v", name ]);
|
||||
}
|
||||
|
||||
function write_file(path, value) {
|
||||
return fs.writefile(as_string(path), as_string(value)) != null;
|
||||
@@ -114,35 +96,14 @@ function file_first_line(path) {
|
||||
return trim(newline >= 0 ? substr(data, 0, newline) : data);
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return fallback;
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
function arg_bool(value) {
|
||||
value = lc(as_string(value));
|
||||
return value == "true" || value == "1" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function bool_option(section, key, fallback) {
|
||||
let value = object_or_empty(section)[key];
|
||||
return value == null ? !!fallback : arg_bool(value);
|
||||
}
|
||||
|
||||
function whitespace_items(value) {
|
||||
let result = [];
|
||||
|
||||
@@ -35,10 +35,20 @@ let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
function read_stdin() {
|
||||
let data = fs.readfile("/dev/stdin");
|
||||
return data == null ? "" : data;
|
||||
}
|
||||
let command_output = common.command_output;
|
||||
let write_json = common.write_json;
|
||||
let list_option = common.list_option;
|
||||
let command_success_from_args = common.command_success_from_args;
|
||||
let bool_option = common.bool_option;
|
||||
let command_output_from_args = common.command_output_from_args;
|
||||
let command_from_args = common.command_from_args;
|
||||
let object_key_count = common.object_key_count;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let read_stdin = common.read_stdin;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
let option = common.option;
|
||||
|
||||
|
||||
|
||||
function read_json(path) {
|
||||
if (path == null || path == "" || path == "-")
|
||||
@@ -60,9 +70,6 @@ function write_file(path, value) {
|
||||
return fs.writefile(path, value) != null;
|
||||
}
|
||||
|
||||
function write_json(path, value) {
|
||||
return write_file(path, sprintf("%J", value) + "\n");
|
||||
}
|
||||
|
||||
function write_stdout_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
@@ -131,21 +138,7 @@ function append_state_list_once(list, value) {
|
||||
print(list, list != "" ? " " : "", value, "\n");
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function option(section, key, fallback) {
|
||||
if (fallback == null)
|
||||
fallback = "";
|
||||
|
||||
let value = object_or_empty(section)[key];
|
||||
if (value == null)
|
||||
return fallback;
|
||||
if (type(value) == "array")
|
||||
return join(" ", value);
|
||||
return as_string(value);
|
||||
}
|
||||
|
||||
function bool_value(value, fallback) {
|
||||
if (value == null)
|
||||
@@ -155,9 +148,6 @@ function bool_value(value, fallback) {
|
||||
return value == "1" || value == "true" || value == "yes" || value == "on";
|
||||
}
|
||||
|
||||
function bool_option(section, key, fallback) {
|
||||
return bool_value(object_or_empty(section)[key], fallback);
|
||||
}
|
||||
|
||||
function unsigned_number(value) {
|
||||
value = as_string(value);
|
||||
@@ -533,14 +523,6 @@ function files_equal(left, right) {
|
||||
|
||||
|
||||
|
||||
function command_from_args(args) {
|
||||
let parts = [];
|
||||
|
||||
for (let arg in args)
|
||||
push(parts, shell_quote(arg));
|
||||
|
||||
return join(" ", parts);
|
||||
}
|
||||
|
||||
function command_env(assignments) {
|
||||
let parts = [];
|
||||
@@ -562,26 +544,8 @@ function command_args_with(base, extra) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function command_output(command) {
|
||||
let pipe = fs.popen(command, "r");
|
||||
if (!pipe)
|
||||
return "";
|
||||
|
||||
let data = pipe.read("all");
|
||||
let status = pipe.close();
|
||||
if (status != 0 || data == null)
|
||||
return "";
|
||||
|
||||
return as_string(data);
|
||||
}
|
||||
|
||||
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) + " >/dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
function command_status_from_args(args) {
|
||||
let status = int(system(command_from_args(args)));
|
||||
@@ -821,14 +785,6 @@ function uci_named_section(section_name_value) {
|
||||
return object_or_empty(uci_core.get_all(CONFIG_NAME, section_name_value));
|
||||
}
|
||||
|
||||
function list_option(section, key) {
|
||||
let value = object_or_empty(section)[key];
|
||||
if (type(value) == "array")
|
||||
return value;
|
||||
if (as_string(value) != "")
|
||||
return [ as_string(value) ];
|
||||
return [];
|
||||
}
|
||||
|
||||
function find_section(sections, name) {
|
||||
name = as_string(name);
|
||||
@@ -1020,9 +976,6 @@ function is_array(value) {
|
||||
return type(value) == "array";
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function int_or_zero(value) {
|
||||
value = as_string(value);
|
||||
@@ -1067,9 +1020,6 @@ function log_subscription_source_summary(section_name_value, source_index, norma
|
||||
log_message(subscription_source_summary(section_name_value, source_index, normalized_path, result), "info");
|
||||
}
|
||||
|
||||
function object_key_count(value) {
|
||||
return type(value) == "object" ? length(keys(value)) : 0;
|
||||
}
|
||||
|
||||
function valid_metadata_object(value) {
|
||||
return type(value) == "object" && object_key_count(value) > 1;
|
||||
|
||||
@@ -6,6 +6,13 @@ let common = require("core.common");
|
||||
let as_string = common.as_string;
|
||||
let shell_quote = common.shell_quote;
|
||||
|
||||
let read_json_file = common.read_json_file;
|
||||
let write_json_file = common.write_json_file;
|
||||
let object_or_empty = common.object_or_empty;
|
||||
let write_json = common.write_json;
|
||||
let array_or_empty = common.array_or_empty;
|
||||
|
||||
|
||||
function trim(value) {
|
||||
value = as_string(value);
|
||||
let start = 0;
|
||||
@@ -287,23 +294,13 @@ function read_file(path) {
|
||||
return fs.readfile(path);
|
||||
}
|
||||
|
||||
function read_json_file(path) {
|
||||
let data = read_file(path);
|
||||
return data == null ? null : json_decode_text(data);
|
||||
}
|
||||
|
||||
function validate_subscription(path) {
|
||||
let value = read_json_file(path);
|
||||
return type(value) == "object" && type(value.outbounds) == "array" && length(value.outbounds) > 0;
|
||||
}
|
||||
|
||||
function write_json_file(path, value) {
|
||||
return fs.writefile(path, sprintf("%J", value) + "\n");
|
||||
}
|
||||
|
||||
function write_json(value) {
|
||||
print(sprintf("%J", value), "\n");
|
||||
}
|
||||
|
||||
function canonical_runtime_value(value) {
|
||||
if (type(value) == "array") {
|
||||
@@ -350,13 +347,7 @@ function runtime_outbounds_equal(left_path, right_path) {
|
||||
return sprintf("%J", left) == sprintf("%J", right);
|
||||
}
|
||||
|
||||
function array_or_empty(value) {
|
||||
return type(value) == "array" ? value : [];
|
||||
}
|
||||
|
||||
function object_or_empty(value) {
|
||||
return type(value) == "object" ? value : {};
|
||||
}
|
||||
|
||||
function base64_decode(value) {
|
||||
value = as_string(value);
|
||||
|
||||
Reference in New Issue
Block a user