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|
|
|
|
| 1 |
+
// proxy/main.ts
|
| 2 |
+
|
| 3 |
+
// 代理服务器配置
|
| 4 |
+
const CONFIG = {
|
| 5 |
+
port: parseInt(Deno.env.get("PORT") || "7860"), // Hugging Face 默认端口
|
| 6 |
+
host: "0.0.0.0"
|
| 7 |
+
};
|
| 8 |
+
|
| 9 |
+
// 生成Clash配置
|
| 10 |
+
function generateClashConfig(spaceDomain: string) {
|
| 11 |
+
return `
|
| 12 |
+
mixed-port: 7890
|
| 13 |
+
allow-lan: true
|
| 14 |
+
mode: Rule
|
| 15 |
+
log-level: info
|
| 16 |
+
proxies:
|
| 17 |
+
- name: "HFProxy"
|
| 18 |
+
type: socks5
|
| 19 |
+
server: ${spaceDomain}
|
| 20 |
+
port: ${CONFIG.port}
|
| 21 |
+
proxy-groups:
|
| 22 |
+
- name: "PROXY"
|
| 23 |
+
type: select
|
| 24 |
+
proxies:
|
| 25 |
+
- HFProxy
|
| 26 |
+
rules:
|
| 27 |
+
- MATCH,PROXY
|
| 28 |
+
`;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// HTTP服务器处理函数
|
| 32 |
+
async function handleHttp(req: Request): Promise<Response> {
|
| 33 |
+
const url = new URL(req.url);
|
| 34 |
+
const spaceDomain = url.hostname;
|
| 35 |
+
|
| 36 |
+
// 提供配置文件下载
|
| 37 |
+
if (url.pathname === "/config") {
|
| 38 |
+
return new Response(generateClashConfig(spaceDomain), {
|
| 39 |
+
headers: {
|
| 40 |
+
"content-type": "text/yaml",
|
| 41 |
+
"content-disposition": "attachment; filename=config.yaml"
|
| 42 |
+
}
|
| 43 |
+
});
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// 默认返回说明页面
|
| 47 |
+
return new Response(`
|
| 48 |
+
代理服务器运行中
|
| 49 |
+
访问 /config 获取Clash配置文件
|
| 50 |
+
当前端口: ${CONFIG.port}
|
| 51 |
+
`, {
|
| 52 |
+
headers: { "content-type": "text/plain;charset=utf-8" }
|
| 53 |
+
});
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// 处理SOCKS5代理连接
|
| 57 |
+
async function handleConnection(conn: Deno.Conn) {
|
| 58 |
+
try {
|
| 59 |
+
// 处理SOCKS5握手
|
| 60 |
+
const buf = new Uint8Array(256);
|
| 61 |
+
const n = await conn.read(buf);
|
| 62 |
+
if (!n) return;
|
| 63 |
+
|
| 64 |
+
// 如果是SOCKS5初始握手
|
| 65 |
+
if (buf[0] === 0x05) {
|
| 66 |
+
// 回应认证方法(无需认证)
|
| 67 |
+
await conn.write(new Uint8Array([0x05, 0x00]));
|
| 68 |
+
|
| 69 |
+
// 读取连接请求
|
| 70 |
+
const n2 = await conn.read(buf);
|
| 71 |
+
if (!n2) return;
|
| 72 |
+
|
| 73 |
+
const atyp = buf[3];
|
| 74 |
+
let targetHost = "";
|
| 75 |
+
let targetPort = 0;
|
| 76 |
+
let addrLen = 0;
|
| 77 |
+
|
| 78 |
+
switch (atyp) {
|
| 79 |
+
case 0x01: // IPv4
|
| 80 |
+
targetHost = Array.from(buf.slice(4, 8)).join(".");
|
| 81 |
+
addrLen = 4;
|
| 82 |
+
break;
|
| 83 |
+
case 0x03: // Domain
|
| 84 |
+
addrLen = buf[4];
|
| 85 |
+
targetHost = new TextDecoder().decode(buf.slice(5, 5 + addrLen));
|
| 86 |
+
break;
|
| 87 |
+
case 0x04: // IPv6
|
| 88 |
+
targetHost = Array.from(buf.slice(4, 20))
|
| 89 |
+
.map(x => x.toString(16))
|
| 90 |
+
.join(":");
|
| 91 |
+
addrLen = 16;
|
| 92 |
+
break;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
targetPort = (buf[4 + addrLen] << 8) | buf[5 + addrLen];
|
| 96 |
+
|
| 97 |
+
try {
|
| 98 |
+
// 连接目标服务器
|
| 99 |
+
const target = await Deno.connect({
|
| 100 |
+
hostname: targetHost,
|
| 101 |
+
port: targetPort,
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
// 发送连接成功响应
|
| 105 |
+
await conn.write(new Uint8Array([
|
| 106 |
+
0x05, 0x00, 0x00, 0x01,
|
| 107 |
+
0, 0, 0, 0, // 绑定地址
|
| 108 |
+
0, 0 // 绑定端口
|
| 109 |
+
]));
|
| 110 |
+
|
| 111 |
+
// 创建双向数据转发
|
| 112 |
+
const pipe = async (from: Deno.Conn, to: Deno.Conn) => {
|
| 113 |
+
const buf = new Uint8Array(8192);
|
| 114 |
+
try {
|
| 115 |
+
while (true) {
|
| 116 |
+
const n = await from.read(buf);
|
| 117 |
+
if (!n) break;
|
| 118 |
+
await to.write(buf.subarray(0, n));
|
| 119 |
+
}
|
| 120 |
+
} catch (e) {
|
| 121 |
+
// 忽略连接关闭错误
|
| 122 |
+
}
|
| 123 |
+
};
|
| 124 |
+
|
| 125 |
+
// 双向转发数据
|
| 126 |
+
await Promise.all([
|
| 127 |
+
pipe(conn, target),
|
| 128 |
+
pipe(target, conn)
|
| 129 |
+
]);
|
| 130 |
+
|
| 131 |
+
target.close();
|
| 132 |
+
} catch (e) {
|
| 133 |
+
// 连接失败响应
|
| 134 |
+
await conn.write(new Uint8Array([0x05, 0x04, 0x00, 0x01, 0, 0, 0, 0, 0, 0]));
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
} catch (err) {
|
| 138 |
+
console.error("代理错误:", err);
|
| 139 |
+
} finally {
|
| 140 |
+
conn.close();
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// 启动服务器
|
| 145 |
+
async function main() {
|
| 146 |
+
// 启动HTTP服务器(配置文件访问)
|
| 147 |
+
const httpServer = Deno.serve({ port: CONFIG.port }, handleHttp);
|
| 148 |
+
console.log(`HTTP服务器运行在端口 ${CONFIG.port}`);
|
| 149 |
+
|
| 150 |
+
// 启动SOCKS5代理服务器
|
| 151 |
+
const listener = Deno.listen({ port: CONFIG.port + 1, hostname: CONFIG.host });
|
| 152 |
+
console.log(`SOCKS5代理服务器运行在端口 ${CONFIG.port + 1}`);
|
| 153 |
+
|
| 154 |
+
for await (const conn of listener) {
|
| 155 |
+
handleConnection(conn);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
await httpServer.finished;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// 运行服务器
|
| 162 |
+
if (import.meta.main) {
|
| 163 |
+
main();
|
| 164 |
+
}
|