From bf8e6e95b948ff6cd77125b3b16c03994bbb9575 Mon Sep 17 00:00:00 2001 From: undefined Date: Thu, 27 Oct 2022 17:09:41 +0800 Subject: [PATCH] install: auto install caddy --- install/install.sh | 8 +++++++ install/install.ts | 58 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/install/install.sh b/install/install.sh index fc92ca63..d3724550 100644 --- a/install/install.sh +++ b/install/install.sh @@ -1,4 +1,12 @@ #!/bin/bash +if [ $EUID != 0 ]; then + echo "This script requires root however you are currently running under another user." + echo "We will call sudo directly for you." + echo "Please input your account password below:" + echo "安装脚本需要使用 root 权限,请在下方输入此账号的密码确认授权:" + sudo "$0" "$@" + exit $? +fi set -e echo "Executing Hydro install script v3.0.0" echo "Hydro includes anonymous system telemetry, diff --git a/install/install.ts b/install/install.ts index 6df05eca..60c848de 100644 --- a/install/install.ts +++ b/install/install.ts @@ -1,7 +1,9 @@ +/* eslint-disable no-await-in-loop */ /* eslint-disable import/no-dynamic-require */ /* eslint-disable no-sequences */ import { execSync, ExecSyncOptions } from 'child_process'; import { existsSync, readFileSync, writeFileSync } from 'fs'; +import net from 'net'; const exec = (command: string, args?: ExecSyncOptions) => { try { @@ -31,6 +33,7 @@ const locales = { 'install.compiler': '正在安装编译器...', 'install.hydro': '正在安装 Hydro...', 'install.done': 'Hydro 安装成功!', + 'install.alldone': '安装已全部完成。', 'extra.dbUser': '数据库用户名: hydro', 'extra.dbPassword': '数据库密码: %s', 'info.skip': '步骤已跳过。', @@ -119,6 +122,30 @@ domainName: executor_server uid: 1536 gid: 1536 `; +const Caddyfile = `\ +# 如果你希望使用其他端口或使用域名,修改此处 :80 的值后在 ~/.hydro 目录下使用 caddy reload 重载配置。 +# 如果你在当前配置下能够通过 http://你的域名/ 正常访问到网站,若需开启 ssl, +# 仅需将 :80 改为你的域名(如 hydro.ac)后直接重载配置即可自动签发 ssl 证书。 +# 清注意在防火墙/安全组中放行端口,且部分运营商会拦截未经备案的域名。 +# For more information, refer to caddy v2 documentation. +:80 { + reverse_proxy http://127.0.0.1:8888 { + header_up x-forwarded-for {remote_host} + header_up x-forwarded-host {hostport} + } +} +`; + +const isPortFree = async (port: number) => { + const server = net.createServer(); + const res = await new Promise((resolve) => { + server.once('error', () => resolve(false)); + server.once('listening', () => resolve(true)); + server.listen(port); + }); + server.close(); + return res; +}; function removeOptionalEsbuildDeps() { const yarnGlobalPath = exec('yarn global dir').output?.trim() || ''; @@ -176,9 +203,7 @@ const steps = [ { init: 'install.mongodb', operations: [ - `nix-env -iA hydro.mongodb${avx2 ? 5 : 4}${CN ? '-cn' : ''}`, - 'nix-env -iA nixpkgs.mongodb-tools', - `nix-env -iA hydro.mongosh${avx2 ? 5 : 4}${CN ? '-cn' : ''}`, + `nix-env -iA hydro.mongodb${avx2 ? 5 : 4}${CN ? '-cn' : ''} hydro.mongosh${avx2 ? 5 : 4}${CN ? '-cn' : ''} nixpkgs.mongodb-tools`, ], }, { @@ -194,6 +219,14 @@ const steps = [ 'nix-env -iA hydro.sandbox', ], }, + { + init: 'install.caddy', + skip: () => !exec('caddy version').code, + operations: [ + 'nix-env -iA nixpkgs.caddy', + () => writeFileSync(`${process.env.HOME}/.hydro/Caddyfile`, Caddyfile), + ], + }, { init: 'install.hydro', operations: [ @@ -234,6 +267,12 @@ const steps = [ () => sleep(1000), `pm2 start bash --name hydro-sandbox -- -c "ulimit -s unlimited && hydro-sandbox -mount-conf ${process.env.HOME}/.hydro/mount.yaml"`, 'pm2 start hydrooj', + async () => { + if (!await isPortFree(80)) log.warn('port.80'); + exec('pm2 start caddy -- run', { cwd: `${process.env.HOME}/.hydro` }); + exec('hydrooj cli system set server.xff x-forwarded-for'); + exec('hydrooj cli system set server.xhost x-forwarded-host'); + }, 'pm2 startup', 'pm2 save', ], @@ -272,6 +311,19 @@ const steps = [ () => rollbackResolveField(), ], }, + { + init: 'install.postinstall', + operations: [ + ['pm2 install pm2-logrotate', { retry: true }], + 'pm2 set pm2-logrotate:max_size 64M', + ], + }, + { + init: 'install.alldone', + operations: [ + () => log.info('install.alldone'), + ], + }, ]; async function main() {