直接贴代码,不解释

1 主服务,用来侦听端口

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 核心代码,处理链接的代理线程

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

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, 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("< " + bytesToHexString(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));// 查找主机和端口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.getInputStream();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 (Exception 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 105public 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  读取线程,负责外面读数据,写入到请求端

package org.javaren.proxy;/*** * 从外部读取,向内部发送信息*/
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.read(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 写入线程,负责读取请求端数据,写入到目标端

package org.javaren.proxy;import java.io.InputStream;
import java.io.OutputStream;/*** 从内部读取,向外部发送信息* * @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");}}
}

效果还不错,用firefox/ ie都测试过,可用。

Java实现Socket5代理服务器相关推荐

  1. 用Java开发HTTP代理服务器

    HTTP代理服务器是一种网络应用,它充当位于客户端和目标服务器之间的中间节点,将客户端发出的HTTP请求转发给目标服务器,并将目标服务器返回的HTTP响应内容回传给客户端.通过使用代理服务器,客户端可 ...

  2. Java实现Http代理服务器通过http代理进行内网安装yum软件

    Java实现Http代理服务器&通过http代理进行内网安装yum软件 1.Http代理服务器简介 2.Http代理服务器Java实现 2.1 Java源码 2.2 代码分析说明 3.通过ht ...

  3. 配置socket5代理服务器

    配置socket5代理服务器 1. 安装dante-server apt-get install dante-server 2. 创建代理用户 2.1 创建组 groupadd proxy 2.2 创 ...

  4. Ubuntu配置socket5代理服务器

    配置socket5代理服务器 1. 安装dante-server apt-get install dante-server 2. 创建代理用户 2.1 创建组 groupadd proxy 2.2 创 ...

  5. JAVA写HTTP代理服务器(三)-https明文捕获

    上一篇用netty实现的http代理服务器还无法对https报文进行解密,原因也说了,就是服务器的私钥不在我们这,根据RSA公钥加密私钥解密的特性,如果我们没有私钥的话是不可能获取到https的真实内 ...

  6. Java后端通过代理服务器请求网络资源

    1.需求 部署在内网服务器的JavaWeb项目,后端需要访问互联网,通过有网络访问权限的另一台服务器的代理转发实现互联网访问. 2.实现 内网设备--设为A,通过外网设备--设为B,访问互联网,需要分 ...

  7. Java实现sock5代理服务器

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

  8. JAVA写HTTP代理服务器(一)-socket实现

    HTTP代理服务器是一种特殊的网络服务,允许一个网络终端(一般为客户端)通过这个服务与另一个网络终端(一般为服务器)进行非直接的连接.一些网关.路由器等网络设备具备网络代理功能.一般认为代理服务有利于 ...

  9. 【Linux服务】socket5代理服务器搭建

    如果在系统中需要搭建socket代理服务,可以参照如下脚本: 1.将账号密码替换成自己想要的设置 2.将"端口"替换成需要的端口号 3.运行脚本,即可完成安装 4.可通过netst ...

最新文章

  1. iOS Masonry的使用详解
  2. 站长之家html视频播放,HTML5视频发展状况
  3. 如何让 Flutter 应用更好地使用 SVG?
  4. 修改linux默认启动级别(包括Ubuntu)
  5. 微软的报表工具 SQL Server 2000 Reporting Services 评估版
  6. 前端技术栈---Vue(1)安装与初始化
  7. 2000条你应知的WPF小姿势 基础篇40-44 启动关闭,Xaml,逻辑树
  8. ENVI4.8下载与安装步骤
  9. java wsimport 调用_Java如何基于wsimport调用wcf接口
  10. PC-DARTS 网络结构搜索程序分析
  11. 163邮箱登录界面在哪里?如何登陆163邮箱呢?邮箱163格式怎么写
  12. 机器人设计的步骤有哪些
  13. 第十一章 枚举与泛型总结
  14. 科技爱好者周刊(第 118 期):高考志愿怎么填
  15. 自学c语言从哪开始,C语言从何开始学习
  16. 寒江独钓-Windows内核安全编程笔记-第4章代码
  17. 龙芯笔记本走出国门的困惑
  18. 可兼容H27U1G8F2CTR的物料AFND1G08U3-CKA
  19. 二分答案(超级详细)
  20. 十次方中的前端知识点随记

热门文章

  1. EEPROM,NOR Flash,NAND Flash,eMMC,UFS,SSD分别和主要参数及特性
  2. linux 跳转到文件夹,一步到位,linux下查找并跳转文件夹
  3. 学习笔记(一)MATLAB-SIMULINK-SIMSCAPE(电器库)
  4. 全连接网络手写数字识别(极详细,互助)
  5. angular js 页面添加数据保存数据库
  6. OMG Partical Practice
  7. 品牌定位的十五种法则
  8. 最优线路c语言课程设计,数据结构课程设计(五)---行车路线
  9. 惠普财报:营收三连降,昔日IT巨头无力回春?
  10. matlab利用经纬度计算距离,MATLAB函数可计算两个坐标(纬度和经度)之间的距离...