MCPcopy Index your code
hub / github.com/Q16G/memory-shell / Socks5Server

Class Socks5Server

src/main/java/org/example/Socks5Server.java:12–288  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10import java.nio.channels.SocketChannel;
11
12public class Socks5Server {
13 private static final byte SOCKS_VERSION = 0x05;
14 private static final byte NO_AUTHENTICATION = 0x00;
15 private static final byte CONNECT_COMMAND = 0x01;
16 private static final byte IPV4_ADDRESS = 0x01;
17 private static final byte DOMAIN_NAME = 0x03;
18 private static final byte IPV6_ADDRESS = 0x04;
19
20 private SocketWrapperBase<?> socketWrapper;
21 private ByteBuffer readBuffer;
22 private int peekPosition;
23
24 public void handleClient(SocketChannel channel, SocketWrapperBase<?> wrapper) throws IOException {
25 this.socketWrapper = wrapper;
26 this.readBuffer = wrapper.getSocketBufferHandler().getReadBuffer();
27 if (channel == null) {
28 throw new IOException("不支持的socket类型");
29 }
30
31 try {
32 // 处理认证方法协商
33 if (!handleAuthenticationNegotiation(channel)) {
34 return;
35 }
36
37 // 处理连接请求
38 if (!handleConnectionRequest(channel)) {
39 return;
40 }
41
42 // 开始转发数据
43 handleDataTransfer(channel);
44 } catch (Exception exception) {
45 throw exception;
46 } finally {
47 try {
48 channel.close();
49 } catch (IOException e) {
50 // 忽略关闭时的异常
51 }
52 }
53 }
54
55 private boolean handleAuthenticationNegotiation(SocketChannel channel) throws IOException {
56 // 读取版本和方法数量
57 ByteBuffer buffer = ByteBuffer.allocate(2);
58 if (!peekRead(buffer, 2)) {
59 return false;
60 }
61 buffer.flip();
62
63 byte version = buffer.get();
64 if (version != SOCKS_VERSION) {
65 return false;
66 }
67
68 int numMethods = buffer.get() & 0xFF;
69

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected