use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshFileController method check.
private SshModel check(String id, String path, String children) {
SshModel sshModel = sshService.getByKey(id, false);
Assert.notNull(sshModel, "不存在对应ssh");
Assert.hasText(path, "请选择文件夹");
List<String> fileDirs = sshModel.fileDirs();
Assert.state(CollUtil.contains(fileDirs, path), "没有配置此文件夹");
//
if (StrUtil.isNotEmpty(children)) {
// 判断是否合法
children = FileUtil.normalize(children);
FileUtil.file(path, children);
}
return sshModel;
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshFileController method listData.
@RequestMapping(value = "list_file_data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public String listData(String id, String path, String children) throws SftpException {
SshModel sshModel = this.check(id, path, children);
//
JSONArray jsonArray = listDir(sshModel, path, children);
return JsonMessage.getString(200, "ok", jsonArray);
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshFileController method delete.
@RequestMapping(value = "delete.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public String delete(String id, String path, String name) {
Assert.hasText(name, "name error");
SshModel sshModel = this.check(id, path, name);
name = FileUtil.normalize(name);
Assert.state(!StrUtil.equals(name, StrUtil.SLASH), "不能删除根目录");
Session session = null;
Sftp sftp = null;
try {
// 验证合法性,防止越权
FileUtil.file(path, name);
//
String normalize = FileUtil.normalize(path + StrUtil.SLASH + name);
session = SshService.getSessionByModel(sshModel);
sftp = new Sftp(session, sshModel.getCharsetT());
// 尝试删除
boolean dirOrFile = this.tryDelDirOrFile(sftp, normalize);
if (dirOrFile) {
String parent = FileUtil.getParent(name, 1);
return JsonMessage.getString(200, "删除成功", parent);
}
return JsonMessage.getString(200, "删除成功");
} catch (Exception e) {
DefaultSystemLog.getLog().error("ssh删除文件异常", e);
return JsonMessage.getString(400, "删除失败:" + e.getMessage());
} finally {
IoUtil.close(sftp);
JschUtil.close(session);
}
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshService method getSessionByModel.
/**
* 获取 ssh 回话
*
* @param sshModel sshModel
* @return session
*/
public static Session getSessionByModel(SshModel sshModel) {
Session session;
SshModel.ConnectType connectType = sshModel.connectType();
if (connectType == SshModel.ConnectType.PASS) {
session = JschUtil.openSession(sshModel.getHost(), sshModel.getPort(), sshModel.getUser(), sshModel.getPassword());
} else if (connectType == SshModel.ConnectType.PUBKEY) {
File rsaFile;
String privateKey = sshModel.getPrivateKey();
if (StrUtil.startWith(privateKey, URLUtil.FILE_URL_PREFIX)) {
String rsaPath = StrUtil.removePrefix(privateKey, URLUtil.FILE_URL_PREFIX);
rsaFile = FileUtil.file(rsaPath);
} else if (StrUtil.isEmpty(privateKey)) {
File home = FileUtil.getUserHomeDir();
Assert.notNull(home, "用户目录没有找到");
File identity = FileUtil.file(home, ".ssh", "identity");
rsaFile = FileUtil.isFile(identity) ? identity : null;
File idRsa = FileUtil.file(home, ".ssh", "id_rsa");
rsaFile = FileUtil.isFile(idRsa) ? idRsa : rsaFile;
File idDsa = FileUtil.file(home, ".ssh", "id_dsa");
rsaFile = FileUtil.isFile(idDsa) ? idDsa : rsaFile;
Assert.notNull(rsaFile, "用户目录没有找到私钥信息");
} else {
File tempPath = ConfigBean.getInstance().getTempPath();
String sshFile = StrUtil.emptyToDefault(sshModel.getId(), IdUtil.fastSimpleUUID());
rsaFile = FileUtil.file(tempPath, "ssh", sshFile);
FileUtil.writeString(privateKey, rsaFile, CharsetUtil.UTF_8);
}
Assert.state(FileUtil.isFile(rsaFile), "私钥文件不存在:" + FileUtil.getAbsolutePath(rsaFile));
byte[] pas = null;
if (StrUtil.isNotEmpty(sshModel.getPassword())) {
pas = sshModel.getPassword().getBytes();
}
session = JschUtil.openSession(sshModel.getHost(), sshModel.getPort(), sshModel.getUser(), FileUtil.getAbsolutePath(rsaFile), pas);
} else {
throw new IllegalArgumentException("不支持的模式");
}
try {
session.setServerAliveInterval((int) TimeUnit.SECONDS.toMillis(5));
session.setServerAliveCountMax(5);
} catch (JSchException ignored) {
}
return session;
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshTerminalExecuteLogService method batch.
/**
* 批量记录日志
*
* @param userInfo 操作的用户
* @param sshItem ssh 对象
* @param ip 操作人的ip
* @param userAgent 浏览器标识
* @param commands 命令行
* @param refuse 是否拒绝执行
*/
public void batch(UserModel userInfo, SshModel sshItem, String ip, String userAgent, boolean refuse, List<String> commands) {
if (sshItem == null) {
return;
}
long optTime = SystemClock.now();
try {
BaseServerController.resetInfo(userInfo);
List<SshTerminalExecuteLog> executeLogs = commands.stream().filter(StrUtil::isNotEmpty).map(s -> {
SshTerminalExecuteLog sshTerminalExecuteLog = new SshTerminalExecuteLog();
// sshTerminalExecuteLog.setId(IdUtil.fastSimpleUUID());
sshTerminalExecuteLog.setSshId(sshItem.getId());
sshTerminalExecuteLog.setSshName(sshItem.getName());
sshTerminalExecuteLog.setWorkspaceId(sshItem.getWorkspaceId());
sshTerminalExecuteLog.setCommands(s);
sshTerminalExecuteLog.setRefuse(refuse);
sshTerminalExecuteLog.setCreateTimeMillis(optTime);
sshTerminalExecuteLog.setIp(ip);
sshTerminalExecuteLog.setUserAgent(userAgent);
// sshTerminalExecuteLog.setUserId(UserModel.getOptUserName(userInfo));
return sshTerminalExecuteLog;
}).collect(Collectors.toList());
super.insert(executeLogs);
} finally {
BaseServerController.removeAll();
}
}
Aggregations