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.

AbstractClientProxyConnector.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  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.internal.transport.sshd.proxy;
  44. import java.net.InetSocketAddress;
  45. import java.util.Arrays;
  46. import java.util.concurrent.Callable;
  47. import java.util.concurrent.TimeUnit;
  48. import java.util.concurrent.atomic.AtomicReference;
  49. import org.apache.sshd.client.session.ClientSession;
  50. import org.eclipse.jgit.annotations.NonNull;
  51. import org.eclipse.jgit.internal.transport.sshd.JGitClientSession;
  52. /**
  53. * Basic common functionality for a {@link StatefulProxyConnector}.
  54. */
  55. public abstract class AbstractClientProxyConnector
  56. implements StatefulProxyConnector {
  57. private static final long DEFAULT_PROXY_TIMEOUT_MILLIS = TimeUnit.SECONDS
  58. .toMillis(30L);
  59. /** Guards {@link #done} and {@link #startSsh}. */
  60. private Object lock = new Object();
  61. private boolean done;
  62. private Callable<Void> startSsh;
  63. private AtomicReference<Runnable> unregister = new AtomicReference<>();
  64. private long remainingProxyProtocolTime = DEFAULT_PROXY_TIMEOUT_MILLIS;
  65. private long lastProxyOperationTime = 0L;
  66. /** The ultimate remote address to connect to. */
  67. protected final InetSocketAddress remoteAddress;
  68. /** The proxy address. */
  69. protected final InetSocketAddress proxyAddress;
  70. /** The user to authenticate at the proxy with. */
  71. protected String proxyUser;
  72. /** The password to use for authentication at the proxy. */
  73. protected char[] proxyPassword;
  74. /**
  75. * Creates a new {@link AbstractClientProxyConnector}.
  76. *
  77. * @param proxyAddress
  78. * of the proxy server we're connecting to
  79. * @param remoteAddress
  80. * of the target server to connect to
  81. * @param proxyUser
  82. * to authenticate at the proxy with; may be {@code null}
  83. * @param proxyPassword
  84. * to authenticate at the proxy with; may be {@code null}
  85. */
  86. public AbstractClientProxyConnector(@NonNull InetSocketAddress proxyAddress,
  87. @NonNull InetSocketAddress remoteAddress, String proxyUser,
  88. char[] proxyPassword) {
  89. this.proxyAddress = proxyAddress;
  90. this.remoteAddress = remoteAddress;
  91. this.proxyUser = proxyUser;
  92. this.proxyPassword = proxyPassword == null ? new char[0]
  93. : proxyPassword;
  94. }
  95. /**
  96. * Initializes this instance. Installs itself as proxy handler on the
  97. * session.
  98. *
  99. * @param session
  100. * to initialize for
  101. */
  102. protected void init(ClientSession session) {
  103. remainingProxyProtocolTime = session.getLongProperty(
  104. StatefulProxyConnector.TIMEOUT_PROPERTY,
  105. DEFAULT_PROXY_TIMEOUT_MILLIS);
  106. if (remainingProxyProtocolTime <= 0L) {
  107. remainingProxyProtocolTime = DEFAULT_PROXY_TIMEOUT_MILLIS;
  108. }
  109. if (session instanceof JGitClientSession) {
  110. JGitClientSession s = (JGitClientSession) session;
  111. unregister.set(() -> s.setProxyHandler(null));
  112. s.setProxyHandler(this);
  113. } else {
  114. // Internal error, no translation
  115. throw new IllegalStateException(
  116. "Not a JGit session: " + session.getClass().getName()); //$NON-NLS-1$
  117. }
  118. }
  119. /**
  120. * Obtains the timeout for the whole rest of the proxy connection protocol.
  121. *
  122. * @return the timeout in milliseconds, always > 0L
  123. */
  124. protected long getTimeout() {
  125. long last = lastProxyOperationTime;
  126. long now = System.nanoTime();
  127. lastProxyOperationTime = now;
  128. long remaining = remainingProxyProtocolTime;
  129. if (last != 0L) {
  130. long elapsed = now - last;
  131. remaining -= elapsed;
  132. if (remaining < 0L) {
  133. remaining = 10L; // Give it grace period.
  134. }
  135. }
  136. remainingProxyProtocolTime = remaining;
  137. return remaining;
  138. }
  139. /**
  140. * Adjusts the timeout calculation to not account of elapsed time since the
  141. * last time the timeout was gotten. Can be used for instance to ignore time
  142. * spent in user dialogs be counted against the overall proxy connection
  143. * protocol timeout.
  144. */
  145. protected void adjustTimeout() {
  146. lastProxyOperationTime = System.nanoTime();
  147. }
  148. /**
  149. * Sets the "done" flag.
  150. *
  151. * @param success
  152. * whether the connector terminated successfully.
  153. * @throws Exception
  154. * if starting ssh fails
  155. */
  156. protected void setDone(boolean success) throws Exception {
  157. Callable<Void> starter;
  158. Runnable unset = unregister.getAndSet(null);
  159. if (unset != null) {
  160. unset.run();
  161. }
  162. synchronized (lock) {
  163. done = true;
  164. starter = startSsh;
  165. startSsh = null;
  166. }
  167. if (success && starter != null) {
  168. starter.call();
  169. }
  170. }
  171. @Override
  172. public void runWhenDone(Callable<Void> starter) throws Exception {
  173. synchronized (lock) {
  174. if (!done) {
  175. this.startSsh = starter;
  176. return;
  177. }
  178. }
  179. starter.call();
  180. }
  181. /**
  182. * Clears the proxy password.
  183. */
  184. protected void clearPassword() {
  185. Arrays.fill(proxyPassword, '\000');
  186. proxyPassword = new char[0];
  187. }
  188. }