04-部署平台进行流量代理
418字约1分钟
2025-07-15
vercel
直接设定 vercel.json 即可进行一个代理
{
"rewrites": [
{ "source": "/proxy/:match/:url*", "destination": "https://:match/:url*" },
{ "source": "/proxy/:match/:url*/", "destination": "https://:match/:url*/" },
{ "source": "/httpproxy/:match/:url*", "destination": "http://:match/:url*" },
{ "source": "/httpproxy/:match/:url*/", "destination": "http://:match/:url*/" }
]
}
couldfare
{
"name": "proxy-server",
"version": "1.0.0",
"description": "包含页面。",
"main": "_worker.js",
"scripts": {
"start": "node _worker.js"
},
"keywords": [
"proxy",
"server"
],
"author": "你的名字",
"license": "MIT",
"dependencies": {
"express": "^4.18.2",
"node-fetch": "^3.3.1"
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url);
// 新增:检查用户是否直接访问代理地址
if (url.pathname === '/' || url.pathname === '/proxy/') {
return createLandingPage();
}
const actualUrlStr = url.pathname.replace("/proxy/","") + url.search + url.hash
const actualUrl = new URL(actualUrlStr)
const modifiedRequest = new Request(actualUrl, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
const response = await fetch(modifiedRequest);
const modifiedResponse = new Response(response.body, response);
// 添加允许跨域访问的响应头
modifiedResponse.headers.set('Access-Control-Allow-Origin', '*');
return modifiedResponse;
}
// 新增:创建引导页面
function createLandingPage() {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
background-color: #fbfbfb;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
color: #444;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
form {
background-color: white;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
padding: 2rem;
border-radius: 8px;
}
input {
display: block;
width: 100%;
font-size: 18px;
padding: 15px;
border: solid 1px #ccc;
border-radius: 4px;
margin: 1rem 0;
}
button {
padding: 15px;
background-color: #0288d1;
color: white;
font-size: 18px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color: #039BE5;
}
</style>
<meta charset="UTF-8">
<title>代理服务器</title>
</head>
<body>
<h1>输入您想访问的网址</h1>
<form id="proxy-form">
<input type="text" id="url" name="url" placeholder="https://example.com" required />
<button type="submit">访问</button>
</form>
<script>
const form = document.getElementById('proxy-form');
form.addEventListener('submit', event => {
event.preventDefault();
const input = document.getElementById('url');
const actualUrl = input.value;
const proxyUrl = '/proxy/' + actualUrl;
location.href = proxyUrl;
});
</script>
</body>
</html>
`;
return new Response(html, {
headers: { 'Content-Type': 'text/html' }
});
}