diff --git a/alist-proxy.js b/alist-proxy.js index 6694536..a8a5821 100644 --- a/alist-proxy.js +++ b/alist-proxy.js @@ -4,129 +4,129 @@ var TOKEN = "YOUR_TOKEN"; // src/verify.ts var verify = async (data, _sign) => { - const signSlice = _sign.split(":"); - if (!signSlice[signSlice.length - 1]) { - return "expire missing"; - } - const expire = parseInt(signSlice[signSlice.length - 1]); - if (isNaN(expire)) { - return "expire invalid"; - } - if (expire < Date.now() && expire > 0) { - return "expire expired"; - } - const right = await hmacSha256Sign(data, expire); - if (_sign !== right) { - return "sign mismatch"; - } - return ""; + const signSlice = _sign.split(":"); + if (!signSlice[signSlice.length - 1]) { + return "expire missing"; + } + const expire = parseInt(signSlice[signSlice.length - 1]); + if (isNaN(expire)) { + return "expire invalid"; + } + if (expire < Date.now() / 1e3 && expire > 0) { + return "expire expired"; + } + const right = await hmacSha256Sign(data, expire); + if (_sign !== right) { + return "sign mismatch"; + } + return ""; }; var hmacSha256Sign = async (data, expire) => { - const key = await crypto.subtle.importKey( - "raw", - new TextEncoder().encode(TOKEN), - { name: "HMAC", hash: "SHA-256" }, - false, - ["sign", "verify"] - ); - const buf = await crypto.subtle.sign( - { - name: "HMAC", - hash: "SHA-256" - }, - key, - new TextEncoder().encode(`${data}:${expire}`) - ); - return btoa(String.fromCharCode(...new Uint8Array(buf))).replace(/\+/g, "-").replace(/\//g, "_") + ":" + expire; + const key = await crypto.subtle.importKey( + "raw", + new TextEncoder().encode(TOKEN), + { name: "HMAC", hash: "SHA-256" }, + false, + ["sign", "verify"] + ); + const buf = await crypto.subtle.sign( + { + name: "HMAC", + hash: "SHA-256" + }, + key, + new TextEncoder().encode(`${data}:${expire}`) + ); + return btoa(String.fromCharCode(...new Uint8Array(buf))).replace(/\+/g, "-").replace(/\//g, "_") + ":" + expire; }; // src/handleDownload.ts async function handleDownload(request) { - const origin = request.headers.get("origin") ?? "*"; - const url = new URL(request.url); - const path = decodeURI(url.pathname); - const sign = url.searchParams.get("sign") ?? ""; - const name = path.split("/").pop() ?? ""; - const verifyResult = await verify(name, sign); - if (verifyResult !== "") { - const resp2 = new Response( - JSON.stringify({ - code: 401, - message: verifyResult - }), - { - headers: { - "content-type": "application/json;charset=UTF-8" - } - } - ); - resp2.headers.set("Access-Control-Allow-Origin", origin); - return resp2; - } - let resp = await fetch(`${ADDRESS}/api/fs/link`, { - method: "POST", + const origin = request.headers.get("origin") ?? "*"; + const url = new URL(request.url); + const path = decodeURI(url.pathname); + const sign = url.searchParams.get("sign") ?? ""; + const name = path.split("/").pop() ?? ""; + const verifyResult = await verify(name, sign); + if (verifyResult !== "") { + const resp2 = new Response( + JSON.stringify({ + code: 401, + message: verifyResult + }), + { headers: { - "content-type": "application/json;charset=UTF-8", - Authorization: TOKEN - }, - body: JSON.stringify({ - path - }) - }); - let res = await resp.json(); - if (res.code !== 200) { - return new Response(JSON.stringify(res)); - } - request = new Request(res.data.url, request); - if (res.data.header) { - for (const k in res.data.header) { - for (const v of res.data.header[k]) { - request.headers.set(k, v); - } + "content-type": "application/json;charset=UTF-8" } + } + ); + resp2.headers.set("Access-Control-Allow-Origin", origin); + return resp2; + } + let resp = await fetch(`${ADDRESS}/api/fs/link`, { + method: "POST", + headers: { + "content-type": "application/json;charset=UTF-8", + Authorization: TOKEN + }, + body: JSON.stringify({ + path + }) + }); + let res = await resp.json(); + if (res.code !== 200) { + return new Response(JSON.stringify(res)); + } + request = new Request(res.data.url, request); + if (res.data.header) { + for (const k in res.data.header) { + for (const v of res.data.header[k]) { + request.headers.set(k, v); + } } - let response = await fetch(request); - response = new Response(response.body, response); - response.headers.set("Access-Control-Allow-Origin", origin); - response.headers.append("Vary", "Origin"); - return response; + } + let response = await fetch(request); + response = new Response(response.body, response); + response.headers.set("Access-Control-Allow-Origin", origin); + response.headers.append("Vary", "Origin"); + return response; } // src/handleOptions.ts function handleOptions(request) { - const corsHeaders = { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", - "Access-Control-Max-Age": "86400" + const corsHeaders = { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", + "Access-Control-Max-Age": "86400" + }; + let headers = request.headers; + if (headers.get("Origin") !== null && headers.get("Access-Control-Request-Method") !== null) { + let respHeaders = { + ...corsHeaders, + "Access-Control-Allow-Headers": request.headers.get("Access-Control-Request-Headers") || "" }; - let headers = request.headers; - if (headers.get("Origin") !== null && headers.get("Access-Control-Request-Method") !== null) { - let respHeaders = { - ...corsHeaders, - "Access-Control-Allow-Headers": request.headers.get("Access-Control-Request-Headers") || "" - }; - return new Response(null, { - headers: respHeaders - }); - } else { - return new Response(null, { - headers: { - Allow: "GET, HEAD, POST, OPTIONS" - } - }); - } + return new Response(null, { + headers: respHeaders + }); + } else { + return new Response(null, { + headers: { + Allow: "GET, HEAD, POST, OPTIONS" + } + }); + } } // src/index.ts var src_default = { - async fetch(request, env, ctx) { - if (request.method === "OPTIONS") { - return handleOptions(request); - } - return handleDownload(request); + async fetch(request, env, ctx) { + if (request.method === "OPTIONS") { + return handleOptions(request); } + return handleDownload(request); + } }; export { - src_default as default + src_default as default }; //# sourceMappingURL=index.js.map