Search in sources :

Example 16 with SshModel

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;
}
Also used : SshModel(io.jpom.model.data.SshModel)

Example 17 with 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);
}
Also used : SshModel(io.jpom.model.data.SshModel) JSONArray(com.alibaba.fastjson.JSONArray) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with SshModel

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);
    }
}
Also used : SshModel(io.jpom.model.data.SshModel) ChannelSftp(com.jcraft.jsch.ChannelSftp) Sftp(cn.hutool.extra.ssh.Sftp) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with SshModel

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;
}
Also used : JSchException(com.jcraft.jsch.JSchException) SshModel(io.jpom.model.data.SshModel) Session(com.jcraft.jsch.Session)

Example 20 with SshModel

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();
    }
}
Also used : StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) BaseWorkspaceService(io.jpom.service.h2db.BaseWorkspaceService) Service(org.springframework.stereotype.Service) SshModel(io.jpom.model.data.SshModel) SystemClock(cn.hutool.core.date.SystemClock) UserModel(io.jpom.model.data.UserModel) Collectors(java.util.stream.Collectors) BaseServerController(io.jpom.common.BaseServerController) SshTerminalExecuteLog(io.jpom.model.log.SshTerminalExecuteLog) StrUtil(cn.hutool.core.util.StrUtil) SshTerminalExecuteLog(io.jpom.model.log.SshTerminalExecuteLog)

Aggregations

SshModel (io.jpom.model.data.SshModel)20 ClassFeature (io.jpom.permission.ClassFeature)10 Feature (io.jpom.permission.Feature)10 MethodFeature (io.jpom.permission.MethodFeature)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 JSONObject (com.alibaba.fastjson.JSONObject)5 UserModel (io.jpom.model.data.UserModel)5 File (java.io.File)5 IOException (java.io.IOException)5 StrUtil (cn.hutool.core.util.StrUtil)3 Session (com.jcraft.jsch.Session)3 BaseServerController (io.jpom.common.BaseServerController)3 NodeModel (io.jpom.model.data.NodeModel)3 SshService (io.jpom.service.node.ssh.SshService)3 Charset (java.nio.charset.Charset)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Service (org.springframework.stereotype.Service)3 Entity (cn.hutool.db.Entity)2