|
基于开源项目修改的 Cloudflare Workers 部署的 Fuclaude 号池代码:
```javascript
const CONFIG = {
ORIGINAL_WEBSITE: "https://demo.fuclaude.com",
SESSION_KEYS: ["sk-ant", "sk-ant", "sk-ant", "sk-ant", "sk-ant", "sk-ant"],
KEY_NAMES: ["c1", "c2", "c3", "c4", "c5", "c6"],
SITE_PASSWORD: "password",
GUEST_ACCOUNTS: {}
};
export default {
async fetch(req, env, ctx) {
return handleRequest(req);
}
};
async function handleRequest(req) {
const url = new URL(req.url);
if (url.pathname === '/login') return handleRootPath(req, url, true);
if (url.pathname === '/') return handleRootPath(req, url);
return proxyRequest(req);
}
async function handleRootPath(req, url, forceLogin = false) {
const cookie = req.headers.get('Cookie') || '';
if (!forceLogin && cookie.includes('_Secure-next-auth.session-data')) return Response.redirect(`${url.origin}/new`, 302);
if (req.method === 'POST') return handleLogin(req, url);
return new Response(formHtml, { headers: { 'Content-Type': 'text/html; charset=utf-8' } });
}
async function handleLogin(req, url) {
try {
const formData = await req.formData();
const loginType = formData.get('login_type');
const selectedKeyIndex = formData.get('session_key_index');
let body = { 'session_key': CONFIG.SESSION_KEYS[selectedKeyIndex] };
if (loginType === 'site') {
const sitePassword = formData.get('site_password');
if (sitePassword !== CONFIG.SITE_PASSWORD) return new Response('站点密码错误', { status: 403, headers: { 'Content-Type': 'text/plain; charset=utf-8' } });
} else if (loginType === 'guest') {
const username = formData.get('username');
const guestPassword = formData.get('guest_password');
if (CONFIG.GUEST_ACCOUNTS[username.trim()] === undefined) return new Response('不存在该用户', { status: 400, headers: { 'Content-Type': 'text/plain; charset=utf-8' } });
if (CONFIG.GUEST_ACCOUNTS[username] !== guestPassword) return new Response('密码错误', { status: 403, headers: { 'Content-Type': 'text/plain; charset=utf-8' } });
body.unique_name = username;
} else return new Response('无效的登录类型', { status: 400, headers: { 'Content-Type': 'text/plain; charset=utf-8' } });
const authUrl = `${CONFIG.ORIGINAL_WEBSITE}/manage-api/auth/oauth_token`;
const apiResponse = await fetch(authUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
if (!apiResponse.ok) throw new Error(`API request failed with status ${apiResponse.status}`);
const respJson = await apiResponse.json();
return Response.redirect(`https://${url.host}${respJson.login_url || '/'}`, 302);
} catch (error) {
console.error('Login error:', error);
return new Response('登录错误', { status: 500, headers: { 'Content-Type': 'text/plain; charset=utf-8' } });
}
}
async function proxyRequest(req) {
const url = new URL(req.url);
const newUrl = `${CONFIG.ORIGINAL_WEBSITE}${url.pathname}${url.search}`;
const modifiedRequest = new Request(newUrl, req);
const response = await fetch(modifiedRequest);
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('text/html') && url.pathname !== '/login_oauth') {
let html = await response.text();
html = html.replace(/]*>(?=[\s\S]*?)(?=[\s\S]*?)(?=[\s\S]*?)[\s\S]*?/gi, '');
return new Response(html, { headers: response.headers });
}
return response;
}
// ... (formHtml remains the same) ...
``` |
|