Skip to content

Commit

Permalink
~
Browse files Browse the repository at this point in the history
  • Loading branch information
H-XZ committed Jan 5, 2025
1 parent 69c09d1 commit b29ecdd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
14 changes: 10 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
async function fetchData(number) {
const proxyUrl = 'https://agent.xz39.xyz/';
const targetUrl = `https://18comic.vip/album/${number}`;
const url = proxyUrl + targetUrl;

// 自定义请求头
const headers = {
Expand All @@ -23,9 +22,16 @@ async function fetchData(number) {
};

try {
const response = await fetch(url, {
method: 'GET', // 请求方法
headers: headers // 添加自定义请求头
const response = await fetch(proxyUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: targetUrl,
method: 'GET',
headers: headers
})
});

if (response.ok) {
Expand Down
25 changes: 25 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import requests

# 服务工作者的 URL
worker_url = 'https://agent.xz39.xyz/' # 请根据实际情况修改

# 要转发的请求信息
payload = {
'url': 'https://httpbin.org/post', # 目标 URL
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Custom-Header': 'CustomValue'
},
'body': {
'key': 'value'
}
}

# 发送 POST 请求到服务工作者
response = requests.post(worker_url, json=payload)

# 输出响应信息
print('Status Code:', response.status_code)
print('Response Headers:', response.headers)
print('Response Body:', response.text)
49 changes: 48 additions & 1 deletion worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const specialCases = {
// 修正:处理请求头
function handleSpecialCases(request) {
const url = new URL(request.url);
const actualUrlStr = url.pathname.replace("/", "") + url.search + url.hash;
const actualUrl = new URL(actualUrlStr);

const rules = specialCases[url.hostname] || specialCases["*"];
const headers = new Headers(request.headers); // 复制原始请求头
for (const [key, value] of Object.entries(rules)) {
Expand All @@ -30,14 +33,48 @@ function handleSpecialCases(request) {
break;
}
}

// 返回一个新的 Request 对象
return new Request(request.url, {
return new Request(actualUrl, {
headers: headers,
method: request.method,
body: request.body
});
}

async function handlePostRequest(request) {
try {
const { url, method, headers: customHeaders, body } = await request.json();
const headers = new Headers(customHeaders);
const modifiedRequest = new Request(url, {
method,
headers,
body: body ? JSON.stringify(body) : null
});

const response = await fetch(modifiedRequest);

const responseHeaders = new Headers(response.headers);
responseHeaders.set('Access-Control-Allow-Origin', '*');
responseHeaders.set('X-Proxy-Success', 'true');

return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: responseHeaders
});
} catch (error) {
const headers = new Headers();
headers.set('Access-Control-Allow-Origin', '*');
headers.set('X-Proxy-Success', 'false');
return new Response('请求转发失败', {
status: 500,
statusText: 'Internal Server Error',
headers: headers
});
}
}

async function handleRequest(request) {
// 处理 OPTIONS 请求(预检请求)
if (request.method === 'OPTIONS') {
Expand All @@ -48,6 +85,16 @@ async function handleRequest(request) {
return new Response(null, { headers });
}

const url = new URL(request.url);
if (url.pathname === "/") {
if (request.method === 'POST') {
return handlePostRequest(request);
} else {
return new Response("Please enter the link after the /")
}
}


try {
// 处理特殊请求头
const modifiedRequest = handleSpecialCases(request);
Expand Down

0 comments on commit b29ecdd

Please sign in to comment.