📄 revproxy.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: RevProxy.java,v 2.14 2003/04/23 20:29:36 fnoe Exp $
*/
package cx.noe.jrevproxy;
import java.io.FileInputStream;
import java.util.Properties;
import java.io.File;
import java.net.*;
import cx.noe.jrevproxy.RequestListener;
import cx.noe.jrevproxy.PoolManager;
import cx.noe.jrevproxy.RulesetParser;
import cx.noe.jrevproxy.logging.*;
/**
* RexProxy
*
* Main entrance class, receiving command line parameters for the properties configuration file
* and XML configuration file
* @author <a href="mailto:frederik@noe.cx">Frederik Noe</a>
* @version <tt>$Revision: 2.14 $</tt>
*/
public class RevProxy {
private static final String version = "v0.5";
private static final int build = 1020;
private static final int REQUIREDNRARGS = 1;
private static RequestListener reqListener = null;
private static PoolManager poolManager = null;
private static ILog logger = null;
/**
* Howto use the jRevProxy package
*/
public static void printUsage() {
System.out.println("jRevProxy " + version + " build "+ build);
System.out.println("Usage:");
System.out.println("java cx.noe.jrevproxy.RevProxy <properties file>");
System.out.println("");
}
/**
* Checks the existence of a file specified by a property
*
* @param fileName the name of the file to be checked
* @param defaultPath the path to used for searching the file
* @return the complete path to the file if the file exists; otherwise null
*/
private static String checkIfFileExists(String fileName, String defaultPath) {
// check if the path specified really points to the XML file
try {
File file = new File(fileName);
if(!file.exists()) {
// let's assume that the specified file resides in the same directory as the properties file
// retrieve the current directory
String path = getPath(defaultPath);
// check again if the file is now in the current directory
file = new File(path+ fileName);
if(!file.exists() || path == null) {
return null;
}
else {
// change the filename to the full one
return path + fileName;
}
}
else
return fileName;
}
catch(Exception e) {
System.err.println(e.toString());
return null;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Properties config = null;
// we need a properties file and xml file as parameter otherwise we cannot proceed
if(args.length != REQUIREDNRARGS ) {
printUsage();
return;
}
System.out.println("jRevProxy " + version + " build "+ build);
System.out.println("");
System.out.println("Java.home: " + System.getProperty("java.home"));
// load the configuration file
config = new Properties();
try {
FileInputStream str = new FileInputStream(args[0]);
config.load(str);
}
catch(Exception e) {
System.err.println(e.toString());
return;
}
// retrieve the protocol from the config file
// this specifies the externally supported protocol
String protocol = null;
if(null == (protocol = config.getProperty("PROTOCOL"))) {
System.out.println("Protocol not specified");
return;
}
// only HTTP(S) is supported
if(protocol.compareToIgnoreCase("HTTP") != 0 && protocol.compareToIgnoreCase("HTTPS") != 0) {
System.out.println("Invalid protocol specified");
return;
}
System.out.println("Protocol: " + protocol.toUpperCase());
// specifying the port is optional
// if the port is not specified, the corresponding default ports will be used
String portstr = null;
int port = 0;
if(null != (portstr = config.getProperty("PORT"))) {
port = Integer.parseInt(portstr);
if(port <=0) {
System.out.println("invalid port specified");
return;
}
System.out.println("Port: " + portstr);
}
String xmlfilename = null;
// retrieve the xmlconfigfile
if(null == (xmlfilename = config.getProperty("XMLCONFIGFILE"))) {
System.out.println("XML configuration file not specified");
return;
}
// check if the path specified really points to the XML file
String updatedfilename = null;
if((updatedfilename = checkIfFileExists(xmlfilename,args[0])) == null) {
System.out.print(xmlfilename + " not found");
return;
}
if(updatedfilename.compareTo(xmlfilename) != 0) {
config.setProperty("XMLCONFIGFILE",updatedfilename);
System.out.println("XML configuration file: " + updatedfilename);
}
// retrieve the logpath
// specification of this property is optional
String logpath = null;
if(null != (logpath = config.getProperty("LOGPATH")) && logpath.length() != 0) {
System.out.println("Logpath: " + logpath);
DataLogger datalogger = new DataLogger(logpath);
logger = (ILog)datalogger;
// check if we have a loglevel setting in the config file
String loglevel = null;
if(null != (loglevel = config.getProperty("LOGLEVEL"))) {
if(loglevel.compareToIgnoreCase("DEBUG") == 0) {
datalogger.setLogLevel(datalogger.DEBUG);
System.setProperty("javax.net.debug","all");
}
else if (loglevel.compareToIgnoreCase("INFO") == 0)
datalogger.setLogLevel(datalogger.INFO);
else if (loglevel.compareToIgnoreCase("METHOD") == 0)
datalogger.setLogLevel(datalogger.METHOD);
else {
System.out.println("Wrong loglevel specified");
return;
}
System.out.println("Loglevel: " + loglevel.toUpperCase());
}
}
else {
System.out.println("Logpath not specified");
return;
}
// add a newline to the logfile so that we can find out where the server started
logger.log(logger.INFO,"\r\n");
// check if we need to do client authentication as well
boolean clientauth = false;
String clientauthstr = null;
if(null != (clientauthstr = config.getProperty("CLIENTAUTHENTICATION"))) {
if(clientauthstr.compareToIgnoreCase("TRUE") == 0)
clientauth = true;
}
System.out.println("ClientAuthentication: " + clientauth);
// create the poolmanager
poolManager = new PoolManager(logger);
poolManager.init();
// create the request listener and provide the retrieved properties
reqListener = new RequestListener(poolManager,config);
try {
if(protocol.compareToIgnoreCase("HTTP") == 0)
reqListener.setProtocol(reqListener.HTTP);
else if (protocol.compareToIgnoreCase("HTTPS") == 0) {
reqListener.setProtocol(reqListener.HTTPS);
// client authentication is only applicable in case we have HTTPS
reqListener.setClientAuthentication(clientauth);
}
else {
System.out.println("Wrong protocol specified");
return;
}
}
catch(IllegalArgumentException e) {
// handle wrong protocol specification
}
// if we will use HTTPS then we need to check the truststore and the keystore
if(reqListener.getProtocol() == reqListener.HTTPS) {
// check the KEYSTORE and TRUSTSTORE properties values to find out if the file exist
// check if the path specified really points to the XML file
String updatedFilename = null, keyStoreFilename = null, trustStoreFilename = null;
if(((keyStoreFilename = config.getProperty("KEYSTORE")) == null) || keyStoreFilename.length() == 0) {
System.out.println("KEYSTORE property not specified");
return;
}
if((updatedFilename = checkIfFileExists(keyStoreFilename,args[0])) == null) {
System.out.print("file " + keyStoreFilename + " not found");
return;
}
if(updatedFilename .compareTo(keyStoreFilename) != 0) {
config.setProperty("KEYSTORE",updatedFilename );
System.out.println("KEYSTORE file: " + updatedFilename );
}
if(((trustStoreFilename = config.getProperty("TRUSTSTORE")) == null) || trustStoreFilename.length() == 0) {
System.out.println("TRUSTSTORE property not specified");
return;
}
updatedFilename = null;
if((updatedFilename = checkIfFileExists(trustStoreFilename,args[0])) == null) {
System.out.print("file " + trustStoreFilename + " not found");
return;
}
if(updatedFilename .compareTo(trustStoreFilename) != 0) {
config.setProperty("TRUSTSTORE",updatedFilename );
System.out.println("TRUSTSTORE file: " + updatedFilename );
}
}
// if we have found a port in the config, let's hand it over
if(port != 0)
reqListener.setPort(port);
try {
// initialize
reqListener.init();
}
catch(Exception ef) {
logger.log(ef);
ef.printStackTrace();
poolManager.destroy();
System.exit(0);
}
// start handling external requests
reqListener.start();
System.out.println("jRevProxy started");
try {
// wait for the thread to stop
reqListener.join();
poolManager.destroy();
}
catch(InterruptedException e){
logger.log(e);
}
catch(Exception ef) {
logger.log(ef);
}
System.out.println("jRevProxy stopped");
}
/**
* Retrieve the path
* @param fullpath
**/
private static String getPath(String fullpath) {
int index = 0;
// find out the directory delimiter (OS specific)
if(fullpath.indexOf('/') > 0){
if((index = fullpath.lastIndexOf('/'))<=0)
return null;
}
else if(fullpath.indexOf('\\') >0){
if((index = fullpath.lastIndexOf('\\'))<=0)
return null;
}
else
// no correct fullpath
return null;
return fullpath.substring(0,index+1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -