@Override @OnWebSocketClose public void onClose(int statusCode, String reason) { if (shell != null) { shell.disconnect(); } if (jschSession != null) { jschSession.disconnect(); } super.onClose(statusCode, reason); }
@Override @OnWebSocketConnect public void onConnect(Session session) { super.onConnect(session); this.session.setIdleTimeout(TimeUnit.MINUTES.toMillis(1)); Connector con = null; try { ConnectorFactory cf = ConnectorFactory.getDefault(); con = cf.createConnector(); } catch (AgentProxyException e) { System.out.println(e); } IdentityRepository irepo = null; if (con != null) { RemoteIdentityRepository rrepo = new RemoteIdentityRepository(con); if (rrepo.getIdentities() != null && rrepo.getIdentities().size() > 0) { irepo = rrepo; jsch.setIdentityRepository(irepo); } } if (irepo == null) { String home = System.getProperty("user.home"); String sshDir = home + File.separator + ".ssh" + File.separator; String[] defaultKeys = new String[] { sshDir + "id_ecdsa", sshDir + "id_id_ed25519", sshDir + "id_rsa", sshDir + "id_dsa", sshDir + "identity" }; for (String nextKey : defaultKeys) { try { jsch.addIdentity(nextKey); log.fine("Key '" + nextKey + "' added"); } catch (JSchException e) { log.log(Level.FINE, "Key '" + nextKey + "' not valid", e); } } } Map<String, List<String>> parameterMap = session.getUpgradeRequest().getParameterMap(); String host = getStringParameter(parameterMap, "host", null); String connectHost = hostLookupService.getResolvableHostname(host); String user = getStringParameter(parameterMap, "user", null); if ("@admin".equals(user)) { user = hostLookupService.getAdminUserFor(host); } Resize resize = new Resize(); resize.cols = getIntParameter(parameterMap, "cols", 80); resize.rows = getIntParameter(parameterMap, "rows", 24); try { java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); jschSession = jsch.getSession(user, connectHost, hostLookupService.getSshPort(host)); jschSession.setConfig(config); jschSession.connect(60000); shell = (ChannelShell) jschSession.openChannel("shell"); shell.setAgentForwarding(true); shell.setPtyType("vt102"); shell.connect(); shell.setPtySize(resize.cols, resize.rows, resize.getPixelWidth(), resize.getPixelHeight()); } catch (JSchException e) { close(1, "Failed to create ssh session", e); } Runnable run; try { run = new RawSentOutputTask( session, new BufferedInputStream(shell.getInputStream(), BUFFER_LEN)); Thread thread = new Thread(run); thread.start(); } catch (IOException e) { close(2, "IOException while getting data from ssh", e); } try { inputToShell = new PrintStream(shell.getOutputStream(), true, "UTF-8"); } catch (IOException e) { close(3, "IOException while creating write stream to ssh", e); } }