* @param {import("@cloudflare/workers-types").Request} request * @param {{UUID: string, PROXYIP: string, DNS_RESOLVER_URL: string, NODE_ID: int, API_HOST: string, API_TOKEN: string}} env * @param {import("@cloudflare/workers-types").ExecutionContext} ctx * @returns {Promise }
(request, env, ctx)
| 26 | * @returns {Promise<Response>} |
| 27 | */ |
| 28 | async fetch(request, env, ctx) { |
| 29 | // uuid_validator(request); |
| 30 | try { |
| 31 | userID = env.UUID || userID; |
| 32 | proxyIP = env.PROXYIP || proxyIP; |
| 33 | dohURL = env.DNS_RESOLVER_URL || dohURL; |
| 34 | let userID_Path = userID; |
| 35 | if (userID.includes(',')) { |
| 36 | userID_Path = userID.split(',')[0]; |
| 37 | } |
| 38 | const upgradeHeader = request.headers.get('Upgrade'); |
| 39 | if (!upgradeHeader || upgradeHeader !== 'websocket') { |
| 40 | const url = new URL(request.url); |
| 41 | switch (url.pathname) { |
| 42 | case '/cf': |
| 43 | return new Response(JSON.stringify(request.cf, null, 4), { |
| 44 | status: 200, |
| 45 | headers: { |
| 46 | "Content-Type": "application/json;charset=utf-8", |
| 47 | }, |
| 48 | }); |
| 49 | case `/${userID_Path}`: { |
| 50 | const vlessConfig = getVLESSConfig(userID, request.headers.get('Host')); |
| 51 | return new Response(`${vlessConfig}`, { |
| 52 | status: 200, |
| 53 | headers: { |
| 54 | "Content-Type": "text/html; charset=utf-8", |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | case `/sub/${userID_Path}`: { |
| 59 | const url = new URL(request.url); |
| 60 | const searchParams = url.searchParams; |
| 61 | let vlessConfig = createVLESSSub(userID, request.headers.get('Host')); |
| 62 | // If 'format' query param equals to 'clash', convert config to base64 |
| 63 | if (searchParams.get('format') === 'clash') { |
| 64 | vlessConfig = btoa(vlessConfig); |
| 65 | } |
| 66 | // Construct and return response object |
| 67 | return new Response(vlessConfig, { |
| 68 | status: 200, |
| 69 | headers: { |
| 70 | "Content-Type": "text/plain;charset=utf-8", |
| 71 | } |
| 72 | }); |
| 73 | } |
| 74 | case `/bestip/${userID_Path}`: { |
| 75 | const bestiplink = `https://sub.xf.free.hr/auto?host=${request.headers.get('Host')}&uuid=${userID_Path}` |
| 76 | const reqHeaders = new Headers(request.headers); |
| 77 | const bestipresponse = await fetch(bestiplink, { redirect: 'manual', headers: reqHeaders, }); |
| 78 | // Construct and return response object |
| 79 | return bestipresponse |
| 80 | } |
| 81 | default: |
| 82 | // return new Response('Not found', { status: 404 }); |
| 83 | // For any other path, reverse proxy to 'www.fmprc.gov.cn' and return the original response, caching it in the process |
| 84 | const hostnames = ['www.fmprc.gov.cn', 'www.xuexi.cn', 'www.gov.cn', 'mail.gov.cn', 'www.mofcom.gov.cn', 'www.gfbzb.gov.cn', 'www.miit.gov.cn', 'www.12377.cn']; |
| 85 | url.hostname = hostnames[Math.floor(Math.random() * hostnames.length)]; |
no test coverage detected