1 11 12 package org.jivesoftware.messenger.net; 13 14 import org.jivesoftware.messenger.ConnectionManager; 15 import org.jivesoftware.messenger.ServerPort; 16 import org.jivesoftware.util.LocaleUtils; 17 import org.jivesoftware.util.Log; 18 19 import javax.net.ssl.SSLException; 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.ServerSocket ; 23 import java.net.Socket ; 24 import java.net.UnknownHostException ; 25 26 30 public class SSLSocketAcceptThread extends Thread { 31 32 35 public static final int DEFAULT_PORT = 5223; 36 37 40 private InetAddress bindInterface; 41 42 45 private ServerPort serverPort; 46 47 50 private boolean notTerminated = true; 51 52 55 private ServerSocket serverSocket; 56 57 60 private ConnectionManager connManager; 61 65 private static final int MAX_SSL_EXCEPTIONS = 10; 66 67 75 public SSLSocketAcceptThread(ConnectionManager connManager, ServerPort serverPort) 76 throws IOException { 77 super("Secure Socket Listener"); 78 this.connManager = connManager; 79 this.serverPort = serverPort; 80 int port = serverPort.getPort(); 81 String interfaceName = serverPort.getInterfaceName(); 82 bindInterface = null; 83 if (interfaceName != null) { 84 try { 85 if (interfaceName.trim().length() > 0) { 86 bindInterface = InetAddress.getByName(interfaceName); 87 } 88 } 89 catch (UnknownHostException e) { 90 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 91 } 92 } 93 serverSocket = SSLConfig.createServerSocket(port, bindInterface); 94 } 95 96 101 public int getPort() { 102 return serverSocket.getLocalPort(); 103 } 104 105 110 public ServerPort getServerPort() { 111 return serverPort; 112 } 113 114 117 public void shutdown() { 118 notTerminated = false; 119 try { 120 ServerSocket sSock = serverSocket; 121 serverSocket = null; 122 if (sSock != null) { 123 sSock.close(); 124 } 125 } 126 catch (IOException e) { 127 } 129 } 130 131 138 public void run() { 139 long lastExceptionTime = 0; 140 int exceptionCounter = 0; 141 while (notTerminated) { 142 try { 143 Socket sock = serverSocket.accept(); 144 Log.debug("SSL Connect " + sock.toString()); 145 connManager.addSocket(sock, true, serverPort); 146 } 147 catch (SSLException se) { 148 long exceptionTime = System.currentTimeMillis(); 149 if (exceptionTime - lastExceptionTime > 1000) { 150 exceptionCounter = 1; 153 } 154 else { 155 exceptionCounter++; 158 } 159 lastExceptionTime = exceptionTime; 160 Log.error(LocaleUtils.getLocalizedString("admin.error.ssl"), se); 161 if (exceptionCounter > MAX_SSL_EXCEPTIONS) { 164 String msg = "Shutting down SSL port - " + 165 "suspected configuration problem"; 166 Log.error(msg); 167 Log.info(msg); 168 shutdown(); 169 } 170 } 171 catch (Exception e) { 172 if (notTerminated) { 173 Log.error(LocaleUtils.getLocalizedString("admin.error.ssl"), e); 174 } 175 } 176 } 177 try { 178 ServerSocket sSock = serverSocket; 179 serverSocket = null; 180 if (sSock != null) { 181 sSock.close(); 182 } 183 } 184 catch (IOException e) { 185 } 187 } 188 } 189 | Popular Tags |