Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

IRCAuthenticator.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Map;
  26. import java.util.HashMap;
  27. /**
  28. * Handles proxy authentication for the parser.
  29. *
  30. * @author Shane Mc Cormack
  31. * @see IRCParser
  32. */
  33. public class IRCAuthenticator extends Authenticator {
  34. /**
  35. * A version number for this class. It should be changed whenever the class
  36. * structure is changed (or anything else that would prevent serialized
  37. * objects being unserialized with the new class).
  38. */
  39. private static final long serialVersionUID = 1;
  40. /** Singleton instance of IRCAuthenticator. */
  41. private static IRCAuthenticator me = null;
  42. /** List of authentication replies. */
  43. private final Map<String,PasswordAuthentication> replies = new HashMap<String,PasswordAuthentication>();
  44. /**
  45. * Create a new IRCAuthenticator.
  46. *
  47. * This creates an IRCAuthenticator and registers it as the default
  48. * Authenticator.
  49. */
  50. private IRCAuthenticator() {
  51. /* try {
  52. final Field field = Authenticator.class.getDeclaredField("theAuthenticator");
  53. field.setAccessible(true);
  54. final Object authenticator = field.get(null);
  55. if (authenticator instanceof Authenticator) {
  56. oldAuthenticator = (Authenticator)authenticator;
  57. }
  58. } catch (NoSuchFieldException nsfe) {
  59. } catch (IllegalAccessException iae) {
  60. }*/
  61. Authenticator.setDefault(this);
  62. }
  63. /**
  64. * Get the instance of IRCAuthenticator.
  65. *
  66. * @return The IRCAuthenticator instance.
  67. */
  68. public static synchronized IRCAuthenticator getIRCAuthenticator() {
  69. if (me == null) {
  70. me = new IRCAuthenticator();
  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. addAuthentication(server.getProxyHost(), server.getProxyPort(), server.getProxyUser(), server.getProxyPass());
  81. }
  82. /**
  83. * Add a host to authenticate for.
  84. *
  85. * @param host Hostname
  86. * @param port Port
  87. * @param username Username to return for authentication
  88. * @param password Password to return for authentication
  89. */
  90. public void addAuthentication(final String host, final int port, final String username, final String password) {
  91. if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
  92. return;
  93. }
  94. final PasswordAuthentication pass = new PasswordAuthentication(username, password.toCharArray());
  95. final String fullhost = host.toLowerCase()+":"+port;
  96. if (replies.containsKey(fullhost)) {
  97. replies.remove(fullhost);
  98. }
  99. replies.put(fullhost, pass);
  100. }
  101. /** {@inheritDoc} */
  102. protected PasswordAuthentication getPasswordAuthentication() {
  103. /*
  104. * getRequestingHost: 85.234.138.2
  105. * getRequestingPort: 1080
  106. * getRequestingPrompt: SOCKS authentication
  107. * getRequestingProtocol: SOCKS5
  108. * getRequestingScheme: null
  109. * getRequestingSite: /85.234.138.2
  110. * getRequestingURL: null
  111. * getRequestorType: SERVER
  112. */
  113. final String fullhost = getRequestingHost().toLowerCase()+":"+getRequestingPort();
  114. return replies.get(fullhost);
  115. }
  116. }