import java.util.Vector;import java.io.*;import java.net.*;//@start java.exe -classpath .\jproxychain.jar l0056509104 class FireWall {private static final int MAX_CONNECTION = 1000;private static boolean DEBUG_MODE = true;private static Vector clients = new Vector();private static int localPort = 8081;private static int remotePort = 1433;private static String remotetHost = "10.10.5.254";private static String connectType = "UNIX";public static void main(String[] args) throws IOException {int i;//String[] szUsage = {//"使用说明: ",//" OracleNet localPort remotePort remotetHost connectType debugMode",//" OracleNet localPort remotePort remotetHost connectType",//" OracleNet localPort remotePort remotetHost debugMode",//" OracleNet localPort remotePort remotetHost", "参数说明:",//" localPort : 客户端tnsnames.ora配置的远程端口",//" remotePort : 服务器侦听端口", " remotetHost: 服务器侦听地址",//" connectType: 服务器类型,其必须为UNIX或WINDOW,缺省为WINDOW",//" debugMode : 调试模式,其值为DEBUG或NODEBUG,缺省为NODEBUG", "使用样例: ",//" OracleNet.exe 1234 1521 192.0.2.91 UNIX DEBUG",//" OracleNet.exe 1234 1521 192.0.2.91 WINDOW DEBUG",//" OracleNet.exe 1234 1521 192.0.2.91 UNIX",//" OracleNet.exe 1234 1521 192.0.2.91 WINDOW",//" OracleNet.exe 1234 1521 192.0.2.91 DEBUG",//" OracleNet.exe 1234 1521 192.0.2.91", "缺省命令行: " };//About();//if (args.length == 3) {//localPort = Integer.parseInt(args[0]);//remotePort = Integer.parseInt(args[1]);//remotetHost = args[2];//} else if (args.length == 4) {//localPort = Integer.parseInt(args[0]);//remotePort = Integer.parseInt(args[1]);//remotetHost = args[2];//if (args[3].equalsIgnoreCase("DEBUG")) {//DEBUG_MODE = true;//} else if (args[3].equalsIgnoreCase("NODEBUG")) {//DEBUG_MODE = false;//} else {//connectType = args[3];//}//} else {//for (i = 0; i < szUsage.length; i++) {//System.out.println(szUsage[i]);//}System.out.println(" OracleNet.exe " + localPort + " "+ remotePort + " " + remotetHost + " " + connectType+ (DEBUG_MODE?" DEBUG":" NODEBUG"));//}new FireWall(localPort, remotePort, remotetHost);}FireWall(int localPort, int remotePort, String remotetHost)throws IOException {ServerSocket server = new ServerSocket(localPort);System.out.println("服务已启动,等待客户端连接......");System.out.println("本地服务端口:" + localPort);System.out.println("远程服务映射端口:" + remotePort);System.out.println("远程服务映射主机:" + remotetHost);try {while (true) {Socket incoming = server.accept();try {System.out.println("客户端【"+ incoming.getInetAddress().getHostName() + "/"+ incoming.getPort() + "】请求连接");if (clients.size() >= MAX_CONNECTION) {System.out.println("连接超过最大连接数,取消连接请求");incoming.close();} else {System.out.println("正在连接重定向【" + remotetHost + "/"+ remotePort + "】");Socket outputing = new Socket(remotetHost, remotePort);System.out.println("重定向成功,当前转发模式【" + connectType + "】");if (connectType.equalsIgnoreCase("UNIX")) {clients.addElement(new SocketMap(outputing,incoming, "SERVER"));// clients.addElement(new// SocketMap(incoming,outputing,"CLIENT"));new SocketMap(incoming, outputing, "CLIENT");} else {clients.addElement(new OracleWinMap(incoming,outputing));}}} catch (IOException e) {System.out.println("建立服务失败.");try {incoming.close();} catch (IOException err) {System.out.println("发生错误,关闭连接");}}}} finally {server.close();}}class OracleWinMap extends Thread {private Socket socket, target;private byte[] dataBuf = new byte[512];private int dataLen;private InputStream clsIn, dbIn;private OutputStream clsOut, dbOut;OracleWinMap(Socket source, Socket dest) throws IOException {socket = source;target = dest;clsIn = socket.getInputStream();clsOut = socket.getOutputStream();dbIn = target.getInputStream();dbOut = target.getOutputStream();start();}public void run() {int dataPort = 0;try {dataLen = clsIn.read(dataBuf);if (dataLen > 0) {dbOut.write(dataBuf, 0, dataLen);dbOut.flush();}dataLen = dbIn.read(dataBuf);if (dataLen > 0) {localPort++;dataPort = setPackageHeader(socket.getLocalAddress().getHostAddress(), localPort);dataBuf[1] = (byte) dataLen;clsOut.write(dataBuf, 0, dataLen);clsOut.flush();}try {if (DEBUG_MODE) {System.out.println(localPort + "->" + dataPort);}DataConnect(localPort, dataPort);localPort++;} catch (Exception e) {System.out.println("处理连接数据库侦听异常:" + e);}} catch (Exception e) {} finally {try {target.close();socket.close();clients.remove(this);} catch (IOException e) {System.out.println("调用DataConnect错误:" + e);System.out.println("发生错误,关闭连接");}}}public int setPackageHeader(String Host,int Port) { byte[] list = new byte[512]; int dbPort,sPos,ePos,len,index; String listStr = "",dbHost; boolean isWindow=false; for (int i=0; i<10; i++) //dbdxj { list [i] =dataBuf[i]; } for (int i=10; i<dataLen; i++) { listStr =listStr + (char) dataBuf[i]; } if(DEBUG_MODE) { System.out.println("原数据通道"+listStr); } len = listStr.length(); sPos = listStr.indexOf("HOST="); ePos = listStr.indexOf(')',sPos); dbHost = listStr.substring(sPos+5,ePos); if(dbHost!=Host && sPos>0) { listStr = listStr.substring(0,sPos + 5) + Host + listStr.substring(ePos); isWindow = true; } sPos = listStr.indexOf("PORT="); //查找数据端口 ePos = listStr.indexOf(')',sPos); dbPort = Integer.parseInt(listStr.substring(sPos+5,ePos)); if(dbPort!=Port && sPos>0) { listStr = listStr.substring(0,sPos + 5) + Port + listStr.substring(ePos); isWindow = true; } if(DEBUG_MODE) { System.out.println("新数据通道"+listStr); } if(isWindow) //数据端口和侦听端口不一致 { for (int i=0; i<listStr.length(); i++) //加上更新后的字符串 { list[10 + i] = (byte) listStr.charAt(i); } dataLen = 10 + listStr.length(); //更新侦听信息 for (int i=0; i<list.length; i++) //dbdxj { dataBuf[i] = list[i]; } } return dbPort; }void DataConnect(int clsPort, int dbPort) {ServerSocket server = null;Socket clsSocket, dbSocket;try {server = new ServerSocket(clsPort);dbSocket = new Socket(remotetHost, dbPort);clsSocket = server.accept();if (DEBUG_MODE) {System.out.println("SocketMap(" + clsSocket + ","+ dbSocket + ",\"CLIENT\");");System.out.println("SocketMap(" + dbSocket + ","+ clsSocket + ",\"SERVER\");");}new SocketMap(dbSocket, clsSocket, "SERVER");new SocketMap(clsSocket, dbSocket, "CLIENT");} catch (Exception e) {System.out.println("建立连接错误:" + e);try {server.close();} catch (Exception f) {e.printStackTrace();}}}}class SocketMap extends Thread {private byte[] dataBuf = new byte[512];private int dataLen;private Socket socket, target;private InputStream thisIn, targetIn;private OutputStream thisOut, targetOut;private String serverSymbol, clientSymbol;SocketMap(Socket source, Socket dest, String symbol) throws IOException {serverSymbol = symbol;socket = source;target = dest;thisIn = socket.getInputStream();thisOut = socket.getOutputStream();targetIn = target.getInputStream();targetOut = target.getOutputStream();if (symbol.equalsIgnoreCase("SERVER")) {clientSymbol = socket.getLocalAddress().getHostName() + "/"+ socket.getPort();} else {clientSymbol = socket.getInetAddress().getHostName() + "/"+ socket.getPort();}start();}public void run() {int dbdataPort = 0;System.out.println(serverSymbol + "->【" + clientSymbol+ "】开始一个新的线程服务");try {while (true) {dataLen = thisIn.read(dataBuf);if (dataLen > 0) {if (DEBUG_MODE) {System.out.println(serverSymbol + "缓冲区长度:"+ dataLen);if (serverSymbol.equalsIgnoreCase("SERVER")) {SetErrLog("d:\\SocketSERVER.Log", dataBuf,dataLen);} else {SetErrLog("d:\\SocketCLIENT.Log", dataBuf,dataLen);}}targetOut.write(dataBuf, 0, dataLen);targetOut.flush();} else {break;}try {this.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(serverSymbol + "->【" + clientSymbol+ "】关闭连接");} catch (IOException e) {} finally {try {socket.close();if (connectType.equalsIgnoreCase("UNIX")&& serverSymbol.equalsIgnoreCase("SERVER")) {clients.remove(this);}} catch (IOException e) {System.out.println("发生错误,关闭连接");}}}public void SetErrLog(String filename, byte[] dataBuffer, int len) {File file = new File(filename);FileOutputStream fos;try {if (!file.exists()) {fos = new FileOutputStream(file);} else {fos = new FileOutputStream(file, true);}fos.write(dataBuffer, 0, len);fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}// 版本声明static void About() {int i;String[] szAbout = { "\n\n\n\t\t\t\t欢迎使用本系统\n\n","\t 本系统是一个网络Socket转发工具,用于远程Socket转发\n","\t及Oracle数据库的连接维护。本产品属于个人所有,未经许可,\n", "\t不得随意传播!\n","\t 本产品如有异常请与作者联系!\n\n", "\t\t\t\t联系人:钱海兵\n","\t\t\t\tE-Mail:qianhb@e-future.com.cn\n" };for (i = 0; i < szAbout.length; i++) {System.out.println(szAbout[i]);}}}