Java实现Socket5代理服务器
直接贴代码,不解释
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 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 读取线程,负责外面读数据,写入到请求端
- 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");
- }
- }
- }
原文:http://blog.csdn.net/java2000_net/article/details/7826660
Java实现Socket5代理服务器的更多相关文章
- Java socket - 使用代理服务器
为什么使用代理服务器不需要多说了. 使用Proxy Java提供了Proxy类实现使用代理进行通信. Proxy类的构造器Proxy(Proxy.Type type, SocketAddress sa ...
- Java实现sock5代理服务器
入职练手socks5代理服务器,过程总结一下. 1.下载火狐浏览器,设定代理为socks5代理,地址为127.0.0.1:1080. 2.socks5协议1928,中文版,原版,认真阅读 3.按照协议 ...
- CentOS搭建socket5代理服务器
1.安装socket5依赖包 yum -y install gcc automake make pam-devel openldap-devel cyrus-sasl-devel 2.下载ss5并 ...
- 用Java开发代理服务器
基础知识 不管以哪种方式应用代理服务器,其监控HTTP传输的过程总是如下: 步骤一:内部的浏览器发送请求给代理服务器.请求的第一行包含了目标URL. 步骤二:代理服务器读取该URL,并把请求转发给合适 ...
- JAVA上百实例源码以及开源项目
简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...
- Java程序通过代理访问网络
问题背景 最近工作上有开发爬虫的任务,对目标网站数据进行抓取,由于大部分网站都在国外,无法直接访问,需要通过代理才能登录.爬虫部署的服务器在香港,所以爬虫部署到服务器后,是可以访问目标网站的,但本地开 ...
- JAVA上百实例源码网站
JAVA源码包1JAVA源码包2JAVA源码包3JAVA源码包4 JAVA开源包1 JAVA开源包2 JAVA开源包3 JAVA开源包4 JAVA开源包5 JAVA开源包6 JAVA开源包7 JAVA ...
- tit.Atitit. http 代理原理 atiHttpProxy 大木马 h
Atitit. http 代理原理 atiHttpProxy 大木马 1. 面这张图可以清晰地阐明HttpProxy的实现原理:1 2. 代理服务器用途1 3. 其中流程具体如下:2 4. 设计规 ...
- Atitit. http 代理原理 atiHttpProxy 大木马
Atitit. http 代理原理 atiHttpProxy 大木马 1. 面这张图可以清晰地阐明HttpProxy的实现原理:1 2. 代理服务器用途1 3. 其中流程具体如下:2 4. 设计规 ...
随机推荐
- ubuntu16.04安装eclipse
1.下载jdk , jdk-8u77-linux-x64.tar.gz 2.下载 eclipse, eclipse-jee-mars-2-linux-gtk-x86_64.tar.gz 注:我下载的都 ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- 百度地图ip定位,不算bug的bug
做为一个入行不足两年的菜鸟,能在博客园写下第一篇博客,是需要多大的勇气啊.主要还是怕大神们喷啊.其次自己文笔实在太差了. 哈哈~还请各位大神,口下留情啊. 首先说下我的需求:一个需要城市分站的手机站. ...
- 对于RegExp反向引用的一点理解
置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<关于js的Array.prototype.slice.call> 作者主页:myvin 博主QQ:85 ...
- 魅蓝Note2 在Android Studio 与 Eclipse中无法被检测到
昨天到手的Note2 结果发现测试不了,一看魅蓝的版本是android 5.1,然后更新的自己的SDK. 最后…… 仍然不能识别到手机. ———————————— 今天在stackoverflow上搜 ...
- [BZOJ 1483][HNOI 2009]梦幻补丁(有序表启发式合并)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1483 分析: 先将不同的颜色的出现位置从小到大用几条链表串起来,然后统计一下答案 对于 ...
- 【The final】软件工程实践总结
软件工程就这么告一段落了,竟然有那么一丢丢的舍不得-- 一.为拖延找的种种借口 [首先声明]以下纯粹是个人吐槽,仅作记录以便日后自己可以回顾一下往昔罢了,可以直接忽略,跳到第二大点:我的拖延之 ...
- Git学习笔记——一个NB的分布式版本控制系统
1. 命令: git init 创建新仓库 (在一个空文件下然后执行命令) git clone + 路径 检出仓库,从本地或从服务器上 git status 查 ...
- SQLHelper---赵晓虎(简洁,全面)
public static class SQLHelper { //获取连接字符串,,首先添加对configuration的引用 private static string connStr = Con ...
- NOIP 2014 Day1 T3飞扬的小鸟
题目描述:http://codevs.cn/problem/3729/ 表示各种shabi的我编和调了半天,思路大体就是一个完全背包的模型,不过会多一些额外的转移(因为题目限制高度之类的),不过值得注 ...