Search in sources :

Example 6 with SessionStore

use of com.orion.remote.channel.SessionStore 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

SessionStore (com.orion.remote.channel.SessionStore)6 IOException (java.io.IOException)3 AuthenticationException (com.orion.exception.AuthenticationException)2 ConnectionRuntimeException (com.orion.exception.ConnectionRuntimeException)2 ProxyType (com.orion.ops.consts.machine.ProxyType)1 MachineInfoDO (com.orion.ops.entity.domain.MachineInfoDO)1 MachineProxyDO (com.orion.ops.entity.domain.MachineProxyDO)1 TerminalConnectDTO (com.orion.ops.entity.dto.TerminalConnectDTO)1 UserDTO (com.orion.ops.entity.dto.UserDTO)1 SftpExecutor (com.orion.remote.channel.sftp.SftpExecutor)1 CommandExecutor (com.orion.remote.channel.ssh.CommandExecutor)1