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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.identd;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.interfaces.ConnectionManager;
  20. import com.dmdirc.config.provider.AggregateConfigProvider;
  21. import com.dmdirc.plugins.PluginDomain;
  22. import com.dmdirc.util.system.SystemInfo;
  23. import java.io.IOException;
  24. import java.net.ServerSocket;
  25. import java.net.Socket;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import javax.inject.Inject;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import static com.dmdirc.util.LogUtils.USER_ERROR;
  32. /**
  33. * The IdentdServer watches over the ident port when required
  34. */
  35. public final class IdentdServer implements Runnable {
  36. private static final Logger LOG = LoggerFactory.getLogger(IdentdServer.class);
  37. /** The Thread in use for this server */
  38. private volatile Thread myThread;
  39. /** The current socket in use for this server */
  40. private ServerSocket serverSocket;
  41. /** Arraylist of all the clients we have */
  42. private final List<IdentClient> clientList = new ArrayList<>();
  43. /** Server manager. */
  44. private final ConnectionManager connectionManager;
  45. /** Have we failed to start this server previously? */
  46. private boolean failed;
  47. /** Global configuration to read plugin's from. */
  48. private final AggregateConfigProvider config;
  49. /** This plugin's config settings domain. */
  50. private final String domain;
  51. /** System info wrapper to use for the ident client. */
  52. private final SystemInfo systemInfo;
  53. /**
  54. * Create the IdentdServer.
  55. *
  56. * @param connectionManager Server manager to iterate over servers
  57. * @param config Global config
  58. * @param domain This plugin's setting domain
  59. */
  60. @Inject
  61. public IdentdServer(final ConnectionManager connectionManager,
  62. @GlobalConfig final AggregateConfigProvider config,
  63. @PluginDomain(IdentdPlugin.class) final String domain,
  64. final SystemInfo systemInfo) {
  65. this.connectionManager = connectionManager;
  66. this.config = config;
  67. this.domain = domain;
  68. this.systemInfo = systemInfo;
  69. }
  70. /**
  71. * Run this IdentdServer.
  72. */
  73. @Override
  74. public void run() {
  75. final Thread thisThread = Thread.currentThread();
  76. while (myThread == thisThread) {
  77. try {
  78. final Socket clientSocket = serverSocket.accept();
  79. final IdentClient client = new IdentClient(this, clientSocket, connectionManager,
  80. config, domain, systemInfo);
  81. client.start();
  82. addClient(client);
  83. } catch (IOException e) {
  84. if (myThread == thisThread) {
  85. LOG.error(USER_ERROR, "Accepting client failed: {}", e.getMessage(), e);
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Add an IdentClient to the clientList
  92. *
  93. * @param client Client to add
  94. */
  95. public void addClient(final IdentClient client) {
  96. synchronized (clientList) {
  97. clientList.add(client);
  98. }
  99. }
  100. /**
  101. * Remove an IdentClient from the clientList
  102. *
  103. * @param client Client to remove
  104. */
  105. public void delClient(final IdentClient client) {
  106. synchronized (clientList) {
  107. for (int i = 0; i < clientList.size(); ++i) {
  108. if (clientList.get(i) == client) {
  109. clientList.remove(i);
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Check if the server is currently running
  117. *
  118. * @return True if the server is running
  119. */
  120. public boolean isRunning() {
  121. return myThread != null;
  122. }
  123. /**
  124. * Start the ident server
  125. */
  126. public void startServer() {
  127. if (!failed && myThread == null) {
  128. try {
  129. final int identPort = config.getOptionInt(domain, "advanced.port");
  130. serverSocket = new ServerSocket(identPort);
  131. myThread = new Thread(this);
  132. myThread.start();
  133. } catch (IOException e) {
  134. LOG.error(USER_ERROR, "Unable to start identd server: {}", e.getMessage(), e);
  135. if ("Permission denied".equals(e.getMessage())) {
  136. failed = true;
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * Stop the ident server
  143. */
  144. public void stopServer() {
  145. if (myThread != null) {
  146. final Thread tmpThread = myThread;
  147. myThread = null;
  148. if (tmpThread != null) {
  149. tmpThread.interrupt();
  150. }
  151. try {
  152. serverSocket.close();
  153. } catch (IOException e) {
  154. LOG.info("Unable to close socket: {}", e.getMessage(), e);
  155. }
  156. synchronized (clientList) {
  157. clientList.forEach(IdentClient::close);
  158. clientList.clear();
  159. }
  160. }
  161. }
  162. }