java实现一个socks5代理 <一>了解nio Selector的基本用法

本文介绍了如何使用Java NIO和Selector来实现一个SOCKS5代理,详细阐述了从创建ServiceSocketChannel监听端口,处理客户端连接,到数据传输的全过程,适合对NIO有一定了解的读者深入学习。
摘要由CSDN通过智能技术生成

上面浏览器可以放心的将数据交给代理了,接下来做的是怎样处理这些数据,以及面对浏览器突然过来的很多连接如何处理他们,这就要用到javax.nio包下的东西了,下面做简单介绍,不会nio可以先去学习下nio selector的基本用法,再回来接着看,下面先讲思路再上代码

1.创建ServiceSocketChannel,监听在5661端口。

2.像selector注册accept事件

3.堵塞式的从selector选择,选择后就开始执行

4.允许所有的连接,对获取到的连接都发送 5 0 表示接受无密码的连接

5.接受到ip port 的信息后,ip port 解析出来,异步连接到此ip port 并注册 connection事件

6。当连接事件触发后,完成连接,像浏览器返回 连接成功的标识。

7.像selector注册两个监听,分别监听client 和 外网 的socketchannel连接 read事件

8.当监听到其中一方的数据就读取并将数据写入另一方的channel中。

9.当读到流的末尾,管理连接,注销掉channel 等。

10.socks4的握手方式和socks5不同其他都相同,所以可以共用大部分代码

package com.proxydemo;
import java.io.IOException;
public class Main {
	//程序启动入口
	public static void main(String[] args) throws IOException {
		Reactor reactor=new Reactor();
		reactor.run();
	}
}

package com.proxydemo;
/*
容器类,selector的创建 以及做选择 并且执行的类
*/
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.util.Set;

//容器类
public class Reactor implements Runnable {
	private Selector selector;	//容器
	private ServerSocketChannel ssc;	//服务端socketchannel
	
	public Reactor() throws IOException{
	//初始化容器和服务器
		selector=Selector.open();
		ssc=ServerSocketChannel.open();
	//绑定事件 和 端口 设置异步
		ssc.bind(new InetSocketAddress(5661));
		ssc.configureBlocking(false);
		SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
		//创建accept事件,以及创建处理的线程
		key.attach(new AcceptAcceptor(selector,ssc));

	}
	
	public void run() {
		while(true){
			try {
				int size = selector.select();
				if(size==0){
					continue;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			Set<SelectionKey> keys = selector.selectedKeys();
			Iterator<SelectionKey> iterator = keys.iterator();
			while(iterator.hasNext()){
				SelectionKey next = iterator.next();
				consume(next);
				iterator.remove();
			}
		}
		
	}
	
	private void consume(SelectionKey key){
		Runnable r = (Runnable) key.attachment();
		r.run();
	}
	

}


package com.proxydemo;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;


//负责接受连接的类,接受完立即注册   读操作 读操作出发 Handler551
public class AcceptAcceptor implements Runnable {
	private Selector selector;
	private ServerSocketChannel ssc;
	
	public AcceptAcceptor(Selector selector,ServerSocketChannel ssc){
		this.selector=selector;
		this.ssc=ssc;
	}
	
	
	public void run() {
		try {
			SocketChannel socket = ssc.accept();
			socket.configureBlocking(false);
			SelectionKey keyclient = socket.register(selector, SelectionKey.OP_READ);
			keyclient.attach(new Handler551(keyclient));		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		
	}

}


package com.proxydemo;

import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Arrays;


/*出发了读操作触发此事件 目的读取 5 1 0 /或者4 1 0 
 * 出发写操作的事件,目的写入 5 0
 * 根据事件的不同做出不同行为

                
可以使用Java Socket类来实现一个简单的Socks5代理。以下是一个示例码: ``` import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Socks5Proxy { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(1080); System.out.println("Socks5 proxy started on port 1080"); while (true) { Socket clientSocket = serverSocket.accept(); System.out.println("Accepted connection from " + clientSocket.getInetAddress()); new Thread(() -> { try { InputStream clientIn = clientSocket.getInputStream(); OutputStream clientOut = clientSocket.getOutputStream(); // Read the Socks5 handshake request byte[] buf = new byte[1024]; int len = clientIn.read(buf); if (buf[0] != 0x05) { throw new IOException("Invalid Socks5 handshake request"); } // Send the Socks5 handshake response clientOut.write(new byte[] { 0x05, 0x00 }); // Read the Socks5 request len = clientIn.read(buf); if (buf[0] != 0x05 || buf[1] != 0x01 || buf[2] != 0x00) { throw new IOException("Invalid Socks5 request"); } // Parse the Socks5 request int port = ((buf[len - 2] & 0xff) << 8) | (buf[len - 1] & 0xff); String host = null; switch (buf[3]) { case 0x01: host = String.format("%d.%d.%d.%d", buf[4], buf[5], buf[6], buf[7]); break; case 0x03: int hostLen = buf[4]; host = new String(buf, 5, hostLen); break; case 0x04: host = String.format("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15], buf[16], buf[17], buf[18], buf[19]); break; default: throw new IOException("Invalid Socks5 request"); } System.out.println("Connecting to " + host + ":" + port); // Connect to the destination server Socket serverSocket = new Socket(host, port); InputStream serverIn = serverSocket.getInputStream(); OutputStream serverOut = serverSocket.getOutputStream(); // Send the Socks5 response byte[] response = new byte[len]; System.arraycopy(buf, 0, response, 0, len); response[1] = 0x00; clientOut.write(response); // Start forwarding data between client and server new Thread(() -> { try { byte[] buf2 = new byte[1024]; int len2; while ((len2 = serverIn.read(buf2)) != -1) { clientOut.write(buf2, 0, len2); } } catch (IOException e) { e.printStackTrace(); } }).start(); byte[] buf3 = new byte[1024]; int len3; while ((len3 = clientIn.read(buf3)) != -1) { serverOut.write(buf3, 0, len3); } } catch (IOException e) { e.printStackTrace(); } }).start(); } } } ``` 这个代理服务器监听1080端口,当有客户端连接时,它会读取Socks5握手请求,然后发送Socks5握手响应。接下来,它会读取Socks5请求,解析出目标主机和端口,然后连接到目标服务器。一旦连接建立,它会发送Socks5响应,然后开始转发数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值