org.apache.sshd.server.channel.ChannelSession Java Examples

The following examples show how to use org.apache.sshd.server.channel.ChannelSession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SshShellListenerServiceTest.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
void name() {
    SshShellListenerService service = new SshShellListenerService(Arrays.asList(
            event -> {
                assertNotNull(event);
                assertNotNull(event.toString());
                assertNotNull(event.getType());
                assertNotNull(event.getSession());
                assertEquals(123L, event.getSessionId());
            }, event -> {
                throw new IllegalArgumentException("[TEST]");
            }
    ));

    ChannelSession session = mockChannelSession(123L);

    service.onSessionStarted(session);
    service.onSessionStopped(session);
    service.onSessionError(session);

    // no exception during executions event with illegal argument exception -> only error log
}
 
Example #2
Source File: ManageSessionsCommand.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
@ShellMethod("Displays active sessions")
public String manageSessionsList() {
    Map<Long, ChannelSession> sessions = sessionManager.listSessions();

    SimpleTable.SimpleTableBuilder builder = SimpleTable.builder()
            .column("Session Id").column("Local address").column("Remote address").column("Authenticated User");

    for (ChannelSession value : sessions.values()) {
        builder.line(Arrays.asList(
                value.getServerSession().getIoSession().getId(),
                value.getServerSession().getIoSession().getLocalAddress(),
                value.getServerSession().getIoSession().getRemoteAddress(),
                sessionUserName(value)
        ));
    }
    return helper.renderTable(builder.build());
}
 
Example #3
Source File: SshShellRunnable.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
public SshShellRunnable(SshShellProperties properties, ChannelSession session,
                        SshShellListenerService shellListenerService, Banner shellBanner,
                        PromptProvider promptProvider, Shell shell,
                        JLineShellAutoConfiguration.CompleterAdapter completerAdapter, Parser parser,
                        Environment environment, org.apache.sshd.server.Environment sshEnv,
                        SshShellCommandFactory sshShellCommandFactory, InputStream is,
                        OutputStream os, ExitCallback ec) {
    this.properties = properties;
    this.session = session;
    this.shellListenerService = shellListenerService;
    this.shellBanner = shellBanner;
    this.promptProvider = promptProvider;
    this.shell = shell;
    this.completerAdapter = completerAdapter;
    this.parser = parser;
    this.environment = environment;
    this.sshEnv = sshEnv;
    this.sshShellCommandFactory = sshShellCommandFactory;
    this.is = is;
    this.os = os;
    this.ec = ec;
}
 
Example #4
Source File: AsyncEchoShellFactory.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public int data(final ChannelSession channel, byte[] buf, int start, int len) throws IOException {
    buffer.append(new String(buf, start, len));
    for (int i = 0; i < buffer.length(); i++) {
        if (buffer.charAt(i) == '\n') {
            final String s = buffer.substring(0, i + 1);
            final byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
            out.write(new ByteArrayBuffer(bytes)).addListener(new SshFutureListener<IoWriteFuture>() {
                @Override
                public void operationComplete(IoWriteFuture future) {
                    Session session = channel.getSession();
                    if (future.isWritten()) {
                        try {
                            Window wLocal = channel.getLocalWindow();
                            wLocal.consumeAndCheck(bytes.length);
                        } catch (IOException e) {
                            session.exceptionCaught(e);
                        }
                    } else {
                        Throwable t = future.getException();
                        session.exceptionCaught(t);
                    }
                }
            });
            buffer = new StringBuilder(buffer.substring(i + 1));
            i = 0;
        }
    }
    return 0;
}
 
Example #5
Source File: TtyCommand.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
@Override
public int data(ChannelSession channel, byte[] buf, int start, int len) throws IOException {
    if (decoder != null) {
        lastAccessedTime = System.currentTimeMillis();
        decoder.write(buf, start, len);
    } else {
        // Data send too early ?
    }
    return len;
}
 
Example #6
Source File: SshSessionInstance.java    From sshd-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void start(ChannelSession channel, Environment env) throws IOException {
    terminalType = env.getEnv().get(Environment.ENV_TERM);
    this.channel = channel;
    sshThread = new Thread(this, "ssh-cli " + channel.getSession().getIoSession().getAttribute(Constants.USER));
    sshThread.start();
}
 
Example #7
Source File: SshShellUtilsTest.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
public static ChannelSession mockChannelSession(Long id) {
    ChannelSession session = mock(ChannelSession.class);
    ServerSession serverSession = mock(ServerSession.class);
    when(session.getSession()).thenReturn(serverSession);
    IoSession ioSession = mock(IoSession.class);
    when(serverSession.getIoSession()).thenReturn(ioSession);
    when(ioSession.getId()).thenReturn(id);
    return session;
}
 
Example #8
Source File: TtyCommand.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public int data(ChannelSession channel, byte[] buf, int start, int len) throws IOException {
  if (decoder != null) {
    lastAccessedTime = System.currentTimeMillis();
    decoder.write(buf, start, len);
  } else {
    // Data send too early ?
  }
  return len;
}
 
Example #9
Source File: SshShellSessionManager.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Search for authenticated user in session
 *
 * @param session ssh session
 * @return user name
 */
public static String sessionUserName(ChannelSession session) {
    SshAuthentication authentication =
            (SshAuthentication) session.getServerSession().getIoSession().getAttribute("authentication");
    if (authentication == null) {
        return null;
    }
    return authentication.getName();
}
 
Example #10
Source File: SshShellSessionManager.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Stop active session
 *
 * @param id session id
 * @return true if session found and stopped, false otherwise
 */
public boolean stopSession(long id) {
    ChannelSession session = getSession(id);
    if (session != null) {
        this.commandFactory.destroy(session);
        return true;
    }
    return false;
}
 
Example #11
Source File: Server.java    From sftpserver with Apache License 2.0 5 votes vote down vote up
@Override
public void start(final ChannelSession channel, final Environment env) throws IOException {
	if (err != null) {
		err.write("shell not allowed\r\n".getBytes("ISO-8859-1"));
		err.flush();
	}
	if (callback != null)
		callback.onExit(-1, "shell not allowed");
}
 
Example #12
Source File: SshShellCommandFactory.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy(ChannelSession channelSession) {
    Thread sshThread = threads.remove(channelSession);
    if (sshThread != null) {
        sshThread.interrupt();
    }
    LOGGER.debug("{}: destroyed [{} session(s) currently active]", channelSession, threads.size());
}
 
Example #13
Source File: SshShellCommandFactory.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Start ssh session
 *
 * @param channelSession ssh channel session
 * @param env            ssh environment
 */
@Override
public void start(ChannelSession channelSession, org.apache.sshd.server.Environment env) {
    SshIO sshIO = SSH_IO_CONTEXT.get();
    Thread sshThread = new Thread(new ThreadGroup("ssh-shell"), new SshShellRunnable(properties,
            channelSession, shellListenerService, shellBanner, promptProvider, shell, completerAdapter, parser,
            environment, env, this, sshIO.getIs(), sshIO.getOs(), sshIO.getEc()),
            "ssh-session-" + System.nanoTime());
    sshThread.start();
    threads.put(channelSession, sshThread);
    LOGGER.debug("{}: started [{} session(s) currently active]", channelSession, threads.size());
}
 
Example #14
Source File: ManageSessionsCommand.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@ShellMethod("Displays session")
public String manageSessionsInfo(@ShellOption(value = {"-i", "--session-id"}) long sessionId) {
    ChannelSession session = sessionManager.getSession(sessionId);
    if (session == null) {
        return helper.getError("Session [" + sessionId + "] not found");
    }
    return helper.getSuccess(sessionTable(session.getServerSession()));
}
 
Example #15
Source File: AsyncEchoShellFactory.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public int data(final ChannelSession channel, byte[] buf, int start, int len) throws IOException {
    buffer.append(new String(buf, start, len));
    for (int i = 0; i < buffer.length(); i++) {
        if (buffer.charAt(i) == '\n') {
            final String s = buffer.substring(0, i + 1);
            final byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
            out.write(new ByteArrayBuffer(bytes)).addListener(new SshFutureListener<IoWriteFuture>() {
                @Override
                public void operationComplete(IoWriteFuture future) {
                    Session session = channel.getSession();
                    if (future.isWritten()) {
                        try {
                            Window wLocal = channel.getLocalWindow();
                            wLocal.consumeAndCheck(bytes.length);
                        } catch (IOException e) {
                            session.exceptionCaught(e);
                        }
                    } else {
                        Throwable t = future.getException();
                        session.exceptionCaught(t);
                    }
                }
            });
            buffer = new StringBuilder(buffer.substring(i + 1));
            i = 0;
        }
    }
    return 0;
}
 
Example #16
Source File: TtyCommand.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public int data(ChannelSession channel, byte[] buf, int start, int len) throws IOException {
  if (decoder != null) {
    lastAccessedTime = System.currentTimeMillis();
    decoder.write(buf, start, len);
  } else {
    // Data send too early ?
  }
  return len;
}
 
Example #17
Source File: Server.java    From sftpserver with Apache License 2.0 4 votes vote down vote up
@Override
public Command createShell(final ChannelSession channel) throws IOException {
	return new SecureShellCommand();
}
 
Example #18
Source File: TtyCommand.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setChannelSession(ChannelSession session) {
  this.session = session;
}
 
Example #19
Source File: AsyncEchoShellFactory.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setChannelSession(ChannelSession session) {
    this.session = session;
}
 
Example #20
Source File: TtyCommand.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setChannelSession(ChannelSession session) {
  this.session = session;
}
 
Example #21
Source File: TtyCommand.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
@Override
public void setChannelSession(ChannelSession session) {
    this.session = session;
}
 
Example #22
Source File: Server.java    From sftpserver with Apache License 2.0 4 votes vote down vote up
@Override
public void destroy(final ChannelSession channel) {
}
 
Example #23
Source File: SshSessionInstance.java    From sshd-shell-spring-boot with Apache License 2.0 4 votes vote down vote up
@Override
public void destroy(ChannelSession channel) throws Exception {
    channel.close();
    sshThread.interrupt();
}
 
Example #24
Source File: SshShellRunnable.java    From ssh-shell-spring-boot with Apache License 2.0 4 votes vote down vote up
@Override
public void setChannelSession(ChannelSession session) {
    this.session = session;
}
 
Example #25
Source File: SshShellCommandFactory.java    From ssh-shell-spring-boot with Apache License 2.0 4 votes vote down vote up
public Map<Long, ChannelSession> listSessions() {
    return threads.keySet().stream()
            .collect(Collectors.toMap(s -> s.getServerSession().getIoSession().getId(), Function.identity()));
}
 
Example #26
Source File: AsyncEchoShellFactory.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setChannelSession(ChannelSession session) {
    this.session = session;
}
 
Example #27
Source File: SshShellListenerService.java    From ssh-shell-spring-boot with Apache License 2.0 2 votes vote down vote up
/**
 * Session stopped with error
 *
 * @param channelSession ssh channel session
 */
public void onSessionError(ChannelSession channelSession) {
    notify(new SshShellEvent(SshShellEventType.SESSION_STOPPED_UNEXPECTEDLY, channelSession));
}
 
Example #28
Source File: SshShellListenerService.java    From ssh-shell-spring-boot with Apache License 2.0 2 votes vote down vote up
/**
 * Session stopped
 *
 * @param channelSession ssh channel session
 */
public void onSessionStopped(ChannelSession channelSession) {
    notify(new SshShellEvent(SshShellEventType.SESSION_STOPPED, channelSession));
}
 
Example #29
Source File: SshShellListenerService.java    From ssh-shell-spring-boot with Apache License 2.0 2 votes vote down vote up
/**
 * Session started
 *
 * @param channelSession ssh channel session
 */
public void onSessionStarted(ChannelSession channelSession) {
    notify(new SshShellEvent(SshShellEventType.SESSION_STARTED, channelSession));
}
 
Example #30
Source File: SshShellSessionManager.java    From ssh-shell-spring-boot with Apache License 2.0 2 votes vote down vote up
/**
 * @param id session id
 * @return found session, or null if not existing
 */
public ChannelSession getSession(long id) {
    return listSessions().get(id);
}