風雲 (ID: 3)
头衔:论坛版主
等级:大天使
积分:1567
发帖:70 篇
来自:保密
注册:2022/3/30 15:28:53
造访:2024/11/22 10:43:44
发帖:70 篇
来自:保密
注册:2022/3/30 15:28:53
造访:2024/11/22 10:43:44
[ 第 1 楼 ]
回复
function proxy(req, res, target) {
if(target?.hostname) req.headers.host = target.hostname; // 重写 Host 头
req.headers['x-real-ip'] = req.connection.remoteAddress.replace(/^::ffff:/, "");
var forward = req.headers["x-forwarded-for"] || "";
if(forward) forward += ", ";
req.headers["x-forwarded-for"] = forward + req.headers['x-real-ip'];
var options = {
hostname: target?.host || "127.34.56.78",
port: target?.port || 80,
path: req.url,
method: req.method,
headers: req.headers
};
var proxy = (target?.protocol == "https" ? https: http).request(options);
req.on("data", chunk => proxy.write(chunk));
req.on("end", () => proxy.end() );
proxy.on("response", pxy => {
res.writeHead(pxy.statusCode, pxy.headers);
pxy.on("data", chunk => res.write(chunk));
pxy.on("end", () => res.end());
});
proxy.on("error", err => {
res.writeHead(502, { "Content-Type": "text/plain" });
res.end(err.message);
});
}
if(target?.hostname) req.headers.host = target.hostname; // 重写 Host 头
req.headers['x-real-ip'] = req.connection.remoteAddress.replace(/^::ffff:/, "");
var forward = req.headers["x-forwarded-for"] || "";
if(forward) forward += ", ";
req.headers["x-forwarded-for"] = forward + req.headers['x-real-ip'];
var options = {
hostname: target?.host || "127.34.56.78",
port: target?.port || 80,
path: req.url,
method: req.method,
headers: req.headers
};
var proxy = (target?.protocol == "https" ? https: http).request(options);
req.on("data", chunk => proxy.write(chunk));
req.on("end", () => proxy.end() );
proxy.on("response", pxy => {
res.writeHead(pxy.statusCode, pxy.headers);
pxy.on("data", chunk => res.write(chunk));
pxy.on("end", () => res.end());
});
proxy.on("error", err => {
res.writeHead(502, { "Content-Type": "text/plain" });
res.end(err.message);
});
}
2022/6/12 11:12:11
IP:已设置保密