一元网络论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 143|回复: 0

How to resolve it? Source site is GBK encoding, how can I solve this problem?

[复制链接]

2万

主题

2万

帖子

8万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
83485
发表于 2024-8-31 15:48:20 | 显示全部楼层 |阅读模式
Please translate the text into English and only return one translation result without any other characters. Do not use the words "pseudo-original" or "rewrite": "As the title suggests, the original worker code was copied from the internet.

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
});

// Preparation for reverse proxy purpose domain, change it to what you want
let target_url = "https://XXXX.com";
// The regular expression of content replacement
let target_url_reg = /XXXX\.com/g;

// You want to run which countries access
const allowed_region = ['CN', 'HK'];

// Block self-access
const blocked_ip_address = ['0.0.0.0', '127.0.0.1'];

async function handleRequest(request) {
    const region = request.headers.get('cf-ipcountry').toUpperCase();
    const ip_address = request.headers.get('cf-connecting-ip');
    let url = new URL(request.url);
    url.hostname = new URL(target_url).hostname;

    // Copy the request object and update its attributes
    let headers = new Headers(request.headers);
    headers.set('Referer', target_url);
    headers.set('User-Agent', request.headers.get('user-agent'));

    const param = {
        method: request.method,
        headers: headers,
        body: request.body,
        redirect: 'manual'
    };

    // If the IP address is blocked by WorkersProxy, return an access denied response with status code 403
    if(blocked_ip_address.includes(ip_address)) {
        response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
            status: 403
        });
    } else if(allowed_region.includes(region)){
        let response = await fetch(url, param);
        // Check the Content-Type header of the response
        const contentType = response.headers.get('content-type');
        if(contentType && contentType.includes('text')) {
            // If the type is text, replace the URL in the response body with the hostname of the requested URL
            let responseBody = await response.text();
            responseBody = await handleResBody(request, responseBody);
            // Copy the response object and update its attributes
            let headers = await handleResHeader(response);
            return new Response(responseBody, {
                status: response.status,
                statusText: response.statusText,
                headers: headers
            });
        } else {
            // If the type is not text, directly return the response object
            return response;
        }
    } else {
        response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
            status: 403
        });
    }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|一元网络论坛

GMT+8, 2024-11-6 07:31 , Processed in 0.105730 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表