install-dat-release.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. # This Bash script to install the latest release of geoip.dat and geosite.dat:
  3. # https://github.com/v2fly/geoip
  4. # https://github.com/v2fly/domain-list-community
  5. # Depends on cURL, please solve it yourself
  6. # You may plan to execute this Bash script regularly:
  7. # install -m 755 install-dat-release.sh /usr/local/bin/install-dat-release
  8. # 0 0 * * * /usr/local/bin/install-dat-release > /dev/null 2>&1
  9. # You can modify it to /usr/local/lib/xray
  10. XRAY="/usr/local/share/xray"
  11. DOWNLOAD_LINK_GEOIP="https://github.com/v2fly/geoip/releases/latest/download/geoip.dat"
  12. DOWNLOAD_LINK_GEOSITE="https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat"
  13. file_ip='geoip.dat'
  14. file_dlc='dlc.dat'
  15. file_site='geosite.dat'
  16. dir_tmp="$(mktemp -d)"
  17. curl() {
  18. $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
  19. }
  20. check_if_running_as_root() {
  21. # If you want to run as another user, please modify $UID to be owned by this user
  22. if [[ "$UID" -ne '0' ]]; then
  23. echo "error: You must run this script as root!"
  24. exit 1
  25. fi
  26. }
  27. download_files() {
  28. if ! curl -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}" "${1}"; then
  29. echo 'error: Download failed! Please check your network or try again.'
  30. exit 1
  31. fi
  32. if ! curl -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}.sha256sum" "${1}.sha256sum"; then
  33. echo 'error: Download failed! Please check your network or try again.'
  34. exit 1
  35. fi
  36. }
  37. check_sum() {
  38. (
  39. cd "${dir_tmp}" || exit
  40. for i in "${dir_tmp}"/*.sha256sum; do
  41. if ! sha256sum -c "${i}"; then
  42. echo 'error: Check failed! Please check your network or try again.'
  43. exit 1
  44. fi
  45. done
  46. )
  47. }
  48. install_file() {
  49. install -m 644 "${dir_tmp}"/${file_dlc} "${XRAY}"/${file_site}
  50. install -m 644 "${dir_tmp}"/${file_ip} "${XRAY}"/${file_ip}
  51. rm -r "${dir_tmp}"
  52. }
  53. main() {
  54. check_if_running_as_root
  55. download_files $DOWNLOAD_LINK_GEOIP $file_ip
  56. download_files $DOWNLOAD_LINK_GEOSITE $file_dlc
  57. check_sum
  58. install_file
  59. }
  60. main "$@"