예제 #1
0
 @OnWebSocketMessage
 @SuppressWarnings("PMD.CollapsibleIfStatements")
 public void onMessage(String message) {
   if (session.isOpen()) {
     if (message != null && !message.isEmpty()) {
       if (message.startsWith("{\"cols\":")) {
         Resize resize = new Gson().fromJson(message, Resize.class);
         if (resize != null) {
           shell.setPtySize(
               resize.cols, resize.rows, resize.getPixelWidth(), resize.getPixelHeight());
         }
       } else if (message.startsWith("{\"ping\":")) {
         if (!shell.isConnected()) {
           try {
             session
                 .getRemote()
                 .sendPing(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)));
           } catch (IOException e) {
             close(4, "IOException while sending ping data to client", e);
           }
         }
       } else {
         try {
           inputToShell.write(message.getBytes(Charset.forName("UTF-8")));
         } catch (IOException e) {
           close(5, "IOException while sending data to ssh", e);
         }
       }
     }
   }
 }
예제 #2
0
 @Override
 public void layoutContainer(Container parent) {
   for (Component component : this.componentMap.keySet()) {
     Resize r = this.componentMap.get(component);
     Rectangle rect = r.resize(parent);
     if (this.padding != 0) {
       rect =
           new Rectangle(
               rect.x + padding,
               rect.y + padding,
               rect.width - padding * 2,
               rect.height - padding * 2);
     }
     component.setBounds(rect);
   }
 }
예제 #3
0
 @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);
   }
 }