Search in sources :

Example 1 with SchSession

use of com.keybox.manage.model.SchSession in project KeyBox by skavanagh.

the class SecureShellWS method onMessage.

@OnMessage
public void onMessage(String message) {
    if (session.isOpen() && StringUtils.isNotEmpty(message)) {
        Map jsonRoot = new Gson().fromJson(message, Map.class);
        String command = (String) jsonRoot.get("command");
        Integer keyCode = null;
        Double keyCodeDbl = (Double) jsonRoot.get("keyCode");
        if (keyCodeDbl != null) {
            keyCode = keyCodeDbl.intValue();
        }
        for (String idStr : (ArrayList<String>) jsonRoot.get("id")) {
            Integer id = Integer.parseInt(idStr);
            //get servletRequest.getSession() for user
            UserSchSessions userSchSessions = SecureShellAction.getUserSchSessionMap().get(sessionId);
            if (userSchSessions != null) {
                SchSession schSession = userSchSessions.getSchSessionMap().get(id);
                if (keyCode != null) {
                    if (keyMap.containsKey(keyCode)) {
                        try {
                            schSession.getCommander().write(keyMap.get(keyCode));
                        } catch (IOException ex) {
                            log.error(ex.toString(), ex);
                        }
                    }
                } else {
                    schSession.getCommander().print(command);
                }
            }
        }
        //update timeout
        AuthUtil.setTimeout(httpSession);
    }
}
Also used : UserSchSessions(com.keybox.manage.model.UserSchSessions) SchSession(com.keybox.manage.model.SchSession) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SchSession

use of com.keybox.manage.model.SchSession in project KeyBox by skavanagh.

the class SecureShellWS method onClose.

@OnClose
public void onClose() {
    if (SecureShellAction.getUserSchSessionMap() != null) {
        UserSchSessions userSchSessions = SecureShellAction.getUserSchSessionMap().get(sessionId);
        if (userSchSessions != null) {
            Map<Integer, SchSession> schSessionMap = userSchSessions.getSchSessionMap();
            for (Integer sessionKey : schSessionMap.keySet()) {
                SchSession schSession = schSessionMap.get(sessionKey);
                //disconnect ssh session
                schSession.getChannel().disconnect();
                schSession.getSession().disconnect();
                schSession.setChannel(null);
                schSession.setSession(null);
                schSession.setInputToChannel(null);
                schSession.setCommander(null);
                schSession.setOutFromChannel(null);
                schSession = null;
                //remove from map
                schSessionMap.remove(sessionKey);
            }
            //clear and remove session map for user
            schSessionMap.clear();
            SecureShellAction.getUserSchSessionMap().remove(sessionId);
            SessionOutputUtil.removeUserSession(sessionId);
        }
    }
}
Also used : UserSchSessions(com.keybox.manage.model.UserSchSessions) SchSession(com.keybox.manage.model.SchSession)

Example 3 with SchSession

use of com.keybox.manage.model.SchSession in project KeyBox by skavanagh.

the class UploadAndPushAction method push.

@Action(value = "/admin/push", results = { @Result(name = "success", location = "/admin/upload_result.jsp") })
public String push() {
    Long userId = AuthUtil.getUserId(servletRequest.getSession());
    Long sessionId = AuthUtil.getSessionId(servletRequest.getSession());
    try {
        //get next pending system
        pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
        if (pendingSystemStatus != null) {
            //get session for system
            SchSession session = null;
            for (Integer instanceId : SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().keySet()) {
                //if host system id matches pending system then upload
                if (pendingSystemStatus.getId().equals(SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId).getHostSystem().getId())) {
                    session = SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId);
                }
            }
            if (session != null) {
                //push upload to system
                currentSystemStatus = SSHUtil.pushUpload(pendingSystemStatus, session.getSession(), UPLOAD_PATH + "/" + uploadFileName, pushDir + "/" + uploadFileName);
                //update system status
                SystemStatusDB.updateSystemStatus(currentSystemStatus, userId);
                pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
            }
        }
        //if push has finished to all servers then delete uploaded file
        if (pendingSystemStatus == null) {
            File delFile = new File(UPLOAD_PATH, uploadFileName);
            FileUtils.deleteQuietly(delFile);
            //delete all expired files in upload path
            File delDir = new File(UPLOAD_PATH);
            if (delDir.isDirectory()) {
                //set expire time to delete all files older than 48 hrs
                Calendar expireTime = Calendar.getInstance();
                expireTime.add(Calendar.HOUR, -48);
                Iterator<File> filesToDelete = FileUtils.iterateFiles(delDir, new AgeFileFilter(expireTime.getTime()), TrueFileFilter.TRUE);
                while (filesToDelete.hasNext()) {
                    delFile = filesToDelete.next();
                    delFile.delete();
                }
            }
        }
        hostSystemList = SystemStatusDB.getAllSystemStatus(userId);
    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    return SUCCESS;
}
Also used : AgeFileFilter(org.apache.commons.io.filefilter.AgeFileFilter) SchSession(com.keybox.manage.model.SchSession) File(java.io.File) Action(org.apache.struts2.convention.annotation.Action)

Aggregations

SchSession (com.keybox.manage.model.SchSession)3 UserSchSessions (com.keybox.manage.model.UserSchSessions)2 Gson (com.google.gson.Gson)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AgeFileFilter (org.apache.commons.io.filefilter.AgeFileFilter)1 Action (org.apache.struts2.convention.annotation.Action)1