install_debian.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SERVICE_NAME="vmess-domain-rotator"
  4. RUN_USER=""
  5. RUN_GROUP=""
  6. RUN_USER_SET="0"
  7. RUN_GROUP_SET="0"
  8. RUN_HOME=""
  9. INTERVAL="1h"
  10. INSTALL_DEPS="1"
  11. GIT_PUSH_ENABLED="1"
  12. GIT_PUSH_REMOTE="origin"
  13. GIT_HTTP_USERNAME="git"
  14. GIT_HTTP_TOKEN=""
  15. GIT_HTTP_TOKEN_FILE=""
  16. GIT_USE_CREDENTIAL_STORE="1"
  17. GIT_CREDENTIALS_FILE=""
  18. usage() {
  19. cat <<'EOF'
  20. Usage: sudo bash scripts/install_debian.sh [options]
  21. Default behavior:
  22. - Uses current git repository directory as working directory (in-place mode)
  23. - Uses the user executing sudo as service user
  24. - Enables git push after runtime-state commits
  25. Options:
  26. --user <name> Service user (default: current sudo user)
  27. --group <name> Service group (default: current sudo user's group)
  28. --interval <value> Timer interval, e.g. 1h/10min (default: 1h)
  29. --git-push <0|1> Enable/disable push to remote (default: 1)
  30. --git-push-remote <name> Remote name for push (default: origin)
  31. --git-http-username <u> Username for HTTPS auth (default: git)
  32. --git-http-token <t> HTTPS token for non-interactive push
  33. --git-http-token-file <f> Read HTTPS token from file
  34. --git-use-credential-store <0|1> Use git credential.helper store (default: 1)
  35. --git-credentials-file <f> Custom credentials file for helper store
  36. --no-install-deps Skip apt dependency install
  37. -h, --help Show help
  38. Examples:
  39. sudo bash scripts/install_debian.sh
  40. sudo bash scripts/install_debian.sh --interval 10min
  41. sudo bash scripts/install_debian.sh --git-push 0
  42. sudo bash scripts/install_debian.sh --git-http-username aurora --git-http-token-file /root/.config/vmess-token
  43. sudo bash scripts/install_debian.sh --git-use-credential-store 1 --git-credentials-file /home/aurora/.git-credentials
  44. EOF
  45. }
  46. run_as_service_user() {
  47. runuser -u "$RUN_USER" -- env HOME="$RUN_HOME" "$@"
  48. }
  49. while [[ $# -gt 0 ]]; do
  50. case "$1" in
  51. --user)
  52. RUN_USER="$2"
  53. RUN_USER_SET="1"
  54. shift 2
  55. ;;
  56. --group)
  57. RUN_GROUP="$2"
  58. RUN_GROUP_SET="1"
  59. shift 2
  60. ;;
  61. --interval)
  62. INTERVAL="$2"
  63. shift 2
  64. ;;
  65. --git-push)
  66. GIT_PUSH_ENABLED="$2"
  67. shift 2
  68. ;;
  69. --git-push-remote)
  70. GIT_PUSH_REMOTE="$2"
  71. shift 2
  72. ;;
  73. --git-http-username)
  74. GIT_HTTP_USERNAME="$2"
  75. shift 2
  76. ;;
  77. --git-http-token)
  78. GIT_HTTP_TOKEN="$2"
  79. shift 2
  80. ;;
  81. --git-http-token-file)
  82. GIT_HTTP_TOKEN_FILE="$2"
  83. shift 2
  84. ;;
  85. --git-use-credential-store)
  86. GIT_USE_CREDENTIAL_STORE="$2"
  87. shift 2
  88. ;;
  89. --git-credentials-file)
  90. GIT_CREDENTIALS_FILE="$2"
  91. shift 2
  92. ;;
  93. --no-install-deps)
  94. INSTALL_DEPS="0"
  95. shift
  96. ;;
  97. -h|--help)
  98. usage
  99. exit 0
  100. ;;
  101. *)
  102. echo "Unknown option: $1" >&2
  103. usage
  104. exit 1
  105. ;;
  106. esac
  107. done
  108. if [[ "$(id -u)" -ne 0 ]]; then
  109. echo "Please run as root (use sudo)." >&2
  110. exit 1
  111. fi
  112. if ! command -v runuser >/dev/null 2>&1; then
  113. echo "Error: runuser is required on Debian for configuring service-user git credentials" >&2
  114. exit 1
  115. fi
  116. SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  117. if ! git -C "$SOURCE_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  118. echo "Error: Current directory is not a git repository." >&2
  119. echo "This script must be run from within a git repository." >&2
  120. exit 1
  121. fi
  122. APP_DIR="$SOURCE_DIR"
  123. if [[ -n "${SUDO_USER:-}" ]] && [[ "$RUN_USER_SET" != "1" ]]; then
  124. RUN_USER="$SUDO_USER"
  125. fi
  126. if [[ -n "${SUDO_USER:-}" ]] && [[ "$RUN_GROUP_SET" != "1" ]]; then
  127. RUN_GROUP="$(id -gn "$SUDO_USER")"
  128. fi
  129. if [[ -z "$RUN_USER" ]]; then
  130. echo "Error: Could not determine service user. Please run with sudo or specify --user" >&2
  131. exit 1
  132. fi
  133. if [[ -z "$RUN_GROUP" ]]; then
  134. echo "Error: Could not determine service group. Please run with sudo or specify --group" >&2
  135. exit 1
  136. fi
  137. if [[ ! "$GIT_PUSH_ENABLED" =~ ^[01]$ ]]; then
  138. echo "Error: --git-push must be 0 or 1" >&2
  139. exit 1
  140. fi
  141. if [[ ! "$GIT_USE_CREDENTIAL_STORE" =~ ^[01]$ ]]; then
  142. echo "Error: --git-use-credential-store must be 0 or 1" >&2
  143. exit 1
  144. fi
  145. if [[ -z "$GIT_PUSH_REMOTE" ]]; then
  146. echo "Error: --git-push-remote cannot be empty" >&2
  147. exit 1
  148. fi
  149. if [[ -n "$GIT_HTTP_TOKEN" ]] && [[ -n "$GIT_HTTP_TOKEN_FILE" ]]; then
  150. echo "Error: provide either --git-http-token or --git-http-token-file, not both" >&2
  151. exit 1
  152. fi
  153. if [[ -n "$GIT_HTTP_TOKEN_FILE" ]] && [[ ! -r "$GIT_HTTP_TOKEN_FILE" ]]; then
  154. echo "Error: cannot read token file: $GIT_HTTP_TOKEN_FILE" >&2
  155. exit 1
  156. fi
  157. if [[ -n "$GIT_HTTP_TOKEN_FILE" ]]; then
  158. GIT_HTTP_TOKEN="$(tr -d '\r\n' < "$GIT_HTTP_TOKEN_FILE")"
  159. fi
  160. if [[ -n "$GIT_HTTP_TOKEN" ]] && [[ -z "$GIT_HTTP_USERNAME" ]]; then
  161. echo "Error: --git-http-username cannot be empty when token is set" >&2
  162. exit 1
  163. fi
  164. if [[ -n "$GIT_HTTP_TOKEN" ]] && [[ "$RUN_USER" == "root" ]]; then
  165. echo "Error: refusing to store git token for root service user" >&2
  166. echo "Use --user <non-root> or disable push with --git-push 0" >&2
  167. exit 1
  168. fi
  169. RUN_HOME="$(getent passwd "$RUN_USER" | cut -d: -f6)"
  170. if [[ -z "$RUN_HOME" ]]; then
  171. echo "Error: could not determine home directory for user: $RUN_USER" >&2
  172. exit 1
  173. fi
  174. if [[ "$INSTALL_DEPS" == "1" ]]; then
  175. export DEBIAN_FRONTEND=noninteractive
  176. apt-get update -y
  177. apt-get install -y python3 ca-certificates git
  178. fi
  179. mkdir -p "$APP_DIR/runtime"
  180. chmod +x "$APP_DIR/scripts/run_update_and_commit.sh" || true
  181. chown -R "$RUN_USER:$RUN_GROUP" "$APP_DIR/runtime"
  182. SERVICE_STATE_DIR="/var/lib/${SERVICE_NAME}"
  183. ENV_FILE="/etc/${SERVICE_NAME}.env"
  184. TOKEN_FILE=""
  185. REMOTE_URL=""
  186. AUTH_MODE="header"
  187. if [[ "$GIT_USE_CREDENTIAL_STORE" == "1" ]]; then
  188. AUTH_MODE="credential-helper-store"
  189. fi
  190. mkdir -p "$SERVICE_STATE_DIR"
  191. chown "$RUN_USER:$RUN_GROUP" "$SERVICE_STATE_DIR"
  192. chmod 750 "$SERVICE_STATE_DIR"
  193. if [[ "$GIT_PUSH_ENABLED" == "1" ]]; then
  194. REMOTE_URL="$(git -C "$APP_DIR" remote get-url "$GIT_PUSH_REMOTE" 2>/dev/null || true)"
  195. if [[ -z "$REMOTE_URL" ]]; then
  196. echo "Warning: remote '$GIT_PUSH_REMOTE' not found now. Push may fail until remote is configured." >&2
  197. fi
  198. fi
  199. if [[ -n "$GIT_HTTP_TOKEN" ]]; then
  200. if [[ "$GIT_USE_CREDENTIAL_STORE" == "1" ]]; then
  201. if [[ "$REMOTE_URL" =~ ^https:// ]]; then
  202. helper_value="store"
  203. if [[ -n "$GIT_CREDENTIALS_FILE" ]]; then
  204. helper_value="store --file ${GIT_CREDENTIALS_FILE}"
  205. mkdir -p "$(dirname "$GIT_CREDENTIALS_FILE")"
  206. touch "$GIT_CREDENTIALS_FILE"
  207. chown "$RUN_USER:$RUN_GROUP" "$GIT_CREDENTIALS_FILE"
  208. chmod 600 "$GIT_CREDENTIALS_FILE"
  209. fi
  210. run_as_service_user git config --global credential.helper "$helper_value"
  211. printf 'url=%s\nusername=%s\npassword=%s\n\n' "$REMOTE_URL" "$GIT_HTTP_USERNAME" "$GIT_HTTP_TOKEN" | run_as_service_user git credential approve
  212. else
  213. echo "Warning: token provided but remote is not HTTPS; credential.helper store setup skipped." >&2
  214. echo "Warning: fallback to header-token-file auth mode for this install." >&2
  215. GIT_USE_CREDENTIAL_STORE="0"
  216. fi
  217. fi
  218. if [[ "$GIT_USE_CREDENTIAL_STORE" != "1" ]]; then
  219. TOKEN_FILE="${SERVICE_STATE_DIR}/git_http_token"
  220. printf '%s\n' "$GIT_HTTP_TOKEN" >"$TOKEN_FILE"
  221. chown "$RUN_USER:$RUN_GROUP" "$TOKEN_FILE"
  222. chmod 600 "$TOKEN_FILE"
  223. AUTH_MODE="header-token-file"
  224. fi
  225. fi
  226. run_as_service_user git config --global --add safe.directory "$APP_DIR" || true
  227. cat >"$ENV_FILE" <<EOF
  228. GIT_PUSH_ENABLED=${GIT_PUSH_ENABLED}
  229. GIT_PUSH_REQUIRED=${GIT_PUSH_ENABLED}
  230. GIT_PUSH_REMOTE=${GIT_PUSH_REMOTE}
  231. GIT_RUNTIME_BRANCH=runtime-state
  232. GIT_HTTP_USERNAME=${GIT_HTTP_USERNAME}
  233. HOME=${RUN_HOME}
  234. EOF
  235. if [[ "$GIT_USE_CREDENTIAL_STORE" == "1" ]]; then
  236. if [[ -n "$GIT_CREDENTIALS_FILE" ]]; then
  237. printf 'GIT_CREDENTIAL_HELPER=store --file %s\n' "$GIT_CREDENTIALS_FILE" >>"$ENV_FILE"
  238. else
  239. printf 'GIT_CREDENTIAL_HELPER=store\n' >>"$ENV_FILE"
  240. fi
  241. fi
  242. if [[ -n "$TOKEN_FILE" ]]; then
  243. printf 'GIT_HTTP_TOKEN_FILE=%s\n' "$TOKEN_FILE" >>"$ENV_FILE"
  244. fi
  245. chown root:root "$ENV_FILE"
  246. chmod 600 "$ENV_FILE"
  247. cat >"/etc/systemd/system/${SERVICE_NAME}.service" <<EOF
  248. [Unit]
  249. Description=VMess Domain Rotator updater
  250. After=network-online.target
  251. Wants=network-online.target
  252. [Service]
  253. Type=oneshot
  254. User=${RUN_USER}
  255. Group=${RUN_GROUP}
  256. WorkingDirectory=${APP_DIR}
  257. EnvironmentFile=-${ENV_FILE}
  258. UMask=0077
  259. ExecStart=/bin/bash ${APP_DIR}/scripts/run_update_and_commit.sh ${APP_DIR}/config.json
  260. EOF
  261. cat >"/etc/systemd/system/${SERVICE_NAME}.timer" <<EOF
  262. [Unit]
  263. Description=Run VMess Domain Rotator every ${INTERVAL}
  264. [Timer]
  265. OnBootSec=2min
  266. OnUnitActiveSec=${INTERVAL}
  267. AccuracySec=30s
  268. Unit=${SERVICE_NAME}.service
  269. Persistent=true
  270. [Install]
  271. WantedBy=timers.target
  272. EOF
  273. systemctl daemon-reload
  274. systemctl enable --now "${SERVICE_NAME}.timer"
  275. systemctl start "${SERVICE_NAME}.service"
  276. echo ""
  277. echo "✓ Installation complete!"
  278. echo ""
  279. echo "Configuration:"
  280. echo " Working directory: ${APP_DIR}"
  281. echo " Service user: ${RUN_USER}"
  282. echo " Service group: ${RUN_GROUP}"
  283. echo " Timer interval: ${INTERVAL}"
  284. echo " Push enabled: ${GIT_PUSH_ENABLED}"
  285. echo " Push remote: ${GIT_PUSH_REMOTE}"
  286. echo " Auth mode: ${AUTH_MODE}"
  287. echo " Env file: ${ENV_FILE}"
  288. echo ""
  289. echo "Commands:"
  290. echo " Check status: systemctl status ${SERVICE_NAME}.timer"
  291. echo " View logs: journalctl -u ${SERVICE_NAME}.service -n 50 --no-pager"
  292. echo " Manual run: sudo systemctl start ${SERVICE_NAME}.service"
  293. echo ""