mirror of
https://github.com/caiwx86/small-packages.git
synced 2026-11-06 20:14:19 +08:00
update 2026-07-28 01:38:11
This commit is contained in:
1 parent
20b786ab77
commit
25d570431d
6243 files changed
+2850542
No files matched your search
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/env ucode
|
||||
|
||||
'use strict';
|
||||
|
||||
import { popen, writefile, stat, readfile } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
|
||||
function to_array(val) {
|
||||
let res =[];
|
||||
if (type(val) == 'array') {
|
||||
for (let i = 0; i < length(val); i++) {
|
||||
if (type(val[i]) == 'string') {
|
||||
let s = replace(val[i], /^\s+|\s+$/g, '');
|
||||
if (s != "") push(res, s);
|
||||
}
|
||||
}
|
||||
} else if (type(val) == 'string') {
|
||||
let parts = split(val, /[ \t\n]+/);
|
||||
for (let i = 0; i < length(parts); i++) {
|
||||
if (parts[i] != "") push(res, parts[i]);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function exec_sys(cmd) {
|
||||
let p = popen(cmd + " 2>&1", "r");
|
||||
if (!p) return { code: -1, stdout: "" };
|
||||
let stdout = p.read("all");
|
||||
let code = p.close();
|
||||
if (type(stdout) == 'string') {
|
||||
stdout = replace(stdout, /^\s+|\s+$/g, '');
|
||||
}
|
||||
return { code: code, stdout: stdout || "" };
|
||||
}
|
||||
|
||||
function get_logfile_path_internal() {
|
||||
let uci_cursor = cursor();
|
||||
uci_cursor.load('mosdns');
|
||||
let configfile = uci_cursor.get('mosdns', 'config', 'configfile');
|
||||
|
||||
if (configfile === '/var/etc/mosdns.json' || !configfile) {
|
||||
return uci_cursor.get('mosdns', 'config', 'log_file');
|
||||
} else {
|
||||
let content = readfile(configfile);
|
||||
if (content) {
|
||||
let m = match(content, /file:\s*([^\r\n]+)/);
|
||||
if (m && m[1]) {
|
||||
let log_path = m[1];
|
||||
log_path = replace(log_path, /^\s+|\s+$/g, '');
|
||||
log_path = replace(log_path, /^["']|["']$/g, '');
|
||||
if (log_path != "") {
|
||||
return log_path;
|
||||
}
|
||||
}
|
||||
}
|
||||
return '/var/log/mosdns.log';
|
||||
}
|
||||
}
|
||||
|
||||
const methods = {
|
||||
flush_cache: {
|
||||
call: function() {
|
||||
try {
|
||||
let uci_cursor = cursor();
|
||||
uci_cursor.load('mosdns');
|
||||
let port = uci_cursor.get('mosdns', 'config', 'listen_port_api');
|
||||
if (!port) return { error: "API listen port not configured." };
|
||||
|
||||
let res = exec_sys(`curl -s 127.0.0.1:${port}/plugins/lazy_cache/flush`);
|
||||
if (res.code === 0) {
|
||||
return { success: true };
|
||||
} else {
|
||||
return { success: false, error: res.stdout };
|
||||
}
|
||||
} catch (e) {
|
||||
return { error: String(e) };
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
print_log: {
|
||||
call: function() {
|
||||
try {
|
||||
let path = get_logfile_path_internal();
|
||||
if (path && stat(path)) {
|
||||
return { log: readfile(path) };
|
||||
} else {
|
||||
return { error: 'Log file not accessible or does not exist.' };
|
||||
}
|
||||
} catch (e) {
|
||||
return { error: String(e) };
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
clean_log: {
|
||||
call: function() {
|
||||
try {
|
||||
let path = get_logfile_path_internal();
|
||||
if (path) {
|
||||
writefile(path, '');
|
||||
return { success: true };
|
||||
} else {
|
||||
return { error: 'Log file path could not be determined.' };
|
||||
}
|
||||
} catch (e) {
|
||||
return { error: String(e) };
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
get_version: {
|
||||
call: function() {
|
||||
let res = exec_sys('mosdns version');
|
||||
if (res.code === 0) {
|
||||
return { version: res.stdout };
|
||||
} else {
|
||||
return { error: res.stdout };
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
start_update: {
|
||||
call: function() {
|
||||
try {
|
||||
if (stat('/var/lock/mosdns_update.lock')) {
|
||||
return { success: false, error: 'Another update is already in progress.' };
|
||||
}
|
||||
exec_sys('echo "" > /var/log/mosdns_update.log');
|
||||
exec_sys('/usr/share/mosdns/mosdns.uc update > /var/log/mosdns_update.log 2>&1 &');
|
||||
return { success: true };
|
||||
} catch (e) {
|
||||
return { success: false, error: String(e) };
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
get_update_log: {
|
||||
call: function() {
|
||||
try {
|
||||
if (stat('/var/log/mosdns_update.log')) {
|
||||
return { log: readfile('/var/log/mosdns_update.log') };
|
||||
} else {
|
||||
return { log: '' };
|
||||
}
|
||||
} catch (e) {
|
||||
return { error: String(e) };
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return { "luci.mosdns": methods };
|
||||
Reference in new issue
Block a user