Java实现Socket5代理服务器

直接贴代码,不解释

1 主服务,用来侦听端口

 

[java] view plaincopy
 
  1. package org.javaren.proxy;  
  2.   
  3. import java.net.ServerSocket;  
  4. import java.net.Socket;  
  5.   
  6. public class SocketProxy {  
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) throws Exception {  
  11.         ServerSocket serverSocket = new ServerSocket(8888);  
  12.         while (true) {  
  13.             Socket socket = null;  
  14.             try {  
  15.                 socket = serverSocket.accept();  
  16.                 new SocketThread(socket).start();  
  17.             } catch (Exception e) {  
  18.                 e.printStackTrace();  
  19.             }  
  20.         }  
  21.     }  
  22. }  


2 核心代码,处理链接的代理线程

 

内部设计了Socket的认证,自己看吧

 

[java] view plaincopy
 
  1. package org.javaren.proxy;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6. import java.net.Socket;  
  7.   
  8. public class SocketThread extends Thread {  
  9.     private Socket socketIn;  
  10.     private InputStream isIn;  
  11.     private OutputStream osIn;  
  12.     //  
  13.     private Socket socketOut;  
  14.     private InputStream isOut;  
  15.     private OutputStream osOut;  
  16.   
  17.     public SocketThread(Socket socket) {  
  18.         this.socketIn = socket;  
  19.     }  
  20.   
  21.     private byte[] buffer = new byte[4096];  
  22.     private static final byte[] VER = { 0x5, 0x0 };  
  23.     private static final byte[] CONNECT_OK = { 0x5, 0x0, 0x0, 0x1, 0, 0, 0, 0, 0, 0 };  
  24.   
  25.     public void run() {  
  26.         try {  
  27.             System.out.println("\n\na client connect " + socketIn.getInetAddress() + ":" + socketIn.getPort());  
  28.             isIn = socketIn.getInputStream();  
  29.             osIn = socketIn.getOutputStream();  
  30.             int len = isIn.read(buffer);  
  31.             System.out.println("< " + bytesToHexString(buffer, 0, len));  
  32.             osIn.write(VER);  
  33.             osIn.flush();  
  34.             System.out.println("> " + bytesToHexString(VER, 0, VER.length));  
  35.             len = isIn.read(buffer);  
  36.             System.out.println("< " + bytesToHexString(buffer, 0, len));  
  37.             // 查找主机和端口  
  38.             String host = findHost(buffer, 4, 7);  
  39.             int port = findPort(buffer, 8, 9);  
  40.             System.out.println("host=" + host + ",port=" + port);  
  41.             socketOut = new Socket(host, port);  
  42.             isOut = socketOut.getInputStream();  
  43.             osOut = socketOut.getOutputStream();  
  44.             //  
  45.             for (int i = 4; i <= 9; i++) {  
  46.                 CONNECT_OK[i] = buffer[i];  
  47.             }  
  48.             osIn.write(CONNECT_OK);  
  49.             osIn.flush();  
  50.             System.out.println("> " + bytesToHexString(CONNECT_OK, 0, CONNECT_OK.length));  
  51.             SocketThreadOutput out = new SocketThreadOutput(isIn, osOut);  
  52.             out.start();  
  53.             SocketThreadInput in = new SocketThreadInput(isOut, osIn);  
  54.             in.start();  
  55.             out.join();  
  56.             in.join();  
  57.         } catch (Exception e) {  
  58.             System.out.println("a client leave");  
  59.         } finally {  
  60.             try {  
  61.                 if (socketIn != null) {  
  62.                     socketIn.close();  
  63.                 }  
  64.             } catch (IOException e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.         }  
  68.         System.out.println("socket close");  
  69.     }  
  70.   
  71.     public static String findHost(byte[] bArray, int begin, int end) {  
  72.         StringBuffer sb = new StringBuffer();  
  73.         for (int i = begin; i <= end; i++) {  
  74.             sb.append(Integer.toString(0xFF & bArray[i]));  
  75.             sb.append(".");  
  76.         }  
  77.         sb.deleteCharAt(sb.length() - 1);  
  78.         return sb.toString();  
  79.     }  
  80.   
  81.     public static int findPort(byte[] bArray, int begin, int end) {  
  82.         int port = 0;  
  83.         for (int i = begin; i <= end; i++) {  
  84.             port <<= 16;  
  85.             port += bArray[i];  
  86.         }  
  87.         return port;  
  88.     }  
  89.   
  90.     // 4A 7D EB 69  
  91.     // 74 125 235 105  
  92.     public static final String bytesToHexString(byte[] bArray, int begin, int end) {  
  93.         StringBuffer sb = new StringBuffer(bArray.length);  
  94.         String sTemp;  
  95.         for (int i = begin; i < end; i++) {  
  96.             sTemp = Integer.toHexString(0xFF & bArray[i]);  
  97.             if (sTemp.length() < 2)  
  98.                 sb.append(0);  
  99.             sb.append(sTemp.toUpperCase());  
  100.             sb.append(" ");  
  101.         }  
  102.         return sb.toString();  
  103.     }  
  104. }  



 

3  读取线程,负责外面读数据,写入到请求端

 

[java] view plaincopy
 
  1. package org.javaren.proxy;  
  2.   
  3. /** 
  4.  * * 从外部读取,向内部发送信息 
  5.  */  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.   
  9. public class SocketThreadInput extends Thread {  
  10.     private InputStream isOut;  
  11.     private OutputStream osIn;  
  12.   
  13.     public SocketThreadInput(InputStream isOut, OutputStream osIn) {  
  14.         this.isOut = isOut;  
  15.         this.osIn = osIn;  
  16.     }  
  17.   
  18.     private byte[] buffer = new byte[409600];  
  19.   
  20.     public void run() {  
  21.         try {  
  22.             int len;  
  23.             while ((len = isOut.read(buffer)) != -1) {  
  24.                 if (len > 0) {  
  25.                     System.out.println(new String(buffer, 0, len));  
  26.                     osIn.write(buffer, 0, len);  
  27.                     osIn.flush();  
  28.                 }  
  29.             }  
  30.         } catch (Exception e) {  
  31.             System.out.println("SocketThreadInput leave");  
  32.         }  
  33.     }  
  34. }  


4 写入线程,负责读取请求端数据,写入到目标端

 

 

[java] view plaincopy
 
  1. package org.javaren.proxy;  
  2.   
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5.   
  6. /** 
  7.  * 从内部读取,向外部发送信息 
  8.  *  
  9.  * @author zxq 
  10.  *  
  11.  */  
  12. public class SocketThreadOutput extends Thread {  
  13.     private InputStream isIn;  
  14.     private OutputStream osOut;  
  15.   
  16.     public SocketThreadOutput(InputStream isIn, OutputStream osOut) {  
  17.         this.isIn = isIn;  
  18.         this.osOut = osOut;  
  19.     }  
  20.   
  21.     private byte[] buffer = new byte[409600];  
  22.   
  23.     public void run() {  
  24.         try {  
  25.             int len;  
  26.             while ((len = isIn.read(buffer)) != -1) {  
  27.                 if (len > 0) {  
  28.                     System.out.println(new String(buffer, 0, len));  
  29.                     osOut.write(buffer, 0, len);  
  30.                     osOut.flush();  
  31.                 }  
  32.             }  
  33.         } catch (Exception e) {  
  34.             System.out.println("SocketThreadOutput leave");  
  35.         }  
  36.     }  
  37. }  

 

 

 

原文:http://blog.csdn.net/java2000_net/article/details/7826660

posted @ 2015-02-10 12:33  jack_ou  阅读(3183)  评论(0编辑  收藏  举报