-
Notifications
You must be signed in to change notification settings - Fork 22
/
SshShellCommandFactory.java
153 lines (130 loc) · 5.35 KB
/
SshShellCommandFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2020 François Onimus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.fonimus.ssh.shell;
import com.github.fonimus.ssh.shell.listeners.SshShellListenerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.sshd.server.ExitCallback;
import org.apache.sshd.server.channel.ChannelSession;
import org.apache.sshd.server.command.Command;
import org.jline.reader.Parser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;
import org.springframework.shell.Shell;
import org.springframework.shell.jline.JLineShellAutoConfiguration;
import org.springframework.shell.jline.PromptProvider;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Ssh shell command implementation, which starts threads of SshShellRunnable
*
* @see SshShellRunnable
*/
@Slf4j
@Component
public class SshShellCommandFactory
implements Command {
public static final ThreadLocal<SshContext> SSH_THREAD_CONTEXT = ThreadLocal.withInitial(() -> null);
private SshShellProperties properties;
private SshShellListenerService shellListenerService;
private Banner shellBanner;
private PromptProvider promptProvider;
private Shell shell;
private JLineShellAutoConfiguration.CompleterAdapter completerAdapter;
private final Parser parser;
private Environment environment;
public static final ThreadLocal<SshIO> SSH_IO_CONTEXT = ThreadLocal.withInitial(SshIO::new);
private Map<ChannelSession, Thread> threads = new ConcurrentHashMap<>();
/**
* Constructor
*
* @param shellListenerService shell listener service
* @param banner shell banner
* @param promptProvider prompt provider
* @param shell spring shell
* @param completerAdapter completer adapter
* @param parser jline parser
* @param environment spring environment
* @param properties ssh shell properties
*/
public SshShellCommandFactory(SshShellListenerService shellListenerService,
@Autowired(required = false) Banner banner,
@Lazy PromptProvider promptProvider,
Shell shell,
JLineShellAutoConfiguration.CompleterAdapter completerAdapter, Parser parser,
Environment environment,
SshShellProperties properties) {
this.shellListenerService = shellListenerService;
this.shellBanner = banner;
this.promptProvider = promptProvider;
this.shell = shell;
this.completerAdapter = completerAdapter;
this.parser = parser;
this.environment = environment;
this.properties = properties;
}
/**
* 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());
}
@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());
}
@Override
public void setErrorStream(OutputStream errOS) {
// not used
}
@Override
public void setExitCallback(ExitCallback ec) {
SSH_IO_CONTEXT.get().setEc(ec);
}
@Override
public void setInputStream(InputStream is) {
SSH_IO_CONTEXT.get().setIs(is);
}
@Override
public void setOutputStream(OutputStream os) {
SSH_IO_CONTEXT.get().setOs(os);
}
public Map<Long, ChannelSession> listSessions() {
return threads.keySet().stream()
.collect(Collectors.toMap(s -> s.getServerSession().getIoSession().getId(), Function.identity()));
}
}