From 4ec3c7e177102a8d6278adf02524c4a06f79e2f3 Mon Sep 17 00:00:00 2001 From: Pk-web6936 <202365630+Pk-web6936@users.noreply.github.com> Date: Wed, 26 Mar 2025 15:27:35 +0330 Subject: [PATCH] increase Performance gen_assets.sh (#130) increase Performance gen_assets.sh --- gen_assets.sh | 77 ++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/gen_assets.sh b/gen_assets.sh index 3a102fa..33a7c34 100644 --- a/gen_assets.sh +++ b/gen_assets.sh @@ -7,75 +7,84 @@ set -o nounset # Set magic variables for current file & dir __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" __file="${__dir}/$(basename "${BASH_SOURCE[0]}")" -__base="$(basename ${__file} .sh)" +__base="$(basename "${__file}" .sh)" +DATADIR="${__dir}/data" -DATADIR=${__dir}/data +# Function to handle errors +error_exit() { + echo -e "Aborted, error $? in command: $BASH_COMMAND" + [[ -d "$TMPDIR" ]] && rm -rf "$TMPDIR" + exit 1 +} -compile_dat () { - local TMPDIR=$(mktemp -d) +# Compile data function +compile_dat() { + TMPDIR=$(mktemp -d) + trap 'error_exit' ERR - trap 'echo -e "Aborted, error $? in command: $BASH_COMMAND"; rm -rf $TMPDIR; trap ERR; exit 1' ERR + local GEOSITE="${GOPATH}/src/github.com/v2ray/domain-list-community" - local GEOSITE=${GOPATH}/src/github.com/v2ray/domain-list-community +# Clone or update the geosite repository if [[ -d ${GEOSITE} ]]; then - cd ${GEOSITE} && git pull + (cd "${GEOSITE}" && git pull) else - mkdir -p ${GEOSITE} - cd ${GEOSITE} && git clone https://github.com/v2ray/domain-list-community.git . + git clone https://github.com/v2ray/domain-list-community.git "${GEOSITE}" fi - go run main.go + + (cd "${GEOSITE}" && go run main.go) - if [[ -e dlc.dat ]]; then - rm -f $DATADIR/geosite.dat - mv dlc.dat $DATADIR/geosite.dat + # Update geosite.dat if dlc.dat exists + if [[ -e "${GEOSITE}/dlc.dat" ]]; then + mv -f "${GEOSITE}/dlc.dat" "$DATADIR/geosite.dat" echo "----------> geosite.dat updated." else echo "----------> geosite.dat failed to update." fi - - if [[ ! -x $GOPATH/bin/geoip ]]; then + # Install geoip if not already installed + if [[ ! -x "$GOPATH/bin/geoip" ]]; then go get -v -u github.com/v2ray/geoip fi - cd $TMPDIR + cd "$TMPDIR" + + # Download and process GeoLite2 data curl -L -O http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip unzip -q GeoLite2-Country-CSV.zip - mkdir geoip && find . -name '*.csv' -exec mv -t ./geoip {} + - $GOPATH/bin/geoip \ + mkdir geoip && mv *.csv geoip/ + + "$GOPATH/bin/geoip" \ --country=./geoip/GeoLite2-Country-Locations-en.csv \ --ipv4=./geoip/GeoLite2-Country-Blocks-IPv4.csv \ --ipv6=./geoip/GeoLite2-Country-Blocks-IPv6.csv + # Update geoip.dat if it exists if [[ -e geoip.dat ]]; then - rm -f $DATADIR/geoip.dat - mv ./geoip.dat $DATADIR/geoip.dat + mv -f geoip.dat "$DATADIR/geoip.dat" echo "----------> geoip.dat updated." else echo "----------> geoip.dat failed to update." fi - trap ERR - return 0 + + trap - ERR # Disable error trap } - -download_dat () { +# Download data function +download_dat() { wget -qO - https://api.github.com/repos/v2ray/geoip/releases/latest \ - | grep browser_download_url | cut -d '"' -f 4 \ - | wget -i - -O $DATADIR/geoip.dat + | jq -r .assets[].browser_download_url | grep geoip.dat \ + | xargs wget -O "$DATADIR/geoip.dat" wget -qO - https://api.github.com/repos/v2ray/domain-list-community/releases/latest \ - | grep browser_download_url | cut -d '"' -f 4 \ - | wget -i - -O $DATADIR/geosite.dat + | grep browser_download_url | cut -d '"' -f 4 \ + | xargs wget -O "$DATADIR/geosite.dat" } -ACTION="${1:-}" -if [[ -z $ACTION ]]; then - ACTION=download -fi +# Main execution logic +ACTION="${1:-download}" case $ACTION in - "download") download_dat;; - "compile") compile_dat;; + "download") download_dat ;; + "compile") compile_dat ;; esac