Workers代码
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and proxy a given request object
* @param {Request} request
*/
async function handleRequest(request) {
// 提取请求的URL参数 "?url=" 后面的部分作为目标URL
const urlParams = new URL(request.url).searchParams;
const targetUrl = urlParams.get('url');
// 检查目标URL是否存在
if (!targetUrl) {
return new Response('Missing target URL', { status: 400 });
}
try {
// 发送新的请求到目标URL
const proxyResponse = await fetch(targetUrl, {
method: request.method,
headers: request.headers
});
// 返回目标资源的响应
return new Response(proxyResponse.body, {
status: proxyResponse.status,
statusText: proxyResponse.statusText,
headers: proxyResponse.headers
});
} catch (error) {
// 返回错误信息
return new Response(error.message, { status: 500 });
}
}
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Proxy Downloader</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f7f6;
margin: 0;
padding: 40px;
display: flex;
justify-content: center;
align-items: center;
height: calc(100vh - 80px);
color: #333;
}
#proxyForm {
width: 100%;
max-width: 400px;
background: #fff;
padding: 25px;
border-radius: 10px;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 24px;
color: #5d647b;
text-align: center;
margin-bottom: 30px;
}
label {
display: block;
margin-bottom: .5rem;
color: #5d647b;
font-size: 0.875rem;
}
input[type="url"], button {
width: 100%;
padding: .75rem;
margin-bottom: 1.5rem;
border: 2px solid #e8ebed;
border-radius: 8px;
box-sizing: border-box;
font-size: 0.875rem;
transition: all 0.3s ease; /* Smoothly transition all properties over 0.3 seconds */
}
input[type="url"]:focus {
border-color: #007bff;
outline: none;
transform: scale(1.02); /* Slightly increase the size to give focus effect */
}
button {
border: none;
border-radius: 8px;
background-color: #007bff;
color: white;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #0056b3;
}
button:active {
transform: translateY(2px);
}
@media (max-width: 768px) { /* Adjust styles for tablets */
body {
padding: 30px;
}
#proxyForm {
padding: 20px;
}
}
@media (max-width: 480px) { /* Adjust styles for mobile phones */
body {
padding: 15px;
}
#proxyForm {
padding: 15px;
}
h1 {
font-size: 20px; /* Reduce title font size for small screens */
}
input[type="url"], button {
padding: 10px; /* Reduce padding for input and button */
}
label {
margin-bottom: 0.4rem; /* Reduce spacing between label and input */
}
}
</style>
</head>
<body><script>
with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","category=&userid=&aplus&yunid=&&trid=ac11000117094576202771416ee1fd&asid=AQAAAADUQORl+zJcXwAAAACUHkuYQkPBfQ==",id="tb-beacon-aplus",src=(location>"https"?"//g":"//g")+".alicdn.com/alilog/mlog/aplus_v2.js")
</script>
<div id="proxyForm">
<h1>Proxy Downloader</h1>
<form>
<label for="url">Enter the target URL:</label>
<input type="url" id="url" name="url" placeholder="https://example.com/resource" required>
<button type="submit">Download</button>
</form>
</div>
<script>
document.querySelector('form').addEventListener('submit', function(event) {
event.preventDefault();
const targetUrl = document.getElementById('url').value;
const proxyBaseUrl = '你的地址';
if (targetUrl.startsWith('http://') || targetUrl.startsWith('https://')) {
const proxyUrl = new URL(proxyBaseUrl);
proxyUrl.searchParams.set('url', targetUrl);
window.location.href = proxyUrl.href;
} else {
alert('Please enter a valid HTTP or HTTPS URL');
}
});
</script>
</body>
</html>
Workers完整版代码
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and proxy a given request object
* @param {Request} request
*/
async function handleRequest(request) {
// 判断请求是否为GET且URL参数包含'?url='
if (request.method === 'GET' && request.url.includes('?url=')) {
const urlParams = new URL(request.url).searchParams;
const targetUrl = urlParams.get('url');
// 检查目标URL是否存在
if (!targetUrl) {
return new Response('Missing target URL', { status: 400 });
}
try {
// 发送新的请求到目标URL
const proxyResponse = await fetch(targetUrl, {
method: 'GET', // 因为是下载,这里限定为GET请求
});
// 返回目标资源的响应
return new Response(proxyResponse.body, {
status: proxyResponse.status,
statusText: proxyResponse.statusText,
headers: proxyResponse.headers
});
} catch (error) {
// 返回错误信息
return new Response(error.message, { status: 500 });
}
} else {
// 对于其他请求,返回前端代码
return new Response(frontendCode, {
headers: {
'Content-Type': 'text/html',
},
});
}
}
const frontendCode = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Proxy Downloader</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f7f6;
margin: 0;
padding: 40px;
display: flex;
justify-content: center;
align-items: center;
height: calc(100vh - 80px);
color: #333;
}
#proxyForm {
width: 100%;
max-width: 400px;
background: #fff;
padding: 25px;
border-radius: 10px;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 24px;
color: #5d647b;
text-align: center;
margin-bottom: 30px;
}
label {
display: block;
margin-bottom: .5rem;
color: #5d647b;
font-size: 0.875rem;
}
input[type="url"], button {
width: 100%;
padding: .75rem;
margin-bottom: 1.5rem;
border: 2px solid #e8ebed;
border-radius: 8px;
box-sizing: border-box;
font-size: 0.875rem;
transition: all 0.3s ease; /* Smoothly transition all properties over 0.3 seconds */
}
input[type="url"]:focus {
border-color: #007bff;
outline: none;
transform: scale(1.02); /* Slightly increase the size to give focus effect */
}
button {
border: none;
border-radius: 8px;
background-color: #007bff;
color: white;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #0056b3;
}
button:active {
transform: translateY(2px);
}
@media (max-width: 768px) { /* Adjust styles for tablets */
body {
padding: 30px;
}
#proxyForm {
padding: 20px;
}
}
@media (max-width: 480px) { /* Adjust styles for mobile phones */
body {
padding: 15px;
}
#proxyForm {
padding: 15px;
}
h1 {
font-size: 20px; /* Reduce title font size for small screens */
}
input[type="url"], button {
padding: 10px; /* Reduce padding for input and button */
}
label {
margin-bottom: 0.4rem; /* Reduce spacing between label and input */
}
}
</style>
</head>
<body><script>
with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","category=&userid=&aplus&yunid=&&trid=ac11000117094576202771416ee1fd&asid=AQAAAADUQORl+zJcXwAAAACUHkuYQkPBfQ==",id="tb-beacon-aplus",src=(location>"https"?"//g":"//g")+".alicdn.com/alilog/mlog/aplus_v2.js")
</script>
<div id="proxyForm">
<h1>Proxy Downloader</h1>
<form>
<label for="url">Enter the target URL:</label>
<input type="url" id="url" name="url" placeholder="https://example.com/resource" required>
<button type="submit">Download</button>
</form>
</div>
<script>
document.querySelector('form').addEventListener('submit', function(event) {
event.preventDefault();
const targetUrl = document.getElementById('url').value;
const proxyBaseUrl = '你的workers域名';
if (targetUrl.startsWith('http://') || targetUrl.startsWith('https://')) {
const proxyUrl = new URL(proxyBaseUrl);
proxyUrl.searchParams.set('url', targetUrl);
window.location.href = proxyUrl.href;
} else {
alert('Please enter a valid HTTP or HTTPS URL');
}
});
</script>
</body>
</html>`;
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)