直接贴代码,不解释

1 主服务,用来侦听端口

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

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

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

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

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

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

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

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

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

Java实现Socket5代理服务器的更多相关文章

  1. Java socket - 使用代理服务器

    为什么使用代理服务器不需要多说了. 使用Proxy Java提供了Proxy类实现使用代理进行通信. Proxy类的构造器Proxy(Proxy.Type type, SocketAddress sa ...

  2. Java实现sock5代理服务器

    入职练手socks5代理服务器,过程总结一下. 1.下载火狐浏览器,设定代理为socks5代理,地址为127.0.0.1:1080. 2.socks5协议1928,中文版,原版,认真阅读 3.按照协议 ...

  3. CentOS搭建socket5代理服务器

    1.安装socket5依赖包 yum -y install gcc automake make pam-devel openldap-devel cyrus-sasl-devel   2.下载ss5并 ...

  4. 用Java开发代理服务器

    基础知识 不管以哪种方式应用代理服务器,其监控HTTP传输的过程总是如下: 步骤一:内部的浏览器发送请求给代理服务器.请求的第一行包含了目标URL. 步骤二:代理服务器读取该URL,并把请求转发给合适 ...

  5. JAVA上百实例源码以及开源项目

    简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...

  6. Java程序通过代理访问网络

    问题背景 最近工作上有开发爬虫的任务,对目标网站数据进行抓取,由于大部分网站都在国外,无法直接访问,需要通过代理才能登录.爬虫部署的服务器在香港,所以爬虫部署到服务器后,是可以访问目标网站的,但本地开 ...

  7. JAVA上百实例源码网站

    JAVA源码包1JAVA源码包2JAVA源码包3JAVA源码包4 JAVA开源包1 JAVA开源包2 JAVA开源包3 JAVA开源包4 JAVA开源包5 JAVA开源包6 JAVA开源包7 JAVA ...

  8. tit.Atitit. http 代理原理  atiHttpProxy  大木马 h

    Atitit. http 代理原理  atiHttpProxy  大木马 1. 面这张图可以清晰地阐明HttpProxy的实现原理:1 2. 代理服务器用途1 3. 其中流程具体如下:2 4. 设计规 ...

  9. Atitit. http 代理原理  atiHttpProxy  大木马

    Atitit. http 代理原理  atiHttpProxy  大木马 1. 面这张图可以清晰地阐明HttpProxy的实现原理:1 2. 代理服务器用途1 3. 其中流程具体如下:2 4. 设计规 ...

随机推荐

  1. LINUX信息安全系统设计基础第一周学习总结

     Linux系统简介 一.实验内容 了解 Linux 的历史,Linux 与 Windows 的区别等入门知识. 二.实验要求 阅读linux简介与历史 三.实验步骤 二.Linux 与 Window ...

  2. HTTP之手机抓包工具篇

    简介 现在手机移动互联网时代 手机app 运用 如日冲天.自然手机app的问题排除也是头疼,明明自己测试 上线的接口正常 到了手机app就不行.怎么办呢?别急,现在有好多手机抓包工具啦! 1. Cha ...

  3. .NET 关键字

    一.base关键字 可以通过base关键字访问上一级父类方法的访问.静态static函数无法调用base 二.new 关键字new new有2个作用. new运算符   用来分配内存空间和初始化对象. ...

  4. 【BZOJ1006】【HNOI2008】神奇的国度(弦图染色)

    1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1467  Solved: 603[Submit][Stat ...

  5. UItableView的编辑--删除移动cell

    // // RootViewController.m // UI__TableView的编辑 // // Created by dllo on 16/3/17. // Copyright © 2016 ...

  6. js回掉页面后台代码-简单demo

    后台代码: public partial class WebForm1 : System.Web.UI.Page, ICallbackEventHandler { protected void Pag ...

  7. Beta冲刺阶段

    Beta冲刺阶段 现阶段工作安排以及问题解决 Struts2框架配置 网上下载Struts 2 的框架代码,按照书上教程进行配置 遇到的问题:书上配置过程和实际操作有出入,按照书上过程无法完成配置过程 ...

  8. 全局唯一标识符(GUID)

    全局唯一标识符,简称GUID(发音为/ˈɡuːɪd/或/ˈɡwɪd/),是一种由算法生成的唯一标识,通常表示成32个16进制数字(0-9,A-F)组成的字符串,如:{21EC2020-3AEA-106 ...

  9. Apache MINA(一)

    Apache MINA is a network application framework which helps users develop high performance and high s ...

  10. 【HDU 2604】Queuing

    题 题意 f和m两种字母组成字符串,fmf 和 fff 这种为不安全的字符串,现在有2*L个字母,问你有多少安全的字符串.答案mod M. 分析 递推,这题本意是要用矩阵快速幂.不过我发现这题好神奇, ...