允许 Shell 脚本强指定请求脚本内容.

This commit is contained in:
LamGC 2023-01-28 21:42:03 +08:00
parent b65f1b8ceb
commit a916923eea
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 32 additions and 19 deletions

View File

@ -7,7 +7,7 @@ sshkey_url="https://q-ssh.lamgc.me/ssh.keys"
# 默认的 Cron 执行计划, 每天凌晨 0 点执行 # 默认的 Cron 执行计划, 每天凌晨 0 点执行
default_cron="0 0 * * *" default_cron="0 0 * * *"
# 脚本 Url # 脚本 Url
script_url="https://q-ssh.lamgc.me" script_url="https://q-ssh.lamgc.me/script.sh"
############ 脚本区 ########## ############ 脚本区 ##########

View File

@ -5,7 +5,9 @@ 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 sshKeyUrls = [`https://github.com/${githubUserName}.keys`]; const sshKeyUrls = [
`https://github.com/${githubUserName}.keys`
];
// 建议在此设置备用的 SSH 公钥, 以防 Github 无法使用. // 建议在此设置备用的 SSH 公钥, 以防 Github 无法使用.
const backupSshKeys = ``; const backupSshKeys = ``;
@ -13,6 +15,26 @@ function getUserAgent(request) {
return request.headers.get("User-Agent"); return request.headers.get("User-Agent");
} }
async function sendScriptContent() {
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"
}
});
}
}
export default { export default {
async fetch(request, env) { async fetch(request, env) {
const { pathname } = new URL(request.url); const { pathname } = new URL(request.url);
@ -46,23 +68,7 @@ export default {
} else if (pathname === "/") { } else if (pathname === "/") {
const userAgent = getUserAgent(request); const userAgent = getUserAgent(request);
if (userAgent != null && userAgent.match(/curl|libcurl/) !== null) { if (userAgent != null && userAgent.match(/curl|libcurl/) !== null) {
let scriptResp = await fetch(new Request(installScriptUrl)); return await sendScriptContent();
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 { } else {
return new Response("", { return new Response("", {
status: 301, status: 301,
@ -72,6 +78,13 @@ export default {
} }
}); });
} }
} else if (pathname === "/script.sh") {
return await sendScriptContent();
} else {
return new Response("Not found.", {
status: 404,
statusText: "Not Found"
})
} }
} }
} }