jljiu commited on
Commit
19deb34
·
verified ·
1 Parent(s): 5b7fb1d

Update proxy/main.ts

Browse files
Files changed (1) hide show
  1. proxy/main.ts +85 -6
proxy/main.ts CHANGED
@@ -72,24 +72,103 @@ async function handleConnection(conn: Deno.Conn) {
72
 
73
  console.log(`有效连接来自: ${clientIP}`);
74
 
75
- // 原有的请求处理逻辑...
76
  if (buf[0] === 0x05) {
77
- // SOCKS5 处理逻辑
78
  await conn.write(new Uint8Array([0x05, 0x00]));
79
 
 
80
  const n2 = await conn.read(buf);
81
  if (!n2) return;
82
 
83
  const cmd = buf[1];
84
- if (cmd !== 0x01) {
 
 
85
  await conn.write(new Uint8Array([0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0]));
86
  return;
87
  }
88
 
89
- // ... 其余 SOCKS5 处理代码 ...
90
- } else if (new TextDecoder().decode(buf.subarray(0, n as number)).includes('HTTP/')) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  // HTTP 处理逻辑
92
- const firstLine = new TextDecoder().decode(buf.subarray(0, n as number)).split('\n')[0];
93
 
94
  if (firstLine.includes('/config')) {
95
  const spaceDomain = "jljiu-proxy.hf.space";
 
72
 
73
  console.log(`有效连接来自: ${clientIP}`);
74
 
75
+ // SOCKS5 处理逻辑
76
  if (buf[0] === 0x05) {
77
+ // 发送认证方法响应
78
  await conn.write(new Uint8Array([0x05, 0x00]));
79
 
80
+ // 读取连接请求
81
  const n2 = await conn.read(buf);
82
  if (!n2) return;
83
 
84
  const cmd = buf[1];
85
+ const atyp = buf[3];
86
+
87
+ if (cmd !== 0x01) { // 仅支持 CONNECT 命令
88
  await conn.write(new Uint8Array([0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0]));
89
  return;
90
  }
91
 
92
+ // 解析目标地址和端口
93
+ let targetHost = "";
94
+ let addrLen = 0;
95
+ let targetPort = 0;
96
+
97
+ switch (atyp) {
98
+ case 0x01: // IPv4
99
+ targetHost = Array.from(buf.slice(4, 8)).join(".");
100
+ addrLen = 4;
101
+ break;
102
+ case 0x03: // Domain
103
+ addrLen = buf[4];
104
+ targetHost = new TextDecoder().decode(buf.slice(5, 5 + addrLen));
105
+ break;
106
+ case 0x04: // IPv6
107
+ targetHost = Array.from(buf.slice(4, 20))
108
+ .map(x => x.toString(16).padStart(2, '0'))
109
+ .join(":");
110
+ addrLen = 16;
111
+ break;
112
+ }
113
+
114
+ targetPort = (buf[addrLen + (atyp === 0x03 ? 5 : 4)] << 8) |
115
+ buf[addrLen + (atyp === 0x03 ? 6 : 5)];
116
+
117
+ console.log(`尝试连接到: ${targetHost}:${targetPort}`);
118
+
119
+ try {
120
+ // 连接目标服务器
121
+ const target = await Deno.connect({
122
+ hostname: targetHost,
123
+ port: targetPort,
124
+ });
125
+
126
+ // 发送连接成功响应
127
+ await conn.write(new Uint8Array([
128
+ 0x05, 0x00, 0x00, 0x01,
129
+ 0, 0, 0, 0, // 绑定地址
130
+ 0, 0 // 绑定端口
131
+ ]));
132
+
133
+ console.log(`成功连接到: ${targetHost}:${targetPort}`);
134
+
135
+ // 双向转发数据
136
+ await Promise.all([
137
+ (async () => {
138
+ const buf = new Uint8Array(32 * 1024);
139
+ try {
140
+ while (true) {
141
+ const n = await conn.read(buf);
142
+ if (!n) break;
143
+ await target.write(buf.subarray(0, n));
144
+ }
145
+ } catch (e) {
146
+ // 忽略连接关闭错误
147
+ }
148
+ })(),
149
+ (async () => {
150
+ const buf = new Uint8Array(32 * 1024);
151
+ try {
152
+ while (true) {
153
+ const n = await target.read(buf);
154
+ if (!n) break;
155
+ await conn.write(buf.subarray(0, n));
156
+ }
157
+ } catch (e) {
158
+ // 忽略连接关闭错误
159
+ }
160
+ })()
161
+ ]);
162
+
163
+ target.close();
164
+ } catch (e) {
165
+ console.error(`连接失败 ${targetHost}:${targetPort}:`, e.message);
166
+ // 发送连接失败响应
167
+ await conn.write(new Uint8Array([0x05, 0x04, 0x00, 0x01, 0, 0, 0, 0, 0, 0]));
168
+ }
169
+ } else if (new TextDecoder().decode(buf.subarray(0, n)).includes('HTTP/')) {
170
  // HTTP 处理逻辑
171
+ const firstLine = new TextDecoder().decode(buf.subarray(0, n)).split('\n')[0];
172
 
173
  if (firstLine.includes('/config')) {
174
  const spaceDomain = "jljiu-proxy.hf.space";