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]}")"
|
2025-03-26 15:27:35 +03:30
|
|
|
__base="$(basename "${__file}" .sh)"
|
2023-12-14 18:44:35 +03:30
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
DATADIR="${__dir}/data"
|
2023-12-14 18:44:35 +03:30
|
|
|
|
2025-03-26 15:27: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
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
# Compile data function
|
|
|
|
compile_dat() {
|
|
|
|
TMPDIR=$(mktemp -d)
|
|
|
|
trap 'error_exit' ERR
|
2023-12-14 18:44:35 +03:30
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
local GEOSITE="${GOPATH}/src/github.com/v2ray/domain-list-community"
|
2023-12-14 18:44:35 +03:30
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
# Clone or update the geosite repository
|
2023-12-14 18:44:35 +03:30
|
|
|
if [[ -d ${GEOSITE} ]]; then
|
2025-03-26 15:27:35 +03:30
|
|
|
(cd "${GEOSITE}" && git pull)
|
2023-12-14 18:44:35 +03:30
|
|
|
else
|
2025-03-26 15:27:35 +03:30
|
|
|
git clone https://github.com/v2ray/domain-list-community.git "${GEOSITE}"
|
2023-12-14 18:44:35 +03:30
|
|
|
fi
|
2025-03-26 15:27:35 +03:30
|
|
|
|
|
|
|
(cd "${GEOSITE}" && go run main.go)
|
2023-12-14 18:44:35 +03:30
|
|
|
|
2025-03-26 15:27: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
|
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
# 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
|
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
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
|
2025-03-26 15:27:35 +03:30
|
|
|
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
|
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
# Update geoip.dat if it exists
|
2023-12-14 18:44:35 +03:30
|
|
|
if [[ -e geoip.dat ]]; then
|
2025-03-26 15:27:35 +03:30
|
|
|
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
|
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
trap - ERR # Disable error trap
|
|
|
|
}
|
2023-12-14 18:44:35 +03:30
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
# Download data function
|
|
|
|
download_dat() {
|
2025-02-14 09:51:18 +08:00
|
|
|
wget -qO - https://api.github.com/repos/v2ray/geoip/releases/latest \
|
2025-03-26 15:27:35 +03:30
|
|
|
| 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 \
|
2025-03-26 15:27:35 +03:30
|
|
|
| grep browser_download_url | cut -d '"' -f 4 \
|
|
|
|
| xargs wget -O "$DATADIR/geosite.dat"
|
2023-12-14 18:44:35 +03:30
|
|
|
}
|
|
|
|
|
2025-03-26 15:27:35 +03:30
|
|
|
# Main execution logic
|
|
|
|
ACTION="${1:-download}"
|
2023-12-14 18:44:35 +03:30
|
|
|
|
|
|
|
case $ACTION in
|
2025-03-26 15:27:35 +03:30
|
|
|
"download") download_dat ;;
|
|
|
|
"compile") compile_dat ;;
|
2023-12-14 18:44:35 +03:30
|
|
|
esac
|