Sfoglia il codice sorgente

change: submit to runtime-state

Dew-OF-Aurora 3 settimane fa
parent
commit
19e1ce59c9
3 ha cambiato i file con 63 aggiunte e 23 eliminazioni
  1. 2 2
      README.md
  2. 60 20
      scripts/run_update_and_commit.sh
  3. 1 1
      substore/operator_template.js

+ 2 - 2
README.md

@@ -134,7 +134,7 @@ If the whole subscription file is itself base64-encoded, add:
 ### Cron
 
 ```cron
-0 */12 * * * /bin/bash /opt/vmess-domain-rotator/scripts/run_update_and_commit.sh /opt/vmess-domain-rotator/config.json >> /var/log/vmess-domain-rotator.log 2>&1
+0 * * * * /bin/bash /opt/vmess-domain-rotator/scripts/run_update_and_commit.sh /opt/vmess-domain-rotator/config.json >> /var/log/vmess-domain-rotator.log 2>&1
 ```
 
 ### systemd timer
@@ -152,7 +152,7 @@ sudo bash scripts/install_debian.sh
 
 If you run this inside a git clone, installer now defaults to in-place mode (service uses current repo path), so auto-commit writes to your real repo. To force copy deployment under `/opt`, set `--app-dir` explicitly.
 
-This installer also initializes a git repo under app dir (if missing) and configures the service to auto-commit only when selected domain changes.
+This installer initializes a git repo under app dir only in copy mode (if missing). Runtime updates are committed and pushed to `runtime-state` branch by default when selected domain changes, so `main` stays clean.
 
 Uninstall:
 

+ 60 - 20
scripts/run_update_and_commit.sh

@@ -37,22 +37,11 @@ if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
   exit 0
 fi
 
-git -C "$APP_DIR" add runtime/current_domain.txt runtime/current_domain.json runtime/state.json runtime/substore_vars.json || true
-
-if git -C "$APP_DIR" diff --cached --quiet; then
-  echo "[vmess-domain-rotator] no staged changes, skip git commit"
-  exit 0
-fi
-
 commit_name="${GIT_COMMIT_NAME:-vmess-domain-rotator}"
 commit_email="${GIT_COMMIT_EMAIL:-vmess-domain-rotator@localhost}"
+runtime_branch="${GIT_RUNTIME_BRANCH:-runtime-state}"
 ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
 
-git -C "$APP_DIR" \
-  -c user.name="$commit_name" \
-  -c user.email="$commit_email" \
-  commit -m "chore: rotate preferred domain to ${after} (${ts})"
-
 push_remote="${GIT_PUSH_REMOTE:-origin}"
 if ! git -C "$APP_DIR" remote get-url "$push_remote" >/dev/null 2>&1; then
   push_remote=""
@@ -62,26 +51,77 @@ if ! git -C "$APP_DIR" remote get-url "$push_remote" >/dev/null 2>&1; then
   done < <(git -C "$APP_DIR" remote)
 fi
 
+work_dir=""
+cleanup_worktree="0"
 current_branch="$(git -C "$APP_DIR" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
 
+if [[ "$current_branch" == "$runtime_branch" ]]; then
+  work_dir="$APP_DIR"
+else
+  work_dir="$(mktemp -d "${TMPDIR:-/tmp}/vmess-runtime-state.XXXXXX")"
+  cleanup_worktree="1"
+
+  if git -C "$APP_DIR" show-ref --verify --quiet "refs/heads/${runtime_branch}"; then
+    git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
+  else
+    if [[ -n "$push_remote" ]] && git -C "$APP_DIR" ls-remote --exit-code --heads "$push_remote" "$runtime_branch" >/dev/null 2>&1; then
+      git -C "$APP_DIR" fetch "$push_remote" "$runtime_branch:$runtime_branch"
+      git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
+    else
+      git -C "$APP_DIR" worktree add --force --detach "$work_dir"
+      git -C "$work_dir" checkout --orphan "$runtime_branch"
+      git -C "$work_dir" rm -rf . >/dev/null 2>&1 || true
+      echo "[vmess-domain-rotator] initialized branch ${runtime_branch}"
+    fi
+  fi
+fi
+
+if [[ "$cleanup_worktree" == "1" ]]; then
+  cleanup() {
+    git -C "$APP_DIR" worktree remove --force "$work_dir" >/dev/null 2>&1 || rm -rf "$work_dir"
+  }
+  trap cleanup EXIT
+fi
+
+mkdir -p "$work_dir/runtime"
+for file in current_domain.txt current_domain.json state.json substore_vars.json; do
+  src="$APP_DIR/runtime/$file"
+  dst="$work_dir/runtime/$file"
+  if [[ -f "$src" ]]; then
+    cp "$src" "$dst"
+  else
+    rm -f "$dst"
+  fi
+done
+
+git -C "$work_dir" add -A runtime/current_domain.txt runtime/current_domain.json runtime/state.json runtime/substore_vars.json || true
+
+if git -C "$work_dir" diff --cached --quiet; then
+  echo "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
+  exit 0
+fi
+
+git -C "$work_dir" \
+  -c user.name="$commit_name" \
+  -c user.email="$commit_email" \
+  commit -m "chore: rotate preferred domain to ${after} (${ts})"
+
 if [[ -z "$push_remote" ]]; then
   echo "[vmess-domain-rotator] no remote found, skip git push"
-elif [[ -z "$current_branch" ]]; then
-  echo "[vmess-domain-rotator] detached HEAD, skip git push"
 else
-  if git -C "$APP_DIR" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
-    if git -C "$APP_DIR" push; then
-      echo "[vmess-domain-rotator] pushed to ${push_remote}/${current_branch}"
+  if git -C "$work_dir" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
+    if git -C "$work_dir" push "$push_remote" "$runtime_branch:$runtime_branch"; then
+      echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch}"
     else
       echo "[vmess-domain-rotator] git push failed"
     fi
   else
-    if git -C "$APP_DIR" push -u "$push_remote" "$current_branch"; then
-      echo "[vmess-domain-rotator] pushed to ${push_remote}/${current_branch} (set upstream)"
+    if git -C "$work_dir" push -u "$push_remote" "$runtime_branch:$runtime_branch"; then
+      echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream)"
     else
       echo "[vmess-domain-rotator] git push failed"
     fi
   fi
 fi
 
-echo "[vmess-domain-rotator] committed domain change: ${before} -> ${after}"
+echo "[vmess-domain-rotator] committed domain change on ${runtime_branch}: ${before} -> ${after}"

+ 1 - 1
substore/operator_template.js

@@ -4,7 +4,7 @@
   - Replace vmess server field for matched nodes
 */
 
-const DOMAIN_JSON_URL = "https://git.dewofaurora.de/aurora/vmess-domain-rotator/raw/main/runtime/current_domain.json";
+const DOMAIN_JSON_URL = "https://git.dewofaurora.de/aurora/vmess-domain-rotator/raw/runtime-state/runtime/current_domain.json";
 const NODE_NAME_REGEX = /(argo|cf|vm|优选)/i;
 const CACHE_KEY = "vmess-domain-rotator:current";
 const CACHE_TTL_MS = 5 * 60 * 1000;