From 2ddcf7c6ba874ff65587084eeccfa43a8b4b4d45 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 28 Jan 2023 14:34:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=20Worker=20?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E4=B8=AD=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install-ssh-worker/src/index.js | 44 ++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/install-ssh-worker/src/index.js b/install-ssh-worker/src/index.js index e034950..c639bd6 100644 --- a/install-ssh-worker/src/index.js +++ b/install-ssh-worker/src/index.js @@ -5,7 +5,7 @@ const githubInstSshProjectName = "quickly-conf-sshd"; const baseUrl = `https://${githubUserName.toLowerCase()}.github.io/${githubInstSshProjectName}/`; const installScriptUrl = `${baseUrl}/conf-sshd.sh`; // 如果出现 Github 无法使用的情况, 可以修改 sshKeyUrl 来变更位置. -const sshKeyUrl = `https://github.com/${githubUserName}.keys`; +const sshKeyUrls = [`https://github.com/${githubUserName}.keys`]; // 建议在此设置备用的 SSH 公钥, 以防 Github 无法使用. const backupSshKeys = ``; @@ -17,9 +17,19 @@ export default { async fetch(request, env) { const { pathname } = new URL(request.url); if (pathname === "/ssh.keys") { - let response = await fetch(new Request(sshKeyUrl)); - if (response.ok) { - return new Response(response.text(), { + for (let url of sshKeyUrls) { + let response = await fetch(new Request(url)); + if (response.ok) { + let keys = await response.text() + return new Response(keys, { + headers: { + "content-type": "text/plain; charset=utf-8" + } + }); + } + } + if (backupSshKeys.length > 0) { + return new Response(backupSshKeys, { headers: { "content-type": "text/plain; charset=utf-8" } @@ -35,14 +45,24 @@ export default { } } else if (pathname === "/") { const userAgent = getUserAgent(request); - if (userAgent.match(/curl|libcurl/) !== null) { - return new Response("", { - status: 301, - statusText: "Redirect", - headers: { - "Location": installScriptUrl - } - }); + if (userAgent != null && userAgent.match(/curl|libcurl/) !== null) { + let scriptResp = await fetch(new Request(installScriptUrl)); + if (scriptResp.ok) { + let scriptContent = await scriptResp.text(); + return new Response(scriptContent, { + headers: { + "content-type": "text/plain; charset=utf-8" + } + }); + } else { + return new Response("Failed to get install script.", { + status: 500, + statusText: "Failed to get install script", + headers: { + "content-type": "text/plain; charset=utf-8" + } + }); + } } else { return new Response("", { status: 301,