Update proxy/main.ts
Browse files- proxy/main.ts +231 -231
proxy/main.ts
CHANGED
|
@@ -1,267 +1,267 @@
|
|
| 1 |
-
const CONFIG = {
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
};
|
| 6 |
|
| 7 |
-
// 添加连接计数和限制
|
| 8 |
-
const connectionTracker = new Map<string, {
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
}>();
|
| 12 |
|
| 13 |
-
// 清理过期的连接记录
|
| 14 |
-
setInterval(() => {
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
}, 60000);
|
| 22 |
|
| 23 |
-
// 检查连接频率
|
| 24 |
-
function checkConnectionLimit(ip: string): boolean {
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
}
|
| 41 |
|
| 42 |
-
async function handleConnection(conn: Deno.Conn) {
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
}
|
| 186 |
|
| 187 |
-
// 生成Clash配置
|
| 188 |
-
function generateClashConfig(spaceDomain: string) {
|
| 189 |
-
|
| 190 |
-
port: 7890
|
| 191 |
-
socks-port: 7891
|
| 192 |
-
mixed-port: 7890
|
| 193 |
-
allow-lan: true
|
| 194 |
-
mode: Rule
|
| 195 |
-
log-level: info
|
| 196 |
-
external-controller: 127.0.0.1:9090
|
| 197 |
|
| 198 |
-
proxies:
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
|
| 205 |
-
proxy-groups:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
|
| 212 |
-
rules:
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
`;
|
| 218 |
-
}
|
| 219 |
|
| 220 |
-
// HTTP服务器处理函数
|
| 221 |
-
async function handleHttp(req: Request): Promise<Response> {
|
| 222 |
-
|
| 223 |
-
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
}
|
| 247 |
|
| 248 |
-
// 启动服务器
|
| 249 |
-
async function main() {
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
}
|
| 264 |
|
| 265 |
-
if (import.meta.main) {
|
| 266 |
-
|
| 267 |
-
}
|
|
|
|
| 1 |
+
// const CONFIG = {
|
| 2 |
+
// port: parseInt(Deno.env.get("PORT") || "7860"),
|
| 3 |
+
// host: "0.0.0.0",
|
| 4 |
+
// timeout: 5000
|
| 5 |
+
// };
|
| 6 |
|
| 7 |
+
// // 添加连接计数和限制
|
| 8 |
+
// const connectionTracker = new Map<string, {
|
| 9 |
+
// count: number,
|
| 10 |
+
// lastTime: number
|
| 11 |
+
// }>();
|
| 12 |
|
| 13 |
+
// // 清理过期的连接记录
|
| 14 |
+
// setInterval(() => {
|
| 15 |
+
// const now = Date.now();
|
| 16 |
+
// for (const [ip, data] of connectionTracker) {
|
| 17 |
+
// if (now - data.lastTime > 60000*1000) { // 60秒后清理
|
| 18 |
+
// connectionTracker.delete(ip);
|
| 19 |
+
// }
|
| 20 |
+
// }
|
| 21 |
+
// }, 60000);
|
| 22 |
|
| 23 |
+
// // 检查连接频率
|
| 24 |
+
// function checkConnectionLimit(ip: string): boolean {
|
| 25 |
+
// const now = Date.now();
|
| 26 |
+
// const data = connectionTracker.get(ip) || { count: 0, lastTime: now };
|
| 27 |
|
| 28 |
+
// // 如果是新的一分钟,重置计数
|
| 29 |
+
// if (now - data.lastTime > 60000) {
|
| 30 |
+
// data.count = 1;
|
| 31 |
+
// data.lastTime = now;
|
| 32 |
+
// } else {
|
| 33 |
+
// data.count++;
|
| 34 |
+
// }
|
| 35 |
|
| 36 |
+
// connectionTracker.set(ip, data);
|
| 37 |
|
| 38 |
+
// // 每分钟允许最多10次连接
|
| 39 |
+
// return data.count <= 10;
|
| 40 |
+
// }
|
| 41 |
|
| 42 |
+
// async function handleConnection(conn: Deno.Conn) {
|
| 43 |
+
// const clientAddr = conn.remoteAddr as Deno.NetAddr;
|
| 44 |
+
// const clientIP = clientAddr.hostname;
|
| 45 |
|
| 46 |
+
// try {
|
| 47 |
+
// const buf = new Uint8Array(256);
|
| 48 |
+
// console.log(`等待读取来自 ${clientIP} 的数据...`);
|
| 49 |
|
| 50 |
+
// const n = await Promise.race([
|
| 51 |
+
// conn.read(buf),
|
| 52 |
+
// new Promise((_, reject) =>
|
| 53 |
+
// setTimeout(() => reject(new Error("读取超时")), 3000)
|
| 54 |
+
// )
|
| 55 |
+
// ]);
|
| 56 |
|
| 57 |
+
// if (!n) {
|
| 58 |
+
// console.log(`空连接来自: ${clientIP}`);
|
| 59 |
+
// return;
|
| 60 |
+
// }
|
| 61 |
|
| 62 |
+
// console.log(`收到数据 ${n} 字节, 首字节: 0x${buf[0].toString(16)}`);
|
| 63 |
|
| 64 |
+
// // SOCKS5 处理逻辑
|
| 65 |
+
// if (buf[0] === 0x05) {
|
| 66 |
+
// console.log("开始处理 SOCKS5 请求");
|
| 67 |
|
| 68 |
+
// // 发送认证方法响应
|
| 69 |
+
// await conn.write(new Uint8Array([0x05, 0x00]));
|
| 70 |
+
// console.log("已发送认证响应");
|
| 71 |
|
| 72 |
+
// // 读取连接请求
|
| 73 |
+
// console.log("等待连接请求...");
|
| 74 |
+
// const n2 = await conn.read(buf);
|
| 75 |
+
// if (!n2) {
|
| 76 |
+
// console.log("未收到连接请求");
|
| 77 |
+
// return;
|
| 78 |
+
// }
|
| 79 |
+
// console.log(`收到连接请求 ${n2} 字节`);
|
| 80 |
|
| 81 |
+
// const cmd = buf[1];
|
| 82 |
+
// const atyp = buf[3];
|
| 83 |
|
| 84 |
+
// console.log(`命令类型: ${cmd}, 地址类型: ${atyp}`);
|
| 85 |
|
| 86 |
+
// if (cmd !== 0x01) {
|
| 87 |
+
// console.log(`不支持的命令类型: ${cmd}`);
|
| 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 |
+
// try {
|
| 98 |
+
// switch (atyp) {
|
| 99 |
+
// case 0x01: // IPv4
|
| 100 |
+
// targetHost = Array.from(buf.slice(4, 8)).join(".");
|
| 101 |
+
// addrLen = 4;
|
| 102 |
+
// break;
|
| 103 |
+
// case 0x03: // Domain
|
| 104 |
+
// addrLen = buf[4];
|
| 105 |
+
// targetHost = new TextDecoder().decode(buf.slice(5, 5 + addrLen));
|
| 106 |
+
// break;
|
| 107 |
+
// case 0x04: // IPv6
|
| 108 |
+
// targetHost = Array.from(buf.slice(4, 20))
|
| 109 |
+
// .map(x => x.toString(16).padStart(2, '0'))
|
| 110 |
+
// .join(":");
|
| 111 |
+
// addrLen = 16;
|
| 112 |
+
// break;
|
| 113 |
+
// }
|
| 114 |
|
| 115 |
+
// targetPort = (buf[addrLen + (atyp === 0x03 ? 5 : 4)] << 8) |
|
| 116 |
+
// buf[addrLen + (atyp === 0x03 ? 6 : 5)];
|
| 117 |
|
| 118 |
+
// console.log(`目标地址: ${targetHost}:${targetPort}`);
|
| 119 |
|
| 120 |
+
// // 连接目标服务器
|
| 121 |
+
// console.log(`尝试连接到 ${targetHost}:${targetPort}`);
|
| 122 |
+
// const target = await Deno.connect({
|
| 123 |
+
// hostname: targetHost,
|
| 124 |
+
// port: targetPort,
|
| 125 |
+
// });
|
| 126 |
+
// console.log("目标连接成功");
|
| 127 |
|
| 128 |
+
// // 发送连接成功响应
|
| 129 |
+
// await conn.write(new Uint8Array([
|
| 130 |
+
// 0x05, 0x00, 0x00, 0x01,
|
| 131 |
+
// 0, 0, 0, 0,
|
| 132 |
+
// 0, 0
|
| 133 |
+
// ]));
|
| 134 |
+
// console.log("已发送成功响应");
|
| 135 |
|
| 136 |
+
// // 设置更大的缓冲区
|
| 137 |
+
// const bufSize = 64 * 1024;
|
| 138 |
|
| 139 |
+
// // 双向转发数据
|
| 140 |
+
// console.log("开始数据转发");
|
| 141 |
+
// await Promise.all([
|
| 142 |
+
// (async () => {
|
| 143 |
+
// const buf = new Uint8Array(bufSize);
|
| 144 |
+
// try {
|
| 145 |
+
// while (true) {
|
| 146 |
+
// const n = await conn.read(buf);
|
| 147 |
+
// if (!n) break;
|
| 148 |
+
// console.log(`客户端 -> 目标: ${n} 字节`);
|
| 149 |
+
// await target.write(buf.subarray(0, n));
|
| 150 |
+
// }
|
| 151 |
+
// } catch (e) {
|
| 152 |
+
// console.log("客户端->目标 传输错误:", e.message);
|
| 153 |
+
// }
|
| 154 |
+
// })(),
|
| 155 |
+
// (async () => {
|
| 156 |
+
// const buf = new Uint8Array(bufSize);
|
| 157 |
+
// try {
|
| 158 |
+
// while (true) {
|
| 159 |
+
// const n = await target.read(buf);
|
| 160 |
+
// if (!n) break;
|
| 161 |
+
// console.log(`目标 -> 客户端: ${n} 字节`);
|
| 162 |
+
// await conn.write(buf.subarray(0, n));
|
| 163 |
+
// }
|
| 164 |
+
// } catch (e) {
|
| 165 |
+
// console.log("目标->客户端 传输错误:", e.message);
|
| 166 |
+
// }
|
| 167 |
+
// })()
|
| 168 |
+
// ]);
|
| 169 |
|
| 170 |
+
// console.log(`${targetHost}:${targetPort} 连接完成`);
|
| 171 |
+
// target.close();
|
| 172 |
+
// } catch (e) {
|
| 173 |
+
// console.error(`连接目标失败: ${e.message}`);
|
| 174 |
+
// await conn.write(new Uint8Array([0x05, 0x04, 0x00, 0x01, 0, 0, 0, 0, 0, 0]));
|
| 175 |
+
// }
|
| 176 |
+
// } else {
|
| 177 |
+
// console.log(`非 SOCKS5 请求: 0x${buf[0].toString(16)}`);
|
| 178 |
+
// }
|
| 179 |
+
// } catch (err) {
|
| 180 |
+
// console.error(`处理错误: ${err.message}`);
|
| 181 |
+
// } finally {
|
| 182 |
+
// console.log(`关闭连接: ${clientIP}`);
|
| 183 |
+
// conn.close();
|
| 184 |
+
// }
|
| 185 |
+
// }
|
| 186 |
|
| 187 |
+
// // 生成Clash配置
|
| 188 |
+
// function generateClashConfig(spaceDomain: string) {
|
| 189 |
+
// return `
|
| 190 |
+
// port: 7890
|
| 191 |
+
// socks-port: 7891
|
| 192 |
+
// mixed-port: 7890
|
| 193 |
+
// allow-lan: true
|
| 194 |
+
// mode: Rule
|
| 195 |
+
// log-level: info
|
| 196 |
+
// external-controller: 127.0.0.1:9090
|
| 197 |
|
| 198 |
+
// proxies:
|
| 199 |
+
// - name: "HFProxy"
|
| 200 |
+
// type: socks5
|
| 201 |
+
// server: ${spaceDomain}
|
| 202 |
+
// port: ${CONFIG.port}
|
| 203 |
+
// skip-cert-verify: true
|
| 204 |
|
| 205 |
+
// proxy-groups:
|
| 206 |
+
// - name: "PROXY"
|
| 207 |
+
// type: select
|
| 208 |
+
// proxies:
|
| 209 |
+
// - HFProxy
|
| 210 |
+
// - DIRECT
|
| 211 |
|
| 212 |
+
// rules:
|
| 213 |
+
// - DOMAIN-SUFFIX,google.com,PROXY
|
| 214 |
+
// - DOMAIN-SUFFIX,github.com,PROXY
|
| 215 |
+
// - DOMAIN-SUFFIX,githubusercontent.com,PROXY
|
| 216 |
+
// - MATCH,DIRECT
|
| 217 |
+
// `;
|
| 218 |
+
// }
|
| 219 |
|
| 220 |
+
// // HTTP服务器处理函数
|
| 221 |
+
// async function handleHttp(req: Request): Promise<Response> {
|
| 222 |
+
// const url = new URL(req.url);
|
| 223 |
+
// const spaceDomain = url.hostname;
|
| 224 |
|
| 225 |
+
// if (url.pathname === "/config") {
|
| 226 |
+
// return new Response(generateClashConfig(spaceDomain), {
|
| 227 |
+
// headers: {
|
| 228 |
+
// "content-type": "text/yaml",
|
| 229 |
+
// "content-disposition": "attachment; filename=config.yaml",
|
| 230 |
+
// "Access-Control-Allow-Origin": "*"
|
| 231 |
+
// }
|
| 232 |
+
// });
|
| 233 |
+
// }
|
| 234 |
|
| 235 |
+
// return new Response(`
|
| 236 |
+
// 代理服务器运行中
|
| 237 |
+
// HTTP端口: ${CONFIG.port}
|
| 238 |
+
// SOCKS5端口: ${CONFIG.port + 1}
|
| 239 |
+
// 访问 /config 获取配置
|
| 240 |
+
// `, {
|
| 241 |
+
// headers: {
|
| 242 |
+
// "content-type": "text/plain;charset=utf-8",
|
| 243 |
+
// "Access-Control-Allow-Origin": "*"
|
| 244 |
+
// }
|
| 245 |
+
// });
|
| 246 |
+
// }
|
| 247 |
|
| 248 |
+
// // 启动服务器
|
| 249 |
+
// async function main() {
|
| 250 |
+
// try {
|
| 251 |
+
// const listener = Deno.listen({ port: CONFIG.port, hostname: CONFIG.host });
|
| 252 |
+
// console.log(`代理服务器启动于 ${CONFIG.host}:${CONFIG.port}`);
|
| 253 |
+
// console.log("等待连接...");
|
| 254 |
|
| 255 |
+
// for await (const conn of listener) {
|
| 256 |
+
// handleConnection(conn).catch(err => {
|
| 257 |
+
// console.error("连接处理错误:", err.message);
|
| 258 |
+
// });
|
| 259 |
+
// }
|
| 260 |
+
// } catch (err) {
|
| 261 |
+
// console.error("服务器启动错误:", err.message);
|
| 262 |
+
// }
|
| 263 |
+
// }
|
| 264 |
|
| 265 |
+
// if (import.meta.main) {
|
| 266 |
+
// main().catch(console.error);
|
| 267 |
+
// }
|