Search in sources :

Example 16 with MachineInfoDO

use of com.orion.ops.entity.domain.MachineInfoDO in project orion-ops by lijiahangmax.

the class TerminalMessageHandler method connect.

/**
 * 建立连接
 *
 * @param session session
 * @param id      id
 * @param token   token
 * @param body    body
 */
private void connect(WebSocketSession session, String id, String token, String body) throws IOException {
    log.info("terminal 尝试建立连接 token: {}, id: {}, body: {}", token, id, body);
    // 检查参数
    TerminalConnectDTO connectInfo = TerminalConnectDTO.parse(body);
    if (connectInfo == null) {
        session.sendMessage(new TextMessage(WsProtocol.MISS_ARGUMENT.get()));
        return;
    }
    // 获取token信息
    Long tokenUserId = MachineTerminalService.getTokenUserId(token);
    String tokenKey = Strings.format(KeyConst.TERMINAL_ACCESS_TOKEN, token);
    Long machineId = Optional.ofNullable(redisTemplate.opsForValue().get(tokenKey)).map(Long::valueOf).orElse(null);
    if (machineId == null) {
        log.info("terminal 建立连接拒绝-token认证失败 token: {}", token);
        session.close(WsCloseCode.INCORRECT_TOKEN.close());
        return;
    }
    // 检查绑定
    String bindKey = Strings.format(KeyConst.TERMINAL_BIND_TOKEN, token);
    String bindValue = redisTemplate.opsForValue().get(bindKey);
    if (bindValue == null || !bindValue.equals(id)) {
        log.info("terminal 建立连接拒绝-bind认证失败 token: {}", token);
        session.close(WsCloseCode.IDENTITY_MISMATCH.close());
        return;
    }
    // 检查操作用户
    UserDTO userDTO = passportService.getUserByToken(connectInfo.getLoginToken(), null);
    if (userDTO == null || !tokenUserId.equals(userDTO.getId())) {
        log.info("terminal 建立连接拒绝-用户认证失败 token: {}", token);
        session.close(WsCloseCode.IDENTITY_MISMATCH.close());
        return;
    }
    // 获取机器信息
    MachineInfoDO machine = machineInfoService.selectById(machineId);
    if (machine == null) {
        log.info("terminal 建立连接拒绝-未查询到机器信息 token: {}, machineId: {}", token, machineId);
        session.close(WsCloseCode.INVALID_MACHINE.close());
        return;
    }
    // 删除token
    redisTemplate.delete(tokenKey);
    session.getAttributes().put(CONNECTED_KEY, 1);
    // 建立连接
    SessionStore sessionStore;
    try {
        // 打开session
        sessionStore = machineInfoService.openSessionStore(machine);
    } catch (Exception e) {
        WebSockets.openSessionStoreThrowClose(session, e);
        log.error("terminal 建立连接失败-连接远程服务器失败 uid: {}, machineId: {}, e: {}", tokenUserId, machineId, e);
        return;
    }
    // 配置
    TerminalConnectHint hint = new TerminalConnectHint();
    String terminalType = machineTerminalService.getMachineConfig(machineId).getTerminalType();
    hint.setUserId(tokenUserId);
    hint.setUsername(userDTO.getUsername());
    hint.setMachineId(machineId);
    hint.setMachineName(machine.getMachineName());
    hint.setMachineHost(machine.getMachineHost());
    hint.setMachineTag(machine.getMachineTag());
    hint.setCols(connectInfo.getCols());
    hint.setRows(connectInfo.getRows());
    hint.setWidth(connectInfo.getWidth());
    hint.setHeight(connectInfo.getHeight());
    hint.setTerminalType(terminalType);
    TerminalOperateHandler terminalHandler = new TerminalOperateHandler(token, hint, session, sessionStore);
    try {
        // 打开shell
        log.info("terminal 尝试建立连接-尝试打开shell token: {}", terminalHandler.getToken());
        terminalHandler.connect();
        log.info("terminal 建立连接成功-打开shell成功 token: {}", terminalHandler.getToken());
    } catch (Exception e) {
        session.close(WsCloseCode.OPEN_SHELL_EXCEPTION.close());
        log.error("terminal 建立连接失败-打开shell失败 machineId: {}, uid: {}, {}", machineId, tokenUserId, e);
        return;
    }
    terminalSessionManager.addSession(token, terminalHandler);
    session.sendMessage(new TextMessage(WsProtocol.CONNECTED.get()));
    log.info("terminal 建立连接成功 uid: {}, machineId: {}", tokenUserId, machineId);
}
Also used : SessionStore(com.orion.remote.channel.SessionStore) UserDTO(com.orion.ops.entity.dto.UserDTO) MachineInfoDO(com.orion.ops.entity.domain.MachineInfoDO) TerminalConnectDTO(com.orion.ops.entity.dto.TerminalConnectDTO) IOException(java.io.IOException)

Aggregations

MachineInfoDO (com.orion.ops.entity.domain.MachineInfoDO)16 Transactional (org.springframework.transaction.annotation.Transactional)4 FileTailListDO (com.orion.ops.entity.domain.FileTailListDO)3 FileTailVO (com.orion.ops.entity.vo.FileTailVO)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)2 MachineEnvDO (com.orion.ops.entity.domain.MachineEnvDO)2 UserDTO (com.orion.ops.entity.dto.UserDTO)2 Date (java.util.Date)2 ApplicationMachineDO (com.orion.ops.entity.domain.ApplicationMachineDO)1 CommandExecDO (com.orion.ops.entity.domain.CommandExecDO)1 MachineTerminalDO (com.orion.ops.entity.domain.MachineTerminalDO)1 FileTailDTO (com.orion.ops.entity.dto.FileTailDTO)1 TerminalConnectDTO (com.orion.ops.entity.dto.TerminalConnectDTO)1 MachineInfoVO (com.orion.ops.entity.vo.MachineInfoVO)1 MachineTerminalVO (com.orion.ops.entity.vo.MachineTerminalVO)1 TerminalAccessVO (com.orion.ops.entity.vo.TerminalAccessVO)1 CommandTaskSubmitVO (com.orion.ops.entity.vo.sftp.CommandTaskSubmitVO)1 ApplicationMachineService (com.orion.ops.service.api.ApplicationMachineService)1 SessionStore (com.orion.remote.channel.SessionStore)1 IOException (java.io.IOException)1