feat(setup): add more cert providers and removed package install

This commit is contained in:
yusing 2025-04-29 08:34:50 +08:00
parent e9a6484bff
commit 7e3b160122

View file

@ -2,6 +2,33 @@
set -e # Exit on error set -e # Exit on error
check_cmd() {
not_available=()
for cmd in "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
not_available+=("$cmd")
fi
done
if [ "${#not_available[@]}" -gt 0 ]; then
echo "Error: ${not_available[*]} unavailable, please install it first"
exit 1
fi
}
check_cmd openssl docker
# quit if running user is root
if [ "$EUID" -eq 0 ]; then
echo "Error: Please do not run this script as root"
exit 1
fi
# check if user has docker permission
if [ -z "$(docker ps >/dev/null 2>&1)" ]; then
echo "Error: User $USER does not have permission to run docker, please add it to docker group"
exit 1
fi
# Detect download tool # Detect download tool
if command -v curl >/dev/null 2>&1; then if command -v curl >/dev/null 2>&1; then
DOWNLOAD_TOOL="curl" DOWNLOAD_TOOL="curl"
@ -10,13 +37,8 @@ elif command -v wget >/dev/null 2>&1; then
DOWNLOAD_TOOL="wget" DOWNLOAD_TOOL="wget"
DOWNLOAD_CMD="wget -qO" DOWNLOAD_CMD="wget -qO"
else else
read -p "Neither curl nor wget is installed, install curl? (y/n): " INSTALL echo "Error: Neither curl nor wget is installed. Please install one of them and try again."
if [ "$INSTALL" == "y" ]; then exit 1
install_pkg "curl"
else
echo "Error: Neither curl nor wget is installed. Please install one of them and try again."
exit 1
fi
fi fi
echo "Using ${DOWNLOAD_TOOL} for downloads" echo "Using ${DOWNLOAD_TOOL} for downloads"
@ -36,44 +58,12 @@ COMPOSE_FILE_NAME="compose.yml"
COMPOSE_EXAMPLE_FILE_NAME="compose.example.yml" COMPOSE_EXAMPLE_FILE_NAME="compose.example.yml"
CONFIG_FILE_NAME="config.yml" CONFIG_FILE_NAME="config.yml"
CONFIG_EXAMPLE_FILE_NAME="config.example.yml" CONFIG_EXAMPLE_FILE_NAME="config.example.yml"
CONFIG_FILE_PATH="${CONFIG_BASE_PATH}/${CONFIG_FILE_NAME}"
REQUIRED_DIRECTORIES=("config" "logs" "error_pages" "data" "certs") REQUIRED_DIRECTORIES=("config" "logs" "error_pages" "data" "certs")
echo "Setting up GoDoxy" echo "Setting up GoDoxy"
echo "Branch: ${BRANCH}" echo "Branch: ${BRANCH}"
install_pkg() {
# detect package manager
if command -v apt >/dev/null 2>&1; then
apt install -y "$1"
elif command -v yum >/dev/null 2>&1; then
yum install -y "$1"
elif command -v pacman >/dev/null 2>&1; then
pacman -S --noconfirm "$1"
else
echo "Error: No supported package manager found"
exit 1
fi
}
check_pkg() {
local cmd="$1"
local pkg="$2"
if ! command -v "$cmd" >/dev/null 2>&1; then
# check if user is root
if [ "$EUID" -ne 0 ]; then
echo "Error: $pkg is not installed and you are not running as root. Please install it and try again."
exit 1
fi
read -p "$pkg is not installed, install it? (y/n): " INSTALL
if [ "$INSTALL" == "y" ]; then
install_pkg "$pkg"
else
echo "Error: $pkg is not installed. Please install it and try again."
exit 1
fi
fi
}
# Function to check if file/directory exists # Function to check if file/directory exists
has_file_or_dir() { has_file_or_dir() {
[ -e "$1" ] [ -e "$1" ]
@ -134,6 +124,32 @@ ask_while_empty() {
eval "$var_name=\"$value\"" eval "$var_name=\"$value\""
} }
ask_multiple_choice() {
local var_name="$1"
local prompt="$2"
shift 2
local choices=("$@")
local n_choices="${#choices[@]}"
local value=""
local valid=0
while [ $valid -eq 0 ]; do
echo -e "$prompt"
for i in "${!choices[@]}"; do
echo "$((i + 1)). ${choices[$i]}"
done
read -p "Enter your choice: " value
if [ -z "$value" ]; then
echo "Error: $var_name cannot be empty, please try again"
fi
if [ "$value" -gt "$n_choices" ] || [ "$value" -lt 1 ]; then
echo "Error: invalid choice, please try again"
else
valid=1
fi
done
eval "$var_name=\"${choices[$((value - 1))]}\""
}
get_timezone() { get_timezone() {
if [ -f /etc/timezone ]; then if [ -f /etc/timezone ]; then
TIMEZONE=$(cat /etc/timezone) TIMEZONE=$(cat /etc/timezone)
@ -143,21 +159,21 @@ get_timezone() {
elif command -v timedatectl >/dev/null 2>&1; then elif command -v timedatectl >/dev/null 2>&1; then
TIMEZONE=$(timedatectl status | grep "Time zone" | awk '{print $3}') TIMEZONE=$(timedatectl status | grep "Time zone" | awk '{print $3}')
if [ -n "$TIMEZONE" ]; then if [ -n "$TIMEZONE" ]; then
echo "$TIMEZONE" echo "Detected timezone: $TIMEZONE"
fi fi
else else
echo "Warning: could not detect timezone, you may set it manually in ${DOT_ENV_PATH} to have correct time in logs" echo "Warning: could not detect timezone, you may set it manually in ${DOT_ENV_PATH} to have correct time in logs"
fi fi
} }
# check if running user is root setenv() {
if [ "$EUID" -ne 0 ]; then local key="$1"
echo "Error: Please run this script as root" local value="$2"
exit 1 # uncomment line if it is commented
fi sed -i "/^# *${key}=/s/^# *//" "$DOT_ENV_PATH"
sed -i "s|${key}=.*|${key}=\"${value}\"|" "$DOT_ENV_PATH"
check_pkg "openssl" "openssl" echo "${key}=${value}"
check_pkg "docker" "docker-ce" }
# Setup required configurations # Setup required configurations
# 1. Setup required directories # 1. Setup required directories
@ -169,20 +185,19 @@ done
fetch_file "$DOT_ENV_EXAMPLE_PATH" "$DOT_ENV_PATH" fetch_file "$DOT_ENV_EXAMPLE_PATH" "$DOT_ENV_PATH"
# set random JWT secret # set random JWT secret
JWT_SECRET=$(openssl rand -base64 32) setenv "GODOXY_API_JWT_SECRET" "$(openssl rand -base64 32)"
sed -i "s|GODOXY_API_JWT_SECRET=.*|GODOXY_API_JWT_SECRET=${JWT_SECRET}|" "$DOT_ENV_PATH"
# set timezone # set timezone
get_timezone get_timezone
if [ -n "$TIMEZONE" ]; then if [ -n "$TIMEZONE" ]; then
sed -i "s|TZ=.*|TZ=${TIMEZONE}|" "$DOT_ENV_PATH" setenv "TZ" "$TIMEZONE"
fi fi
# 3. docker-compose.yml # 3. docker-compose.yml
fetch_file "$COMPOSE_EXAMPLE_FILE_NAME" "$COMPOSE_FILE_NAME" fetch_file "$COMPOSE_EXAMPLE_FILE_NAME" "$COMPOSE_FILE_NAME"
# 4. config.yml # 4. config.yml
fetch_file "$CONFIG_EXAMPLE_FILE_NAME" "${CONFIG_BASE_PATH}/${CONFIG_FILE_NAME}" fetch_file "$CONFIG_EXAMPLE_FILE_NAME" "$CONFIG_FILE_PATH"
# 5. setup authentication # 5. setup authentication
@ -191,50 +206,71 @@ echo "Setting up login user"
ask_while_empty "Enter login username: " LOGIN_USERNAME ask_while_empty "Enter login username: " LOGIN_USERNAME
ask_while_empty "Enter login password: " LOGIN_PASSWORD ask_while_empty "Enter login password: " LOGIN_PASSWORD
echo "Setting up login user \"$LOGIN_USERNAME\" with password \"$LOGIN_PASSWORD\"" echo "Setting up login user \"$LOGIN_USERNAME\" with password \"$LOGIN_PASSWORD\""
sed -i "s|GODOXY_API_USERNAME=.*|GODOXY_API_USERNAME=${LOGIN_USERNAME}|" "$DOT_ENV_PATH" setenv "GODOXY_API_USERNAME" "$LOGIN_USERNAME"
sed -i "s|GODOXY_API_PASSWORD=.*|GODOXY_API_PASSWORD=${LOGIN_PASSWORD}|" "$DOT_ENV_PATH" setenv "GODOXY_API_PASSWORD" "$LOGIN_PASSWORD"
# 6. setup autocert # 6. setup autocert
# ask if want to enable autocert
echo "Setting up autocert for SSL certificate"
ask_while_empty "Do you want to enable autocert? (y/n): " ENABLE_AUTOCERT
# quit if not using autocert # quit if not using autocert
if [ "$ENABLE_AUTOCERT" == "y" ]; then if [ "$ENABLE_AUTOCERT" == "y" ]; then
# ask for domain # ask for domain
echo "Setting up autocert" echo "Setting up autocert"
ask_while_empty "Enter domain (e.g. example.com): " DOMAIN skip=false
# ask for email # ask for email
ask_while_empty "Enter email for Let's Encrypt: " EMAIL ask_while_empty "Enter email for Let's Encrypt: " EMAIL
# ask if using cloudflare # select dns provider
ask_while_empty "Is cloudflare the current DNS nameserver? (y/n): " USE_CLOUDFLARE ask_multiple_choice DNS_PROVIDER "Select DNS provider:" \
"Cloudflare" \
"CloudDNS" \
"DuckDNS" \
"Other"
# ask for cloudflare api key # ask for dns provider credentials
if [ "$USE_CLOUDFLARE" = "y" ]; then if [ "$DNS_PROVIDER" == "Cloudflare" ]; then
ask_while_empty "Enter cloudflare zone api key: " CLOUDFLARE_API_KEY provider="cloudflare"
cat <<EOF >>"$CONFIG_BASE_PATH/$CONFIG_FILE_NAME" read -p "Enter cloudflare zone api key: " auth_token
autocert: options=("auth_token: \"$auth_token\"")
provider: cloudflare elif [ "$DNS_PROVIDER" == "CloudDNS" ]; then
email: $EMAIL provider="clouddns"
domains: read -p "Enter clouddns client_id: " client_id
- "*.${DOMAIN}" read -p "Enter clouddns email: " email
- "${DOMAIN}" read -p "Enter clouddns password: " password
options: options=(
auth_token: "$CLOUDFLARE_API_KEY" "client_id: \"$client_id\""
EOF "email: \"$email\""
"password: \"$password\""
)
elif [ "$DNS_PROVIDER" == "DuckDNS" ]; then
provider="duckdns"
read -p "Enter duckdns token: " token
options=("token: \"$token\"")
else else
echo "Not using cloudflare, skipping autocert setup" echo "Please check Wiki for other DNS providers: ${WIKI_URL}/Supported-DNS%E2%80%9001-Providers"
echo "Please refer to ${WIKI_URL}/Supported-DNS-01-Providers for more information" echo "Skipping autocert setup"
skip=true
fi
if [ "$skip" == false ]; then
autocert_config="
autocert:
provider: \"${provider}\"
email: \"${EMAIL}\"
domains:
- \"*.${BASE_DOMAIN}\"
- \"${BASE_DOMAIN}\"
options:
"
for option in "${options[@]}"; do
autocert_config+=" ${option}\n"
done
autocert_config+="\n"
echo -e "${autocert_config}$(<"$CONFIG_FILE_PATH")" >"$CONFIG_FILE_PATH"
fi fi
fi fi
# 7. setup permission # 7. set uid and gid
setenv "GODOXY_UID" "$(id -u)"
echo "Setting up permissions" setenv "GODOXY_GID" "$(id -g)"
chown -R 1000:1000 .
chmod -R 644 .
echo "Setup finished" echo "Setup finished"