SSLServer.java
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:7k
- /* CVS ID: $Id: SSLServer.java,v 1.2 2000/04/06 08:02:02 wastl Exp $ */
- package net.wastl.webmail.standalone;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import java.security.*;
- import javax.net.ssl.*;
- import de.tu_darmstadt.sp.ssl.*;
- import net.wastl.webmail.config.ConfigurationListener;
- import net.wastl.webmail.server.*;
- import net.wastl.webmail.server.http.*;
- import net.wastl.webmail.ui.html.*;
- import net.wastl.webmail.debug.ErrorHandler;
- /*
- * HTTPServer.java
- *
- * Created: Tue Feb 2 12:15:48 1999
- *
- * Copyright (C) 1999-2000 Sebastian Schaffert
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /**
- *
- *
- *
- * @author Sebastian Schaffert
- * @version $Revision: 1.2 $
- */
- public class SSLServer extends HTTPServer implements ConfigurationListener {
-
-
- private boolean shutdown=false;
-
- protected boolean handshake_completed;
-
- private int port;
-
- private ConnectionTimer timer;
-
- private SSLServerSocket socket;
-
- //private SSLContext serverContext;
-
- private WebMailServer parent;
-
- private Hashtable html_loaders;
-
- private long start_time;
- private long nr_connections=0;
-
- public SSLServer(WebMailServer parent) {
- super();
-
- System.err.print("- SSL Server ...");
- parent.getConfigScheme().configRegisterIntegerKey(this,"SSL PORT","6790","Port where the SSL server will accept connections");
- parent.getConfigScheme().configRegisterStringKey(this,"SSL CERTS","../data/ssl/certs/","Path to SSL certificates");
- parent.getConfigScheme().configRegisterStringKey(this,"SSL ADDRESS","0.0.0.0","Address for the SSL Server to listen on (default: all addresses)");
- parent.getConfigScheme().configRegisterIntegerKey(this,"SSL BACKLOG","50","SSL Server Socket Backlog (how many connections to hold in Queue)");
- parent.getConfigScheme().configRegisterIntegerKey(this,"SSL CONNECTION LIMIT","50","Maximum number of simultaneous connections (reduce to avoid server crash on machines with low memory, 20 should be ok for medium-high load machines)");
- parent.getConfigScheme().configRegisterYesNoKey(this,"SSL ENABLE","Enable the SSL server.");
-
- this.timer=parent.getConnectionTimer();
- this.parent=parent;
-
- port=6790;
- try {
- port=Integer.parseInt(parent.getStorage().getConfig("SSL PORT"));
- } catch(NumberFormatException e) {
- }
-
- max_connections=50;
- current_connections=0;
- try {
- max_connections=Integer.parseInt(parent.getStorage().getConfig("SSL CONNECTION LIMIT"));
- } catch(NumberFormatException e) {
- }
-
- int backlog=50;
- try {
- backlog=Integer.parseInt(parent.getStorage().getConfig("SSL BACKLOG"));
- } catch(NumberFormatException e) {
- }
-
-
-
- if(parent.getStorage().getConfig("SSL ENABLE").toUpperCase().equals("YES")) {
-
-
- try {
-
- Properties props=System.getProperties();
- props.put("iti.ssl.ca_file",parent.getConfig("SSL CERTS")+"/ca.pem");
- props.put("iti.ssl.cert_file",parent.getConfig("SSL CERTS")+"/cert.pem");
- props.put("iti.ssl.key_file",parent.getConfig("SSL CERTS")+"/key.pem");
- System.setProperties(props);
-
- System.loadLibrary("itissl");
-
-
- SSLSocketFactory.setDefault(new SSLeaySocketFactory());
- SSLServerSocketFactory.setDefault(new SSLeayServerSocketFactory());
- // get a server socket and listen..
- SSLServerSocketFactory factory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
- socket = (SSLServerSocket)factory.createServerSocket(port,backlog,
- InetAddress.getByName(parent.getStorage().getConfig("SSL ADDRESS")));
-
- socket.setNeedClientAuth(false);
-
- socket.setSoTimeout(1000);
- this.start();
- System.err.println(" initialization complete. Listening on port "+port+".");
- start_time=System.currentTimeMillis();
-
- } catch(Throwable ex) {
- //new ErrorHandler(ex);
- System.err.println(" initialization failed! ("+ex.getMessage()+")");
- }
- } else {
- System.err.println(" SSL disabled in configuration. Server not started.");
- }
- }
-
-
- public void shutdown() {
- shutdown=true;
- System.err.print("- SSL Server shutdown requested ...");
- try {
- socket.close();
- } catch(Exception e) {}
- try {
- // Give connections enough time to terminate
- sleep(2000);
- } catch(InterruptedException ex) {}
- //this.stop();
- System.err.println("complete!");
- }
-
- public String getStatus() {
- String status;
- if(isAlive()) {
- status="SSL Server listening on "+parent.getStorage().getConfig("SSL ADDRESS")+", Port "+port;
- // Disabled because it hangs on certain systems
- //status="SSL Server listening on "+socket.getInetAddress()+", Port "+port;
- long up=System.currentTimeMillis()-start_time;
- status+="nUptime: "+up/1000+" secondsn";
- status+="Number of connections so far: "+nr_connections+", average "+ (nr_connections*60000/up) +" conn/minn";
- status+="There are currently "+current_connections+" connections out of a maximum of "+max_connections+".n";
- } else {
- status="SSL Server disabled.";
- }
-
- return status;
- }
-
- public void notifyConfigurationChange(String key) {
- parent.reinitServer("SSL");
- }
-
- public void run() {
- while(!shutdown) {
- /* Enter critical resource. Connections must release this on termination! */
-
- if(current_connections < max_connections) {
-
- try {
-
- if(Runtime.getRuntime().freeMemory() > required_free_memory) {
- //Runtime.getRuntime().traceInstructions(true);
- SSLSocket client=(SSLSocket)socket.accept();
- client.setNeedClientAuth(false);
-
- System.err.println("Handshake completed");
- System.err.println(client.toString());
- Connection conn=new Connection(client,parent,this);
- nr_connections++;
- //Runtime.getRuntime().traceInstructions(false);
-
- } else {
- parent.getStorage().log(Storage.LOG_ERR,"Error: Ran out of memory. Garbage collecting."+
- " You might want to increase the minimum memory size in webmail.sh to avoid this.");
- System.gc();
- }
-
- } catch(InterruptedIOException ex) {
- } catch(IOException ex) {
- if(!ex.getMessage().equals("Socket closed")) {
- //new ErrorHandler(ex);
- ex.printStackTrace();
- }
- }
-
-
- } else {
- parent.getStorage().log(Storage.LOG_DEBUG,"SSL Server: Maximum number of SSL connections reached."+
- " You might want to increase the SSL CONNECTION LIMIT if your server is fast enough");
- try {
- synchronized(this) {
- wait();
- }
- } catch(InterruptedException ex) {}
- }
-
-
- }
- }
- } // Server