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.

ProxyAuthenticator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.common;
  23. import java.net.Authenticator;
  24. import java.net.PasswordAuthentication;
  25. import java.net.URI;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.concurrent.Semaphore;
  31. /**
  32. * Handles proxy authentication for a {@link BaseSocketAwareParser} to a proxy.
  33. */
  34. public final class ProxyAuthenticator extends Authenticator {
  35. /**
  36. * A version number for this class. It should be changed whenever the class
  37. * structure is changed (or anything else that would prevent serialized
  38. * objects being unserialized with the new class).
  39. */
  40. private static final long serialVersionUID = 1;
  41. /** Singleton instance of IRCAuthenticator. */
  42. private static ProxyAuthenticator me;
  43. /** List of authentication replies. */
  44. private final Map<String, PasswordAuthentication> replies = new HashMap<>();
  45. /** List of servers for each host. */
  46. private final Map<String, List<URI>> owners = new HashMap<>();
  47. /** Semaphore for connection limiting. */
  48. private final Semaphore mySemaphore = new Semaphore(1, true);
  49. /**
  50. * Create a new IRCAuthenticator.
  51. *
  52. * This creates an IRCAuthenticator and registers it as the default
  53. * Authenticator.
  54. */
  55. private ProxyAuthenticator() {
  56. Authenticator.setDefault(this);
  57. }
  58. /**
  59. * Get the instance of IRCAuthenticator.
  60. *
  61. * @return The IRCAuthenticator instance.
  62. */
  63. public static synchronized ProxyAuthenticator getProxyAuthenticator() {
  64. if (me == null) {
  65. me = new ProxyAuthenticator();
  66. } else {
  67. // Make ourself the default authenticator again just incase.
  68. Authenticator.setDefault(me);
  69. }
  70. return me;
  71. }
  72. /**
  73. * Add a server to authenticate for.
  74. *
  75. * @param server ServerInfo object with server details.
  76. * @param proxy The URI of the proxy that is being used.
  77. */
  78. public void addAuthentication(final URI server, final URI proxy) {
  79. final String userInfo = proxy.getUserInfo();
  80. final int offset;
  81. if (userInfo == null || (offset = userInfo.indexOf(':')) == -1) {
  82. return;
  83. }
  84. final String username = userInfo.substring(0, offset);
  85. final String password = userInfo.substring(offset + 1);
  86. final String host = proxy.getHost();
  87. final int port = proxy.getPort();
  88. final PasswordAuthentication pass = new PasswordAuthentication(username, password.toCharArray());
  89. final String fullhost = host.toLowerCase() + ':' + port;
  90. // Delete old username/password if one exists and then add the new one
  91. replies.remove(fullhost);
  92. replies.put(fullhost, pass);
  93. // Store which servers are associated with which proxy
  94. final List<URI> servers = owners.containsKey(fullhost) ? owners.get(fullhost) : new ArrayList<>();
  95. owners.remove(fullhost);
  96. servers.add(server);
  97. owners.put(fullhost, servers);
  98. }
  99. /**
  100. * Get a copy of the semaphore.
  101. *
  102. * @return the IRCAuthenticator semaphore.
  103. */
  104. public Semaphore getSemaphore() {
  105. return mySemaphore;
  106. }
  107. /**
  108. * Remove a server to authenticate for.
  109. *
  110. * @param server ServerInfo object with server details.
  111. * @param proxy The proxy that was in use for the given server.
  112. */
  113. public void removeAuthentication(final URI server, final URI proxy) {
  114. final String host = proxy.getHost();
  115. final int port = proxy.getPort();
  116. final String fullhost = host.toLowerCase() + ':' + port;
  117. // See if any other servers are associated with this proxy.
  118. final List<URI> servers = owners.containsKey(fullhost) ? owners.get(fullhost) : new ArrayList<>();
  119. servers.remove(server);
  120. if (servers.isEmpty()) {
  121. // No more servers need this authentication info, remove.
  122. owners.remove(fullhost);
  123. replies.remove(fullhost);
  124. }
  125. }
  126. @Override
  127. protected PasswordAuthentication getPasswordAuthentication() {
  128. /*
  129. * getRequestingHost: 85.234.138.2
  130. * getRequestingPort: 1080
  131. * getRequestingPrompt: SOCKS authentication
  132. * getRequestingProtocol: SOCKS5
  133. * getRequestingScheme: null
  134. * getRequestingSite: /85.234.138.2
  135. * getRequestingURL: null
  136. * getRequestorType: SERVER
  137. */
  138. final String fullhost = getRequestingHost().toLowerCase() + ':' + getRequestingPort();
  139. return replies.get(fullhost);
  140. }
  141. }