Exemple #1
0
 public String start() throws Exception {
   authSocket = AprLibrary.createLocalSocketAddress();
   pool = Pool.create(AprLibrary.getInstance().getRootPool());
   handle = Local.create(authSocket, pool);
   int result = Local.bind(handle, 0);
   if (result != Status.APR_SUCCESS) {
     throwException(result);
   }
   AprLibrary.secureLocalSocket(authSocket, handle);
   result = Local.listen(handle, 0);
   if (result != Status.APR_SUCCESS) {
     throwException(result);
   }
   thread =
       new Thread() {
         public void run() {
           try {
             while (true) {
               long clientSock = Local.accept(handle);
               Socket.timeoutSet(clientSock, 10000000);
               new SshAgentSession(clientSock, agent);
             }
           } catch (Exception e) {
             e.printStackTrace();
           }
         }
       };
   thread.start();
   return authSocket;
 }
  @Override
  protected OpenFuture doInit(Buffer buffer) {
    OpenFuture f = new DefaultOpenFuture(this);
    try {
      out =
          new ChannelOutputStream(
              this, getRemoteWindow(), log, SshConstants.SSH_MSG_CHANNEL_DATA, true);
      authSocket = PropertyResolverUtils.getString(this, SshAgent.SSH_AUTHSOCKET_ENV_NAME);
      pool = Pool.create(AprLibrary.getInstance().getRootPool());
      handle = Local.create(authSocket, pool);
      int result = Local.connect(handle, 0);
      if (result != Status.APR_SUCCESS) {
        throwException(result);
      }

      ExecutorService service = getExecutorService();
      forwardService =
          (service == null)
              ? ThreadUtils.newSingleThreadExecutor("ChannelAgentForwarding[" + authSocket + "]")
              : service;
      shutdownForwarder = service != forwardService || isShutdownOnExit();

      final int copyBufSize =
          PropertyResolverUtils.getIntProperty(
              this, FORWARDER_BUFFER_SIZE, DEFAULT_FORWARDER_BUF_SIZE);
      ValidateUtils.checkTrue(
          copyBufSize >= MIN_FORWARDER_BUF_SIZE, "Copy buf size below min.: %d", copyBufSize);
      ValidateUtils.checkTrue(
          copyBufSize <= MAX_FORWARDER_BUF_SIZE, "Copy buf size above max.: %d", copyBufSize);

      forwarder =
          forwardService.submit(
              () -> {
                try {
                  byte[] buf = new byte[copyBufSize];
                  while (true) {
                    int len = Socket.recv(handle, buf, 0, buf.length);
                    if (len > 0) {
                      out.write(buf, 0, len);
                      out.flush();
                    }
                  }
                } catch (IOException e) {
                  close(true);
                }
              });

      signalChannelOpenSuccess();
      f.setOpened();
    } catch (Throwable t) {
      Throwable e = GenericUtils.peelException(t);
      signalChannelOpenFailure(e);
      f.setException(e);
    }

    return f;
  }