git problem

This commit is contained in:
Cheney
2025-03-11 16:50:44 +08:00
parent 5713e9e663
commit 8149846f16
16 changed files with 1615 additions and 1307 deletions
BIN
View File
Binary file not shown.
+622 -555
View File
File diff suppressed because it is too large Load Diff
+7 -2
View File
@@ -3,17 +3,22 @@
"version": "0.1", "version": "0.1",
"bin": "src/index.js", "bin": "src/index.js",
"scripts": { "scripts": {
"linux": "pkg --compress GZip . -t node18-linux -o ./dist/linux/kit", "linux": "bun build ./src/index.js --compile --bytecode --minify --target=bun-linux-x64-baseline --outfile=./dist/linux/kit",
"windows": "pkg --compress GZip . -t win -o ./dist/windows/kit.exe", "windows": "pkg --compress GZip . -t win -o ./dist/windows/kit.exe",
"alpine" : "pkg --compress GZip . -t node14-alpine-arm64 -o ./dist/linux/kit" "alpine": "pkg --compress GZip . -t node14-alpine-arm64 -o ./dist/linux/kit",
"centos": "pkg --compress GZip . -t node14-linux-arm64 -o ./dist/linux/kit"
}, },
"dependencies": { "dependencies": {
"auto-launch": "^5.0.6",
"cross-port-killer": "^1.4.0", "cross-port-killer": "^1.4.0",
"diskusage-ng": "^1.0.4",
"extract-zip": "^2.0.1", "extract-zip": "^2.0.1",
"get-port": "^7.0.0", "get-port": "^7.0.0",
"json5": "^2.2.3", "json5": "^2.2.3",
"log4js": "^6.9.1", "log4js": "^6.9.1",
"physical-cpu-count": "^2.0.0",
"sm-crypto": "^0.3.13", "sm-crypto": "^0.3.13",
"systeminformation": "^5.25.11",
"webdav": "^5.3.1" "webdav": "^5.3.1"
} }
} }
+364 -328
View File
@@ -1,328 +1,364 @@
/** /**
* kit cli 注册和调用执行 * kit cli 注册和调用执行
*/ */
require("./init") require("./init")
const { CliCmdParser } = require("../../argv") const { CliCmdParser } = require("../../argv")
const parser = new CliCmdParser("kit V" + $version); const parser = new CliCmdParser("kit V" + $version);
const net = require("./net") const net = require("./net")
const shell = require("./shell") const shell = require("./shell")
const {sm4en3, sm4de3} = require("./crypto"); const {sm4en3, sm4de3} = require("./crypto");
const convutil = require("./util/convutil") const convutil = require("./util/convutil")
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const {sm3} = require("sm-crypto"); const {sm3} = require("sm-crypto");
const zlib = require("zlib"); const zlib = require("zlib");
const mem = require("./mem") const mem = require("./mem")
const { getAllDeviceInfo } = require("./platform")
parser.addCmdLine("man", "打开在线帮助;", async function (){ const kitcommand = require("./kitcommand")
const uConfig = await $context.get("uConfig") kitcommand.init()
shell.execSync("start " + uConfig.get("man")) kitcommand.regTo( parser )
});
parser.addCmdLine("manu", "人工维护配置;", function (){
shell.execSync("start " + $sConfig.getUserConfigPath()) // parser.addCmdLine("man", "打开在线帮助;", async function (){
}); // const uConfig = await $context.get("uConfig")
// shell.execSync("start " + uConfig.get("man"))
parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) { // });
$logger.info( "kit 当前执行目录 " + process.cwd()); // parser.addCmdLine("manu", "人工维护配置;", function (){
const fs = require("fs"); // shell.execSync("start " + $sConfig.getUserConfigPath())
// });
let step = cli.getParamValue("func"); //
if ( ! step ) { // parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
step = "main" // $logger.info( "kit 当前执行目录 " + process.cwd());
} // const fs = require("fs");
//
let file = cli.getParamValue("file"); // let step = cli.getParamValue("func");
if ( ! file ) { // if ( ! step ) {
file = "kit" // step = "main"
} // }
//
const stat = fs.existsSync(`./${file}.js`) // let file = cli.getParamValue("file");
if ( ! stat ) { // if ( ! file ) {
if ( ! cli.getParamValue("file") ) { // file = "kit"
fs.writeFileSync("./kit.js", // }
` //
async function main(){ // const stat = fs.existsSync(`./${file}.js`)
console.log("hello kit!") // if ( ! stat ) {
} // if ( ! cli.getParamValue("file") ) {
`); // fs.writeFileSync("./kit.js",
} else { // `
$logger.info( `文件 ${file} 不存在` ); // async function main(){
return // console.log("hello kit!")
} // }
} // `);
// } else {
// $logger.info( `文件 ${file} 不存在` );
const projectScript = fs.readFileSync("./kit.js", "utf-8"); // return
const vm = require('node:vm'); // }
const script = new vm.Script(projectScript + ";" + step + "()"); // }
const context = { //
animal: 'cat', //
count: 2, // const projectScript = fs.readFileSync("./kit.js", "utf-8");
$logger // const vm = require('node:vm');
}; // const script = new vm.Script(projectScript + ";" + step + "()");
// const context = {
script.runInNewContext(context, { // animal: 'cat',
timeout : 10 * 60 * 1000, // count: 2,
breakOnSigint : true, // $logger,
}); // shell
$logger.info( "暂未实现" ); // };
}); //
// script.runInNewContext(context, {
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli){ // timeout : 10 * 60 * 1000,
let key = cli.getParamValue("key"); // breakOnSigint : true,
let value = cli.getParamValue("value"); // });
const uConfig = await $context.get("uConfig") // $logger.info( "暂未实现" );
if (null != key && null == value) { // });
let v = uConfig.get(key); //
$logger.info("{} : {}", key, v); // parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli){
} else if (null != key && null != value) { // let key = cli.getParamValue("key");
uConfig.set(key, value); // let value = cli.getParamValue("value");
$logger.info("set {} : {}", key, value); // const uConfig = await $context.get("uConfig")
} else { // if (null != key && null == value) {
$logger.error("参数异常"); // let v = uConfig.get(key);
} // $logger.info("{} : {}", key, v);
}); // } else if (null != key && null != value) {
// uConfig.set(key, value);
parser.addCmdLine("remote-check", "验证远程仓库;", async function (){ // $logger.info("set {} : {}", key, value);
let remote = await $context.get("remote") // } else {
if (! ( await remote.check()) ) { // $logger.error("参数异常");
$logger.error("远程仓库不可用") // }
return // });
} else { //
$logger.info("远程仓库可用") // parser.addCmdLine("remote-check", "验证远程仓库;", async function (){
} // let remote = await $context.get("remote")
}); // if (! ( await remote.check()) ) {
// $logger.error("远程仓库不可用")
parser.addCmdLine("publish", "发布到远程仓库;", async function (){ // return
let remote = await $context.get("remote") // } else {
if (! ( await remote.check()) ) { // $logger.info("远程仓库可用")
$logger.error("远程仓库不可用") // }
return // });
} else { //
$logger.info("远程仓库可用") // parser.addCmdLine("publish", "发布到远程仓库;", async function (){
} // let remote = await $context.get("remote")
remote.publish() // if (! ( await remote.check()) ) {
}); // $logger.error("远程仓库不可用")
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli){ // return
let remote = await $context.get("remote") // } else {
if (! ( await remote.check()) ) { // $logger.info("远程仓库可用")
$logger.error("远程仓库不可用") // }
return // remote.publish()
} else { // });
$logger.info("远程仓库可用") // parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli){
} // let remote = await $context.get("remote")
remote.install(cli.getParamValue("module")) // if (! ( await remote.check()) ) {
}); // $logger.error("远程仓库不可用")
// return
// } else {
parser.addCmdLine("freem [level]", "释放内存", function (cli) { // $logger.info("远程仓库可用")
let level = cli.getParamValue("level") // }
mem.free( level ) // remote.install(cli.getParamValue("module"))
}); // });
parser.addCmdLine("killport <port>", "释放端口", function (cli) { parser.addCmdLine("platform", "平台信息", function (cli) {
let port = cli.getParamValue("port") // 调用主函数并打印结果
net.killport( parseInt(port, 10) ); getAllDeviceInfo().then(info => {
}); console.log('Device Information:');
console.log( JSON.stringify(info, null, 4));
}).catch(error => {
parser.addCmdLine("getport [--port]", "获取一个可用端口;或查询指定端口状态;", async function (cli) { console.error('Error getting device information:', error);
const getPort = await import('get-port').then(psl => psl.default); });
let port = cli.getParamValue("port") });
if( ! port ) {
let ret = await getPort()
$logger.info(`端口 ${ret} 未被使用`)
} else { parser.addCmdLine("freem [level]", "释放内存", function (cli) {
await net.getport( port ) let level = cli.getParamValue("level")
mem.free( level )
} });
});
parser.addCmdLine("checkip", "获取自己的公网 ip;", async function (cli) { parser.addCmdLine("killport <port>", "释放端口", function (cli) {
let ret = await net.checkip() let port = cli.getParamValue("port")
if( "ok" === ret.ret ) { net.killport( parseInt(port, 10) );
$logger.info( "ip : " + ret.data.ip ) });
$logger.info( "location : " + ret.data.location.join(" ") )
$logger.info("\n" + ret.data.ip)
} else { parser.addCmdLine("getport [--port]", "获取一个可用端口;或查询指定端口状态;", async function (cli) {
$logger.error("获取失败") const getPort = await import('get-port').then(psl => psl.default);
} let port = cli.getParamValue("port")
}); if( ! port ) {
let ret = await getPort()
$logger.info(`端口 ${ret} 未被使用`)
parser.addCmdLine("telnet [--timeout] <ip-port> [port]", "验证远程 TCP 端口是否可用;", async function (cli) { } else {
let ip = cli.getParamValue("ip-port") await net.getport( port )
let port = 80;
}
// 允许冒号形式指定端口 });
const re = /^(?<ip>.*):(?<port>[0-9]+)$/
const match = ip.match( re ); parser.addCmdLine("checkip", "获取自己的公网 ip;", async function (cli) {
if ( match ) { let ret = await net.checkip()
ip = match.groups.ip; if( "ok" === ret.ret ) {
port = match.groups.port; $logger.info( "ip : " + ret.data.ip )
} else { $logger.info( "location : " + ret.data.location.join(" ") )
// 如果指定了 port 参数,那么使用 port 参数 $logger.info("\n" + ret.data.ip)
let dport = cli.getParamValue("port") } else {
if ( dport ) { $logger.error("获取失败")
port = dport; }
} });
}
// 支持不填写 ip,默认访问本地 parser.addCmdLine("telnet [--timeout] <ip-port> [port]", "验证远程 TCP 端口是否可用;", async function (cli) {
if( "" === ip.trim() ) { let ip = cli.getParamValue("ip-port")
ip = "127.0.0.1"; let port = 80;
}
// 允许冒号形式指定端口
try { const re = /^(?<ip>.*):(?<port>[0-9]+)$/
await net.telnet(port, ip, cli.getParamValue("timeout")) const match = ip.match( re );
$logger.info("连接成功") if ( match ) {
}catch (e) { ip = match.groups.ip;
$logger.error( e ) port = match.groups.port;
} } else {
// 如果指定了 port 参数,那么使用 port 参数
}); let dport = cli.getParamValue("port")
if ( dport ) {
parser.addCmdLine("checklist <checkListFile>", "生成/验证文件 hash", async function (cli) { port = dport;
let checkListFile = cli.getParamValue("checkListFile") }
if ( !fs.existsSync( checkListFile ) ) { }
$logger.error(`file ${checkListFile} 不存在`)
process.exit(-2) // 支持不填写 ip,默认访问本地
} if( "" === ip.trim() ) {
const sm3 = require('sm-crypto').sm3 ip = "127.0.0.1";
const list = fs.readFileSync(checkListFile, "UTF-8") }
const files = list.split('\n').filter(line => line.trim() !== '');
for ( let file of files ) { try {
const infos = file.split(/\s+/) await net.telnet(port, ip, cli.getParamValue("timeout"))
const info = {} $logger.info("连接成功")
if ( infos.length > 2 ) { process.exit(0)
info.file = infos[0] }catch (e) {
info.hash = infos[1] $logger.error( e )
info.alg = infos[2] process.exit(-1)
} else { }
info.file = infos[0]
info.hash = infos[1] });
info.alg = "md5"
} parser.addCmdLine("checklist <checkListFile>", "生成/验证文件 hash", async function (cli) {
if ( "sm3" === info.alg ) { let checkListFile = cli.getParamValue("checkListFile")
if ( !fs.existsSync( info.file ) ) { if ( !fs.existsSync( checkListFile ) ) {
$logger.error(`file ${info.file} 不存在`) $logger.error(`file ${checkListFile} 不存在`)
process.exit(-2) process.exit(-2)
} }
const sm3 = require('sm-crypto').sm3
const data = fs.readFileSync(info.file); const list = fs.readFileSync(checkListFile, "UTF-8")
const arrayBuffer = Buffer.from(data); const files = list.split('\n').filter(line => line.trim() !== '');
const uint8Array = new Uint8Array(arrayBuffer); for ( let file of files ) {
let hashData = sm3( uint8Array ) // 杂凑 const infos = file.split(/\s+/)
if ( hashData === info.hash ) { const info = {}
$logger.info(`file ${file} 验证通过`) if ( infos.length > 2 ) {
} else { info.file = infos[0]
$logger.error(`file ${file} 验证不通过`) info.hash = infos[1]
process.exit(-400) info.alg = infos[2]
} } else {
info.file = infos[0]
} else { info.hash = infos[1]
$logger.error(`不支持 file ${file} `) info.alg = "md5"
process.exit(-2) }
} if ( "sm3" === info.alg ) {
} if ( !fs.existsSync( info.file ) ) {
$logger.error(`file ${info.file} 不存在`)
$logger.info(`${checkListFile} 中的文件全部验证通过`) process.exit(-2)
}
})
const data = fs.readFileSync(info.file);
parser.addCmdLine("sm3 [--file] [--text] [--hash]", "生成/验证 SM3 值;", async function (cli) { const arrayBuffer = Buffer.from(data);
let filePath = cli.getParamValue("file") const uint8Array = new Uint8Array(arrayBuffer);
let text = cli.getParamValue("text") let hashData = sm3( uint8Array ) // 杂凑
if( !filePath && !text ) { if ( hashData === info.hash ) {
$logger.error(`file 和 text 入参不能同时有`) $logger.info(`file ${file} 验证通过`)
process.exit(-1) } else {
} $logger.error(`file ${file} 验证不通过`)
const sm3 = require('sm-crypto').sm3 process.exit(-400)
if ( text ) { }
let hashData = sm3( text ) // 杂凑
$logger.info( hashData ) } else {
} $logger.error(`不支持 file ${file} `)
if ( filePath ) { process.exit(-2)
if ( !fs.existsSync( filePath ) ) { }
$logger.error(`file ${filePath} 不存在`) }
process.exit(-2)
} $logger.info(`${checkListFile} 中的文件全部验证通过`)
const data = fs.readFileSync(filePath);
const arrayBuffer = Buffer.from(data); })
const uint8Array = new Uint8Array(arrayBuffer);
let hashData = sm3( uint8Array ) // 杂凑 parser.addCmdLine("sm3 [--file] [--text] [--hash]", "生成/验证 SM3 值;", async function (cli) {
$logger.info( hashData ) let filePath = cli.getParamValue("file")
} let text = cli.getParamValue("text")
}); if( !filePath && !text ) {
$logger.error(`file 和 text 入参不能同时有`)
parser.addCmdLine("sm4en3 <key> <data>", "sm4 加密,密钥通过 sm3 制备;", function (cli) { process.exit(-1)
const {sm4en3} = require("./crypto"); }
let key = cli.getParamValue("key") const sm3 = require('sm-crypto').sm3
let data = cli.getParamValue("data") if ( text ) {
let out = sm4en3( key, data ) let hashData = sm3( text ) // 杂凑
$logger.info( out ); $logger.info( hashData )
}); }
if ( filePath ) {
parser.addCmdLine("sm4de3 <key> <data>", "sm4 解密,密钥通过 sm3 制备;", function (cli) { if ( !fs.existsSync( filePath ) ) {
const {sm4de3} = require("./crypto"); $logger.error(`file ${filePath} 不存在`)
let key = cli.getParamValue("key") process.exit(-2)
let data = cli.getParamValue("data") }
let out = sm4de3( key, data ) const data = fs.readFileSync(filePath);
$logger.info( out ); const arrayBuffer = Buffer.from(data);
}); const uint8Array = new Uint8Array(arrayBuffer);
let hashData = sm3( uint8Array ) // 杂凑
// parser.addCmdLine("base64 <-en/-de> <data>", "base64 编解码", function (cli) { $logger.info( hashData )
parser.addCmdLine("base64 [-en] [-de] [-hex] <data>", "base64 编解码;", function (cli) { }
let data = cli.getParamValue("data") });
if ( cli.hasFlag("de") ) {
if ( cli.hasFlag("hex") ) { parser.addCmdLine("sm4en3 <key> <data>", "sm4 加密,密钥通过 sm3 制备;", function (cli) {
$logger.info( "执行 base64 decode to hex: " ); const {sm4en3} = require("./crypto");
$logger.info( convutil.base64ToHex( data ) ); let key = cli.getParamValue("key")
} else { let data = cli.getParamValue("data")
$logger.info( "执行 base64 decode to string: " ); let out = sm4en3( key, data )
$logger.info( convutil.base64ToString( data ) ); $logger.info( out );
} });
} else {
if ( cli.hasFlag("hex") ) { parser.addCmdLine("sm4de3 <key> <data>", "sm4 解密,密钥通过 sm3 制备;", function (cli) {
$logger.info( "执行 hex encode to base64: " ); const {sm4de3} = require("./crypto");
$logger.info( convutil.hexToBase64( data ) ); let key = cli.getParamValue("key")
} else { let data = cli.getParamValue("data")
$logger.info( "执行 string encode to base64: " ); let out = sm4de3( key, data )
$logger.info( convutil.stringToBase64( data ) ); $logger.info( out );
} });
}
}); // parser.addCmdLine("base64 <-en/-de> <data>", "base64 编解码", function (cli) {
parser.addCmdLine("base64 [-en] [-de] [-hex] <data>", "base64 编解码;", function (cli) {
parser.addCmdLine("replace [-d] <file_name> <re1> <re2>", "文件内容正则替换;", function (cli) { let data = cli.getParamValue("data")
$logger.info( "暂未实现" ); if ( cli.hasFlag("de") ) {
}); if ( cli.hasFlag("hex") ) {
$logger.info( "执行 base64 decode to hex: " );
parser.addCmdLine("crlf [-d] [-r] [--path] [--filter-name] [--filter-path] <crlf:{CRLF,CR,LF,\\r\\n,\\r,\\n}>", "回车一致化", function (cli) { $logger.info( convutil.base64ToHex( data ) );
$logger.info( "暂未实现" ); } else {
}); $logger.info( "执行 base64 decode to string: " );
$logger.info( convutil.base64ToString( data ) );
parser.addCmdLine("tcpc [-d] [-x] [--host] [--ip] [--port] [--headlen] [--timeout] [--filepath]", "tcp 报文发收", function (cli) { }
$logger.info( "暂未实现" ); } else {
}); if ( cli.hasFlag("hex") ) {
$logger.info( "执行 hex encode to base64: " );
async function main(){ $logger.info( convutil.hexToBase64( data ) );
} else {
try { $logger.info( "执行 string encode to base64: " );
let cmd = parser.parse(process.argv.slice(2)) $logger.info( convutil.stringToBase64( data ) );
if ( ! cmd ) { }
console.log( parser.help() ) }
} else { });
$logger.info(`执行命令 ${cmd.cmdline.cmd}`)
if ( cmd.canEval() ){
await cmd.eval() parser.addCmdLine("touch [-d] <file_name>", "文件时间修改为当前时间;", function (cli) {
} else { const fs = require('fs');
console.log( parser.help() ) const path = cli.getParamValue("file_name")
} // 获取当前日期时间
} const now = new Date();
} catch ( e ) { // 修改文件的访问时间和修改时间为当前时间
console.error( e ) fs.utimes(path, now, now, (err) => {
} if (err) {
} $logger.error('修改文件时间出错:', err);
} else {
main() $logger.info('文件时间修改成功');
}
});
});
parser.addCmdLine("replace [-d] <file_name> <re1> <re2>", "文件内容正则替换;", function (cli) {
$logger.info( "暂未实现" );
});
parser.addCmdLine("crlf [-d] [-r] [--path] [--filter-name] [--filter-path] <crlf:{CRLF,CR,LF,\\r\\n,\\r,\\n}>", "回车一致化", function (cli) {
$logger.info( "暂未实现" );
});
parser.addCmdLine("tcpc [-d] [-x] [--host] [--ip] [--port] [--headlen] [--timeout] [--filepath]", "tcp 报文发收", function (cli) {
$logger.info( "暂未实现" );
});
async function main(){
try {
let cmd = parser.parse(process.argv.slice(2))
if ( ! cmd ) {
console.log( parser.help() )
} else {
$logger.info(`执行命令 ${cmd.cmdline.cmd}`)
if ( cmd.canEval() ){
await cmd.eval()
} else {
console.log( parser.help() )
}
}
} catch ( e ) {
console.error( e )
}
}
main()
+107 -108
View File
@@ -1,108 +1,107 @@
const shell = require("./shell"); const shell = require("./shell");
const fs = require("fs");
const vm = require("node:vm");
module.exports = {
init : function () {
module.exports = { return this;
init : function () { },
return this; regTo: function (parser) {
},
regTo: function (parser) { parser.addCmdLine("help", "打开在线帮助;", async function () {
const uConfig = await $context.get("uConfig")
parser.addCmdLine("help", "打开在线帮助;", async function () { shell.execSync("start " + uConfig.get("man"))
const uConfig = await $context.get("uConfig") });
shell.execSync("start " + uConfig.get("man")) parser.addCmdLine("manu", "人工维护配置;", function () {
}); shell.execSync("start " + $sConfig.getUserConfigPath())
parser.addCmdLine("manu", "人工维护配置;", function () { });
shell.execSync("start " + $sConfig.getUserConfigPath())
}); parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
$logger.info("kit 当前执行目录 " + process.cwd());
parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) { const fs = require("fs");
$logger.info("kit 当前执行目录 " + process.cwd());
const fs = require("fs"); let step = cli.getParamValue("func");
if (!step) {
let step = cli.getParamValue("func"); step = "main"
if (!step) { }
step = "main"
} let file = cli.getParamValue("file");
if (!file) {
let file = cli.getParamValue("file"); file = "kit"
if (!file) { }
file = "kit"
} const stat = fs.existsSync(`./${file}.js`)
if (!stat) {
const stat = fs.existsSync(`./${file}.js`) if (!cli.getParamValue("file")) {
if (!stat) { fs.writeFileSync("./kit.js", `
if (!cli.getParamValue("file")) { async function main(){
fs.writeFileSync("./kit.js", ` console.log("hello kit!")
async function main(){ }
console.log("hello kit!") `);
} } else {
`); $logger.info(`文件 ${file} 不存在`);
} else { return
$logger.info(`文件 ${file} 不存在`); }
return }
}
}
const projectScript = fs.readFileSync("./kit.js", "utf-8");
const vm = require('node:vm');
const projectScript = fs.readFileSync("./kit.js", "utf-8"); const script = new vm.Script(projectScript + ";" + step + "()");
const vm = require('node:vm'); const context = {
const script = new vm.Script(projectScript + ";" + step + "()"); animal: 'cat', count: 2, $logger, shell
const context = { };
animal: 'cat', count: 2, $logger
}; script.runInNewContext(context, {
timeout: 10 * 60 * 1000, breakOnSigint: true,
script.runInNewContext(context, { });
timeout: 10 * 60 * 1000, breakOnSigint: true, $logger.info("执行结束");
}); });
$logger.info("暂未实现");
}); parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli) {
let key = cli.getParamValue("key");
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli) { let value = cli.getParamValue("value");
let key = cli.getParamValue("key"); const uConfig = await $context.get("uConfig")
let value = cli.getParamValue("value"); if (null != key && null == value) {
const uConfig = await $context.get("uConfig") let v = uConfig.get(key);
if (null != key && null == value) { $logger.info("{} : {}", key, v);
let v = uConfig.get(key); } else if (null != key && null != value) {
$logger.info("{} : {}", key, v); uConfig.set(key, value);
} else if (null != key && null != value) { $logger.info("set {} : {}", key, value);
uConfig.set(key, value); } else {
$logger.info("set {} : {}", key, value); $logger.error("参数异常");
} else { }
$logger.error("参数异常"); });
}
}); parser.addCmdLine("remote-check", "验证远程仓库;", async function () {
let remote = await $context.get("remote")
parser.addCmdLine("remote-check", "验证远程仓库;", async function () { if (!(await remote.check())) {
let remote = await $context.get("remote") $logger.error("远程仓库不可用")
if (!(await remote.check())) { return
$logger.error("远程仓库不可用") } else {
return $logger.info("远程仓库可用")
} else { }
$logger.info("远程仓库可用") });
}
}); parser.addCmdLine("publish", "发布到远程仓库;", async function () {
let remote = await $context.get("remote")
parser.addCmdLine("publish", "发布到远程仓库;", async function () { if (!(await remote.check())) {
let remote = await $context.get("remote") $logger.error("远程仓库不可用")
if (!(await remote.check())) { return
$logger.error("远程仓库不可用") } else {
return $logger.info("远程仓库可用")
} else { }
$logger.info("远程仓库可用") remote.publish()
} });
remote.publish() parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli) {
}); let remote = await $context.get("remote")
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli) { if (!(await remote.check())) {
let remote = await $context.get("remote") $logger.error("远程仓库不可用")
if (!(await remote.check())) { return
$logger.error("远程仓库不可用") } else {
return $logger.info("远程仓库可用")
} else { }
$logger.info("远程仓库可用") remote.install(cli.getParamValue("module"))
} });
remote.install(cli.getParamValue("module"))
}); }
} }
}
+21
View File
@@ -0,0 +1,21 @@
/**
* 将 kit 或其他文件注册为开机启动
* kit 启动通过 auto-launch 模块实现,其他文件通过 kit 调用系统命令启动。
*/
const AutoLaunch = require('auto-launch');
function reg(){
var launch = new AutoLaunch({
name: 'Kit',
path: 'node D:\\fullstack\\TDevOps\\kit\\src\\index.js launch',
});
launch.enable();
}
reg()
+21 -21
View File
@@ -1,22 +1,22 @@
const osutil = require("./util/osutil") const osutil = require("./util/osutil")
const shell = require("./shell") const shell = require("./shell")
function free(level) { function free(level) {
osutil.choise({ osutil.choise({
[osutil.Windows] : ( level ) => { [osutil.Windows] : ( level ) => {
throw Error("暂不支持") throw Error("暂不支持")
}, },
[osutil.Linux] : ( level ) => { [osutil.Linux] : ( level ) => {
if ( [1,2,3].indexOf( parseInt(level,10)) === -1 ) { if ( [1,2,3].indexOf( parseInt(level,10)) === -1 ) {
level = 1 level = 1
} }
shell.execSync("sync") shell.execSync("sync")
shell.execSync(`echo ${level} > /proc/sys/vm/drop_caches`) shell.execSync(`echo ${level} > /proc/sys/vm/drop_caches`)
}, },
}) })
} }
module.exports = { module.exports = {
free free
} }
+83 -83
View File
@@ -1,84 +1,84 @@
require("./init") require("./init")
const osutil = require("./util/osutil") const osutil = require("./util/osutil")
const killer = require('cross-port-killer'); const killer = require('cross-port-killer');
const net = require('node:net'); const net = require('node:net');
/** /**
* 模拟 telnet ,检测端口是否开放 * 模拟 telnet ,检测端口是否开放
* @param port * @param port
* @param host * @param host
* @param timeout * @param timeout
* @returns {Promise<unknown>} * @returns {Promise<unknown>}
*/ */
async function telnet(port, host, timeout=3000){ async function telnet(port, host, timeout=3000){
$logger.debug(`port=${port} host=${host} timeout=${timeout}`) $logger.debug(`port=${port} host=${host} timeout=${timeout}`)
return new Promise(function (resolve, reject){ return new Promise(function (resolve, reject){
const socket = new net.Socket(); const socket = new net.Socket();
socket.setTimeout(timeout, function() { socket.setTimeout(timeout, function() {
socket.end(); socket.end();
reject("链接超时") reject("链接超时")
}); });
socket.connect(port, host); socket.connect(port, host);
socket.on('connect', function() { socket.on('connect', function() {
resolve('连接成功'); resolve('连接成功');
socket.end(); socket.end();
}); });
socket.on('error', function(err) { socket.on('error', function(err) {
reject('连接失败'); reject('连接失败');
}); });
}) })
} }
async function killport(port) { async function killport(port) {
return killer.kill( port ); return killer.kill( port );
// let func = osutil.choise([ // let func = osutil.choise([
// require("./windows/net").killport, // require("./windows/net").killport,
// require("./linux/net").killport, // require("./linux/net").killport,
// ]) // ])
// return func && func(port); // return func && func(port);
} }
/** /**
* 获取端口信息 * 获取端口信息
* 因为命令输出缓存问题,有缺陷。 * 因为命令输出缓存问题,有缺陷。
* @param port * @param port
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async function getport(port) { async function getport(port) {
$logger.info( "暂未实现" ); $logger.info( "暂未实现" );
} }
async function checkip(){ async function checkip(){
const url = "https://myip.ipip.net/json" const url = "https://myip.ipip.net/json"
const http = url.startsWith("https:") ? require("node:https") : require('node:http'); const http = url.startsWith("https:") ? require("node:https") : require('node:http');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
http.get(url, (res) => { http.get(url, (res) => {
let data = ''; let data = '';
// A chunk of data has been recieved. // A chunk of data has been recieved.
res.on('data', (chunk) => { res.on('data', (chunk) => {
data += chunk; data += chunk;
}); });
// The whole response has been received. Resolve the promise with result // The whole response has been received. Resolve the promise with result
res.on('end', () => { res.on('end', () => {
resolve(JSON.parse(data)); resolve(JSON.parse(data));
}); });
}).on("error", (err) => { }).on("error", (err) => {
// In case of any error reject the promise // In case of any error reject the promise
reject(err); reject(err);
}); });
}); });
} }
module.exports = { module.exports = {
killport, getport, telnet, checkip killport, getport, telnet, checkip
} }
+87
View File
@@ -0,0 +1,87 @@
const os = require('os');
const diskusage = require('diskusage-ng');
const { physicalCpuCount } = require('physical-cpu-count');
const { formatBytes } = require("./util/convutil")
// 获取操作系统信息
function getOSInfo() {
return {
type: os.type(),
platform: os.platform(),
release: os.release(),
arch: os.arch()
};
}
// 获取 CPU 信息
function getCPUInfo() {
const cpus = os.cpus();
return {
model: cpus[0].model,
speed: cpus[0].speed,
cores: cpus.length,
physicalCores: physicalCpuCount
};
}
// 获取内存信息
function getMemoryInfo() {
let info = {
totalSize: os.totalmem(),
freeSize: os.freemem()
}
return Object.assign(info, {
total : formatBytes(info.totalSize),
free : formatBytes(info.freeSize),
});
}
// 获取硬盘信息
async function getDiskInfo() {
try {
const path = os.platform() === 'win32' ? 'C:' : '/';
return new Promise(function (resolve, reject) {
diskusage(path, function (err, usage) {
if (err) {
console.error('Error getting disk information:', err);
reject(err);
}
resolve({
totalSize: usage.total,
freeSize: usage.available,
total : formatBytes(usage.total),
free : formatBytes(usage.available),
});
});
})
} catch (error) {
console.error('Error getting disk information:', error);
return null;
}
}
// 主函数,调用上述函数获取所有信息
async function getAllDeviceInfo() {
const osInfo = getOSInfo();
const cpuInfo = getCPUInfo();
const memoryInfo = getMemoryInfo();
const diskInfo = await getDiskInfo();
return {
os: osInfo,
cpu: cpuInfo,
memory: memoryInfo,
disk: diskInfo
};
}
module.exports = {
getOSInfo,
getCPUInfo,
getMemoryInfo,
getDiskInfo,
getAllDeviceInfo
};
+6
View File
@@ -23,5 +23,11 @@ module.exports = {
$logger.debug(`execSync ${cmd}`) $logger.debug(`execSync ${cmd}`)
const output = execSync(cmd); const output = execSync(cmd);
return output.toString() return output.toString()
},
callSync(cmd){
$logger.debug(`callSync ${cmd}`)
const output = execSync(cmd);
console.log(output.toString())
} }
} }
+155 -130
View File
@@ -1,3 +1,21 @@
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return "0 B"; // 处理 0 字节的情况
const units = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
let unitIndex = 0;
// 循环除以 1024,直到字节数小于 1024 或单位用完
while (bytes >= 1024 && unitIndex < units.length - 1) {
bytes /= 1024;
unitIndex++;
}
// 保留小数位数,并移除末尾多余的零(如 2.0 → 2)
const formattedNumber = bytes.toFixed(decimals).replace(/\.0+$/, "");
return `${formattedNumber} ${units[unitIndex]}`;
}
var codePoint2utf8 = function (code) { var codePoint2utf8 = function (code) {
var bin = parseInt(code).toString(2) var bin = parseInt(code).toString(2)
var ln = 0; var ln = 0;
@@ -209,135 +227,6 @@ function base64ArrayBuffer(arrayBuffer) {
return base64 return base64
} }
(function() {
var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
base64DecodeChars = new Array(( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 62, ( - 1), ( - 1), ( - 1), 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1));
this.base64encode = function(e) {
var r, a, c, h, o, t;
for (c = e.length, a = 0, r = ''; a < c;) {
if (h = 255 & e.charCodeAt(a++), a == c) {
r += base64EncodeChars.charAt(h >> 2),
r += base64EncodeChars.charAt((3 & h) << 4),
r += '==';
break
}
if (o = e.charCodeAt(a++), a == c) {
r += base64EncodeChars.charAt(h >> 2),
r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
r += base64EncodeChars.charAt((15 & o) << 2),
r += '=';
break
}
t = e.charCodeAt(a++),
r += base64EncodeChars.charAt(h >> 2),
r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
r += base64EncodeChars.charAt((15 & o) << 2 | (192 & t) >> 6),
r += base64EncodeChars.charAt(63 & t)
}
return r
}
this.base64decode = function(e) {
var r, a, c, h, o, t, d;
for (t = e.length, o = 0, d = ''; o < t;) {
do r = base64DecodeChars[255 & e.charCodeAt(o++)];
while (o < t && r == -1);
if (r == -1) break;
do a = base64DecodeChars[255 & e.charCodeAt(o++)];
while (o < t && a == -1);
if (a == -1) break;
d += String.fromCharCode(r << 2 | (48 & a) >> 4);
do {
if (c = 255 & e.charCodeAt(o++), 61 == c) return d;
c = base64DecodeChars[c]
} while ( o < t && c == - 1 );
if (c == -1) break;
d += String.fromCharCode((15 & a) << 4 | (60 & c) >> 2);
do {
if (h = 255 & e.charCodeAt(o++), 61 == h) return d;
h = base64DecodeChars[h]
} while ( o < t && h == - 1 );
if (h == -1) break;
d += String.fromCharCode((3 & c) << 6 | h)
}
return d
}
this.HexToBase64 = function(str) {
return base64encode(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
this.Base64Tohex = function(str) {
for (var i = 0,
bin = base64decode(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
var tmp = bin.charCodeAt(i).toString(16);
if (tmp.length === 1) tmp = "0" + tmp;
hex[hex.length] = tmp;
}
return hex.join("");
},
this.toBytes = function(str) {
for (var bytes = [], c = 0; c < str.length; c += 2)
bytes.push(parseInt(str.substr(c, 2), 16));
return bytes;
}
}) ();
//hexToBase64 Base64Tohex base64decode base64encode
var bytesToString = function(bytes){
return hexToString(bytesToHex(bytes));
}
var bytesToBase64 = function(bytes){
return base64ArrayBuffer(bytes);
}
// Convert a byte array to a hex string
var bytesToHex = function(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
}
var stringToBase64 = function(str){
return base64encode(str);
}
var stringToBytes = function(str){
return hexToBytes(stringToHex(str));
}
// Convert a ASCII string to a hex string
var stringToHex = function(str) {
return str.split("").map(function(c) {
return ("0" + c.charCodeAt(0).toString(16)).slice(-2);
}).join("");
}
var hexToBytes= function(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
// Convert a hex string to a ASCII string
var hexToString = function(hexStr) {
var hex = hexStr.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
var hexToBase64 = function(hexStr){
return HexToBase64(hexStr);
}
var base64ToString = function(base64str){
return base64decode(base64str);
}
const Base64 = { const Base64 = {
@@ -434,6 +323,138 @@ const Base64 = {
} }
} }
// (function() {
// var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
// base64DecodeChars = new Array(( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 62, ( - 1), ( - 1), ( - 1), 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1));
// this.base64encode = function(e) {
// var r, a, c, h, o, t;
// for (c = e.length, a = 0, r = ''; a < c;) {
// if (h = 255 & e.charCodeAt(a++), a == c) {
// r += base64EncodeChars.charAt(h >> 2),
// r += base64EncodeChars.charAt((3 & h) << 4),
// r += '==';
// break
// }
// if (o = e.charCodeAt(a++), a == c) {
// r += base64EncodeChars.charAt(h >> 2),
// r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
// r += base64EncodeChars.charAt((15 & o) << 2),
// r += '=';
// break
// }
// t = e.charCodeAt(a++),
// r += base64EncodeChars.charAt(h >> 2),
// r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
// r += base64EncodeChars.charAt((15 & o) << 2 | (192 & t) >> 6),
// r += base64EncodeChars.charAt(63 & t)
// }
// return r
// }
// this.base64decode = function(e) {
// var r, a, c, h, o, t, d;
// for (t = e.length, o = 0, d = ''; o < t;) {
// do r = base64DecodeChars[255 & e.charCodeAt(o++)];
// while (o < t && r == -1);
// if (r == -1) break;
// do a = base64DecodeChars[255 & e.charCodeAt(o++)];
// while (o < t && a == -1);
// if (a == -1) break;
// d += String.fromCharCode(r << 2 | (48 & a) >> 4);
// do {
// if (c = 255 & e.charCodeAt(o++), 61 == c) return d;
// c = base64DecodeChars[c]
// } while ( o < t && c == - 1 );
// if (c == -1) break;
// d += String.fromCharCode((15 & a) << 4 | (60 & c) >> 2);
// do {
// if (h = 255 & e.charCodeAt(o++), 61 == h) return d;
// h = base64DecodeChars[h]
// } while ( o < t && h == - 1 );
// if (h == -1) break;
// d += String.fromCharCode((3 & c) << 6 | h)
// }
// return d
// }
// this.HexToBase64 = function(str) {
// return base64encode(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
// }
// this.Base64Tohex = function(str) {
// for (var i = 0,
// bin = base64decode(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
// var tmp = bin.charCodeAt(i).toString(16);
// if (tmp.length === 1) tmp = "0" + tmp;
// hex[hex.length] = tmp;
// }
// return hex.join("");
// },
// this.toBytes = function(str) {
// for (var bytes = [], c = 0; c < str.length; c += 2)
// bytes.push(parseInt(str.substr(c, 2), 16));
// return bytes;
// }
//
// }) ();
//hexToBase64 Base64Tohex base64decode base64encode
var bytesToString = function(bytes){
return hexToString(bytesToHex(bytes));
}
var bytesToBase64 = function(bytes){
return base64ArrayBuffer(bytes);
}
// Convert a byte array to a hex string
var bytesToHex = function(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
}
var stringToBase64 = function(str){
return base64encode(str);
}
var stringToBytes = function(str){
return hexToBytes(stringToHex(str));
}
// Convert a ASCII string to a hex string
var stringToHex = function(str) {
return str.split("").map(function(c) {
return ("0" + c.charCodeAt(0).toString(16)).slice(-2);
}).join("");
}
var hexToBytes= function(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
// Convert a hex string to a ASCII string
var hexToString = function(hexStr) {
var hex = hexStr.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
var hexToBase64 = function(hexStr){
return HexToBase64(hexStr);
}
var base64ToString = function(base64str){
return base64decode(base64str);
}
function encodeUtf8(text) { function encodeUtf8(text) {
const code = encodeURIComponent(text); const code = encodeURIComponent(text);
const bytes = []; const bytes = [];
@@ -546,6 +567,10 @@ if ( module ) {
utf8ToBase64, utf8ToBase64,
base64ToUtf8, base64ToUtf8,
utf8ToHex, utf8ToHex,
hexToUtf8 hexToUtf8,
formatBytes
} }
} }
+50 -50
View File
@@ -1,51 +1,51 @@
const os = require('os'); const os = require('os');
const fs = require("fs"); const fs = require("fs");
const Windows = "Windows"; const Windows = "Windows";
const Linux = "Linux"; const Linux = "Linux";
module.exports = { module.exports = {
Windows, Linux, Windows, Linux,
/** /**
* 根据 os 不同,载入不同的方法 * 根据 os 不同,载入不同的方法
*/ */
choise : function ( cbs ) { choise : function ( cbs ) {
if (os.type() === 'Windows_NT') { if (os.type() === 'Windows_NT') {
// windows平台 // windows平台
return cbs && cbs[ Windows ] return cbs && cbs[ Windows ]
} }
else if (os.type() === 'Linux') { else if (os.type() === 'Linux') {
// Linux平台 // Linux平台
return cbs && cbs[ Linux ] return cbs && cbs[ Linux ]
} }
else { else {
throw { message : "不支持的 OS" } throw { message : "不支持的 OS" }
} }
}, },
cur : function () { cur : function () {
let isLinux = false; let isLinux = false;
// 操作系统不同,安装方式不同 // 操作系统不同,安装方式不同
let curOS = os.type() let curOS = os.type()
if ( curOS === "Windows_NT" ) { if ( curOS === "Windows_NT" ) {
curOS = "Windows" curOS = "Windows"
} }
else if ( curOS === "Linux" ) { else if ( curOS === "Linux" ) {
isLinux = true; isLinux = true;
const fs = require('fs'); const fs = require('fs');
if( fs.existsSync('/etc/alpine-release') ){ if( fs.existsSync('/etc/alpine-release') ){
curOS = "Alpine" curOS = "Alpine"
} }
} }
let curPlatform = os.arch() let curPlatform = os.arch()
if ( curPlatform === "x64" ) { if ( curPlatform === "x64" ) {
curPlatform = "X86_64" curPlatform = "X86_64"
} else if ( curPlatform.startsWith("arm") ) { } else if ( curPlatform.startsWith("arm") ) {
curPlatform = "ARM64" curPlatform = "ARM64"
} }
return { isLinux, curOS, curPlatform } return { isLinux, curOS, curPlatform }
} }
} }
BIN
View File
Binary file not shown.
+4 -4
View File
@@ -1,4 +1,4 @@
const net = require(__dirname + "/../src/net") const net = require(__dirname + "/../src/net")
net.killport(6667); net.killport(6667);
+88 -19
View File
@@ -1,7 +1,56 @@
const fs = require("fs"); const fs = require("fs");
const { pipeline } = require('node:stream/promises'); const { Transform } = require('stream');
const { pipeline } = require('node:stream/promises');
const paths = require('path');
function convertBytes(bytes) {
if (bytes >= 1024 * 1024 * 1024) {
const gigabytes = bytes / (1024 * 1024 * 1024);
return `${gigabytes.toFixed(2)}GB`;
} else if (bytes >= 1024 * 1024) {
const megabytes = bytes / (1024 * 1024);
return `${megabytes.toFixed(2)}MB`;
} else if (bytes >= 1024 ) {
const megabytes = bytes / (1024 );
return `${megabytes.toFixed(2)}KB`;
} else {
return `${bytes} B`;
}
}
// 创建一个用于统计字节数的Transform流
class ByteCounter extends Transform {
constructor(totalSize) {
super();
this.bytesCount = 0;
this.totalSize = totalSize;
this.timestamp = (new Date()).getTime()
this.recordBytesCount = 0;
this.speed = 0;
this.intervalId = setInterval(()=>{
let now = (new Date()).getTime()
let timecost = now - this.timestamp;
this.speed = ((this.bytesCount - this.recordBytesCount ) * 1000 / timecost).toFixed(2)
this.timestamp = now;
this.recordBytesCount = this.bytesCount;
}, 2000);
}
_transform(chunk, encoding, callback) {
this.bytesCount += chunk.length;
let process = (this.bytesCount * 100 / this.totalSize).toFixed(2)
console.log(`进度 ${process}%\t速度${ convertBytes(this.speed) }/s\t${convertBytes(this.bytesCount)} / ${convertBytes(this.totalSize)}`);
callback(null, chunk);
}
_flush(callback) {
if (this.intervalId) {
clearInterval(this.intervalId);
}
callback();
}
}
async function wUpload(client , from, to){ async function wUpload(client , from, to){
const totalSize = fs.statSync(from).size; const totalSize = fs.statSync(from).size;
const rs = fs.createReadStream( from ); const rs = fs.createReadStream( from );
@@ -22,33 +71,53 @@ async function wUpload(client , from, to){
return pipeline( rs, ws ); return pipeline( rs, ws );
} }
/**
* 文件不存在会抛出异常
* @param client
* @param file
* @param to
* @returns {Promise<void>}
*/
async function wDownload(client, file, to){
// 确认文件存在以及大小
let stat = await client.stat(file);
let totalSize = stat.size;
if ( ! to ) {
to = paths.basename(file);
}
const byteCounter = new ByteCounter(totalSize);
await pipeline(
client.createReadStream(file),
byteCounter,
fs.createWriteStream(to)
);
await pipeline(rs , ws );
}
async function main(){ async function main(){
const webdav = await import("webdav"); const webdav = await import("webdav");
const client = webdav.createClient(
"http://baishe.fullstack.club:5244/dav",
{
username: "cheney",
password: "722V587"
}
);
// const client = webdav.createClient( // const client = webdav.createClient(
// "http://172.16.17.230:5244/dav", // "http://baishe.fullstack.club:5244/dav",
// { // {
// username: "kit", // username: "cheney",
// password: "kitkit" // password: "722V587"
// } // }
// ); // );
const client = webdav.createClient(
"http://172.16.18.45:5244/dav",
{
username: "kit",
password: "kitkit"
}
);
// Get directory contents // Get directory contents
const directoryItems = await client.getDirectoryContents("/"); const directoryItems = await client.getDirectoryContents("/");
console.log(directoryItems) console.log(directoryItems)
// await pipeline( // 下载测试
// client.createReadStream( await wDownload(client, "/kit")
// "/index.json"
// ),
// fs.createWriteStream("./xxx.json")
// );
// let rs = fs.createReadStream("D:\\fullstack\\TDevOps\\kit\\dist\\windows\\kit.exe") // let rs = fs.createReadStream("D:\\fullstack\\TDevOps\\kit\\dist\\windows\\kit.exe")
// rs.on("data", (r)=>{ // rs.on("data", (r)=>{
@@ -64,10 +133,10 @@ async function main(){
// ws.on("data", (r)=>{ // ws.on("data", (r)=>{
// console.log("w", r.length) // console.log("w", r.length)
// }) // })
// await pipeline(rs , ws );
// console.log("end") // console.log("end")
await wUpload( client, "./kit", "/kit" ) // await wUpload( client, "./kit", "/kit" )
// await wUpload( client, "../dist/windows/kit.exe", "/modules/kit/0.1.20240227094429/Windows/X86_64/kit.exe" ) // await wUpload( client, "../dist/windows/kit.exe", "/modules/kit/0.1.20240227094429/Windows/X86_64/kit.exe" )
console.log("over") console.log("over")
} }
-7
View File
@@ -1,7 +0,0 @@
[2024-09-02T17:10:24.971] [INFO] default - 执行命令 checkip
[2024-09-02T17:10:25.242] [INFO] default - ip : 61.164.40.82
[2024-09-02T17:10:25.243] [INFO] default - location : 中国 浙江 杭州 电信
[2024-09-02T17:10:25.244] [INFO] default -
61.164.40.82
[2024-09-02T17:32:08.694] [INFO] default - 执行命令 freem
[2024-09-02T17:32:22.663] [INFO] default - 执行命令 freem