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
kit/bun.lockb Normal file

Binary file not shown.

1177
kit/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,17 +3,22 @@
"version": "0.1",
"bin": "src/index.js",
"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",
"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": {
"auto-launch": "^5.0.6",
"cross-port-killer": "^1.4.0",
"diskusage-ng": "^1.0.4",
"extract-zip": "^2.0.1",
"get-port": "^7.0.0",
"json5": "^2.2.3",
"log4js": "^6.9.1",
"physical-cpu-count": "^2.0.0",
"sm-crypto": "^0.3.13",
"systeminformation": "^5.25.11",
"webdav": "^5.3.1"
}
}

View File

@ -1,328 +1,364 @@
/**
* kit cli 注册和调用执行
*/
require("./init")
const { CliCmdParser } = require("../../argv")
const parser = new CliCmdParser("kit V" + $version);
const net = require("./net")
const shell = require("./shell")
const {sm4en3, sm4de3} = require("./crypto");
const convutil = require("./util/convutil")
const path = require("path");
const fs = require("fs");
const {sm3} = require("sm-crypto");
const zlib = require("zlib");
const mem = require("./mem")
parser.addCmdLine("man", "打开在线帮助;", async function (){
const uConfig = await $context.get("uConfig")
shell.execSync("start " + uConfig.get("man"))
});
parser.addCmdLine("manu", "人工维护配置;", function (){
shell.execSync("start " + $sConfig.getUserConfigPath())
});
parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
$logger.info( "kit 当前执行目录 " + process.cwd());
const fs = require("fs");
let step = cli.getParamValue("func");
if ( ! step ) {
step = "main"
}
let file = cli.getParamValue("file");
if ( ! file ) {
file = "kit"
}
const stat = fs.existsSync(`./${file}.js`)
if ( ! stat ) {
if ( ! cli.getParamValue("file") ) {
fs.writeFileSync("./kit.js",
`
async function main(){
console.log("hello kit!")
}
`);
} else {
$logger.info( `文件 ${file} 不存在` );
return
}
}
const projectScript = fs.readFileSync("./kit.js", "utf-8");
const vm = require('node:vm');
const script = new vm.Script(projectScript + ";" + step + "()");
const context = {
animal: 'cat',
count: 2,
$logger
};
script.runInNewContext(context, {
timeout : 10 * 60 * 1000,
breakOnSigint : true,
});
$logger.info( "暂未实现" );
});
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli){
let key = cli.getParamValue("key");
let value = cli.getParamValue("value");
const uConfig = await $context.get("uConfig")
if (null != key && null == value) {
let v = uConfig.get(key);
$logger.info("{} : {}", key, v);
} else if (null != key && null != value) {
uConfig.set(key, value);
$logger.info("set {} : {}", key, value);
} else {
$logger.error("参数异常");
}
});
parser.addCmdLine("remote-check", "验证远程仓库;", async function (){
let remote = await $context.get("remote")
if (! ( await remote.check()) ) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
});
parser.addCmdLine("publish", "发布到远程仓库;", async function (){
let remote = await $context.get("remote")
if (! ( await remote.check()) ) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.publish()
});
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli){
let remote = await $context.get("remote")
if (! ( await remote.check()) ) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.install(cli.getParamValue("module"))
});
parser.addCmdLine("freem [level]", "释放内存", function (cli) {
let level = cli.getParamValue("level")
mem.free( level )
});
parser.addCmdLine("killport <port>", "释放端口", function (cli) {
let port = cli.getParamValue("port")
net.killport( parseInt(port, 10) );
});
parser.addCmdLine("getport [--port]", "获取一个可用端口;或查询指定端口状态;", async function (cli) {
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 {
await net.getport( port )
}
});
parser.addCmdLine("checkip", "获取自己的公网 ip;", async function (cli) {
let ret = await net.checkip()
if( "ok" === ret.ret ) {
$logger.info( "ip : " + ret.data.ip )
$logger.info( "location : " + ret.data.location.join(" ") )
$logger.info("\n" + ret.data.ip)
} else {
$logger.error("获取失败")
}
});
parser.addCmdLine("telnet [--timeout] <ip-port> [port]", "验证远程 TCP 端口是否可用;", async function (cli) {
let ip = cli.getParamValue("ip-port")
let port = 80;
// 允许冒号形式指定端口
const re = /^(?<ip>.*):(?<port>[0-9]+)$/
const match = ip.match( re );
if ( match ) {
ip = match.groups.ip;
port = match.groups.port;
} else {
// 如果指定了 port 参数,那么使用 port 参数
let dport = cli.getParamValue("port")
if ( dport ) {
port = dport;
}
}
// 支持不填写 ip默认访问本地
if( "" === ip.trim() ) {
ip = "127.0.0.1";
}
try {
await net.telnet(port, ip, cli.getParamValue("timeout"))
$logger.info("连接成功")
}catch (e) {
$logger.error( e )
}
});
parser.addCmdLine("checklist <checkListFile>", "生成/验证文件 hash", async function (cli) {
let checkListFile = cli.getParamValue("checkListFile")
if ( !fs.existsSync( checkListFile ) ) {
$logger.error(`file ${checkListFile} 不存在`)
process.exit(-2)
}
const sm3 = require('sm-crypto').sm3
const list = fs.readFileSync(checkListFile, "UTF-8")
const files = list.split('\n').filter(line => line.trim() !== '');
for ( let file of files ) {
const infos = file.split(/\s+/)
const info = {}
if ( infos.length > 2 ) {
info.file = infos[0]
info.hash = infos[1]
info.alg = infos[2]
} else {
info.file = infos[0]
info.hash = infos[1]
info.alg = "md5"
}
if ( "sm3" === info.alg ) {
if ( !fs.existsSync( info.file ) ) {
$logger.error(`file ${info.file} 不存在`)
process.exit(-2)
}
const data = fs.readFileSync(info.file);
const arrayBuffer = Buffer.from(data);
const uint8Array = new Uint8Array(arrayBuffer);
let hashData = sm3( uint8Array ) // 杂凑
if ( hashData === info.hash ) {
$logger.info(`file ${file} 验证通过`)
} else {
$logger.error(`file ${file} 验证不通过`)
process.exit(-400)
}
} else {
$logger.error(`不支持 file ${file} `)
process.exit(-2)
}
}
$logger.info(`${checkListFile} 中的文件全部验证通过`)
})
parser.addCmdLine("sm3 [--file] [--text] [--hash]", "生成/验证 SM3 值;", async function (cli) {
let filePath = cli.getParamValue("file")
let text = cli.getParamValue("text")
if( !filePath && !text ) {
$logger.error(`file 和 text 入参不能同时有`)
process.exit(-1)
}
const sm3 = require('sm-crypto').sm3
if ( text ) {
let hashData = sm3( text ) // 杂凑
$logger.info( hashData )
}
if ( filePath ) {
if ( !fs.existsSync( filePath ) ) {
$logger.error(`file ${filePath} 不存在`)
process.exit(-2)
}
const data = fs.readFileSync(filePath);
const arrayBuffer = Buffer.from(data);
const uint8Array = new Uint8Array(arrayBuffer);
let hashData = sm3( uint8Array ) // 杂凑
$logger.info( hashData )
}
});
parser.addCmdLine("sm4en3 <key> <data>", "sm4 加密,密钥通过 sm3 制备;", function (cli) {
const {sm4en3} = require("./crypto");
let key = cli.getParamValue("key")
let data = cli.getParamValue("data")
let out = sm4en3( key, data )
$logger.info( out );
});
parser.addCmdLine("sm4de3 <key> <data>", "sm4 解密,密钥通过 sm3 制备;", function (cli) {
const {sm4de3} = require("./crypto");
let key = cli.getParamValue("key")
let data = cli.getParamValue("data")
let out = sm4de3( key, data )
$logger.info( out );
});
// parser.addCmdLine("base64 <-en/-de> <data>", "base64 编解码", function (cli) {
parser.addCmdLine("base64 [-en] [-de] [-hex] <data>", "base64 编解码;", function (cli) {
let data = cli.getParamValue("data")
if ( cli.hasFlag("de") ) {
if ( cli.hasFlag("hex") ) {
$logger.info( "执行 base64 decode to hex: " );
$logger.info( convutil.base64ToHex( data ) );
} else {
$logger.info( "执行 base64 decode to string: " );
$logger.info( convutil.base64ToString( data ) );
}
} else {
if ( cli.hasFlag("hex") ) {
$logger.info( "执行 hex encode to base64: " );
$logger.info( convutil.hexToBase64( data ) );
} else {
$logger.info( "执行 string encode to base64: " );
$logger.info( convutil.stringToBase64( data ) );
}
}
});
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()
/**
* kit cli 注册和调用执行
*/
require("./init")
const { CliCmdParser } = require("../../argv")
const parser = new CliCmdParser("kit V" + $version);
const net = require("./net")
const shell = require("./shell")
const {sm4en3, sm4de3} = require("./crypto");
const convutil = require("./util/convutil")
const path = require("path");
const fs = require("fs");
const {sm3} = require("sm-crypto");
const zlib = require("zlib");
const mem = require("./mem")
const { getAllDeviceInfo } = require("./platform")
const kitcommand = require("./kitcommand")
kitcommand.init()
kitcommand.regTo( parser )
// parser.addCmdLine("man", "打开在线帮助;", async function (){
// const uConfig = await $context.get("uConfig")
// shell.execSync("start " + uConfig.get("man"))
// });
// parser.addCmdLine("manu", "人工维护配置;", function (){
// shell.execSync("start " + $sConfig.getUserConfigPath())
// });
//
// parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
// $logger.info( "kit 当前执行目录 " + process.cwd());
// const fs = require("fs");
//
// let step = cli.getParamValue("func");
// if ( ! step ) {
// step = "main"
// }
//
// let file = cli.getParamValue("file");
// if ( ! file ) {
// file = "kit"
// }
//
// const stat = fs.existsSync(`./${file}.js`)
// if ( ! stat ) {
// if ( ! cli.getParamValue("file") ) {
// fs.writeFileSync("./kit.js",
// `
// async function main(){
// console.log("hello kit!")
// }
// `);
// } else {
// $logger.info( `文件 ${file} 不存在` );
// return
// }
// }
//
//
// const projectScript = fs.readFileSync("./kit.js", "utf-8");
// const vm = require('node:vm');
// const script = new vm.Script(projectScript + ";" + step + "()");
// const context = {
// animal: 'cat',
// count: 2,
// $logger,
// shell
// };
//
// script.runInNewContext(context, {
// timeout : 10 * 60 * 1000,
// breakOnSigint : true,
// });
// $logger.info( "暂未实现" );
// });
//
// parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli){
// let key = cli.getParamValue("key");
// let value = cli.getParamValue("value");
// const uConfig = await $context.get("uConfig")
// if (null != key && null == value) {
// let v = uConfig.get(key);
// $logger.info("{} : {}", key, v);
// } else if (null != key && null != value) {
// uConfig.set(key, value);
// $logger.info("set {} : {}", key, value);
// } else {
// $logger.error("参数异常");
// }
// });
//
// parser.addCmdLine("remote-check", "验证远程仓库;", async function (){
// let remote = await $context.get("remote")
// if (! ( await remote.check()) ) {
// $logger.error("远程仓库不可用")
// return
// } else {
// $logger.info("远程仓库可用")
// }
// });
//
// parser.addCmdLine("publish", "发布到远程仓库;", async function (){
// let remote = await $context.get("remote")
// if (! ( await remote.check()) ) {
// $logger.error("远程仓库不可用")
// return
// } else {
// $logger.info("远程仓库可用")
// }
// remote.publish()
// });
// parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli){
// let remote = await $context.get("remote")
// if (! ( await remote.check()) ) {
// $logger.error("远程仓库不可用")
// return
// } else {
// $logger.info("远程仓库可用")
// }
// remote.install(cli.getParamValue("module"))
// });
parser.addCmdLine("platform", "平台信息", function (cli) {
// 调用主函数并打印结果
getAllDeviceInfo().then(info => {
console.log('Device Information:');
console.log( JSON.stringify(info, null, 4));
}).catch(error => {
console.error('Error getting device information:', error);
});
});
parser.addCmdLine("freem [level]", "释放内存", function (cli) {
let level = cli.getParamValue("level")
mem.free( level )
});
parser.addCmdLine("killport <port>", "释放端口", function (cli) {
let port = cli.getParamValue("port")
net.killport( parseInt(port, 10) );
});
parser.addCmdLine("getport [--port]", "获取一个可用端口;或查询指定端口状态;", async function (cli) {
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 {
await net.getport( port )
}
});
parser.addCmdLine("checkip", "获取自己的公网 ip;", async function (cli) {
let ret = await net.checkip()
if( "ok" === ret.ret ) {
$logger.info( "ip : " + ret.data.ip )
$logger.info( "location : " + ret.data.location.join(" ") )
$logger.info("\n" + ret.data.ip)
} else {
$logger.error("获取失败")
}
});
parser.addCmdLine("telnet [--timeout] <ip-port> [port]", "验证远程 TCP 端口是否可用;", async function (cli) {
let ip = cli.getParamValue("ip-port")
let port = 80;
// 允许冒号形式指定端口
const re = /^(?<ip>.*):(?<port>[0-9]+)$/
const match = ip.match( re );
if ( match ) {
ip = match.groups.ip;
port = match.groups.port;
} else {
// 如果指定了 port 参数,那么使用 port 参数
let dport = cli.getParamValue("port")
if ( dport ) {
port = dport;
}
}
// 支持不填写 ip默认访问本地
if( "" === ip.trim() ) {
ip = "127.0.0.1";
}
try {
await net.telnet(port, ip, cli.getParamValue("timeout"))
$logger.info("连接成功")
process.exit(0)
}catch (e) {
$logger.error( e )
process.exit(-1)
}
});
parser.addCmdLine("checklist <checkListFile>", "生成/验证文件 hash", async function (cli) {
let checkListFile = cli.getParamValue("checkListFile")
if ( !fs.existsSync( checkListFile ) ) {
$logger.error(`file ${checkListFile} 不存在`)
process.exit(-2)
}
const sm3 = require('sm-crypto').sm3
const list = fs.readFileSync(checkListFile, "UTF-8")
const files = list.split('\n').filter(line => line.trim() !== '');
for ( let file of files ) {
const infos = file.split(/\s+/)
const info = {}
if ( infos.length > 2 ) {
info.file = infos[0]
info.hash = infos[1]
info.alg = infos[2]
} else {
info.file = infos[0]
info.hash = infos[1]
info.alg = "md5"
}
if ( "sm3" === info.alg ) {
if ( !fs.existsSync( info.file ) ) {
$logger.error(`file ${info.file} 不存在`)
process.exit(-2)
}
const data = fs.readFileSync(info.file);
const arrayBuffer = Buffer.from(data);
const uint8Array = new Uint8Array(arrayBuffer);
let hashData = sm3( uint8Array ) // 杂凑
if ( hashData === info.hash ) {
$logger.info(`file ${file} 验证通过`)
} else {
$logger.error(`file ${file} 验证不通过`)
process.exit(-400)
}
} else {
$logger.error(`不支持 file ${file} `)
process.exit(-2)
}
}
$logger.info(`${checkListFile} 中的文件全部验证通过`)
})
parser.addCmdLine("sm3 [--file] [--text] [--hash]", "生成/验证 SM3 值;", async function (cli) {
let filePath = cli.getParamValue("file")
let text = cli.getParamValue("text")
if( !filePath && !text ) {
$logger.error(`file 和 text 入参不能同时有`)
process.exit(-1)
}
const sm3 = require('sm-crypto').sm3
if ( text ) {
let hashData = sm3( text ) // 杂凑
$logger.info( hashData )
}
if ( filePath ) {
if ( !fs.existsSync( filePath ) ) {
$logger.error(`file ${filePath} 不存在`)
process.exit(-2)
}
const data = fs.readFileSync(filePath);
const arrayBuffer = Buffer.from(data);
const uint8Array = new Uint8Array(arrayBuffer);
let hashData = sm3( uint8Array ) // 杂凑
$logger.info( hashData )
}
});
parser.addCmdLine("sm4en3 <key> <data>", "sm4 加密,密钥通过 sm3 制备;", function (cli) {
const {sm4en3} = require("./crypto");
let key = cli.getParamValue("key")
let data = cli.getParamValue("data")
let out = sm4en3( key, data )
$logger.info( out );
});
parser.addCmdLine("sm4de3 <key> <data>", "sm4 解密,密钥通过 sm3 制备;", function (cli) {
const {sm4de3} = require("./crypto");
let key = cli.getParamValue("key")
let data = cli.getParamValue("data")
let out = sm4de3( key, data )
$logger.info( out );
});
// parser.addCmdLine("base64 <-en/-de> <data>", "base64 编解码", function (cli) {
parser.addCmdLine("base64 [-en] [-de] [-hex] <data>", "base64 编解码;", function (cli) {
let data = cli.getParamValue("data")
if ( cli.hasFlag("de") ) {
if ( cli.hasFlag("hex") ) {
$logger.info( "执行 base64 decode to hex: " );
$logger.info( convutil.base64ToHex( data ) );
} else {
$logger.info( "执行 base64 decode to string: " );
$logger.info( convutil.base64ToString( data ) );
}
} else {
if ( cli.hasFlag("hex") ) {
$logger.info( "执行 hex encode to base64: " );
$logger.info( convutil.hexToBase64( data ) );
} else {
$logger.info( "执行 string encode to base64: " );
$logger.info( convutil.stringToBase64( data ) );
}
}
});
parser.addCmdLine("touch [-d] <file_name>", "文件时间修改为当前时间;", function (cli) {
const fs = require('fs');
const path = cli.getParamValue("file_name")
// 获取当前日期时间
const now = new Date();
// 修改文件的访问时间和修改时间为当前时间
fs.utimes(path, now, now, (err) => {
if (err) {
$logger.error('修改文件时间出错:', err);
} else {
$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()

View File

@ -1,108 +1,107 @@
const shell = require("./shell");
const fs = require("fs");
const vm = require("node:vm");
module.exports = {
init : function () {
return this;
},
regTo: function (parser) {
parser.addCmdLine("help", "打开在线帮助;", async function () {
const uConfig = await $context.get("uConfig")
shell.execSync("start " + uConfig.get("man"))
});
parser.addCmdLine("manu", "人工维护配置;", function () {
shell.execSync("start " + $sConfig.getUserConfigPath())
});
parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
$logger.info("kit 当前执行目录 " + process.cwd());
const fs = require("fs");
let step = cli.getParamValue("func");
if (!step) {
step = "main"
}
let file = cli.getParamValue("file");
if (!file) {
file = "kit"
}
const stat = fs.existsSync(`./${file}.js`)
if (!stat) {
if (!cli.getParamValue("file")) {
fs.writeFileSync("./kit.js", `
async function main(){
console.log("hello kit!")
}
`);
} else {
$logger.info(`文件 ${file} 不存在`);
return
}
}
const projectScript = fs.readFileSync("./kit.js", "utf-8");
const vm = require('node:vm');
const script = new vm.Script(projectScript + ";" + step + "()");
const context = {
animal: 'cat', count: 2, $logger
};
script.runInNewContext(context, {
timeout: 10 * 60 * 1000, breakOnSigint: true,
});
$logger.info("暂未实现");
});
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli) {
let key = cli.getParamValue("key");
let value = cli.getParamValue("value");
const uConfig = await $context.get("uConfig")
if (null != key && null == value) {
let v = uConfig.get(key);
$logger.info("{} : {}", key, v);
} else if (null != key && null != value) {
uConfig.set(key, value);
$logger.info("set {} : {}", key, value);
} else {
$logger.error("参数异常");
}
});
parser.addCmdLine("remote-check", "验证远程仓库;", async function () {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
});
parser.addCmdLine("publish", "发布到远程仓库;", async function () {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.publish()
});
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli) {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.install(cli.getParamValue("module"))
});
}
}
const shell = require("./shell");
module.exports = {
init : function () {
return this;
},
regTo: function (parser) {
parser.addCmdLine("help", "打开在线帮助;", async function () {
const uConfig = await $context.get("uConfig")
shell.execSync("start " + uConfig.get("man"))
});
parser.addCmdLine("manu", "人工维护配置;", function () {
shell.execSync("start " + $sConfig.getUserConfigPath())
});
parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
$logger.info("kit 当前执行目录 " + process.cwd());
const fs = require("fs");
let step = cli.getParamValue("func");
if (!step) {
step = "main"
}
let file = cli.getParamValue("file");
if (!file) {
file = "kit"
}
const stat = fs.existsSync(`./${file}.js`)
if (!stat) {
if (!cli.getParamValue("file")) {
fs.writeFileSync("./kit.js", `
async function main(){
console.log("hello kit!")
}
`);
} else {
$logger.info(`文件 ${file} 不存在`);
return
}
}
const projectScript = fs.readFileSync("./kit.js", "utf-8");
const vm = require('node:vm');
const script = new vm.Script(projectScript + ";" + step + "()");
const context = {
animal: 'cat', count: 2, $logger, shell
};
script.runInNewContext(context, {
timeout: 10 * 60 * 1000, breakOnSigint: true,
});
$logger.info("执行结束");
});
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli) {
let key = cli.getParamValue("key");
let value = cli.getParamValue("value");
const uConfig = await $context.get("uConfig")
if (null != key && null == value) {
let v = uConfig.get(key);
$logger.info("{} : {}", key, v);
} else if (null != key && null != value) {
uConfig.set(key, value);
$logger.info("set {} : {}", key, value);
} else {
$logger.error("参数异常");
}
});
parser.addCmdLine("remote-check", "验证远程仓库;", async function () {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
});
parser.addCmdLine("publish", "发布到远程仓库;", async function () {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.publish()
});
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli) {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.install(cli.getParamValue("module"))
});
}
}

21
kit/src/launch.js Normal file
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()

View File

@ -1,22 +1,22 @@
const osutil = require("./util/osutil")
const shell = require("./shell")
function free(level) {
osutil.choise({
[osutil.Windows] : ( level ) => {
throw Error("暂不支持")
},
[osutil.Linux] : ( level ) => {
if ( [1,2,3].indexOf( parseInt(level,10)) === -1 ) {
level = 1
}
shell.execSync("sync")
shell.execSync(`echo ${level} > /proc/sys/vm/drop_caches`)
},
})
}
module.exports = {
free
const osutil = require("./util/osutil")
const shell = require("./shell")
function free(level) {
osutil.choise({
[osutil.Windows] : ( level ) => {
throw Error("暂不支持")
},
[osutil.Linux] : ( level ) => {
if ( [1,2,3].indexOf( parseInt(level,10)) === -1 ) {
level = 1
}
shell.execSync("sync")
shell.execSync(`echo ${level} > /proc/sys/vm/drop_caches`)
},
})
}
module.exports = {
free
}

View File

@ -1,84 +1,84 @@
require("./init")
const osutil = require("./util/osutil")
const killer = require('cross-port-killer');
const net = require('node:net');
/**
* 模拟 telnet 检测端口是否开放
* @param port
* @param host
* @param timeout
* @returns {Promise<unknown>}
*/
async function telnet(port, host, timeout=3000){
$logger.debug(`port=${port} host=${host} timeout=${timeout}`)
return new Promise(function (resolve, reject){
const socket = new net.Socket();
socket.setTimeout(timeout, function() {
socket.end();
reject("链接超时")
});
socket.connect(port, host);
socket.on('connect', function() {
resolve('连接成功');
socket.end();
});
socket.on('error', function(err) {
reject('连接失败');
});
})
}
async function killport(port) {
return killer.kill( port );
// let func = osutil.choise([
// require("./windows/net").killport,
// require("./linux/net").killport,
// ])
// return func && func(port);
}
/**
* 获取端口信息
* 因为命令输出缓存问题有缺陷
* @param port
* @returns {Promise<void>}
*/
async function getport(port) {
$logger.info( "暂未实现" );
}
async function checkip(){
const url = "https://myip.ipip.net/json"
const http = url.startsWith("https:") ? require("node:https") : require('node:http');
return new Promise((resolve, reject) => {
http.get(url, (res) => {
let data = '';
// A chunk of data has been recieved.
res.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Resolve the promise with result
res.on('end', () => {
resolve(JSON.parse(data));
});
}).on("error", (err) => {
// In case of any error reject the promise
reject(err);
});
});
}
module.exports = {
killport, getport, telnet, checkip
require("./init")
const osutil = require("./util/osutil")
const killer = require('cross-port-killer');
const net = require('node:net');
/**
* 模拟 telnet 检测端口是否开放
* @param port
* @param host
* @param timeout
* @returns {Promise<unknown>}
*/
async function telnet(port, host, timeout=3000){
$logger.debug(`port=${port} host=${host} timeout=${timeout}`)
return new Promise(function (resolve, reject){
const socket = new net.Socket();
socket.setTimeout(timeout, function() {
socket.end();
reject("链接超时")
});
socket.connect(port, host);
socket.on('connect', function() {
resolve('连接成功');
socket.end();
});
socket.on('error', function(err) {
reject('连接失败');
});
})
}
async function killport(port) {
return killer.kill( port );
// let func = osutil.choise([
// require("./windows/net").killport,
// require("./linux/net").killport,
// ])
// return func && func(port);
}
/**
* 获取端口信息
* 因为命令输出缓存问题有缺陷
* @param port
* @returns {Promise<void>}
*/
async function getport(port) {
$logger.info( "暂未实现" );
}
async function checkip(){
const url = "https://myip.ipip.net/json"
const http = url.startsWith("https:") ? require("node:https") : require('node:http');
return new Promise((resolve, reject) => {
http.get(url, (res) => {
let data = '';
// A chunk of data has been recieved.
res.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Resolve the promise with result
res.on('end', () => {
resolve(JSON.parse(data));
});
}).on("error", (err) => {
// In case of any error reject the promise
reject(err);
});
});
}
module.exports = {
killport, getport, telnet, checkip
}

87
kit/src/platform.js Normal file
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
};

View File

@ -23,5 +23,11 @@ module.exports = {
$logger.debug(`execSync ${cmd}`)
const output = execSync(cmd);
return output.toString()
},
callSync(cmd){
$logger.debug(`callSync ${cmd}`)
const output = execSync(cmd);
console.log(output.toString())
}
}

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 bin = parseInt(code).toString(2)
var ln = 0;
@ -209,135 +227,6 @@ function base64ArrayBuffer(arrayBuffer) {
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 = {
@ -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) {
const code = encodeURIComponent(text);
const bytes = [];
@ -546,6 +567,10 @@ if ( module ) {
utf8ToBase64,
base64ToUtf8,
utf8ToHex,
hexToUtf8
hexToUtf8,
formatBytes
}
}

View File

@ -1,51 +1,51 @@
const os = require('os');
const fs = require("fs");
const Windows = "Windows";
const Linux = "Linux";
module.exports = {
Windows, Linux,
/**
* 根据 os 不同载入不同的方法
*/
choise : function ( cbs ) {
if (os.type() === 'Windows_NT') {
// windows平台
return cbs && cbs[ Windows ]
}
else if (os.type() === 'Linux') {
// Linux平台
return cbs && cbs[ Linux ]
}
else {
throw { message : "不支持的 OS" }
}
},
cur : function () {
let isLinux = false;
// 操作系统不同,安装方式不同
let curOS = os.type()
if ( curOS === "Windows_NT" ) {
curOS = "Windows"
}
else if ( curOS === "Linux" ) {
isLinux = true;
const fs = require('fs');
if( fs.existsSync('/etc/alpine-release') ){
curOS = "Alpine"
}
}
let curPlatform = os.arch()
if ( curPlatform === "x64" ) {
curPlatform = "X86_64"
} else if ( curPlatform.startsWith("arm") ) {
curPlatform = "ARM64"
}
return { isLinux, curOS, curPlatform }
}
const os = require('os');
const fs = require("fs");
const Windows = "Windows";
const Linux = "Linux";
module.exports = {
Windows, Linux,
/**
* 根据 os 不同载入不同的方法
*/
choise : function ( cbs ) {
if (os.type() === 'Windows_NT') {
// windows平台
return cbs && cbs[ Windows ]
}
else if (os.type() === 'Linux') {
// Linux平台
return cbs && cbs[ Linux ]
}
else {
throw { message : "不支持的 OS" }
}
},
cur : function () {
let isLinux = false;
// 操作系统不同,安装方式不同
let curOS = os.type()
if ( curOS === "Windows_NT" ) {
curOS = "Windows"
}
else if ( curOS === "Linux" ) {
isLinux = true;
const fs = require('fs');
if( fs.existsSync('/etc/alpine-release') ){
curOS = "Alpine"
}
}
let curPlatform = os.arch()
if ( curPlatform === "x64" ) {
curPlatform = "X86_64"
} else if ( curPlatform.startsWith("arm") ) {
curPlatform = "ARM64"
}
return { isLinux, curOS, curPlatform }
}
}

BIN
kit/test/kit Normal file

Binary file not shown.

View File

@ -1,4 +1,4 @@
const net = require(__dirname + "/../src/net")
net.killport(6667);
const net = require(__dirname + "/../src/net")
net.killport(6667);

View File

@ -1,7 +1,56 @@
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){
const totalSize = fs.statSync(from).size;
const rs = fs.createReadStream( from );
@ -22,33 +71,53 @@ async function wUpload(client , from, to){
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(){
const webdav = await import("webdav");
const client = webdav.createClient(
"http://baishe.fullstack.club:5244/dav",
{
username: "cheney",
password: "722V587"
}
);
// const client = webdav.createClient(
// "http://172.16.17.230:5244/dav",
// "http://baishe.fullstack.club:5244/dav",
// {
// username: "kit",
// password: "kitkit"
// username: "cheney",
// password: "722V587"
// }
// );
const client = webdav.createClient(
"http://172.16.18.45:5244/dav",
{
username: "kit",
password: "kitkit"
}
);
// Get directory contents
const directoryItems = await client.getDirectoryContents("/");
console.log(directoryItems)
// await pipeline(
// client.createReadStream(
// "/index.json"
// ),
// fs.createWriteStream("./xxx.json")
// );
// 下载测试
await wDownload(client, "/kit")
// let rs = fs.createReadStream("D:\\fullstack\\TDevOps\\kit\\dist\\windows\\kit.exe")
// rs.on("data", (r)=>{
@ -64,10 +133,10 @@ async function main(){
// ws.on("data", (r)=>{
// console.log("w", r.length)
// })
// await pipeline(rs , ws );
// 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" )
console.log("over")
}

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