-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackingSshShellCommandFactory.java
193 lines (169 loc) · 5.94 KB
/
TrackingSshShellCommandFactory.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* ==================================================================
* TrackingSshShellCommandFactory.java - 3/08/2020 11:42:15 AM
*
* Copyright 2020 SolarNetwork Foundation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ==================================================================
*/
package org.snf.accounting.cli;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import javax.validation.ValidatorFactory;
import org.apache.sshd.server.channel.ChannelSession;
import org.apache.sshd.server.session.ServerSession;
import org.jline.reader.Parser;
import org.springframework.boot.Banner;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.shell.CompletionContext;
import org.springframework.shell.CompletionProposal;
import org.springframework.shell.Input;
import org.springframework.shell.InputProvider;
import org.springframework.shell.MethodTarget;
import org.springframework.shell.ParameterResolver;
import org.springframework.shell.Shell;
import org.springframework.shell.jline.JLineShellAutoConfiguration.CompleterAdapter;
import org.springframework.shell.jline.PromptProvider;
import org.springframework.stereotype.Component;
import com.github.fonimus.ssh.shell.SshContext;
import com.github.fonimus.ssh.shell.SshShellCommandFactory;
import com.github.fonimus.ssh.shell.SshShellProperties;
import com.github.fonimus.ssh.shell.listeners.SshShellListenerService;
/**
* Extension of {@link SshShellCommandFactory} to provide tracking of active SSH sessions.
*
* @author matt
* @version 1.0
*/
@Primary
@Component("trackingSshShellCommandFactory")
public class TrackingSshShellCommandFactory extends SshShellCommandFactory {
private static final Map<SshContext, Boolean> SSH_CONTEXTS = new WeakHashMap<>(4, 0.9f);
// CHECKSTYLE OFF: LineLength
private static final Map<ServerSession, org.apache.sshd.server.Environment> SSH_ENVIRONMENTS = new WeakHashMap<>(
4, 0.9f);
// CHECKSTYLE ON: LineLength
/**
* 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
* shell properties
*/
public TrackingSshShellCommandFactory(SshShellListenerService shellListenerService, Banner banner,
PromptProvider promptProvider, Shell shell, CompleterAdapter completerAdapter, Parser parser,
Environment environment, SshShellProperties properties) {
super(shellListenerService, banner, promptProvider, new TrackingShell(shell), completerAdapter,
parser, environment, properties);
}
@Override
public void start(ChannelSession channelSession, org.apache.sshd.server.Environment env) {
super.start(channelSession, env);
SSH_ENVIRONMENTS.put(channelSession.getServerSession(), env);
}
private static final class TrackingShell extends Shell {
private final Shell delegate;
private TrackingShell(Shell delegate) {
super(null);
this.delegate = delegate;
}
@Override
public void setValidatorFactory(ValidatorFactory validatorFactory) {
delegate.setValidatorFactory(validatorFactory);
}
@Override
public Map<String, MethodTarget> listCommands() {
return delegate.listCommands();
}
@Override
public void gatherMethodTargets() throws Exception {
delegate.gatherMethodTargets();
}
@Override
public void setParameterResolvers(List<ParameterResolver> resolvers) {
delegate.setParameterResolvers(resolvers);
}
@Override
public void run(InputProvider inputProvider) throws IOException {
// assumption that SSH_THREAD_CONTEXT has been set for the current thread at this point
SshContext ctx = SSH_THREAD_CONTEXT.get();
if (ctx != null) {
synchronized (SSH_CONTEXTS) {
SSH_CONTEXTS.put(ctx, Boolean.TRUE);
}
}
delegate.run(inputProvider);
}
@Override
public Object evaluate(Input input) {
return delegate.evaluate(input);
}
@Override
public List<CompletionProposal> complete(CompletionContext context) {
return delegate.complete(context);
}
}
/**
* Get all available SSH contexts.
*
* @return all available contexts
*/
public static Iterable<SshContext> sshContexts() {
synchronized (SSH_CONTEXTS) {
Set<SshContext> keys = SSH_CONTEXTS.keySet();
List<SshContext> contexts = new ArrayList<>(keys.size());
keys.forEach(c -> {
if (c != null) {
contexts.add(c);
}
});
return contexts;
}
}
/**
* Get the active SSH environment.
*
* @return the active SSH environment
*/
public static org.apache.sshd.server.Environment sshEnvironment() {
SshContext ctx = SSH_THREAD_CONTEXT.get();
if (ctx != null) {
ServerSession sess = ctx.getSshSession();
if (sess != null) {
return SSH_ENVIRONMENTS.get(sess);
}
}
return null;
}
}