@xtail/ssh
Version:
X-Tail SSH 库,包含 SSH 连接服务器的类和工具方法等
4 lines (3 loc) • 3.57 kB
JavaScript
"use strict";var h=Object.defineProperty;var u=(n,s,t)=>s in n?h(n,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[s]=t;var i=(n,s,t)=>u(n,typeof s!="symbol"?s+"":s,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("ssh2-promise"),l=require("../node_modules/.pnpm/strip-ansi@7.1.0/node_modules/strip-ansi/index.js"),g=require("../node_modules/.pnpm/split-lines@3.0.0/node_modules/split-lines/index.js"),e=require("../utils/index.js"),f="SSH";class y{constructor(s){i(this,"config");i(this,"ssh",null);i(this,"connected",!1);i(this,"socket",null);i(this,"isSession",!1);i(this,"currentCmd",{});this.config=s}get isConnected(){return this.connected}async connect(){if(this.connected)return e.SSHUtil.genSuccessResultByConf(this.config,{message:"已连接成功"});try{return this.ssh=new S(this.config),await this.ssh.connect(),this.connected=!0,e.SSHUtil.genSuccessResultByConf(this.config,{message:"连接成功"})}catch(s){return this.connected=!1,this.ssh=null,e.SSHUtil.genFailResultByConf(this.config,{message:s.message??"连接失败",detail:s})}}async close(){var s;(!this.ssh||!this.connected)&&e.SSHUtil.genSuccessResultByConf(this.config,{message:"已断开服务器连接"});try{return await((s=this.ssh)==null?void 0:s.close()),this.connected=!1,this.ssh=null,e.SSHUtil.genSuccessResultByConf(this.config,{message:"断开服务器连接成功"})}catch(t){return e.SSHUtil.genFailResultByConf(this.config,{message:t.message??"断开服务器连接失败",detail:t})}}async exec(s){if(!this.ssh||!this.connected)return e.SSHUtil.genFailResultByConf(this.config,{message:"未连接服务器,请先连接服务器"});try{const t=await this.ssh.exec(s);return e.SSHUtil.genSuccessResultByConf(this.config,{message:`${s} 执行成功`,detail:t})}catch(t){return e.SSHUtil.genFailResultByConf(this.config,{message:t.message??`${s} 执行失败`,detail:t})}}get isSessionOpen(){return this.isSession}async sessionOpen(s,t){if(!this.ssh||!this.connected)return e.SSHUtil.genFailResultByConf(this.config,{message:"未连接服务器,请先连接服务器"});try{return this.socket=await this.ssh.shell(),this.socket.on("data",r=>{let o="";const a=g(r.toString("utf8"));for(const c of a)Reflect.has(this.currentCmd,c.trim())?Reflect.deleteProperty(this.currentCmd,c.trim()):Reflect.has(this.currentCmd,l(c).trim())?Reflect.deleteProperty(this.currentCmd,l(c).trim()):o+=c+`
`;o.trim()&&s(o)}),this.socket.on("close",t),this.isSession=!0,e.SSHUtil.genSuccessResultByConf(this.config,{message:"会话创建成功"})}catch(r){return this.isSession=!1,this.socket=null,e.SSHUtil.genFailResultByConf(this.config,{message:r.message??"会话创建失败",detail:r})}}async sessionExec(s){if(!this.socket||!this.isSession)return e.SSHUtil.genFailResultByConf(this.config,{message:"未与服务器创建会话,请先创建会话"});try{return this.currentCmd[s]=s,this.socket.write(s+`
`),e.SSHUtil.genSuccessResultByConf(this.config,{message:`${s} 执行成功`})}catch(t){return e.SSHUtil.genFailResultByConf(this.config,{message:t.message??`${s} 执行失败`,detail:t})}}async sessionClose(){if(!this.socket||!this.isSession)return e.SSHUtil.genSuccessResultByConf(this.config,{message:"会话已关闭"});try{return await this.socket.destroy(),this.currentCmd={},this.isSession=!1,this.socket=null,e.SSHUtil.genSuccessResultByConf(this.config,{message:"会话关闭成功"})}catch(s){return e.SSHUtil.genFailResultByConf(this.config,{message:s.message??"会话关闭失败",detail:s})}}}exports.SSH=y;exports.SSH_Name=f;