📄 socketfactory.java
字号:
/*
* jRevProxy, an opensource Java reverse proxy server
*
* 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, USA.
*
* $Id: SocketFactory.java,v 2.3 2003/03/27 15:49:37 fnoe Exp $
*/
package cx.noe.jrevproxy;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.IOException;
import java.io.FileInputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.*;
import javax.net.*;
import java.security.*;
import java.util.Properties;
import java.util.Enumeration;
import cx.noe.jrevproxy.logging.ILog;
/**
* SocketFactory
*
* @author <a href="mailto:frederik@noe.cx">Frederik Noe</a>
* @version <tt>$Revision: 2.3 $</tt>
*/
public class SocketFactory implements ILog{
private int backLog = 0;
private Properties properties = null;
private ILog logger = null;
public SocketFactory(Properties properties, ILog logger) {
this.properties = properties;
this.logger = logger;
}
public ServerSocket getServerSocket(int port) throws IOException {
log(METHOD,"--getServerSocket--");
InetAddress bindAddress = null;
ServerSocket serversocket = null;
if((bindAddress = getBindAddress()) != null)
serversocket = new ServerSocket(port,backLog,bindAddress);
else
serversocket = new ServerSocket(port);
log(INFO,"Serversocket created; port "+ port);
return serversocket;
}
public SSLServerSocket getSSLServerSocket(int port, boolean requireClientAuthentication) throws Exception{
log(METHOD,"--getSSLServerSocket--");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
KeyStore keystore = KeyStore.getInstance("JKS");
String keyStoreName = null;
keystore.load(new FileInputStream((keyStoreName = properties.getProperty("KEYSTORE"))), properties.getProperty("KEYSTOREPASSWD").toCharArray());
log(INFO,"keyStore: " + keyStoreName);
KeyStore truststore = KeyStore.getInstance("JKS");
String trustStoreName = null;
truststore.load(new FileInputStream((trustStoreName = properties.getProperty("TRUSTSTORE"))), properties.getProperty("TRUSTSTOREPASSWD").toCharArray());
log(INFO,"trustStore: " + trustStoreName);
Enumeration enum = truststore.aliases();
log(DEBUG,"trustStore contains the following aliases:");
int counter = 1;
while(enum.hasMoreElements()) {
String alias = (String)enum.nextElement();
log(DEBUG,"alias "+ counter++ + ": " + alias);
}
TrustManagerFactory trustfactory = TrustManagerFactory.getInstance("SunX509");
trustfactory.init(truststore);
KeyManagerFactory keyfactory = KeyManagerFactory.getInstance("SunX509");
keyfactory.init(keystore, properties.getProperty("KEYPASSWD").toCharArray());
SSLContext context = SSLContext.getInstance("SSLv3");
context.init(keyfactory.getKeyManagers(), trustfactory.getTrustManagers(), null);
ServerSocketFactory socketfactory = context.getServerSocketFactory();
SSLServerSocket serverSocket = (SSLServerSocket) socketfactory.createServerSocket(port);
serverSocket.setNeedClientAuth(requireClientAuthentication);
log(INFO,"SSLserversocket created; port "+ port + "; ClientAuthentication: " + (requireClientAuthentication?"yes":"no"));
return serverSocket;
}
public Socket getClientSocket(String host, int port) throws Exception{
log(METHOD,"--getClientSocket--");
return new Socket(host,port);
}
public Socket getSSLClientSocket(String host, int port) throws Exception{
log(METHOD,"--getSSLClientSocket--");
return null;
}
private InetAddress getBindAddress() {
log(METHOD,"--getBindAddress--");
String address = null;
if(null != (address = properties.getProperty("INTERFACE"))) {
try {
InetAddress bindAddress = InetAddress.getByName(address);
log(DEBUG,"BindAddress: " + address);
return bindAddress;
}
catch(UnknownHostException ef)
{
log(ef);
}
}
return null;
}
public void log(Throwable e) {
if(logger != null)
logger.log(e);
}
public void log(int level, String str) {
if(logger != null)
logger.log(level,"[SocketFactory] " + str);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -