Skip to content

Convert Merged YAML Rulesets to MRS Format #18

Convert Merged YAML Rulesets to MRS Format

Convert Merged YAML Rulesets to MRS Format #18

Workflow file for this run

name: Convert Merged YAML Rulesets to MRS Format
on:
workflow_dispatch:
schedule:
- cron: "50 22 * * *"
push:
branches:
- main
paths-ignore:
- "**/README.md"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set variables
run: |
echo "RELEASE_NAME=Released on $(date +%Y%m%d%H%M)" >> $GITHUB_ENV
echo "TAG_NAME=$(date +%Y%m%d%H%M)" >> $GITHUB_ENV
echo "LOYALSOLDIER_URL=https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release" >> $GITHUB_ENV
echo "METACUBEX_URL=https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/meta/geo/geosite" >> $GITHUB_ENV
shell: bash
- name: Create necessary directories
run: |
mkdir -p loyalsoldier metacubex merged converter publish
- name: Download Loyalsoldier rulesets
run: |
files=(apple applications cncidr direct gfw google greatfire icloud lancidr private proxy reject telegramcidr tld-not-cn)
for file in ${files[@]}; do
wget ${LOYALSOLDIER_URL}/${file}.txt -O loyalsoldier/${file}.yaml
done
- name: Download MetaCubeX rulesets
run: |
files=(apple@cn cn gfw google@cn greatfire icloud private geolocation-!cn category-ads-all tld-!cn)
for file in ${files[@]}; do
wget ${METACUBEX_URL}/${file}.yaml -O metacubex/${file}.yaml
done
- name: Merge rulesets
run: |
merge_rulesets() {
local output_file=$1
shift
local input_files=("$@")
(
cat "${input_files[@]}" | \
sed "s/^[ ]*-[ ]*//; s/^'//; s/'$//" | \
sort -u | \
awk '{
if ($0 ~ /^\+\./) {
domain = substr($0, 3)
seen[domain] = 1
print $0
} else if (!seen[$0]) {
print $0
}
}' | \
sed "s/^/ - /" > merged/temp.txt
)
echo "payload:" > "merged/${output_file}"
grep -v "payload:" merged/temp.txt >> "merged/${output_file}"
rm merged/temp.txt
}
merge_rulesets "[email protected]" loyalsoldier/apple.yaml metacubex/[email protected]
merge_rulesets "direct.yaml" loyalsoldier/direct.yaml metacubex/cn.yaml
merge_rulesets "gfw.yaml" loyalsoldier/gfw.yaml metacubex/gfw.yaml
merge_rulesets "[email protected]" loyalsoldier/google.yaml metacubex/[email protected]
merge_rulesets "greatfire.yaml" loyalsoldier/greatfire.yaml metacubex/greatfire.yaml
merge_rulesets "icloud.yaml" loyalsoldier/icloud.yaml metacubex/icloud.yaml
merge_rulesets "private.yaml" loyalsoldier/private.yaml metacubex/private.yaml
merge_rulesets "proxy.yaml" loyalsoldier/proxy.yaml metacubex/geolocation-!cn.yaml
merge_rulesets "ads.yaml" loyalsoldier/reject.yaml metacubex/category-ads-all.yaml
merge_rulesets "tld-!cn.yaml" loyalsoldier/tld-not-cn.yaml metacubex/tld-!cn.yaml
shell: bash
- name: Download and prepare converter
run: |
LATEST_VERSION=$(curl -s https://api.github.com/repos/MetaCubeX/mihomo/releases/latest | jq -r .tag_name)
curl -L "https://github.com/MetaCubeX/mihomo/releases/download/${LATEST_VERSION}/mihomo-linux-amd64-${LATEST_VERSION}.gz" -o converter/mihomo.gz
gzip -d converter/mihomo.gz
chmod +x converter/mihomo
- name: Convert YAML rulesets to MRS format and prepare for release
run: |
files=(
apple@cn direct gfw google@cn greatfire icloud private proxy ads tld-!cn
cncidr lancidr telegramcidr
)
merged_files=(apple@cn direct gfw google@cn greatfire icloud private proxy ads tld-!cn)
for file in "${files[@]}"; do
if [[ " ${merged_files[*]} " =~ " ${file} " ]]; then
input_dir="merged"
ruleset_type="domain"
else
input_dir="loyalsoldier"
ruleset_type="ipcidr"
fi
./converter/mihomo convert-ruleset ${ruleset_type} yaml ${input_dir}/${file}.yaml publish/${file}.mrs
cp ${input_dir}/${file}.yaml publish/${file}.yaml
done
cp loyalsoldier/applications.yaml publish/applications.yaml
- name: Release and upload assets
uses: softprops/action-gh-release@v2
with:
name: "${{ env.RELEASE_NAME }}"
tag_name: "${{ env.TAG_NAME }}"
draft: false
prerelease: false
files: |
publish/*.mrs
publish/*.yaml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Git push assets to release branch
run: |
cd publish || exit 1
git init
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -b release
git add .
git commit -m "${{ env.RELEASE_NAME }}"
git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}"
git push -f origin release
- name: Purge jsDelivr CDN
run: |
cd publish || exit 1
for file in $(ls); do
curl -i "https://purge.jsdelivr.net/gh/${{ github.repository }}@release/${file}"
done