修复了 Worker 脚本中的问题。

This commit is contained in:
LamGC 2023-01-28 14:34:10 +08:00
parent de1f094a3e
commit 2ddcf7c6ba
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -5,7 +5,7 @@ const githubInstSshProjectName = "quickly-conf-sshd";
const baseUrl = `https://${githubUserName.toLowerCase()}.github.io/${githubInstSshProjectName}/`; const baseUrl = `https://${githubUserName.toLowerCase()}.github.io/${githubInstSshProjectName}/`;
const installScriptUrl = `${baseUrl}/conf-sshd.sh`; const installScriptUrl = `${baseUrl}/conf-sshd.sh`;
// 如果出现 Github 无法使用的情况, 可以修改 sshKeyUrl 来变更位置. // 如果出现 Github 无法使用的情况, 可以修改 sshKeyUrl 来变更位置.
const sshKeyUrl = `https://github.com/${githubUserName}.keys`; const sshKeyUrls = [`https://github.com/${githubUserName}.keys`];
// 建议在此设置备用的 SSH 公钥, 以防 Github 无法使用. // 建议在此设置备用的 SSH 公钥, 以防 Github 无法使用.
const backupSshKeys = ``; const backupSshKeys = ``;
@ -17,9 +17,19 @@ export default {
async fetch(request, env) { async fetch(request, env) {
const { pathname } = new URL(request.url); const { pathname } = new URL(request.url);
if (pathname === "/ssh.keys") { if (pathname === "/ssh.keys") {
let response = await fetch(new Request(sshKeyUrl)); for (let url of sshKeyUrls) {
let response = await fetch(new Request(url));
if (response.ok) { if (response.ok) {
return new Response(response.text(), { 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: { headers: {
"content-type": "text/plain; charset=utf-8" "content-type": "text/plain; charset=utf-8"
} }
@ -35,14 +45,24 @@ export default {
} }
} else if (pathname === "/") { } else if (pathname === "/") {
const userAgent = getUserAgent(request); const userAgent = getUserAgent(request);
if (userAgent.match(/curl|libcurl/) !== null) { if (userAgent != null && userAgent.match(/curl|libcurl/) !== null) {
return new Response("", { let scriptResp = await fetch(new Request(installScriptUrl));
status: 301, if (scriptResp.ok) {
statusText: "Redirect", let scriptContent = await scriptResp.text();
return new Response(scriptContent, {
headers: { headers: {
"Location": installScriptUrl "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 { } else {
return new Response("", { return new Response("", {
status: 301, status: 301,