]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.core/src/org/argeo/ssh/AbstractSsh.java
Make web socket configuration more extensible.
[lgpl/argeo-commons.git] / org.argeo.core / src / org / argeo / ssh / AbstractSsh.java
1 package org.argeo.ssh;
2
3 import java.io.Console;
4 import java.io.IOException;
5 import java.net.URI;
6 import java.security.GeneralSecurityException;
7 import java.security.KeyPair;
8 import java.util.Arrays;
9 import java.util.Scanner;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.sshd.client.SshClient;
14 import org.apache.sshd.client.config.keys.ClientIdentityLoader;
15 import org.apache.sshd.client.future.ConnectFuture;
16 import org.apache.sshd.client.session.ClientSession;
17 import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
18 import org.apache.sshd.common.config.keys.FilePasswordProvider;
19
20 abstract class AbstractSsh {
21 private final static Log log = LogFactory.getLog(AbstractSsh.class);
22
23 private static SshClient sshClient;
24 private static SftpFileSystemProvider sftpFileSystemProvider;
25
26 private boolean passwordSet = false;
27 private ClientSession session;
28
29 synchronized SshClient getSshClient() {
30 if (sshClient == null) {
31 long begin = System.currentTimeMillis();
32 sshClient = SshClient.setUpDefaultClient();
33 sshClient.start();
34 long duration = System.currentTimeMillis() - begin;
35 if (log.isDebugEnabled())
36 log.debug("SSH client started in " + duration + " ms");
37 Runtime.getRuntime().addShutdownHook(new Thread(() -> sshClient.stop(), "Stop SSH client"));
38 }
39 return sshClient;
40 }
41
42 synchronized SftpFileSystemProvider getSftpFileSystemProvider() {
43 if (sftpFileSystemProvider == null) {
44 sftpFileSystemProvider = new SftpFileSystemProvider(sshClient);
45 }
46 return sftpFileSystemProvider;
47 }
48
49 void authenticate() {
50 try {
51 if (!passwordSet) {
52 String password;
53 Console console = System.console();
54 if (console == null) {// IDE
55 System.out.print("Password: ");
56 Scanner s = new Scanner(System.in);
57 password = s.next();
58 } else {
59 console.printf("Password: ");
60 char[] pwd = console.readPassword();
61 password = new String(pwd);
62 Arrays.fill(pwd, ' ');
63 }
64 session.addPasswordIdentity(password);
65 passwordSet = true;
66 }
67 session.auth().verify(1000l);
68 } catch (IOException e) {
69 throw new IllegalStateException(e);
70 }
71 }
72
73 void addPassword(String password) {
74 session.addPasswordIdentity(password);
75 }
76
77 void loadKey(String password) {
78 loadKey(password, System.getProperty("user.home") + "/.ssh/id_rsa");
79 }
80
81 void loadKey(String password, String keyPath) {
82 // try {
83 // KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
84 // FilePasswordProvider.of(password));
85 // session.addPublicKeyIdentity(keyPair);
86 // } catch (IOException | GeneralSecurityException e) {
87 // throw new IllegalStateException(e);
88 // }
89 }
90
91 void openSession(URI uri) {
92 openSession(uri.getUserInfo(), uri.getHost(), uri.getPort() > 0 ? uri.getPort() : null);
93 }
94
95 void openSession(String login, String host, Integer port) {
96 if (session != null)
97 throw new IllegalStateException("Session is already open");
98
99 if (host == null)
100 host = "localhost";
101 if (port == null)
102 port = 22;
103 if (login == null)
104 login = System.getProperty("user.name");
105 String password = null;
106 int sepIndex = login.indexOf(':');
107 if (sepIndex > 0)
108 if (sepIndex + 1 < login.length()) {
109 password = login.substring(sepIndex + 1);
110 login = login.substring(0, sepIndex);
111 } else {
112 throw new IllegalArgumentException("Illegal authority: " + login);
113 }
114 try {
115 ConnectFuture connectFuture = getSshClient().connect(login, host, port);
116 connectFuture.await();
117 ClientSession session = connectFuture.getSession();
118 if (password != null) {
119 session.addPasswordIdentity(password);
120 passwordSet = true;
121 }
122 this.session = session;
123 } catch (IOException e) {
124 throw new IllegalStateException("Cannot connect to " + host + ":" + port);
125 }
126 }
127
128 void closeSession() {
129 if (session == null)
130 throw new IllegalStateException("No session is open");
131 try {
132 session.close();
133 } catch (IOException e) {
134 e.printStackTrace();
135 } finally {
136 session = null;
137 }
138 }
139
140 ClientSession getSession() {
141 return session;
142 }
143
144 }