Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Daemon.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.InterruptedIOException;
  47. import java.io.OutputStream;
  48. import java.net.InetAddress;
  49. import java.net.InetSocketAddress;
  50. import java.net.ServerSocket;
  51. import java.net.Socket;
  52. import java.net.SocketAddress;
  53. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.PersonIdent;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.storage.pack.PackConfig;
  58. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  59. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  60. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  61. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  62. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  63. /** Basic daemon for the anonymous <code>git://</code> transport protocol. */
  64. public class Daemon {
  65. /** 9418: IANA assigned port number for Git. */
  66. public static final int DEFAULT_PORT = 9418;
  67. private static final int BACKLOG = 5;
  68. private InetSocketAddress myAddress;
  69. private final DaemonService[] services;
  70. private final ThreadGroup processors;
  71. private boolean run;
  72. private Thread acceptThread;
  73. private int timeout;
  74. private PackConfig packConfig;
  75. private volatile RepositoryResolver<DaemonClient> repositoryResolver;
  76. private volatile UploadPackFactory<DaemonClient> uploadPackFactory;
  77. private volatile ReceivePackFactory<DaemonClient> receivePackFactory;
  78. /** Configure a daemon to listen on any available network port. */
  79. public Daemon() {
  80. this(null);
  81. }
  82. /**
  83. * Configure a new daemon for the specified network address.
  84. *
  85. * @param addr
  86. * address to listen for connections on. If null, any available
  87. * port will be chosen on all network interfaces.
  88. */
  89. public Daemon(final InetSocketAddress addr) {
  90. myAddress = addr;
  91. processors = new ThreadGroup("Git-Daemon");
  92. repositoryResolver = (RepositoryResolver<DaemonClient>) RepositoryResolver.NONE;
  93. uploadPackFactory = new UploadPackFactory<DaemonClient>() {
  94. public UploadPack create(DaemonClient req, Repository db)
  95. throws ServiceNotEnabledException,
  96. ServiceNotAuthorizedException {
  97. UploadPack up = new UploadPack(db);
  98. up.setTimeout(getTimeout());
  99. up.setPackConfig(getPackConfig());
  100. return up;
  101. }
  102. };
  103. receivePackFactory = new ReceivePackFactory<DaemonClient>() {
  104. public ReceivePack create(DaemonClient req, Repository db)
  105. throws ServiceNotEnabledException,
  106. ServiceNotAuthorizedException {
  107. ReceivePack rp = new ReceivePack(db);
  108. InetAddress peer = req.getRemoteAddress();
  109. String host = peer.getCanonicalHostName();
  110. if (host == null)
  111. host = peer.getHostAddress();
  112. String name = "anonymous";
  113. String email = name + "@" + host;
  114. rp.setRefLogIdent(new PersonIdent(name, email));
  115. rp.setTimeout(getTimeout());
  116. return rp;
  117. }
  118. };
  119. services = new DaemonService[] {
  120. new DaemonService("upload-pack", "uploadpack") {
  121. {
  122. setEnabled(true);
  123. }
  124. @Override
  125. protected void execute(final DaemonClient dc,
  126. final Repository db) throws IOException,
  127. ServiceNotEnabledException,
  128. ServiceNotAuthorizedException {
  129. UploadPack up = uploadPackFactory.create(dc, db);
  130. InputStream in = dc.getInputStream();
  131. OutputStream out = dc.getOutputStream();
  132. up.upload(in, out, null);
  133. }
  134. }, new DaemonService("receive-pack", "receivepack") {
  135. {
  136. setEnabled(false);
  137. }
  138. @Override
  139. protected void execute(final DaemonClient dc,
  140. final Repository db) throws IOException,
  141. ServiceNotEnabledException,
  142. ServiceNotAuthorizedException {
  143. ReceivePack rp = receivePackFactory.create(dc, db);
  144. InputStream in = dc.getInputStream();
  145. OutputStream out = dc.getOutputStream();
  146. rp.receive(in, out, null);
  147. }
  148. } };
  149. }
  150. /** @return the address connections are received on. */
  151. public synchronized InetSocketAddress getAddress() {
  152. return myAddress;
  153. }
  154. /**
  155. * Lookup a supported service so it can be reconfigured.
  156. *
  157. * @param name
  158. * name of the service; e.g. "receive-pack"/"git-receive-pack" or
  159. * "upload-pack"/"git-upload-pack".
  160. * @return the service; null if this daemon implementation doesn't support
  161. * the requested service type.
  162. */
  163. public synchronized DaemonService getService(String name) {
  164. if (!name.startsWith("git-"))
  165. name = "git-" + name;
  166. for (final DaemonService s : services) {
  167. if (s.getCommandName().equals(name))
  168. return s;
  169. }
  170. return null;
  171. }
  172. /** @return timeout (in seconds) before aborting an IO operation. */
  173. public int getTimeout() {
  174. return timeout;
  175. }
  176. /**
  177. * Set the timeout before willing to abort an IO call.
  178. *
  179. * @param seconds
  180. * number of seconds to wait (with no data transfer occurring)
  181. * before aborting an IO read or write operation with the
  182. * connected client.
  183. */
  184. public void setTimeout(final int seconds) {
  185. timeout = seconds;
  186. }
  187. /** @return configuration controlling packing, may be null. */
  188. public PackConfig getPackConfig() {
  189. return packConfig;
  190. }
  191. /**
  192. * Set the configuration used by the pack generator.
  193. *
  194. * @param pc
  195. * configuration controlling packing parameters. If null the
  196. * source repository's settings will be used.
  197. */
  198. public void setPackConfig(PackConfig pc) {
  199. this.packConfig = pc;
  200. }
  201. /**
  202. * Set the resolver used to locate a repository by name.
  203. *
  204. * @param resolver
  205. * the resolver instance.
  206. */
  207. public void setRepositoryResolver(RepositoryResolver<DaemonClient> resolver) {
  208. repositoryResolver = resolver;
  209. }
  210. /**
  211. * Set the factory to construct and configure per-request UploadPack.
  212. *
  213. * @param factory
  214. * the factory. If null upload-pack is disabled.
  215. */
  216. @SuppressWarnings("unchecked")
  217. public void setUploadPackFactory(UploadPackFactory<DaemonClient> factory) {
  218. if (factory != null)
  219. uploadPackFactory = factory;
  220. else
  221. uploadPackFactory = (UploadPackFactory<DaemonClient>) UploadPackFactory.DISABLED;
  222. }
  223. /**
  224. * Set the factory to construct and configure per-request ReceivePack.
  225. *
  226. * @param factory
  227. * the factory. If null receive-pack is disabled.
  228. */
  229. @SuppressWarnings("unchecked")
  230. public void setReceivePackFactory(ReceivePackFactory<DaemonClient> factory) {
  231. if (factory != null)
  232. receivePackFactory = factory;
  233. else
  234. receivePackFactory = (ReceivePackFactory<DaemonClient>) ReceivePackFactory.DISABLED;
  235. }
  236. /**
  237. * Start this daemon on a background thread.
  238. *
  239. * @throws IOException
  240. * the server socket could not be opened.
  241. * @throws IllegalStateException
  242. * the daemon is already running.
  243. */
  244. public synchronized void start() throws IOException {
  245. if (acceptThread != null)
  246. throw new IllegalStateException(JGitText.get().daemonAlreadyRunning);
  247. final ServerSocket listenSock = new ServerSocket(
  248. myAddress != null ? myAddress.getPort() : 0, BACKLOG,
  249. myAddress != null ? myAddress.getAddress() : null);
  250. myAddress = (InetSocketAddress) listenSock.getLocalSocketAddress();
  251. run = true;
  252. acceptThread = new Thread(processors, "Git-Daemon-Accept") {
  253. public void run() {
  254. while (isRunning()) {
  255. try {
  256. startClient(listenSock.accept());
  257. } catch (InterruptedIOException e) {
  258. // Test again to see if we should keep accepting.
  259. } catch (IOException e) {
  260. break;
  261. }
  262. }
  263. try {
  264. listenSock.close();
  265. } catch (IOException err) {
  266. //
  267. } finally {
  268. synchronized (Daemon.this) {
  269. acceptThread = null;
  270. }
  271. }
  272. }
  273. };
  274. acceptThread.start();
  275. }
  276. /** @return true if this daemon is receiving connections. */
  277. public synchronized boolean isRunning() {
  278. return run;
  279. }
  280. /** Stop this daemon. */
  281. public synchronized void stop() {
  282. if (acceptThread != null) {
  283. run = false;
  284. acceptThread.interrupt();
  285. }
  286. }
  287. private void startClient(final Socket s) {
  288. final DaemonClient dc = new DaemonClient(this);
  289. final SocketAddress peer = s.getRemoteSocketAddress();
  290. if (peer instanceof InetSocketAddress)
  291. dc.setRemoteAddress(((InetSocketAddress) peer).getAddress());
  292. new Thread(processors, "Git-Daemon-Client " + peer.toString()) {
  293. public void run() {
  294. try {
  295. dc.execute(s);
  296. } catch (ServiceNotEnabledException e) {
  297. // Ignored. Client cannot use this repository.
  298. } catch (ServiceNotAuthorizedException e) {
  299. // Ignored. Client cannot use this repository.
  300. } catch (IOException e) {
  301. // Ignore unexpected IO exceptions from clients
  302. } finally {
  303. try {
  304. s.getInputStream().close();
  305. } catch (IOException e) {
  306. // Ignore close exceptions
  307. }
  308. try {
  309. s.getOutputStream().close();
  310. } catch (IOException e) {
  311. // Ignore close exceptions
  312. }
  313. }
  314. }
  315. }.start();
  316. }
  317. synchronized DaemonService matchService(final String cmd) {
  318. for (final DaemonService d : services) {
  319. if (d.handles(cmd))
  320. return d;
  321. }
  322. return null;
  323. }
  324. Repository openRepository(DaemonClient client, String name)
  325. throws ServiceMayNotContinueException {
  326. // Assume any attempt to use \ was by a Windows client
  327. // and correct to the more typical / used in Git URIs.
  328. //
  329. name = name.replace('\\', '/');
  330. // git://thishost/path should always be name="/path" here
  331. //
  332. if (!name.startsWith("/"))
  333. return null;
  334. try {
  335. return repositoryResolver.open(client, name.substring(1));
  336. } catch (RepositoryNotFoundException e) {
  337. // null signals it "wasn't found", which is all that is suitable
  338. // for the remote client to know.
  339. return null;
  340. } catch (ServiceNotAuthorizedException e) {
  341. // null signals it "wasn't found", which is all that is suitable
  342. // for the remote client to know.
  343. return null;
  344. } catch (ServiceNotEnabledException e) {
  345. // null signals it "wasn't found", which is all that is suitable
  346. // for the remote client to know.
  347. return null;
  348. }
  349. }
  350. }