基于CF大善人代理一个账号池

Linux.do的热心老哥们分享了很多的Key
但是我又没有服务器来部署new-api组账号池
所以想起来了CF大善人
CF新建一个worker 把下面的代码复制进去
下面贴代码

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

const key_pool = [
    {
        url: "https://goapi.gptnb.me",
        key: "sk-wxDzBDX7fCX6g8PiCd2984C648D04d6aB195A43d48828e1a"
    },
    {
        url: "https://api-us.gptnb.me",
        key: "sk-2Q7ey836XRTCLogs1b15Bc301e434c1fBb96C2C41a8cEe53"
    },
    {
        url: "https://api.freegpt.art",
        key: "sk-XkHflG5YzIc6cs2D12C6BcC3D4D04cAdA898629e786c6340"
    },
]

// 获取 0-n 的随机数
const getRandomNumber = (n) => {
    return Math.floor(Math.random() * (n + 1));
}

async function handleRequest(request) {
    const url = new URL(request.url)

    // 处理预检请求(OPTIONS 请求)
    if (request.method === 'OPTIONS') {
        return handleOptions(request)
    }

    // 处理GET请求
    if (request.method === 'GET') {
        const responseStr = `
        <html>
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>使用说明</title>
                <style>
                    body {
                        font-family: Arial, sans-serif;
                        margin: 0;
                        padding: 2rem;
                        background-color: #f2f2f2;
                    }
                    .content {
                        max-width: 600px;
                        margin: 0 auto;
                        padding: 2rem;
                        background-color: #fff;
                        border-radius: 10px;
                        box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
                    }
                    h1 {
                        text-align: center;
                        margin-bottom: 2rem;
                    }
                    p {
                        font-size: 1.1rem;
                        color: #333;
                        line-height: 1.6;
                    }
                    .author {
                        font-style: italic;
                        color: #888;
                        text-align: right;
                    }
                </style>
            </head>
            <body>
                <div class="content">
                    <h1>使用说明</h1>
                    <p>与 OpenAI 的调用方式相同,您只需随意传递一个 Key 即可使用。</p>
                    <h3 style="text-align:left;margin-top:-10px">特别感谢</h3>
                    <ul style="margin-top:-10px">
                        <li>Linux.do 社区热心用户分享的API Key</li>
                        <li>cloudflare大善人</li>
                    </ul>
                    <p class="author">作者:Linux.do热心用户</p>
                </div>
            </body>
        </html>
        `

        const headers = {
            'Content-Type': 'text/html',
        };

        return new Response(responseStr, { status: 200, headers });
    }

    const randomNum = getRandomNumber(key_pool.length - 1)

    console.log("randomNum: " + randomNum);

    // 创建 OpenAI API 的请求 URL
    const openaiUrl = `${key_pool[randomNum].url}${url.pathname}`

    // 复制客户端请求的头
    let headers = new Headers(request.headers)

    headers.set('Authorization', `Bearer ${key_pool[randomNum].key}`);

    // 创建新的请求对象,带有客户端的请求头和请求体
    const modifiedRequestInit = {
        method: request.method,
        headers: headers,
        body: request.method === 'GET' || request.method === 'HEAD' ? null : request.body,
    }

    const modifiedRequest = new Request(openaiUrl, modifiedRequestInit)

    // 发送请求到 OpenAI API
    const response = await fetch(modifiedRequest)

    console.log("responseStatus: " + response.status);

    // 复制响应并添加 CORS 头
    const newHeaders = new Headers(response.headers)
    newHeaders.set('Access-Control-Allow-Origin', '*')
    newHeaders.set('Access-Control-Allow-Headers', request.headers.get('Access-Control-Request-Headers') || '*')
    newHeaders.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')

    if (response.status != 200) {

        const errorJson = {
            invalid_key: randomNum,
            error: 'Failed to invalid Key, Please contact the author to delete the invalid Key.',
            author: 'NOCN',
            email: '[email protected]',
        };

        // 返回包含错误信息的 JSON 响应
        return new Response(JSON.stringify(errorJson), {
            status: response.status,
            statusText: response.statusText,
            headers: newHeaders,
        });

    }


    const newResponse = new Response(response.body, {
        status: response.status,
        statusText: response.statusText,
        headers: newHeaders,
    })

    return newResponse
}

function handleOptions(request) {
    const headers = new Headers()
    headers.set('Access-Control-Allow-Origin', '*')
    headers.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
    headers.set('Access-Control-Allow-Headers', request.headers.get('Access-Control-Request-Headers') || '*')
    return new Response(null, { headers })
}

部署完成后绑定自己的域名即可

key_pool 里面就是api地址和Key

使用方法和原本的方法一致 Key随便传即可
例如使用始皇的 https://api.oaipro.com
使用自己这个部署的 https://自定义域名

感谢热心大佬分享的账号
https://linux.do/t/topic/94917

16 个赞

6哇

1 个赞

可以使用koyeb,render,serv00,huggingface

2 个赞

牛啊

1 个赞

我一看就知道很牛逼。尽管我啥都看不懂!

1 个赞

可以的, 谢谢分享

1 个赞

牛逼

1 个赞

牛啊,佬,大善人果然什么都行

1 个赞

加锁呀,别让坏人刷

1 个赞

可以白嫖render的,能搭new-api

1 个赞

刚想说不许C大善人,仔细一看,没事了 :sweat_smile:

这个有意思,:+1:t2:

这么多免费平台

常规话题软件开发

1 个赞

什么账号啊

666

From #dev to 开发调优

1 个赞