Skip to content

Instantly share code, notes, and snippets.

@sko00o
Forked from akillcool/clash.service
Last active January 3, 2024 14:12
Show Gist options
  • Save sko00o/43021bfd02d8a368215e570ce3539165 to your computer and use it in GitHub Desktop.
Save sko00o/43021bfd02d8a368215e570ce3539165 to your computer and use it in GitHub Desktop.
clash auto start and update subcribe configuration
#!/bin/bash
# save this file to /usr/local/bin/clash-start
# save pid file
echo $$ > ${HOME}/.config/clash/clash.pid
CLASH_URL="your subscribe address" # NEED EDIT!
CLASH_REMOTE_CFG="${HOME}/.config/clash/config-remote.yaml"
CLASH_LOCAL_CFG="${HOME}/.config/clash/config-local.yaml"
CLASH_TMP_CFG="/tmp/clash-config.yaml"
CLASH_TARGET_CFG="${HOME}/.config/clash/config.yaml"
curl -s -o "${CLASH_REMOTE_CFG}" "${CLASH_URL}"
merge-yaml ${CLASH_LOCAL_CFG} ${CLASH_REMOTE_CFG} > ${CLASH_TMP_CFG}
if $(diff ${CLASH_TARGET_CFG} ${CLASH_TMP_CFG}); then
clash
else
TIME=`date '+%Y%m%d%H%M%S'`
cp ${CLASH_TARGET_CFG} "${CLASH_TARGET_CFG}.${TIME}.bak"
mv ${CLASH_TMP_CFG} ${CLASH_TARGET_CFG}
clash
fi
#!/bin/bash
# save this file to /usr/local/bin/clash-stop
# read pid file
PID=`cat ${HOME}/.config/clash/clash.pid`
kill -9 ${PID}
rm ${HOME}/.config/clash/clash.pid
# edit and save this file to /usr/lib/systemd/system/clash.service
[Unit]
Description=Clash
After=network.target
[Service]
ExecStart=/usr/local/bin/clash-start
ExecStop=/usr/local/bin/clash-stop
Environment="HOME=/root/"
User=root
[Install]
WantedBy=multi-user.target
# save in ${HOME}/.config/clash/config-local.yaml
port: 0
socks-port: 0
mixed-port: 7890
external-controller: :9090
allow-lan: true
# git clone https://github.com/Dreamacro/clash-dashboard.git ${HOME}/.config/clash/clash-dashboard
external-ui: clash-dashboard
secret: "A-Secret-Password" # NEED EDIT!
#!/usr/bin/python3
# save in /usr/local/bin/merge-yaml
import yaml
import sys
def merge_yaml_files(input_files):
merged_data = {}
# Iterate through each input file
for file_path in input_files:
with open(file_path, 'r', encoding='utf-8') as file:
yaml_data = yaml.safe_load(file)
# Merge the array fields (lists)
for key, value in yaml_data.items():
if isinstance(value, list):
if key in merged_data:
merged_data[key].extend(value)
else:
merged_data[key] = value
# Replace the key-value fields (dictionaries)
for key, value in yaml_data.items():
if not isinstance(value, list):
if key not in merged_data:
merged_data[key] = value
# Convert the merged data to YAML
merged_yaml = yaml.dump(merged_data, allow_unicode=True)
# Output the merged YAML content in UTF-8 encoding
sys.stdout.buffer.write(merged_yaml.encode('utf-8'))
# Example usage
if __name__ == '__main__':
# Check if input file paths are provided
if len(sys.argv) < 2:
sys.exit("Please provide at least one input file path.")
input_files = sys.argv[1:]
merge_yaml_files(input_files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment