You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransportGitAnon.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.transport;
  13. import java.io.BufferedInputStream;
  14. import java.io.BufferedOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import java.net.ConnectException;
  19. import java.net.InetAddress;
  20. import java.net.InetSocketAddress;
  21. import java.net.Socket;
  22. import java.net.UnknownHostException;
  23. import java.util.Collections;
  24. import java.util.EnumSet;
  25. import java.util.Set;
  26. import org.eclipse.jgit.errors.NotSupportedException;
  27. import org.eclipse.jgit.errors.TransportException;
  28. import org.eclipse.jgit.internal.JGitText;
  29. import org.eclipse.jgit.lib.Repository;
  30. /**
  31. * Transport through a git-daemon waiting for anonymous TCP connections.
  32. * <p>
  33. * This transport supports the <code>git://</code> protocol, usually run on
  34. * the IANA registered port 9418. It is a popular means for distributing open
  35. * source projects, as there are no authentication or authorization overheads.
  36. */
  37. class TransportGitAnon extends TcpTransport implements PackTransport {
  38. static final int GIT_PORT = Daemon.DEFAULT_PORT;
  39. static final TransportProtocol PROTO_GIT = new TransportProtocol() {
  40. @Override
  41. public String getName() {
  42. return JGitText.get().transportProtoGitAnon;
  43. }
  44. @Override
  45. public Set<String> getSchemes() {
  46. return Collections.singleton("git"); //$NON-NLS-1$
  47. }
  48. @Override
  49. public Set<URIishField> getRequiredFields() {
  50. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  51. URIishField.PATH));
  52. }
  53. @Override
  54. public Set<URIishField> getOptionalFields() {
  55. return Collections.unmodifiableSet(EnumSet.of(URIishField.PORT));
  56. }
  57. @Override
  58. public int getDefaultPort() {
  59. return GIT_PORT;
  60. }
  61. @Override
  62. public Transport open(URIish uri, Repository local, String remoteName)
  63. throws NotSupportedException {
  64. return new TransportGitAnon(local, uri);
  65. }
  66. @Override
  67. public Transport open(URIish uri) throws NotSupportedException, TransportException {
  68. return new TransportGitAnon(uri);
  69. }
  70. };
  71. TransportGitAnon(Repository local, URIish uri) {
  72. super(local, uri);
  73. }
  74. TransportGitAnon(URIish uri) {
  75. super(uri);
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public FetchConnection openFetch() throws TransportException {
  80. return new TcpFetchConnection();
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public PushConnection openPush() throws TransportException {
  85. return new TcpPushConnection();
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public void close() {
  90. // Resources must be established per-connection.
  91. }
  92. Socket openConnection() throws TransportException {
  93. final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
  94. final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
  95. @SuppressWarnings("resource") // Closed by the caller
  96. final Socket s = new Socket();
  97. try {
  98. final InetAddress host = InetAddress.getByName(uri.getHost());
  99. s.bind(null);
  100. s.connect(new InetSocketAddress(host, port), tms);
  101. } catch (IOException c) {
  102. try {
  103. s.close();
  104. } catch (IOException closeErr) {
  105. // ignore a failure during close, we're already failing
  106. }
  107. if (c instanceof UnknownHostException)
  108. throw new TransportException(uri, JGitText.get().unknownHost);
  109. if (c instanceof ConnectException)
  110. throw new TransportException(uri, c.getMessage());
  111. throw new TransportException(uri, c.getMessage(), c);
  112. }
  113. return s;
  114. }
  115. void service(String name, PacketLineOut pckOut)
  116. throws IOException {
  117. final StringBuilder cmd = new StringBuilder();
  118. cmd.append(name);
  119. cmd.append(' ');
  120. cmd.append(uri.getPath());
  121. cmd.append('\0');
  122. cmd.append("host="); //$NON-NLS-1$
  123. cmd.append(uri.getHost());
  124. if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
  125. cmd.append(":"); //$NON-NLS-1$
  126. cmd.append(uri.getPort());
  127. }
  128. cmd.append('\0');
  129. pckOut.writeString(cmd.toString());
  130. pckOut.flush();
  131. }
  132. class TcpFetchConnection extends BasePackFetchConnection {
  133. private Socket sock;
  134. TcpFetchConnection() throws TransportException {
  135. super(TransportGitAnon.this);
  136. sock = openConnection();
  137. try {
  138. InputStream sIn = sock.getInputStream();
  139. OutputStream sOut = sock.getOutputStream();
  140. sIn = new BufferedInputStream(sIn);
  141. sOut = new BufferedOutputStream(sOut);
  142. init(sIn, sOut);
  143. service("git-upload-pack", pckOut); //$NON-NLS-1$
  144. } catch (IOException err) {
  145. close();
  146. throw new TransportException(uri,
  147. JGitText.get().remoteHungUpUnexpectedly, err);
  148. }
  149. readAdvertisedRefs();
  150. }
  151. @Override
  152. public void close() {
  153. super.close();
  154. if (sock != null) {
  155. try {
  156. sock.close();
  157. } catch (IOException err) {
  158. // Ignore errors during close.
  159. } finally {
  160. sock = null;
  161. }
  162. }
  163. }
  164. }
  165. class TcpPushConnection extends BasePackPushConnection {
  166. private Socket sock;
  167. TcpPushConnection() throws TransportException {
  168. super(TransportGitAnon.this);
  169. sock = openConnection();
  170. try {
  171. InputStream sIn = sock.getInputStream();
  172. OutputStream sOut = sock.getOutputStream();
  173. sIn = new BufferedInputStream(sIn);
  174. sOut = new BufferedOutputStream(sOut);
  175. init(sIn, sOut);
  176. service("git-receive-pack", pckOut); //$NON-NLS-1$
  177. } catch (IOException err) {
  178. close();
  179. throw new TransportException(uri,
  180. JGitText.get().remoteHungUpUnexpectedly, err);
  181. }
  182. readAdvertisedRefs();
  183. }
  184. @Override
  185. public void close() {
  186. super.close();
  187. if (sock != null) {
  188. try {
  189. sock.close();
  190. } catch (IOException err) {
  191. // Ignore errors during close.
  192. } finally {
  193. sock = null;
  194. }
  195. }
  196. }
  197. }
  198. }