Directly paste the code without explanation
1 main service, used to listen on port
package org.javaren.proxy;import java.net.ServerSocket;import java.net.Socket;public class SocketProxy {/** * @param args */public static void main(String[] args) throws Exception {ServerSocket serverSocket = new ServerSocket(8888);while (true) {Socket socket = null;try {socket = serverSocket.accept();new SocketThread(socket).start();} catch (Exception e) {e.printStackTrace();}}}}
2. The core code processes the linked proxy thread.
Socket authentication is designed internally. Let's take a look.
Package Org. javaren. proxy; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import java.net. socket; public class socketthread extends thread {private socket socketin; private inputstream isin; private outputstream osin; // Private socket socketout; private inputstream isout; private outputstream osout; Public socketthread (Socket socket) {This. socketin = socket;} private byte [] Buffer = new byte [4096]; Private Static final byte [] ver = {0x5, 0x0}; Private Static final byte [] connect_ OK = {0x5, 0x0, 0x0, 0x1, 0, 0, 0, 0, 0}; Public void run () {try {system. out. println ("\ n \ na client connect" + socketin. getinetaddress () + ":" + socketin. getport (); ISIN = socketin. getinputstream (); osin = socketin. getoutputstream (); int Len = isin. read (buffer); system. out. println ("<" + B Ytestohexstring (buffer, 0, Len); osin. write (VER); osin. flush (); system. out. println (">" + bytestohexstring (ver, 0, Ver. length); Len = isin. read (buffer); system. out. println ("<" + bytestohexstring (buffer, 0, Len); // find the host and port string host = findhost (buffer, 4, 7 ); int Port = findport (buffer, 8, 9); system. out. println ("host =" + host + ", Port =" + port); socketout = new socket (host, Port); isout = socketout. get Inputstream (); osout = socketout. getoutputstream (); // For (INT I = 4; I <= 9; I ++) {connect_ OK [I] = buffer [I];} osin. write (connect_ OK); osin. flush (); system. out. println (">" + bytestohexstring (connect_ OK, 0, connect_ OK .length); socketthreadoutput out = new socketthreadoutput (ISIN, osout); out. start (); socketthreadinput in = new socketthreadinput (isout, osin); In. start (); out. join (); In. join ();} catch (excep Tion E) {system. Out. println ("a client leave");} finally {try {If (socketin! = NULL) {socketin. close () ;}} catch (ioexception e) {e. printstacktrace () ;}} system. out. println ("socket close");} public static string findhost (byte [] barray, int begin, int end) {stringbuffer sb = new stringbuffer (); for (INT I = begin; I <= end; I ++) {sb. append (integer. tostring (0xff & barray [I]); sb. append (". ");} sb. deletecharat (sb. length ()-1); return sb. tostring ();} public static int findport (byte [] barray, int begin, int end) {int Port = 0; For (INT I = begin; I <= end; I ++) {port <= 16; port + = barray [I];} return port ;} // 4A 7d EB 69 // 74 125 235 105 public static final string bytestohexstring (byte [] barray, int begin, int end) {stringbuffer sb = new stringbuffer (barray. length); string stemp; For (INT I = begin; I <end; I ++) {stemp = integer. tohexstring (0xff & barray [I]); If (stemp. length () <2) Sb. append (0); sb. append (stemp. touppercase (); sb. append ("");} return sb. tostring ();}}
3. The reading thread reads data from the outside and writes the data to the requester.
Package Org. javaren. proxy;/***** read from the outside and send information to the inside */import Java. io. inputstream; import Java. io. outputstream; public class socketthreadinput extends thread {private inputstream isout; private outputstream osin; Public socketthreadinput (inputstream isout, outputstream osin) {This. isout = isout; this. osin = osin;} private byte [] buffer = new byte [409600]; Public void run () {try {int Len; while (LEN = isout. rea D (buffer ))! =-1) {If (LEN> 0) {system. out. println (new string (buffer, 0, Len); osin. write (buffer, 0, Len); osin. flush () ;}} catch (exception e) {system. out. println ("socketthreadinput leave ");}}}
4. The write thread reads data from the requester and writes the data to the target.
Package Org. javaren. proxy; import Java. io. inputstream; import Java. io. outputstream;/*** read from the inside and send information to the outside ** @ author zxq **/public class socketthreadoutput extends thread {private inputstream isin; private outputstream osout; public socketthreadoutput (inputstream isin, outputstream osout) {This. ISIN = isin; this. osout = osout;} private byte [] buffer = new byte [409600]; Public void run () {try {int Len; while (LEN = isin. Read (buffer ))! =-1) {If (LEN> 0) {system. out. println (new string (buffer, 0, Len); osout. write (buffer, 0, Len); osout. flush () ;}} catch (exception e) {system. out. println ("socketthreadoutput leave ");}}}
The results are good. They have been tested and available in Firefox/IE.