Skip to content

Instantly share code, notes, and snippets.

@Cloud0310
Last active October 11, 2025 16:42
Show Gist options
  • Select an option

  • Save Cloud0310/1e2b65482de42aec847e55502f46de84 to your computer and use it in GitHub Desktop.

Select an option

Save Cloud0310/1e2b65482de42aec847e55502f46de84 to your computer and use it in GitHub Desktop.
Mihomo config update script and systemd service file
[Unit]
Description=Auto update mihomo config file
After=mihomo.service network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/update-mihomo-config.sh
[Unit]
Description="Auto update mihomo config file"
[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=10s
[Install]
WantedBy=timers.target
#!/bin/bash
# /usr/local/bin/update-mihomo-config.sh
# Exit immediately if a command exits with a non-zero status.
set -e
# --- Configuration ---
REMOTE_URL="YOUR config URL"
LOCAL_FILE="/etc/mihomo/config.yaml"
MIHOMO_API_URL="http://localhost:9090"
MIHOMO_API_TOKEN="" # Your actual bearer token
# --- Helper Functions ---
modify_dns_settings() {
# --- Dependency Check ---
if ! command -v yq &> /dev/null; then
echo "Error: 'yq' is not installed. Please install it to proceed." >&2
exit 1
fi
echo "Modifying DNS nameserver settings to use localhost..."
# This command finds the 'dns.nameserver' key and replaces its value
# with a new list containing only '127.0.0.1'.
# The -i flag modifies the file in-place.
yq -i '.dns.nameserver = ["127.0.0.1"]' "$LOCAL_FILE"
echo "DNS settings updated."
}
trigger_upgrades() {
echo "Triggering Mihomo UI and Geo data upgrades..."
# We add --fail to exit with an error if the server returns HTTP >= 400
curl -sfL -X POST -H "Authorization: Bearer $MIHOMO_API_TOKEN" "${MIHOMO_API_URL}/upgrade/ui"
curl -sfL -X POST -H "Authorization: Bearer $MIHOMO_API_TOKEN" "${MIHOMO_API_URL}/upgrade/geo"
echo "Upgrades triggered."
}
# --- Main Logic ---
# Ensure the parent directory exists
mkdir -p "$(dirname "$LOCAL_FILE")"
# Case 1: The local file does not exist. Must perform an initial download.
if [ ! -f "$LOCAL_FILE" ]; then
echo "Local file not found. Performing initial download..."
curl --noproxy "*" -fLo "$LOCAL_FILE" "$REMOTE_URL"
# NEW: Modify DNS settings after initial download
# modify_dns_settings
echo "Initial download complete. Restarting mihomo service..."
systemctl restart mihomo.service
sleep 5
trigger_upgrades
exit 0
fi
# Case 2: The local file exists. Perform a conditional check.
echo "Local file exists. Checking for remote updates..."
LAST_MOD_HEADER=$(date -Rur "$LOCAL_FILE" | sed 's/+0000/GMT/')
if [ -z "$LAST_MOD_HEADER" ]; then
echo "Error: Could not determine modification date of local file." >&2
exit 1
fi
TEMP_FILE=$(mktemp)
trap 'rm -f "$TEMP_FILE"' EXIT
HTTP_STATUS=$(curl --noproxy "*" -sL -w '%{http_code}' -o "$TEMP_FILE" -H "If-Modified-Since: $LAST_MOD_HEADER" "$REMOTE_URL")
case "$HTTP_STATUS" in
200)
echo "Remote file is newer (HTTP 200 OK). Proceeding with update."
if [ ! -s "$TEMP_FILE" ]; then
echo "Error: Download resulted in an empty file. Aborting update." >&2
exit 1
fi
echo "Backing up old config to ${LOCAL_FILE}.bak"
mv "$LOCAL_FILE" "${LOCAL_FILE}.bak"
echo "Installing new config file."
mv "$TEMP_FILE" "$LOCAL_FILE"
# Modify DNS settings on the newly downloaded file
# modify_dns_settings
echo "Restarting mihomo service..."
systemctl restart mihomo.service
sleep 5
trigger_upgrades
echo "Update process completed successfully."
;;
304)
echo "Local file is up to date (HTTP 304 Not Modified). No action needed."
;;
*)
echo "Error: Received unexpected HTTP status code: $HTTP_STATUS. No action taken." >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment