comparison src/org/tmatesoft/hg/internal/remote/SshConnector.java @ 685:9897cbfd2790

Towards ssh remote repositories: use ganymed library for ssh transport
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 25 Jul 2013 21:32:09 +0200
parents
children 9859fcea475d
comparison
equal deleted inserted replaced
684:2353e4217f59 685:9897cbfd2790
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal.remote;
18
19 import java.io.BufferedReader;
20 import java.io.Closeable;
21 import java.io.EOFException;
22 import java.io.File;
23 import java.io.FilterInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36
37 import org.tmatesoft.hg.core.HgRemoteConnectionException;
38 import org.tmatesoft.hg.core.Nodeid;
39 import org.tmatesoft.hg.core.SessionContext;
40 import org.tmatesoft.hg.internal.Internals;
41 import org.tmatesoft.hg.repo.HgBundle;
42 import org.tmatesoft.hg.repo.HgRemoteRepository.Range;
43 import org.tmatesoft.hg.repo.HgRuntimeException;
44 import org.tmatesoft.hg.util.LogFacility.Severity;
45
46 import ch.ethz.ssh2.Connection;
47 import ch.ethz.ssh2.ConnectionInfo;
48 import ch.ethz.ssh2.Session;
49 import ch.ethz.ssh2.StreamGobbler;
50
51 /**
52 * Remote repository via SSH
53 *
54 * @author Artem Tikhomirov
55 * @author TMate Software Ltd.
56 */
57 public class SshConnector {
58 private SessionContext sessionCtx;
59 private URL url;
60 private Connection conn;
61 private Session session;
62 private int sessionUse;
63
64 private StreamGobbler remoteErr, remoteOut;
65 private OutputStream remoteIn;
66
67 public void connect(URL url, SessionContext sessionContext, Object globalConfig) throws HgRemoteConnectionException {
68 sessionCtx = sessionContext;
69 this.url = url;
70 try {
71 conn = new Connection(url.getHost(), url.getPort() == -1 ? 22 : url.getPort());
72 conn.connect();
73 } catch (IOException ex) {
74 throw new HgRemoteConnectionException("Failed to establish connection");
75 }
76 try {
77 conn.authenticateWithPublicKey(System.getProperty("user.name"), new File(System.getProperty("user.home"), ".ssh/id_rsa"), null);
78 ConnectionInfo ci = conn.getConnectionInfo();
79 System.out.printf("%s %s %s %d %s %s %s\n", ci.clientToServerCryptoAlgorithm, ci.clientToServerMACAlgorithm, ci.keyExchangeAlgorithm, ci.keyExchangeCounter, ci.serverHostKeyAlgorithm, ci.serverToClientCryptoAlgorithm, ci.serverToClientMACAlgorithm);
80 } catch (IOException ex) {
81 throw new HgRemoteConnectionException("Failed to authenticate", ex).setServerInfo(getLocation());
82 }
83 }
84
85 public void disconnect() throws HgRemoteConnectionException {
86 if (session != null) {
87 forceSessionClose();
88 }
89 if (conn != null) {
90 conn.close();
91 conn = null;
92 }
93 }
94
95 public void sessionBegin() throws HgRemoteConnectionException {
96 if (sessionUse > 0) {
97 assert session != null;
98 sessionUse++;
99 return;
100 }
101 try {
102 session = conn.openSession();
103 session.execCommand(String.format("hg -R %s serve --stdio", url.getPath()));
104 remoteErr = new StreamGobbler(session.getStderr());
105 remoteOut = new StreamGobbler(session.getStdout());
106 sessionUse = 1;
107 } catch (IOException ex) {
108 throw new HgRemoteConnectionException("Failed to create ssh session", ex);
109 }
110 }
111
112 public void sessionEnd() throws HgRemoteConnectionException {
113 assert sessionUse > 0;
114 assert session != null;
115 if (sessionUse > 1) {
116 sessionUse--;
117 return;
118 }
119 forceSessionClose();
120 }
121
122 public String getLocation() {
123 return "";
124 }
125
126 public InputStream heads() throws HgRemoteConnectionException {
127 return executeCommand("heads", Collections.<Parameter>emptyList());
128 }
129
130 public InputStream between(Collection<Range> ranges) throws HgRemoteConnectionException {
131 StringBuilder sb = new StringBuilder(ranges.size() * 82);
132 for (Range r : ranges) {
133 r.append(sb).append(' ');
134 }
135 if (!ranges.isEmpty()) {
136 sb.setLength(sb.length() - 1);
137 }
138 return executeCommand("between", Collections.singletonList(new Parameter("pairs", sb.toString())));
139 }
140
141 public InputStream branches(List<Nodeid> nodes) throws HgRemoteConnectionException {
142 String l = join(nodes, ' ');
143 return executeCommand("branches", Collections.singletonList(new Parameter("nodes", l)));
144 }
145
146 public InputStream changegroup(List<Nodeid> roots) throws HgRemoteConnectionException, HgRuntimeException {
147 String l = join(roots, ' ');
148 return executeCommand("changegroup", Collections.singletonList(new Parameter("roots", l)));
149 }
150
151 public void unbundle(HgBundle bundle, List<Nodeid> remoteHeads) throws HgRemoteConnectionException, HgRuntimeException {
152 String l = join(remoteHeads, ' ');
153 Collections.singletonList(new Parameter("heads", l));
154 throw Internals.notImplemented();
155 }
156
157 public InputStream pushkey(String opName, String namespace, String key, String oldValue, String newValue) throws HgRemoteConnectionException, HgRuntimeException {
158 ArrayList<Parameter> p = new ArrayList<Parameter>();
159 p.add(new Parameter("namespace", namespace));
160 p.add(new Parameter("key", key));
161 p.add(new Parameter("old", oldValue));
162 p.add(new Parameter("new", newValue));
163 return executeCommand("pushkey", p);
164 }
165
166 public InputStream listkeys(String namespace, String actionName) throws HgRemoteConnectionException, HgRuntimeException {
167 return executeCommand("listkeys", Collections.singletonList(new Parameter("namespace", namespace)));
168 }
169
170
171 public Set<String> initCapabilities() throws HgRemoteConnectionException {
172 try {
173 final String CMD_CAPABILITIES = "capabilities";
174 final String CMD_HEADS = "heads";
175 final String CMD_HELLO = "hello";
176 consume(remoteOut);
177 consume(remoteErr);
178 remoteIn.write(CMD_HELLO.getBytes());
179 remoteIn.write('\n');
180 remoteIn.write(CMD_CAPABILITIES.getBytes()); // see http connector for
181 remoteIn.write('\n');
182 remoteIn.write(CMD_HEADS.getBytes());
183 remoteIn.write('\n');
184 checkError();
185 int responseLen = readResponseLength();
186 checkError();
187 FilterStream s = new FilterStream(remoteOut, responseLen);
188 BufferedReader r = new BufferedReader(new InputStreamReader(s));
189 String line;
190 while ((line = r.readLine()) != null) {
191 if (line.startsWith(CMD_CAPABILITIES) && line.length() > (CMD_CAPABILITIES.length()+1)) {
192 line = line.substring(CMD_CAPABILITIES.length());
193 if (line.charAt(0) == ':') {
194 String[] caps = line.substring(CMD_CAPABILITIES.length() + 1).split("\\s");
195 return new HashSet<String>(Arrays.asList(caps));
196 }
197 }
198 }
199 r.close();
200 consume(remoteOut);
201 checkError();
202 return Collections.emptySet();
203 } catch (IOException ex) {
204 throw new HgRemoteConnectionException("Failed to initiate dialog with server", ex).setRemoteCommand("hello").setServerInfo(getLocation());
205 } catch (HgRemoteConnectionException ex) {
206 ex.setRemoteCommand("hello").setServerInfo(getLocation());
207 throw ex;
208 }
209 }
210
211 private InputStream executeCommand(String cmd, List<Parameter> parameters) throws HgRemoteConnectionException {
212 try {
213 consume(remoteOut);
214 consume(remoteErr);
215 remoteIn.write(cmd.getBytes());
216 remoteIn.write('\n');
217 for (Parameter p : parameters) {
218 remoteIn.write(p.name().getBytes());
219 remoteIn.write(' ');
220 remoteIn.write(String.valueOf(p.size()).getBytes());
221 remoteIn.write('\n');
222 remoteIn.write(p.data());
223 remoteIn.write('\n');
224 }
225 checkError();
226 int responseLen = readResponseLength();
227 checkError();
228 return new FilterStream(remoteOut, responseLen);
229 } catch (IOException ex) {
230 throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand(cmd).setServerInfo(getLocation());
231 } catch (HgRemoteConnectionException ex) {
232 ex.setRemoteCommand(cmd).setServerInfo(getLocation());
233 throw ex;
234 }
235 }
236
237 private void consume(InputStream is) throws IOException {
238 while (is.available() > 0) {
239 is.read();
240 }
241 }
242
243 private void checkError() throws IOException, HgRemoteConnectionException {
244 if (remoteErr.available() > 0) {
245 StringBuilder sb = new StringBuilder();
246 int c;
247 while ((c = remoteErr.read()) != -1) {
248 sb.append((char)c);
249 }
250 throw new HgRemoteConnectionException(sb.toString());
251 }
252 }
253
254 private int readResponseLength() throws IOException {
255 int c;
256 StringBuilder sb = new StringBuilder();
257 while ((c = remoteOut.read()) != -1) {
258 if (c == '\n') {
259 break;
260 }
261 sb.append((char) c);
262 }
263 if (c == -1) {
264 throw new EOFException();
265 }
266 try {
267 return Integer.parseInt(sb.toString());
268 } catch (NumberFormatException ex) {
269 throw new IOException(String.format("Expected response length instead of %s", sb));
270 }
271 }
272
273
274 private void forceSessionClose() {
275 if (session != null) {
276 closeQuietly(remoteErr);
277 closeQuietly(remoteOut);
278 remoteErr = remoteOut = null;
279 closeQuietly(remoteIn);
280 remoteIn = null;
281 session.close();
282 session = null;
283 }
284 sessionUse = 0;
285 }
286
287 private void closeQuietly(Closeable c) {
288 try {
289 if (c != null) {
290 c.close();
291 }
292 } catch (IOException ex) {
293 sessionCtx.getLog().dump(getClass(), Severity.Warn, ex, null);
294 }
295 }
296
297 private static String join(List<Nodeid> values, char sep) {
298 StringBuilder sb = new StringBuilder(values.size() * 41);
299 for (Nodeid n : values) {
300 sb.append(n.toString());
301 sb.append(sep);
302 }
303 if (!values.isEmpty()) {
304 // strip last space
305 sb.setLength(sb.length() - 1);
306 }
307 return sb.toString();
308 }
309
310 private static final class Parameter {
311 private final String name;
312 private final byte[] data;
313
314 public Parameter(String paramName, String paramValue) {
315 assert paramName != null;
316 assert paramValue != null;
317 name = paramName;
318 data = paramValue.getBytes();
319 }
320
321 public String name() {
322 return name;
323 }
324 public int size() {
325 return data.length;
326 }
327 public byte[] data() {
328 return data;
329 }
330 }
331
332 private static final class FilterStream extends FilterInputStream {
333 private int length;
334
335 public FilterStream(InputStream is, int initialLength) {
336 super(is);
337 length = initialLength;
338 }
339
340 @Override
341 public int available() throws IOException {
342 return Math.min(super.available(), length);
343 }
344 @Override
345 public int read() throws IOException {
346 if (length == 0) {
347 return -1;
348 }
349 int r = super.read();
350 if (r >= 0) {
351 length--;
352 }
353 return r;
354 }
355 @Override
356 public int read(byte[] b, int off, int len) throws IOException {
357 if (length == 0) {
358 return -1;
359 }
360 int r = super.read(b, off, Math.min(len, length));
361 if (r >= 0) {
362 assert r <= length;
363 length -= r;
364 }
365 return r;
366 }
367 @Override
368 public void close() throws IOException {
369 // INTENTIONALLY DOES NOT CLOSE THE STREAM
370 }
371 }
372 }