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.5KB

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