?? threadcommunication.java
字號:
/*This file is part of Socks via HTTP.This package is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.Socks via HTTP is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Socks via HTTP; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/// Title : ThreadCommunication.java// Version : 0.40// Copyright : Copyright (c) 2001// Author : Florent CUETO (fcueto@wanadoo.fr)// Description : Communication between socks via HTTP client & the applicationpackage socks4;import java.net.*;import java.io.*;import java.util.zip.*;public class ThreadCommunication extends Thread{ public static final int PRIMARY_DELAY = 1500; public static final int DELAY = 20; public static final int CONNECTION_TIMEOUT = 180000; // 3mn private String servlet_url = null; private Connection m_source = null; private URL m_url_communication = null; private String id_conn = null; private String m_destination = null; private boolean initOk = false; private String m_proxyUser = null; private String m_proxyPassword = null; private String m_httpServerUser = null; private String m_httpServerPassword = null; public ThreadCommunication(Connection source, String viaUrl, String proxyUser, String proxyPassword, String httpServerUser, String httpServerPassword) { m_source = source; servlet_url = viaUrl; m_proxyUser = proxyUser; m_proxyPassword = proxyPassword; m_httpServerUser = httpServerUser; m_httpServerPassword = httpServerPassword; try { m_url_communication = new URL(servlet_url); } catch(MalformedURLException e) { Log.printLog("MalformedURLException : " + e); } // Get the destination try { Thread.sleep(PRIMARY_DELAY); } catch (Exception e){} SocksPacket reqPacket = new SocksPacket(m_source.read()); if (reqPacket.version != 4) { Log.printLog("SOCKS version " + reqPacket.version + " not supported. Closing connection."); return; } if (reqPacket.command == 2) { Log.printLog("BIND command not supported yet. Closing connection."); return; } if (reqPacket.dnsName != null) { // SOCKS 4A m_destination = reqPacket.dnsName + ":" + reqPacket.destPort; } else { // SOCKS 4 m_destination = reqPacket.destIP + ":" + reqPacket.destPort; } // Create a connection on the server DataPacket dataPacket = new DataPacket(); dataPacket.type = Const.CONNECTION_CREATE; dataPacket.id = m_httpServerUser + ":" + m_httpServerPassword + ":" + CONNECTION_TIMEOUT; dataPacket.tab = m_destination.getBytes(); // Send the connection HttpMessage mess = new HttpMessage(m_url_communication); InputStream is = null; ObjectInputStream ois = null; int type = Const.CONNECTION_UNSPECIFIED_TYPE; String err = null; try { if (m_proxyUser != null) { // Set the proxy authorization mess.setProxyAuthorization(m_proxyUser, m_proxyPassword); } // Send the object and get the inputstream is = mess.sendGZippedPostMessage(dataPacket); // Create the GZIPInputStream GZIPInputStream zis = new GZIPInputStream(is); // Create the ObjectInputStream ois = new ObjectInputStream(zis); DataPacket response = (DataPacket)ois.readObject(); ois.close(); type = response.type; id_conn = response.id; err = new String(response.tab); } catch(Exception e) { Log.printLog("Exception : " + e); } if (type == Const.CONNECTION_CREATE_OK) { initOk = true; //System.out.println("ThreadCommunication created to " + m_destination + " via " + servlet_url + "..."); Log.printLog(id_conn + " <<"); // Send the response packet to the socks client SocksPacket replyPacket = new SocksPacket(); replyPacket.version = SocksPacket.SOCKS4_REPLY_VERSION; replyPacket.command = SocksPacket.SOCKS4_OK; /*replyPacket.destPort = reqPacket.destPort; replyPacket.destIP = reqPacket.destIP;*/ String[] resp = ServletSocks.stringSplit(err, ":", false); replyPacket.destIP = resp[0]; replyPacket.destPort = Integer.parseInt(resp[1]); /*Log.printLog("destIP : " + resp[0]); Log.printLog("destPort : " + resp[1]);*/ m_source.write(replyPacket.toBytes()); } else { //Log.printLog("ThreadCommunication refused to " + m_destination + " via " + servlet_url + "..."); Log.printLog(err); // Send the response packet to the socks client SocksPacket replyPacket = new SocksPacket(); replyPacket.version = SocksPacket.SOCKS4_REPLY_VERSION; replyPacket.command = SocksPacket.SOCKS4_KO; replyPacket.destPort = reqPacket.destPort; replyPacket.destIP = reqPacket.destIP; m_source.write(replyPacket.toBytes()); } } // Main task public void run() { if (!initOk) { m_source.disconnect(); return; } boolean state = true; byte[] line; while (state == true) { try { line = m_source.read(); DataPacket dataPacket = new DataPacket(); if (line == null) { // Connection closed dataPacket.type = Const.CONNECTION_DESTROY; dataPacket.id = id_conn; dataPacket.tab = Const.TAB_EMPTY; } else { dataPacket.type = Const.CONNECTION_REQUEST; dataPacket.id = id_conn; dataPacket.tab = line; if (line.length > 0) { //System.out.println(id_conn + " ->"); //System.out.println(new String(line)); } } // Send the message HttpMessage mess = new HttpMessage(m_url_communication); InputStream is = null; ObjectInputStream ois = null; if (m_proxyUser != null) { // Set the proxy authorization mess.setProxyAuthorization(m_proxyUser, m_proxyPassword); } // Create the InputStream is = mess.sendGZippedPostMessage(dataPacket); // Create the GZIPInputStream GZIPInputStream zis = new GZIPInputStream(is); // Create the ObjectInputStream ois = new ObjectInputStream(zis); DataPacket response = (DataPacket)ois.readObject(); ois.close(); // Write the received bytes switch (response.type) { case Const.CONNECTION_RESPONSE: if (response.tab.length > 0) { //System.out.println(response.id + " <-"); //System.out.println(new String(response.tab)); } m_source.write(response.tab); break; case Const.CONNECTION_NOT_FOUND: Log.printLog("HTTP Server said : Connection id not found : " + id_conn); break; case Const.CONNECTION_DESTROY_OK: Log.printLog("HTTP Server said : Connection closed : " + id_conn); break; default: Log.printLog("HTTP Server sent an unexpected response type : " + response.type); break; } // Sleep Thread.sleep(DELAY); // If the connection has been closed if (response.isConnClosed) { // Log //System.out.println("Connexion closed by remote server."); Log.printLog(response.id + " >>"); // Tell the server to destroy the connection /*dataPacket = new DataPacket(); dataPacket.type = Const.CONNECTION_DESTROY; dataPacket.id = id_conn; dataPacket.tab = Const.TAB_EMPTY;*/ // Close the source connection m_source.disconnect(); // Stop the thread state = false; } if (response.type == Const.CONNECTION_DESTROY_OK) { // Close the source connection m_source.disconnect(); // Stop the thread state = false; } if (response.type == Const.CONNECTION_NOT_FOUND) { // Close the source connection m_source.disconnect(); // Stop the thread state = false; } } catch (Exception e) { Log.printLog("Exception : " + e); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -