mirror of
https://github.com/LamGC/quickly-conf-sshd.git
synced 2025-04-29 22:27:32 +00:00
将部分脚本参数交给 Worker 管理.
This commit is contained in:
parent
dbd05b2ca8
commit
347dbe2135
@ -1,24 +1,38 @@
|
|||||||
|
// 改成你自己的 Github 用户名,注意是登录 Github 的那个用户名.
|
||||||
const githubUserName = "LamGC";
|
const githubUserName = "LamGC";
|
||||||
|
// 改成你 Fork 后的仓库名,记得要开启 Github Pages 功能.
|
||||||
const githubInstSshProjectName = "quickly-conf-sshd";
|
const githubInstSshProjectName = "quickly-conf-sshd";
|
||||||
|
// 如果可以,建议在此设置备用的 SSH 公钥, 以防 Github 无法使用.
|
||||||
|
const backupSshKeys = ``;
|
||||||
|
// Worker 的访问地址, 如果不填的话默认为请求的地址, 填了就会用这里的地址(要去 Worker 的触发器那绑定, 否则无效).
|
||||||
|
const defaultBaseUrl = "";
|
||||||
|
// Cron 表达式, 默认 1 天执行一次更新.
|
||||||
|
const cronExpression = "0 0 0 * * ?";
|
||||||
|
|
||||||
// 一般不用改.
|
// 下面的东西一般不用改.
|
||||||
const baseUrl = `https://${githubUserName.toLowerCase()}.github.io/${githubInstSshProjectName}/`;
|
const baseRepoPageUrl = `https://${githubUserName.toLowerCase()}.github.io/${githubInstSshProjectName}/`;
|
||||||
const installScriptUrl = `${baseUrl}/conf-sshd.sh`;
|
const installScriptUrl = `${baseRepoPageUrl}/conf-sshd.sh`;
|
||||||
// 如果出现 Github 无法使用的情况, 可以修改 sshKeyUrl 来变更位置.
|
// 如果出现 Github 无法使用的情况, 可以修改 sshKeyUrl 来变更位置.
|
||||||
|
// 也可以添加额外的 SSH 公钥地址(比如 KeyBase).
|
||||||
const sshKeyUrls = [
|
const sshKeyUrls = [
|
||||||
`https://github.com/${githubUserName}.keys`
|
`https://github.com/${githubUserName}.keys`
|
||||||
];
|
];
|
||||||
// 建议在此设置备用的 SSH 公钥, 以防 Github 无法使用.
|
|
||||||
const backupSshKeys = ``;
|
|
||||||
|
|
||||||
function getUserAgent(request) {
|
// 下面是脚本的占位符.
|
||||||
return request.headers.get("User-Agent");
|
const SCRIPT_PH_SSH_KEY_URL = "{{ SSH_KEY_URL }}"
|
||||||
}
|
const SCRIPT_PH_SCRIPT_URL = "{{ SCRIPT_URL }}"
|
||||||
|
const SCRIPT_PH_DEFAULT_CRON = "{{ DEFAULT_CRON }}"
|
||||||
|
|
||||||
async function sendScriptContent() {
|
|
||||||
|
async function sendScriptContent(baseUrl) {
|
||||||
let scriptResp = await fetch(new Request(installScriptUrl));
|
let scriptResp = await fetch(new Request(installScriptUrl));
|
||||||
if (scriptResp.ok) {
|
if (scriptResp.ok) {
|
||||||
let scriptContent = await scriptResp.text();
|
let scriptContent = await scriptResp.text();
|
||||||
|
|
||||||
|
scriptContent = scriptContent.replace(SCRIPT_PH_SSH_KEY_URL, `${baseUrl}/ssh.keys`)
|
||||||
|
.replace(SCRIPT_PH_SCRIPT_URL, `${baseUrl}/script.sh`)
|
||||||
|
.replace(SCRIPT_PH_DEFAULT_CRON, cronExpression)
|
||||||
|
|
||||||
return new Response(scriptContent, {
|
return new Response(scriptContent, {
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "text/plain; charset=utf-8"
|
"content-type": "text/plain; charset=utf-8"
|
||||||
@ -37,7 +51,8 @@ async function sendScriptContent() {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
async fetch(request, env) {
|
async fetch(request, env) {
|
||||||
const { pathname } = new URL(request.url);
|
const { pathname, host } = new URL(request.url);
|
||||||
|
const baseUrl = defaultBaseUrl || `https://${host}`;
|
||||||
if (pathname === "/ssh.keys") {
|
if (pathname === "/ssh.keys") {
|
||||||
for (let url of sshKeyUrls) {
|
for (let url of sshKeyUrls) {
|
||||||
let response = await fetch(new Request(url));
|
let response = await fetch(new Request(url));
|
||||||
@ -66,7 +81,7 @@ export default {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (pathname === "/") {
|
} else if (pathname === "/") {
|
||||||
const userAgent = getUserAgent(request);
|
const userAgent = request.headers.get("User-Agent");
|
||||||
if (userAgent != null && userAgent.match(/curl|libcurl/) !== null) {
|
if (userAgent != null && userAgent.match(/curl|libcurl/) !== null) {
|
||||||
return await sendScriptContent();
|
return await sendScriptContent();
|
||||||
} else {
|
} else {
|
||||||
@ -74,7 +89,7 @@ export default {
|
|||||||
status: 301,
|
status: 301,
|
||||||
statusText: "Redirect",
|
statusText: "Redirect",
|
||||||
headers: {
|
headers: {
|
||||||
"Location": baseUrl
|
"Location": baseRepoPageUrl
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -3,11 +3,11 @@
|
|||||||
########## 一些配置 ##########
|
########## 一些配置 ##########
|
||||||
|
|
||||||
# 默认获取 SSH key 的地方,一般是 Github.
|
# 默认获取 SSH key 的地方,一般是 Github.
|
||||||
sshkey_url="https://q-ssh.lamgc.me/ssh.keys"
|
sshkey_url="{{ SSH_KEY_URL }}"
|
||||||
# 默认的 Cron 执行计划, 每天凌晨 0 点执行
|
# 默认的 Cron 执行计划, 每天凌晨 0 点执行
|
||||||
default_cron="0 0 * * *"
|
default_cron="{{ DEFAULT_CRON }}"
|
||||||
# 脚本 Url
|
# 脚本 Url
|
||||||
script_url="https://q-ssh.lamgc.me/script.sh"
|
script_url="{{ SCRIPT_URL }}"
|
||||||
|
|
||||||
############ 脚本区 ##########
|
############ 脚本区 ##########
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user