changeset 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 2353e4217f59
children f1f095e42555
files .classpath LICENSE-ganymed.txt build.xml lib/ganymed-ssh2-build251beta1.jar src/org/tmatesoft/hg/internal/remote/SshConnector.java src/org/tmatesoft/hg/repo/HgRemoteRepository.java
diffstat 6 files changed, 478 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/.classpath	Thu Jul 25 21:31:23 2013 +0200
+++ b/.classpath	Thu Jul 25 21:32:09 2013 +0200
@@ -5,5 +5,6 @@
 	<classpathentry kind="src" path="cmdline"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
 	<classpathentry kind="lib" path="lib/junit-4.8.2.jar" sourcepath="lib/junit-4.8.2-src.jar"/>
+	<classpathentry kind="lib" path="lib/ganymed-ssh2-build251beta1.jar"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE-ganymed.txt	Thu Jul 25 21:32:09 2013 +0200
@@ -0,0 +1,87 @@
+Copyright (c) 2006 - 2010 Christian Plattner. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+a.) Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+b.) Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+c.) Neither the name of Christian Plattner nor the names of its contributors may
+    be used to endorse or promote products derived from this software
+    without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+This software includes work that was released under the following license:
+
+Copyright (c) 2005 - 2006 Swiss Federal Institute of Technology (ETH Zurich),
+  Department of Computer Science (http://www.inf.ethz.ch),
+  Christian Plattner. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+a.) Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+b.) Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+c.) Neither the name of ETH Zurich nor the names of its contributors may
+    be used to endorse or promote products derived from this software
+    without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+The Java implementations of the AES, Blowfish and 3DES ciphers have been
+taken (and slightly modified) from the cryptography package released by
+"The Legion Of The Bouncy Castle".
+
+Their license states the following:
+
+Copyright (c) 2000 - 2004 The Legion Of The Bouncy Castle
+(http://www.bouncycastle.org)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE. 
+
--- a/build.xml	Thu Jul 25 21:31:23 2013 +0200
+++ b/build.xml	Thu Jul 25 21:32:09 2013 +0200
@@ -26,6 +26,7 @@
 	</description>
 
 	<property name="junit.jar" value="lib/junit-4.8.2.jar" />
+	<property name="ssh.jar" value="lib/ganymed-ssh2-build251beta1.jar" />
 	<property name="ver.qualifier" value="" />
 	<property name="version.lib" value="1.2" />
 	<property name="version.jar" value="${version.lib}${ver.qualifier}" />
@@ -144,7 +145,11 @@
 
 	<target name="build-lib">
 		<mkdir dir="bin" />
-		<javac srcdir="src" destdir="bin" debug="${compile-with-debug}" includeantruntime="no" source="1.5" encoding="UTF-8"/>
+		<javac srcdir="src" destdir="bin" debug="${compile-with-debug}" includeantruntime="no" source="1.5" encoding="UTF-8">
+			<classpath>
+				<pathelement location="${ssh.jar}"/>
+			</classpath>
+		</javac>
 		<jar destfile="${hg4j.jar}">
 			<fileset dir="bin/">
 				<include name="org/tmatesoft/hg/core/**" />
Binary file lib/ganymed-ssh2-build251beta1.jar has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/internal/remote/SshConnector.java	Thu Jul 25 21:32:09 2013 +0200
@@ -0,0 +1,372 @@
+/*
+ * Copyright (c) 2013 TMate Software Ltd
+ *  
+ * 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; version 2 of the License.
+ *
+ * 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.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.internal.remote;
+
+import java.io.BufferedReader;
+import java.io.Closeable;
+import java.io.EOFException;
+import java.io.File;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.tmatesoft.hg.core.HgRemoteConnectionException;
+import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.core.SessionContext;
+import org.tmatesoft.hg.internal.Internals;
+import org.tmatesoft.hg.repo.HgBundle;
+import org.tmatesoft.hg.repo.HgRemoteRepository.Range;
+import org.tmatesoft.hg.repo.HgRuntimeException;
+import org.tmatesoft.hg.util.LogFacility.Severity;
+
+import ch.ethz.ssh2.Connection;
+import ch.ethz.ssh2.ConnectionInfo;
+import ch.ethz.ssh2.Session;
+import ch.ethz.ssh2.StreamGobbler;
+
+/**
+ * Remote repository via SSH
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class SshConnector {
+	private SessionContext sessionCtx;
+	private URL url;
+	private Connection conn;
+	private Session session;
+	private int sessionUse;
+
+	private StreamGobbler remoteErr, remoteOut;
+	private OutputStream remoteIn;
+	
+	public void connect(URL url, SessionContext sessionContext, Object globalConfig) throws HgRemoteConnectionException {
+		sessionCtx = sessionContext;
+		this.url = url;
+		try {
+			conn = new Connection(url.getHost(), url.getPort() == -1 ? 22 : url.getPort());
+			conn.connect();
+		} catch (IOException ex) {
+			throw new HgRemoteConnectionException("Failed to establish connection");
+		}
+		try {
+			conn.authenticateWithPublicKey(System.getProperty("user.name"), new File(System.getProperty("user.home"), ".ssh/id_rsa"), null);
+			ConnectionInfo ci = conn.getConnectionInfo();
+			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);
+		} catch (IOException ex) {
+			throw new HgRemoteConnectionException("Failed to authenticate", ex).setServerInfo(getLocation());
+		}
+	}
+	
+	public void disconnect() throws HgRemoteConnectionException {
+		if (session != null) {
+			forceSessionClose();
+		}
+		if (conn != null) {
+			conn.close();
+			conn = null;
+		}
+	}
+	
+	public void sessionBegin() throws HgRemoteConnectionException {
+		if (sessionUse > 0) {
+			assert session != null;
+			sessionUse++;
+			return;
+		}
+		try {
+			session = conn.openSession();
+			session.execCommand(String.format("hg -R %s serve --stdio", url.getPath()));
+			remoteErr = new StreamGobbler(session.getStderr());
+			remoteOut = new StreamGobbler(session.getStdout());
+			sessionUse = 1;
+		} catch (IOException ex) {
+			throw new HgRemoteConnectionException("Failed to create ssh session", ex);
+		}
+	}
+	
+	public void sessionEnd() throws HgRemoteConnectionException {
+		assert sessionUse > 0;
+		assert session != null;
+		if (sessionUse > 1) {
+			sessionUse--;
+			return;
+		}
+		forceSessionClose();
+	}
+
+	public String getLocation() {
+		return "";
+	}
+
+	public InputStream heads() throws HgRemoteConnectionException {
+		return executeCommand("heads", Collections.<Parameter>emptyList());
+	}
+	
+	public InputStream between(Collection<Range> ranges) throws HgRemoteConnectionException {
+		StringBuilder sb = new StringBuilder(ranges.size() * 82);
+		for (Range r : ranges) {
+			r.append(sb).append(' ');
+		}
+		if (!ranges.isEmpty()) {
+			sb.setLength(sb.length() - 1);
+		}
+		return executeCommand("between", Collections.singletonList(new Parameter("pairs", sb.toString())));
+	}
+	
+	public InputStream branches(List<Nodeid> nodes) throws HgRemoteConnectionException {
+		String l = join(nodes, ' ');
+		return executeCommand("branches", Collections.singletonList(new Parameter("nodes", l)));
+	}
+	
+	public InputStream changegroup(List<Nodeid> roots) throws HgRemoteConnectionException, HgRuntimeException {
+		String l = join(roots, ' ');
+		return executeCommand("changegroup", Collections.singletonList(new Parameter("roots", l)));
+	}
+
+	public void unbundle(HgBundle bundle, List<Nodeid> remoteHeads) throws HgRemoteConnectionException, HgRuntimeException {
+		String l = join(remoteHeads, ' ');
+		Collections.singletonList(new Parameter("heads", l));
+		throw Internals.notImplemented();
+	}
+
+	public InputStream pushkey(String opName, String namespace, String key, String oldValue, String newValue) throws HgRemoteConnectionException, HgRuntimeException {
+		ArrayList<Parameter> p = new ArrayList<Parameter>();
+		p.add(new Parameter("namespace", namespace));
+		p.add(new Parameter("key", key));
+		p.add(new Parameter("old", oldValue));
+		p.add(new Parameter("new", newValue));
+		return executeCommand("pushkey", p);
+	}
+	
+	public InputStream listkeys(String namespace, String actionName) throws HgRemoteConnectionException, HgRuntimeException {
+		return executeCommand("listkeys", Collections.singletonList(new Parameter("namespace", namespace)));
+	}
+	
+	
+	public Set<String> initCapabilities() throws HgRemoteConnectionException {
+		try {
+			final String CMD_CAPABILITIES = "capabilities";
+			final String CMD_HEADS = "heads";
+			final String CMD_HELLO = "hello";
+			consume(remoteOut);
+			consume(remoteErr);
+			remoteIn.write(CMD_HELLO.getBytes());
+			remoteIn.write('\n');
+			remoteIn.write(CMD_CAPABILITIES.getBytes()); // see http connector for
+			remoteIn.write('\n');
+			remoteIn.write(CMD_HEADS.getBytes());
+			remoteIn.write('\n');
+			checkError();
+			int responseLen = readResponseLength();
+			checkError();
+			FilterStream s = new FilterStream(remoteOut, responseLen);
+			BufferedReader r = new BufferedReader(new InputStreamReader(s));
+			String line;
+			while ((line = r.readLine()) != null) {
+				if (line.startsWith(CMD_CAPABILITIES) && line.length() > (CMD_CAPABILITIES.length()+1)) {
+					line = line.substring(CMD_CAPABILITIES.length());
+					if (line.charAt(0) == ':') {
+						String[] caps = line.substring(CMD_CAPABILITIES.length() + 1).split("\\s");
+						return new HashSet<String>(Arrays.asList(caps));
+					}
+				}
+			}
+			r.close();
+			consume(remoteOut);
+			checkError();
+			return Collections.emptySet();
+		} catch (IOException ex) {
+			throw new HgRemoteConnectionException("Failed to initiate dialog with server", ex).setRemoteCommand("hello").setServerInfo(getLocation());
+		} catch (HgRemoteConnectionException ex) {
+			ex.setRemoteCommand("hello").setServerInfo(getLocation());
+			throw ex;
+		}
+	}
+	
+	private InputStream executeCommand(String cmd, List<Parameter> parameters) throws HgRemoteConnectionException {
+		try {
+			consume(remoteOut);
+			consume(remoteErr);
+			remoteIn.write(cmd.getBytes());
+			remoteIn.write('\n');
+			for (Parameter p : parameters) {
+				remoteIn.write(p.name().getBytes());
+				remoteIn.write(' ');
+				remoteIn.write(String.valueOf(p.size()).getBytes());
+				remoteIn.write('\n');
+				remoteIn.write(p.data());
+				remoteIn.write('\n');
+			}
+			checkError();
+			int responseLen = readResponseLength();
+			checkError();
+			return new FilterStream(remoteOut, responseLen);
+		} catch (IOException ex) {
+			throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand(cmd).setServerInfo(getLocation());
+		} catch (HgRemoteConnectionException ex) {
+			ex.setRemoteCommand(cmd).setServerInfo(getLocation());
+			throw ex;
+		}
+	}
+
+	private void consume(InputStream is) throws IOException {
+		while (is.available() > 0) {
+			is.read();
+		}
+	}
+
+	private void checkError() throws IOException, HgRemoteConnectionException {
+		if (remoteErr.available() > 0) {
+			StringBuilder sb = new StringBuilder();
+			int c;
+			while ((c = remoteErr.read()) != -1) {
+				sb.append((char)c);
+			}
+			throw new HgRemoteConnectionException(sb.toString());
+		}
+	}
+	
+	private int readResponseLength() throws IOException {
+		int c;
+		StringBuilder sb = new StringBuilder();
+		while ((c = remoteOut.read()) != -1) {
+			if (c == '\n') {
+				break;
+			}
+			sb.append((char) c);
+		}
+		if (c == -1) {
+			throw new EOFException();
+		}
+		try {
+			return Integer.parseInt(sb.toString());
+		} catch (NumberFormatException ex) {
+			throw new IOException(String.format("Expected response length instead of %s", sb));
+		}
+	}
+
+
+	private void forceSessionClose() {
+		if (session != null) {
+			closeQuietly(remoteErr);
+			closeQuietly(remoteOut);
+			remoteErr = remoteOut = null;
+			closeQuietly(remoteIn);
+			remoteIn = null;
+			session.close();
+			session = null;
+		}
+		sessionUse = 0;
+	}
+
+	private void closeQuietly(Closeable c) {
+		try {
+			if (c != null) {
+				c.close();
+			}
+		} catch (IOException ex) {
+			sessionCtx.getLog().dump(getClass(), Severity.Warn, ex, null);
+		}
+	}
+
+	private static String join(List<Nodeid> values, char sep) {
+		StringBuilder sb = new StringBuilder(values.size() * 41);
+		for (Nodeid n : values) {
+			sb.append(n.toString());
+			sb.append(sep);
+		}
+		if (!values.isEmpty()) {
+			// strip last space 
+			sb.setLength(sb.length() - 1);
+		}
+		return sb.toString();
+	}
+
+	private static final class Parameter {
+		private final String name;
+		private final byte[] data;
+
+		public Parameter(String paramName, String paramValue) {
+			assert paramName != null;
+			assert paramValue != null;
+			name = paramName;
+			data = paramValue.getBytes();
+		}
+
+		public String name() {
+			return name;
+		}
+		public int size() {
+			return data.length;
+		}
+		public byte[] data() {
+			return data;
+		}
+	}
+
+	private static final class FilterStream extends FilterInputStream {
+		private int length;
+
+		public FilterStream(InputStream is, int initialLength) {
+			super(is);
+			length = initialLength;
+		}
+		
+		@Override
+		public int available() throws IOException {
+			return Math.min(super.available(), length);
+		}
+		@Override
+		public int read() throws IOException {
+			if (length == 0) {
+				return -1;
+			}
+			int r = super.read();
+			if (r >= 0) {
+				length--;
+			}
+			return r;
+		}
+		@Override
+		public int read(byte[] b, int off, int len) throws IOException {
+			if (length == 0) {
+				return -1;
+			}
+			int r = super.read(b, off, Math.min(len, length));
+			if (r >= 0) {
+				assert r <= length;
+				length -= r;
+			}
+			return r;
+		}
+		@Override
+		public void close() throws IOException {
+			// INTENTIONALLY DOES NOT CLOSE THE STREAM
+		}
+	}
+}
--- a/src/org/tmatesoft/hg/repo/HgRemoteRepository.java	Thu Jul 25 21:31:23 2013 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRemoteRepository.java	Thu Jul 25 21:32:09 2013 +0200
@@ -242,9 +242,7 @@
 		StringBuilder sb = new StringBuilder(20 + ranges.size() * 82);
 		sb.append("pairs=");
 		for (Range r : ranges) {
-			sb.append(r.end.toString());
-			sb.append('-');
-			sb.append(r.start.toString());
+			r.append(sb);
 			sb.append('+');
 		}
 		if (sb.charAt(sb.length() - 1) == '+') {
@@ -587,6 +585,7 @@
 			}
 			checkResponseOk(c, actionName, "listkeys");
 			ArrayList<Pair<String, String>> rv = new ArrayList<Pair<String, String>>();
+			// output of listkeys is encoded with UTF-8
 			BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream(), EncodingHelper.getUTF8()));
 			String l;
 			while ((l = r.readLine()) != null) {
@@ -724,6 +723,16 @@
 			start = from;
 			end = to;
 		}
+		
+		/**
+		 * Append this range as pair of values 'end-start' to the supplied buffer and return the buffer.
+		 */
+		public StringBuilder append(StringBuilder sb) {
+			sb.append(end.toString());
+			sb.append('-');
+			sb.append(start.toString());
+			return sb;
+		}
 	}
 
 	public static final class RemoteBranch {