Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions ct/alpine-cloudflared.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env bash
# shellcheck source=misc/build.func
source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/build.func)
# Copyright (c) 2021-2026 community-scripts ORG
# Author: bandogora
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
# Source: https://www.cloudflare.com/

APP="Alpine-Cloudflared"
var_tags="${var_tags:-network;cloudflare}"
var_cpu="${var_cpu:-1}"
var_ram="${var_ram:-512}"
var_disk="${var_disk:-2}"
var_os="${var_os:-alpine}"
var_version="${var_version:-3.23}"
var_unprivileged="${var_unprivileged:-1}"
var_token="${var_token:-}"
var_config_path="${var_config_path:-/usr/local/etc/cloudflared}"

header_info "$APP"
variables
color
catch_errors

user_configuration() {
token_valid() {
local token="$1"

# Sanitize token of unprintable chars
token=$(echo "$token" | tr -cd '[:print:]')

# Validate token is present and alphanumeric (should be Base64)
if [ -z "$token" ] || ! [[ "$token" =~ ^[[:alnum:]]+$ ]]; then
return 1
fi

# export for use in install script
export TOKEN=$token
}

# If user supplied $var_token and it's valid skip menu
token_valid "$var_token" && return 0

while true; do
local type
if type=$(
whiptail --title "Tunnel Type" \
--menu "Select Tunnel Type:" 9 52 2 \
"remotely-managed" "Uses a token (Recommended)" \
"locally-managed" "Uses a local configuration file" \
3>&1 1>&2 2>&3
); then
# if "remotely-managed" get token from user
if [[ "$type" == "remotely-managed" ]]; then
if var_token=$(
whiptail --title "Tunnel Token" \
--inputbox "Enter Tunnel Token" 10 80 "" \
3>&1 1>&2 2>&3
); then
if token_valid "$var_token"; then
# break to continue script
break
else
# || true to prevent failure on escape key
whiptail --msgbox "Invalid token: contains special characters" 7 46 || true
fi
else
# continue to prevent failure on escape key
continue
fi
else
# export for use in install script and break to continue script
export CONFIG_PATH=$var_config_path
break
fi
else
clear
printf "\e[?25h"
echo -e "\n${CROSS}${RD}User exited script${CL}\n"
kill 0
exit 1
fi
done
}

function update_script() {
header_info
check_container_storage
check_container_resources

# Check if installation is present
if [[ ! -f /etc/init.d/cloudflared ]]; then
msg_error "No ${APP} Installation Found!"
exit
fi

# Stopping Services
msg_info "Stopping $APP"
rc-service cloudflared stop
msg_ok "Stopped $APP"

# Execute Update
msg_info "Updating $APP"
$STD apk -U upgrade
$STD cloudflared update
msg_ok "Updated $APP to $(cloudflared -V)"

# Starting Services
msg_info "Starting $APP"
rc-service cloudflared start
msg_ok "Started $APP"

# Cleaning up
msg_info "Cleaning Up"
$STD apk cache clean
find /var/log -type f -delete 2>/dev/null
find /tmp -mindepth 1 -delete 2>/dev/null
$STD apk update
msg_ok "Cleanup Completed"

# Last Action
msg_ok "Update Successful"
exit
}

start
user_configuration
if [ -z "$var_token" ]; then
echo -e "${TAB}📝${TAB}${BOLD}${DGN}Tunnel Config: ${BGN}${CONFIG_PATH}/config.yml${CL}"
else
echo -e "${TAB}🪪${TAB}${BOLD}${DGN}Tunnel Token: ${BGN}${TOKEN:0:33}...${CL}"
fi

build_container
description

msg_ok "Completed successfully!\n"
echo -e "${CREATING}${GN}${APP} setup has been successfully initialized!${CL}"
if [ -z "$var_token" ]; then
echo -e "${INFO}${YW} Edit the config file at:${CL}"
echo -e "${TAB}${ADVANCED}${GN} ${var_config_path}/config.yml${CL}"
echo -e "${INFO}${BGN}Run \"rc-service cloudflared start\" to start!${CL}"
fi
44 changes: 44 additions & 0 deletions frontend/public/json/alpine-cloudflared.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "Cloudflared",
"slug": "cloudflared",
"categories": [
4
],
"date_created": "2024-05-02",
"type": "ct",
"updateable": true,
"privileged": false,
"interface_port": null,
"documentation": "https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel",
"config_path": "/usr/local/etc/cloudflared/config.yml",
"website": "https://www.cloudflare.com",
"logo": "https://cdn.jsdelivr.net/gh/selfhst/icons@main/webp/cloudflare.webp",
"description": "Cloudflared is a command-line tool that allows you to securely access resources on the Cloudflare network, such as websites and APIs, from your local computer. It works by creating a secure tunnel between your computer and the Cloudflare network, allowing you to access resources as if they were on your local network.",
"install_methods": [
{
"type": "alpine",
"script": "ct/alpine-cloudflared.sh",
"resources": {
"cpu": 1,
"ram": 512,
"hdd": 2,
"os": "Alpine",
"version": "3.23"
}
}
],
"default_credentials": {
"username": null,
"password": null
},
"notes": [
{
"text": "After install, run: cloudflared tunnel login && cloudflared tunnel create <NAME>",
"type": "info"
},
{
"text": "Or create tunnel via Cloudflare Zero Trust Dashboard",
"type": "info"
}
]
}
96 changes: 96 additions & 0 deletions install/alpine-cloudflared-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash

# Copyright (c) 2021-2026 community-scripts ORG
# Author: bandogora
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
# Source: https://www.cloudflare.com/

# shellcheck disable=SC1091
source /dev/stdin <<<"$FUNCTIONS_FILE_PATH"
color
verb_ip6
catch_errors
setting_up_container
network_check
update_os

msg_info "Creating Cloudflared User"

# Enable sysctl service so conf is applied on start
$STD rc-update add sysctl
# Increase ping_group_range by one to create space for cloudflared group
cat <<EOF >/etc/sysctl.d/90-cloudflared.conf
# Increse ICMP ping_group_range
net.ipv4.ping_group_range = 65534 65535
EOF

# Apply 90-cloudflared.conf now
$STD sysctl -p /etc/sysctl.d/90-cloudflared.conf
# Create cloudflared group in ping_group_range
addgroup -g 65535 cloudflared
adduser -DH -s /sbin/nologin -G cloudflared cloudflared
msg_ok "Created Cloudflared User"

msg_info "Installing Cloudflared"
fetch_and_deploy_gh_release cloudflared cloudflare/cloudflared singlefile latest /usr/bin "cloudflared-linux-$(get_system_arch)"
msg_ok "Installed Cloudflared"

msg_info "Creating Service"
cat <<EOF >/etc/init.d/cloudflared
#!/sbin/openrc-run

name="cloudflared"
pidfile="/run/\$name.pid"
output_log="/var/log/\$name.log"
error_log="/var/log/\$name.err"

command="/usr/bin/cloudflared"
EOF


if [ -z "${TOKEN:-}" ]; then
mkdir -p "$CONFIG_PATH"
# Create empty config file so permissions are correct and users can find it
echo "tunnel: proxmoxve" >"$CONFIG_PATH/config.yml"
chown -R cloudflared:cloudflared "$CONFIG_PATH"
echo "command_args=\"tunnel --config $CONFIG_PATH/config.yml run proxmoxve\"" >>/etc/init.d/cloudflared
else
echo "command_args=\"tunnel run --token $TOKEN\"" >>/etc/init.d/cloudflared
fi

cat <<EOF >>/etc/init.d/cloudflared
command_user="cloudflared"
command_background="yes"

start_pre() {
checkpath -f -m 0644 -o "\$command_user:\$command_user" "/var/log/\$name.log"
checkpath -f -m 0644 -o "\$command_user:\$command_user" "/var/log/\$name.err"
}
EOF

chmod +x /etc/init.d/cloudflared
msg_ok "Created Service"

msg_info "Enabling $APPLICATION service"
if $STD rc-update add cloudflared; then
msg_ok "Enabled $APPLICATION service"
else
msg_error "Failed to enable $APPLICATION service"
exit 1
fi

# Start service now if externally managed, otherwise user needs to setup config first.
if [ -n "${TOKEN:-}" ]; then
msg_info "Starting $APPLICATION service"
if $STD rc-service cloudflared start; then
msg_ok "$APPLICATION service Running"
else
msg_error "Failed to start $APPLICATION service"
cat /var/log/cloudflared.err
exit 1
fi
fi

motd_ssh
customize
cleanup_lxc