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.

IRCAuthenticator.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2006-2012 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.irc;
  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 the parser.
  33. *
  34. * @see IRCParser
  35. */
  36. public final class IRCAuthenticator extends Authenticator {
  37. /**
  38. * A version number for this class. It should be changed whenever the class
  39. * structure is changed (or anything else that would prevent serialized
  40. * objects being unserialized with the new class).
  41. */
  42. private static final long serialVersionUID = 1;
  43. /** Singleton instance of IRCAuthenticator. */
  44. private static IRCAuthenticator me;
  45. /** List of authentication replies. */
  46. private final Map<String, PasswordAuthentication> replies = new HashMap<String, PasswordAuthentication>();
  47. /** List of servers for each host. */
  48. private final Map<String, List<URI>> owners = new HashMap<String, List<URI>>();
  49. /** Semaphore for connection limiting. */
  50. private final Semaphore mySemaphore = new Semaphore(1, true);
  51. /**
  52. * Create a new IRCAuthenticator.
  53. *
  54. * This creates an IRCAuthenticator and registers it as the default
  55. * Authenticator.
  56. */
  57. private IRCAuthenticator() {
  58. super();
  59. Authenticator.setDefault(this);
  60. }
  61. /**
  62. * Get the instance of IRCAuthenticator.
  63. *
  64. * @return The IRCAuthenticator instance.
  65. */
  66. public static synchronized IRCAuthenticator getIRCAuthenticator() {
  67. if (me == null) {
  68. me = new IRCAuthenticator();
  69. } else {
  70. // Make ourself the default authenticator again just incase.
  71. Authenticator.setDefault(me);
  72. }
  73. return me;
  74. }
  75. /**
  76. * Add a server to authenticate for.
  77. *
  78. * @param server ServerInfo object with server details.
  79. * @param proxy The URI of the proxy that is being used.
  80. */
  81. public void addAuthentication(final URI server, final URI proxy) {
  82. final String userInfo = proxy.getUserInfo();
  83. int offset;
  84. if (userInfo == null || (offset = userInfo.indexOf(':')) == -1) {
  85. return;
  86. }
  87. final String username = userInfo.substring(0, offset);
  88. final String password = userInfo.substring(offset + 1);
  89. final String host = proxy.getHost();
  90. final int port = proxy.getPort();
  91. final PasswordAuthentication pass = new PasswordAuthentication(username, password.toCharArray());
  92. final String fullhost = host.toLowerCase() + ":" + port;
  93. // Delete old username/password if one exists and then add the new one
  94. replies.remove(fullhost);
  95. replies.put(fullhost, pass);
  96. // Store which servers are associated with which proxy
  97. final List<URI> servers = owners.containsKey(fullhost) ? owners.get(fullhost) : new ArrayList<URI>();
  98. owners.remove(fullhost);
  99. servers.add(server);
  100. owners.put(fullhost, servers);
  101. }
  102. /**
  103. * Get a copy of the semaphore.
  104. *
  105. * @return the IRCAuthenticator semaphore.
  106. */
  107. public Semaphore getSemaphore() {
  108. return mySemaphore;
  109. }
  110. /**
  111. * Remove a server to authenticate for.
  112. *
  113. * @param server ServerInfo object with server details.
  114. * @param proxy The proxy that was in use for the given server.
  115. */
  116. public void removeAuthentication(final URI server, final URI proxy) {
  117. final String host = proxy.getHost();
  118. final int port = proxy.getPort();
  119. final String fullhost = host.toLowerCase() + ":" + port;
  120. // See if any other servers are associated with this proxy.
  121. final List<URI> servers = owners.containsKey(fullhost) ? owners.get(fullhost) : new ArrayList<URI>();
  122. servers.remove(server);
  123. if (servers.isEmpty()) {
  124. // No more servers need this authentication info, remove.
  125. owners.remove(fullhost);
  126. replies.remove(fullhost);
  127. }
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. protected PasswordAuthentication getPasswordAuthentication() {
  132. /*
  133. * getRequestingHost: 85.234.138.2
  134. * getRequestingPort: 1080
  135. * getRequestingPrompt: SOCKS authentication
  136. * getRequestingProtocol: SOCKS5
  137. * getRequestingScheme: null
  138. * getRequestingSite: /85.234.138.2
  139. * getRequestingURL: null
  140. * getRequestorType: SERVER
  141. */
  142. final String fullhost = getRequestingHost().toLowerCase() + ":" + getRequestingPort();
  143. return replies.get(fullhost);
  144. }
  145. }