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.

IdentdServer.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.identd;
  23. import com.dmdirc.config.GlobalConfig;
  24. import com.dmdirc.interfaces.ConnectionManager;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import com.dmdirc.plugins.PluginDomain;
  27. import com.dmdirc.util.SystemInfo;
  28. import java.io.IOException;
  29. import java.net.ServerSocket;
  30. import java.net.Socket;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import javax.inject.Inject;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import static com.dmdirc.util.LogUtils.USER_ERROR;
  37. /**
  38. * The IdentdServer watches over the ident port when required
  39. */
  40. public final class IdentdServer implements Runnable {
  41. private static final Logger LOG = LoggerFactory.getLogger(IdentdServer.class);
  42. /** The Thread in use for this server */
  43. private volatile Thread myThread;
  44. /** The current socket in use for this server */
  45. private ServerSocket serverSocket;
  46. /** Arraylist of all the clients we have */
  47. private final List<IdentClient> clientList = new ArrayList<>();
  48. /** Server manager. */
  49. private final ConnectionManager connectionManager;
  50. /** Have we failed to start this server previously? */
  51. private boolean failed;
  52. /** Global configuration to read plugin's from. */
  53. private final AggregateConfigProvider config;
  54. /** This plugin's config settings domain. */
  55. private final String domain;
  56. /** System info wrapper to use for the ident client. */
  57. private final SystemInfo systemInfo;
  58. /**
  59. * Create the IdentdServer.
  60. *
  61. * @param connectionManager Server manager to iterate over servers
  62. * @param config Global config
  63. * @param domain This plugin's setting domain
  64. */
  65. @Inject
  66. public IdentdServer(final ConnectionManager connectionManager,
  67. @GlobalConfig final AggregateConfigProvider config,
  68. @PluginDomain(IdentdPlugin.class) final String domain,
  69. final SystemInfo systemInfo) {
  70. this.connectionManager = connectionManager;
  71. this.config = config;
  72. this.domain = domain;
  73. this.systemInfo = systemInfo;
  74. }
  75. /**
  76. * Run this IdentdServer.
  77. */
  78. @Override
  79. public void run() {
  80. final Thread thisThread = Thread.currentThread();
  81. while (myThread == thisThread) {
  82. try {
  83. final Socket clientSocket = serverSocket.accept();
  84. final IdentClient client = new IdentClient(this, clientSocket, connectionManager,
  85. config, domain, systemInfo);
  86. client.start();
  87. addClient(client);
  88. } catch (IOException e) {
  89. if (myThread == thisThread) {
  90. LOG.error(USER_ERROR, "Accepting client failed: {}", e.getMessage(), e);
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Add an IdentClient to the clientList
  97. *
  98. * @param client Client to add
  99. */
  100. public void addClient(final IdentClient client) {
  101. synchronized (clientList) {
  102. clientList.add(client);
  103. }
  104. }
  105. /**
  106. * Remove an IdentClient from the clientList
  107. *
  108. * @param client Client to remove
  109. */
  110. public void delClient(final IdentClient client) {
  111. synchronized (clientList) {
  112. for (int i = 0; i < clientList.size(); ++i) {
  113. if (clientList.get(i) == client) {
  114. clientList.remove(i);
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Check if the server is currently running
  122. *
  123. * @return True if the server is running
  124. */
  125. public boolean isRunning() {
  126. return myThread != null;
  127. }
  128. /**
  129. * Start the ident server
  130. */
  131. public void startServer() {
  132. if (!failed && myThread == null) {
  133. try {
  134. final int identPort = config.getOptionInt(domain, "advanced.port");
  135. serverSocket = new ServerSocket(identPort);
  136. myThread = new Thread(this);
  137. myThread.start();
  138. } catch (IOException e) {
  139. LOG.error(USER_ERROR, "Unable to start identd server: {}", e.getMessage(), e);
  140. if ("Permission denied".equals(e.getMessage())) {
  141. failed = true;
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Stop the ident server
  148. */
  149. public void stopServer() {
  150. if (myThread != null) {
  151. final Thread tmpThread = myThread;
  152. myThread = null;
  153. if (tmpThread != null) {
  154. tmpThread.interrupt();
  155. }
  156. try {
  157. serverSocket.close();
  158. } catch (IOException e) {
  159. LOG.info("Unable to close socket: {}", e.getMessage(), e);
  160. }
  161. synchronized (clientList) {
  162. clientList.forEach(IdentClient::close);
  163. clientList.clear();
  164. }
  165. }
  166. }
  167. }