Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TransportGitAnon.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.Collection;
  24. import java.util.Collections;
  25. import java.util.EnumSet;
  26. import java.util.Set;
  27. import org.eclipse.jgit.errors.NotSupportedException;
  28. import org.eclipse.jgit.errors.TransportException;
  29. import org.eclipse.jgit.internal.JGitText;
  30. import org.eclipse.jgit.lib.Repository;
  31. /**
  32. * Transport through a git-daemon waiting for anonymous TCP connections.
  33. * <p>
  34. * This transport supports the <code>git://</code> protocol, usually run on
  35. * the IANA registered port 9418. It is a popular means for distributing open
  36. * source projects, as there are no authentication or authorization overheads.
  37. */
  38. class TransportGitAnon extends TcpTransport implements PackTransport {
  39. static final int GIT_PORT = Daemon.DEFAULT_PORT;
  40. static final TransportProtocol PROTO_GIT = new TransportProtocol() {
  41. @Override
  42. public String getName() {
  43. return JGitText.get().transportProtoGitAnon;
  44. }
  45. @Override
  46. public Set<String> getSchemes() {
  47. return Collections.singleton("git"); //$NON-NLS-1$
  48. }
  49. @Override
  50. public Set<URIishField> getRequiredFields() {
  51. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  52. URIishField.PATH));
  53. }
  54. @Override
  55. public Set<URIishField> getOptionalFields() {
  56. return Collections.unmodifiableSet(EnumSet.of(URIishField.PORT));
  57. }
  58. @Override
  59. public int getDefaultPort() {
  60. return GIT_PORT;
  61. }
  62. @Override
  63. public Transport open(URIish uri, Repository local, String remoteName)
  64. throws NotSupportedException {
  65. return new TransportGitAnon(local, uri);
  66. }
  67. @Override
  68. public Transport open(URIish uri) throws NotSupportedException, TransportException {
  69. return new TransportGitAnon(uri);
  70. }
  71. };
  72. TransportGitAnon(Repository local, URIish uri) {
  73. super(local, uri);
  74. }
  75. TransportGitAnon(URIish uri) {
  76. super(uri);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public FetchConnection openFetch() throws TransportException {
  81. return new TcpFetchConnection();
  82. }
  83. @Override
  84. public FetchConnection openFetch(Collection<RefSpec> refSpecs,
  85. String... additionalPatterns)
  86. throws NotSupportedException, TransportException {
  87. return new TcpFetchConnection(refSpecs, additionalPatterns);
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public PushConnection openPush() throws TransportException {
  92. return new TcpPushConnection();
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public void close() {
  97. // Resources must be established per-connection.
  98. }
  99. Socket openConnection() throws TransportException {
  100. final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
  101. final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
  102. @SuppressWarnings("resource") // Closed by the caller
  103. final Socket s = new Socket();
  104. try {
  105. final InetAddress host = InetAddress.getByName(uri.getHost());
  106. s.bind(null);
  107. s.connect(new InetSocketAddress(host, port), tms);
  108. } catch (IOException c) {
  109. try {
  110. s.close();
  111. } catch (IOException closeErr) {
  112. // ignore a failure during close, we're already failing
  113. }
  114. if (c instanceof UnknownHostException)
  115. throw new TransportException(uri, JGitText.get().unknownHost);
  116. if (c instanceof ConnectException)
  117. throw new TransportException(uri, c.getMessage());
  118. throw new TransportException(uri, c.getMessage(), c);
  119. }
  120. return s;
  121. }
  122. void service(String name, PacketLineOut pckOut,
  123. TransferConfig.ProtocolVersion gitProtocol)
  124. throws IOException {
  125. final StringBuilder cmd = new StringBuilder();
  126. cmd.append(name);
  127. cmd.append(' ');
  128. cmd.append(uri.getPath());
  129. cmd.append('\0');
  130. cmd.append("host="); //$NON-NLS-1$
  131. cmd.append(uri.getHost());
  132. if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
  133. cmd.append(":"); //$NON-NLS-1$
  134. cmd.append(uri.getPort());
  135. }
  136. cmd.append('\0');
  137. if (TransferConfig.ProtocolVersion.V2.equals(gitProtocol)) {
  138. cmd.append('\0');
  139. cmd.append(GitProtocolConstants.VERSION_2_REQUEST);
  140. cmd.append('\0');
  141. }
  142. pckOut.writeString(cmd.toString());
  143. pckOut.flush();
  144. }
  145. class TcpFetchConnection extends BasePackFetchConnection {
  146. private Socket sock;
  147. TcpFetchConnection() throws TransportException {
  148. this(Collections.emptyList());
  149. }
  150. TcpFetchConnection(Collection<RefSpec> refSpecs,
  151. String... additionalPatterns) throws TransportException {
  152. super(TransportGitAnon.this);
  153. sock = openConnection();
  154. try {
  155. InputStream sIn = sock.getInputStream();
  156. OutputStream sOut = sock.getOutputStream();
  157. sIn = new BufferedInputStream(sIn);
  158. sOut = new BufferedOutputStream(sOut);
  159. init(sIn, sOut);
  160. TransferConfig.ProtocolVersion gitProtocol = protocol;
  161. if (gitProtocol == null) {
  162. gitProtocol = TransferConfig.ProtocolVersion.V2;
  163. }
  164. service("git-upload-pack", pckOut, gitProtocol); //$NON-NLS-1$
  165. } catch (IOException err) {
  166. close();
  167. throw new TransportException(uri,
  168. JGitText.get().remoteHungUpUnexpectedly, err);
  169. }
  170. if (!readAdvertisedRefs()) {
  171. lsRefs(refSpecs, additionalPatterns);
  172. }
  173. }
  174. @Override
  175. public void close() {
  176. super.close();
  177. if (sock != null) {
  178. try {
  179. sock.close();
  180. } catch (IOException err) {
  181. // Ignore errors during close.
  182. } finally {
  183. sock = null;
  184. }
  185. }
  186. }
  187. }
  188. class TcpPushConnection extends BasePackPushConnection {
  189. private Socket sock;
  190. TcpPushConnection() throws TransportException {
  191. super(TransportGitAnon.this);
  192. sock = openConnection();
  193. try {
  194. InputStream sIn = sock.getInputStream();
  195. OutputStream sOut = sock.getOutputStream();
  196. sIn = new BufferedInputStream(sIn);
  197. sOut = new BufferedOutputStream(sOut);
  198. init(sIn, sOut);
  199. service("git-receive-pack", pckOut, null); //$NON-NLS-1$
  200. } catch (IOException err) {
  201. close();
  202. throw new TransportException(uri,
  203. JGitText.get().remoteHungUpUnexpectedly, err);
  204. }
  205. readAdvertisedRefs();
  206. }
  207. @Override
  208. public void close() {
  209. super.close();
  210. if (sock != null) {
  211. try {
  212. sock.close();
  213. } catch (IOException err) {
  214. // Ignore errors during close.
  215. } finally {
  216. sock = null;
  217. }
  218. }
  219. }
  220. }
  221. }