AndroidLibXrayLite/gen_assets.sh

91 lines
2.5 KiB
Bash
Raw Normal View History

2023-12-14 18:44:35 +03:30
#!/bin/bash
set -o errexit
set -o pipefail
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)"
2023-12-14 18:44:35 +03:30
DATADIR="${__dir}/data"
2023-12-14 18:44:35 +03:30
# Function to handle errors
error_exit() {
echo -e "Aborted, error $? in command: $BASH_COMMAND"
[[ -d "$TMPDIR" ]] && rm -rf "$TMPDIR"
exit 1
}
2023-12-14 18:44:35 +03:30
# Compile data function
compile_dat() {
TMPDIR=$(mktemp -d)
trap 'error_exit' ERR
2023-12-14 18:44:35 +03:30
local GEOSITE="${GOPATH}/src/github.com/v2ray/domain-list-community"
2023-12-14 18:44:35 +03:30
# Clone or update the geosite repository
2023-12-14 18:44:35 +03:30
if [[ -d ${GEOSITE} ]]; then
(cd "${GEOSITE}" && git pull)
2023-12-14 18:44:35 +03:30
else
git clone https://github.com/v2ray/domain-list-community.git "${GEOSITE}"
2023-12-14 18:44:35 +03:30
fi
(cd "${GEOSITE}" && go run main.go)
2023-12-14 18:44:35 +03:30
# Update geosite.dat if dlc.dat exists
if [[ -e "${GEOSITE}/dlc.dat" ]]; then
mv -f "${GEOSITE}/dlc.dat" "$DATADIR/geosite.dat"
2023-12-14 18:44:35 +03:30
echo "----------> geosite.dat updated."
else
echo "----------> geosite.dat failed to update."
fi
# Install geoip if not already installed
if [[ ! -x "$GOPATH/bin/geoip" ]]; then
2023-12-14 18:44:35 +03:30
go get -v -u github.com/v2ray/geoip
fi
cd "$TMPDIR"
# Download and process GeoLite2 data
2023-12-14 18:44:35 +03:30
curl -L -O http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip
unzip -q GeoLite2-Country-CSV.zip
mkdir geoip && mv *.csv geoip/
"$GOPATH/bin/geoip" \
2023-12-14 18:44:35 +03:30
--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
2023-12-14 18:44:35 +03:30
if [[ -e geoip.dat ]]; then
mv -f geoip.dat "$DATADIR/geoip.dat"
2023-12-14 18:44:35 +03:30
echo "----------> geoip.dat updated."
else
echo "----------> geoip.dat failed to update."
fi
trap - ERR # Disable error trap
}
2023-12-14 18:44:35 +03:30
# Download data function
download_dat() {
wget -qO - https://api.github.com/repos/v2ray/geoip/releases/latest \
| jq -r .assets[].browser_download_url | grep geoip.dat \
| xargs wget -O "$DATADIR/geoip.dat"
2023-12-14 18:44:35 +03:30
wget -qO - https://api.github.com/repos/v2ray/domain-list-community/releases/latest \
| grep browser_download_url | cut -d '"' -f 4 \
| xargs wget -O "$DATADIR/geosite.dat"
2023-12-14 18:44:35 +03:30
}
# Main execution logic
ACTION="${1:-download}"
2023-12-14 18:44:35 +03:30
case $ACTION in
"download") download_dat ;;
"compile") compile_dat ;;
2023-12-14 18:44:35 +03:30
esac