@Override
  public void initializeNativeSession(Session session) {
    super.initializeNativeSession(session);

    this.id = ObjectUtils.getIdentityHexString(getNativeSession());
    this.uri = session.getUpgradeRequest().getRequestURI();

    this.headers = new HttpHeaders();
    this.headers.putAll(getNativeSession().getUpgradeRequest().getHeaders());
    this.headers = HttpHeaders.readOnlyHttpHeaders(headers);

    this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();

    List<ExtensionConfig> source = getNativeSession().getUpgradeResponse().getExtensions();
    this.extensions = new ArrayList<WebSocketExtension>(source.size());
    for (ExtensionConfig ec : source) {
      this.extensions.add(new WebSocketExtension(ec.getName(), ec.getParameters()));
    }

    if (this.user == null) {
      this.user = session.getUpgradeRequest().getUserPrincipal();
    }
  }
Ejemplo n.º 2
0
 private String getFirstValue(Session sess, String name) {
   final List<String> vals = sess.getUpgradeRequest().getParameterMap().get(name);
   return vals != null ? vals.get(0) : null;
 }
Ejemplo n.º 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);
   }
 }