فهرست منبع

💪🏼 Improve bash install & update scripts

Now, All offline IP database files will download and update by download.sh script. The script will also track local version and latest version of those database.
BrettonYe 2 سال پیش
والد
کامیت
64877c3133
9فایلهای تغییر یافته به همراه342 افزوده شده و 75 حذف شده
  1. 32 7
      app/Utils/IP.php
  2. 5 1
      database/.gitignore
  3. BIN
      database/GeoLite2-City.mmdb
  4. BIN
      database/ipipfree.ipdb
  5. 86 0
      download.sh
  6. 94 44
      install.sh
  7. 38 0
      scripts/download_dbs.sh
  8. 79 0
      scripts/download_utils.sh
  9. 8 23
      update.sh

+ 32 - 7
app/Utils/IP.php

@@ -54,7 +54,7 @@ class IP
 
 
         if (app()->getLocale() === 'zh_CN') {
         if (app()->getLocale() === 'zh_CN') {
             if (self::$is_ipv4) {
             if (self::$is_ipv4) {
-                $ret = self::IPLookup(['ipApi', 'Baidu', 'baiduBce', 'ipw', 'ipGeoLocation', 'TaoBao', 'speedtest', 'bjjii', 'TenAPI', 'fkcoder', 'vore', 'juHe', 'vvhan', 'ipjiance', 'ip2Region', 'IPIP']);
+                $ret = self::IPLookup(['ipApi', 'Baidu', 'baiduBce', 'ipw', 'ipGeoLocation', 'TaoBao', 'speedtest', 'bjjii', 'TenAPI', 'fkcoder', 'vore', 'juHe', 'vvhan', 'ipjiance', 'ip2Region', 'IPDB']);
             } else {
             } else {
                 $ret = self::IPLookup(['ipApi', 'Baidu', 'baiduBce', 'ipw', 'ipGeoLocation', 'TenAPI', 'vore', 'ip2Region']);
                 $ret = self::IPLookup(['ipApi', 'Baidu', 'baiduBce', 'ipw', 'ipGeoLocation', 'TenAPI', 'vore', 'ip2Region']);
             }
             }
@@ -103,7 +103,7 @@ class IP
             'fkcoder' => self::fkcoder($ip),
             'fkcoder' => self::fkcoder($ip),
             'juHe' => self::juHe($ip),
             'juHe' => self::juHe($ip),
             'ip2Region' => self::ip2Region($ip),
             'ip2Region' => self::ip2Region($ip),
-            'IPIP' => self::IPIP($ip),
+            'IPDB' => self::IPDB($ip),
             'ipjiance' => self::ipjiance($ip),
             'ipjiance' => self::ipjiance($ip),
             'IPSB' => self::IPSB($ip),
             'IPSB' => self::IPSB($ip),
             'ipinfo' => self::ipinfo($ip),
             'ipinfo' => self::ipinfo($ip),
@@ -369,16 +369,16 @@ class IP
         return null;
         return null;
     }
     }
 
 
-    private static function IPIP(string $ip): array
-    { // 通过IPIP离线数据查询IP地址的详细信息
-        $filePath = database_path('ipipfree.ipdb'); // 来源: https://www.ipip.net/free_download/
+    private static function IPDB(string $ip): array
+    { // 通过IPDB格式的离线数据查询IP地址的详细信息
+        $filePath = database_path('qqwry.ipdb'); // 来源: https://github.com/metowolf/qqwry.ipdb
         $location = (new City($filePath))->findMap($ip, 'CN');
         $location = (new City($filePath))->findMap($ip, 'CN');
 
 
         return [
         return [
             'country' => $location['country_name'],
             'country' => $location['country_name'],
             'region' => $location['region_name'],
             'region' => $location['region_name'],
             'city' => $location['city_name'],
             'city' => $location['city_name'],
-            'isp' => null,
+            'isp' => $location['isp_domain'],
             'area' => null,
             'area' => null,
         ];
         ];
     }
     }
@@ -570,7 +570,7 @@ class IP
 
 
     private static function ip2Location(string $ip): ?array
     private static function ip2Location(string $ip): ?array
     { // 通过ip2Location查询IP地址的详细信息
     { // 通过ip2Location查询IP地址的详细信息
-        $filePath = database_path('IP2LOCATION-LITE-DB5.IPV6.BIN'); // 来源: https://lite.ip2location.com/database-download
+        $filePath = database_path('IP2LOCATION-LITE-DB11.IPV6.BIN'); // 来源: https://lite.ip2location.com/database-download
         try {
         try {
             $location = (new Database($filePath, Database::FILE_IO))
             $location = (new Database($filePath, Database::FILE_IO))
                 ->lookup($ip, [Database::CITY_NAME, Database::REGION_NAME, Database::COUNTRY_NAME, Database::LATITUDE, Database::LONGITUDE]);
                 ->lookup($ip, [Database::CITY_NAME, Database::REGION_NAME, Database::COUNTRY_NAME, Database::LATITUDE, Database::LONGITUDE]);
@@ -673,6 +673,31 @@ class IP
         return null;
         return null;
     }
     }
 
 
+    private static function cz88(string $ip): ?array
+    {
+        $response = self::$basicRequest->get("https://www.cz88.net/api/cz88/ip/base?ip=$ip");
+        if ($response->ok()) {
+            $data = $response->json();
+
+            if ($data['success'] && $data['data']['ip'] === $ip) {
+                $data = $data['data'];
+                $location = $data['locations'] ? $data['locations'][0] : null;
+
+                return [
+                    'country' => $data['country'],
+                    'region' => $data['province'],
+                    'city' => $data['city'],
+                    'isp' => $data['isp'],
+                    'area' => $data['districts'],
+                    'latitude' => $location ? $location['latitude'] : null,
+                    'longitude' => $location ? $location['longitude'] : null,
+                ];
+            }
+        }
+
+        return null;
+    }
+
     private static function ipw(string $ip): ?array
     private static function ipw(string $ip): ?array
     { // 开发依据: https://api.vore.top/
     { // 开发依据: https://api.vore.top/
         if (self::$is_ipv4) {
         if (self::$is_ipv4) {

+ 5 - 1
database/.gitignore

@@ -1 +1,5 @@
-*.sqlite*
+*.sqlite*
+*.mmdb
+*.ipdb
+*.BIN
+*.CVS

BIN
database/GeoLite2-City.mmdb


BIN
database/ipipfree.ipdb


+ 86 - 0
download.sh

@@ -0,0 +1,86 @@
+#!/bin/bash
+
+db_dir="./database"
+version_file="$db_dir/version.json"
+
+# 检查 jq 是否已安装
+if ! command -v jq >/dev/null 2>&1; then
+
+  # Ubuntu/Debian
+  if command -v apt-get >/dev/null 2>&1; then
+    sudo apt-get update
+    sudo apt-get install -y jq
+
+  # CentOS/RHEL
+  elif command -v yum >/dev/null 2>&1; then
+    sudo yum install -y epel-release
+    sudo yum install -y jq
+
+  # Fedora
+  elif command -v dnf >/dev/null 2>&1; then
+    sudo dnf install -y jq
+
+  # Arch Linux
+  elif command -v pacman >/dev/null 2>&1; then
+    sudo pacman -S jq
+
+  # openSUSE
+  elif command -v zypper >/dev/null 2>&1; then
+    sudo zypper install -y jq
+
+  else
+    echo -e "\e[31mUnable to install jq, unsupported Linux distro\e[0m"
+    exit 1
+  fi
+fi
+
+# 如果版本文件不存在,创建一个空的
+if [ ! -f "$version_file" ]; then
+  echo '{"GeoLite2":"0.0","IP2Location":"0.0","qqwry":"0.0"}' >"$version_file"
+fi
+
+# 读取本地版本信息
+geo_lite_local=$(jq -r .GeoLite2 $version_file)
+ip2location_local=$(jq -r .IP2Location $version_file)
+qqwry_local=$(jq -r .qqwry $version_file)
+
+# 获取线上的最新版本
+geo_lite_remote=$(curl -s https://api.github.com/repos/PrxyHunter/GeoLite2/releases/latest | grep tag_name | cut -d\" -f4)
+ip2location_remote=$(curl -s https://api.github.com/repos/renfei/ip2location/releases/latest | grep tag_name | cut -d\" -f4)
+qqwry_remote=$(curl -s https://api.github.com/repos/metowolf/qqwry.dat/releases/latest | grep tag_name | cut -d\" -f4)
+
+echo -e "\e[1;47;34mGeoLite2 Version Info: 【本地版本】$geo_lite_local | 【最新版本】$geo_lite_remote\e[0m"
+echo -e "\e[1;47;34mIP2Location Version Info: 【本地版本】$ip2location_local | 【最新版本】$ip2location_remote\e[0m"
+echo -e "\e[1;47;34mQQwry Version Info: 【本地版本】$qqwry_local | 【最新版本】$qqwry_remote\e[0m"
+
+# 如果线上版本大于本地版本,则执行下载更新
+if [ "$geo_lite_remote" != "$geo_lite_local" ]; then
+  # 下载GeoLite2的代码
+  echo "Updating GeoLite2 to $geo_lite_remote"
+  # Download the latest release of GeoLite2-City.mmdb from PrxyHunter/GeoLite2
+  url=$(curl -s https://api.github.com/repos/PrxyHunter/GeoLite2/releases/latest | grep "browser_download_url.*Country.mmdb" | cut -d : -f 2,3 | tr -d \")
+  if ! curl -L -S $url -o $db_dir/GeoLite2-City.mmdb; then
+    echo -e "\e[31mFailed to download GeoLite2\e[0m"
+  fi
+fi
+
+if [ "$ip2location_remote" != "$ip2location_local" ]; then
+  # 下载IP2Location的代码
+  echo "Updating IP2Location to $ip2location_remote"
+  # Download the latest release of IP2LOCATION-LITE-DB11.IPV6.BIN from renfei/ip2location
+  url=$(curl -s https://api.github.com/repos/renfei/ip2location/releases/latest | grep "browser_download_url.*IP2LOCATION-LITE-DB11.IPV6.BIN" | cut -d : -f 2,3 | tr -d \")
+  if ! curl -L -S $url -o $db_dir/IP2LOCATION-LITE-DB11.IPV6.BIN; then
+    echo -e "\e[31mFailed to download IP2Location\e[0m"
+  fi
+fi
+
+if [ "$qqwry_remote" != "$qqwry_local" ]; then
+  # 下载qqwry的代码
+  echo "Updating qqwry to $qqwry_remote"
+  if ! curl -L -S https://cdn.jsdelivr.net/npm/qqwry.ipdb/qqwry.ipdb -o $db_dir/qqwry.ipdb; then
+    echo -e "\e[31mFailed to download qqwry\e[0m"
+  fi
+fi
+
+# 更新版本文件
+echo "{\"GeoLite2\":\"$geo_lite_remote\",\"IP2Location\":\"$ip2location_remote\",\"qqwry\":\"$qqwry_remote\"}" >"$version_file"

+ 94 - 44
install.sh

@@ -1,4 +1,63 @@
-#!/usr/bin/env bash
+#!/bin/bash
+
+#安装依赖
+install_dependencies() {
+  #判断系统
+  if [[ -f /etc/debian_version ]]; then
+    PM=apt-get
+  elif [[ -f /etc/redhat-release ]]; then
+    PM=yum
+  elif [[ -f /etc/SuSE-release ]]; then
+    PM=zypper
+  elif [[ -f /etc/arch-release ]]; then
+    PM=pacman
+  elif [[ -f /etc/alpine-release ]]; then
+    PM=apk
+  else
+    echo "不支持的Linux发行版。"
+    exit 1
+  fi
+
+  if which supervisorctl >/dev/null; then
+    echo -e "\e[32mSupervisor installed! | Supervisor 已完成!\e[0m"
+  else
+    echo -e "\e[31mSupervisor did not installed! | Supervisor 未安装!\e[0m"
+    # Install Supervisor
+    case $PM in
+    apt-get)
+      sudo apt-get update
+      sudo apt-get install -y supervisor
+      ;;
+    yum)
+      sudo yum install -y epel-release
+      sudo yum install -y supervisor
+      ;;
+    zypper)
+      sudo zypper install -y supervisor
+      ;;
+    apk)
+      sudo apk add supervisor
+      ;;
+    pacman)
+      sudo pacman -S supervisor
+      ;;
+    esac
+
+    #激活
+    case $PM in
+    yum)
+      sudo service supervisord start
+      sudo chkconfig supervisord on
+      ;;
+    *)
+      sudo systemctl start supervisor.service
+      sudo systemctl enable supervisor.service
+      ;;
+    esac
+    echo -e "\e[32mSupervisor installation completed! | Supervisor 安装完成!\e[0m"
+  fi
+}
+
 #清理不需要的文件
 #清理不需要的文件
 clean_files() {
 clean_files() {
   if [ -f .user.ini ]; then
   if [ -f .user.ini ]; then
@@ -7,49 +66,45 @@ clean_files() {
   rm -rf .htaccess 404.html index.html .user.ini
   rm -rf .htaccess 404.html index.html .user.ini
 }
 }
 
 
-#检查系统
-check_sys() {
-  # shellcheck disable=SC2002
-  if [[ -f /etc/redhat-release ]]; then
-    release="centos"
-  elif cat /etc/issue | grep -q -E -i "debian"; then
-    release="debian"
-  elif cat /etc/issue | grep -q -E -i "ubuntu"; then
-    release="ubuntu"
-  elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
-    release="centos"
-  elif cat /proc/version | grep -q -E -i "debian"; then
-    release="debian"
-  elif cat /proc/version | grep -q -E -i "ubuntu"; then
-    release="ubuntu"
-  elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
-    release="centos"
-  fi
+check_available() {
+  tools=$1
+  available=$(command -v $tools >/dev/null 2>&1)
 
 
-  echo "========= Checking for Software dependency | 检查依赖软件是否安装/运行 ========="
-  if which redis-cli >/dev/null; then
-    echo -e "\e[37;42m Redis Installed! | Redis 已安装!\e[0m"
-    redis-cli ping
-  else
-    echo -e "\e[37;1;41m Redis did not installed! | redis 未安装!\e[0m"
-  fi
+  if $available; then
+    echo -e "\e[32m$tools Installed! | $tools 已安装!\e[0m"
 
 
-  if which php >/dev/null; then
-    echo -e "\e[37;42m PHP Installed! | PHP 已安装!\e[0m"
-    php -v
+    case $tools in
+    redis-cli)
+      redis-cli ping
+      ;;
+    php)
+      php -v
+      ;;
+    esac
   else
   else
-    echo -e "\e[37;1;41m PHP did not installed! | PHP 未安装!\e[0m"
+    echo -e "\e[31m$tools did not installed! | $tools 未安装!\e[0m"
   fi
   fi
+}
+
+#检查环境
+check_env() {
+  echo "========= Checking for Software dependency | 检查依赖软件是否安装/运行 ========="
+  # 需要检查的软件数组
+  check_list=(redis-cli php)
+
+  for item in "${check_list[@]}"; do
+    check_available "$item"
+  done
 
 
   if which nginx >/dev/null || which httpd >/dev/null; then
   if which nginx >/dev/null || which httpd >/dev/null; then
-    echo -e "\e[37;42m Nginx/Apache Installed! | Nginx 或 Apache 已安装!\e[0m"
+    echo -e "\e[32mNginx/Apache Installed! | Nginx 或 Apache 已安装!\e[0m"
     if which nginx >/dev/null; then
     if which nginx >/dev/null; then
       nginx -v
       nginx -v
     else
     else
       httpd -v
       httpd -v
     fi
     fi
   else
   else
-    echo -e "\e[37;1;41m Nginx/Apache did not installed! | Nginx 或 Apache 未安装!\e[0m"
+    echo -e "\e[31mNginx/Apache did not installed! | Nginx 或 Apache 未安装!\e[0m"
   fi
   fi
 }
 }
 #检查composer是否安装
 #检查composer是否安装
@@ -84,14 +139,6 @@ set_schedule() {
 }
 }
 
 
 set_horizon() {
 set_horizon() {
-  if ! command -v supervisorctl &>/dev/null; then
-    if [[ "${release}" == "centos" ]]; then
-      yum install -y supervisor
-    else
-      apt-get install -y supervisor
-    fi
-  fi
-
   if [ ! -f /etc/supervisor/conf.d/horizon.conf ]; then
   if [ ! -f /etc/supervisor/conf.d/horizon.conf ]; then
     echo "
     echo "
       [program:horizon]
       [program:horizon]
@@ -104,17 +151,20 @@ set_horizon() {
       stdout_logfile=$PWD/storage/logs/horizon.log
       stdout_logfile=$PWD/storage/logs/horizon.log
       stopwaitsecs=3600" >>/etc/supervisor/conf.d/horizon.conf
       stopwaitsecs=3600" >>/etc/supervisor/conf.d/horizon.conf
 
 
-    supervisorctl reread
-    supervisorctl update
-    supervisorctl start horizon
+    sudo supervisorctl reread
+    sudo supervisorctl update
+    sudo supervisorctl start horizon
   fi
   fi
 }
 }
 
 
 clean_files
 clean_files
-check_sys
+install_dependencies
+check_env
 check_composer
 check_composer
 composer install
 composer install
 php artisan panel:install
 php artisan panel:install
 set_permissions
 set_permissions
 set_schedule
 set_schedule
 set_horizon
 set_horizon
+echo -e "\e[32mGoing to Download some IP database files... | 将下载一些IP数据附件文件...\e[0m"
+cd scripts/ && bash download_dbs.sh && cd ../

+ 38 - 0
scripts/download_dbs.sh

@@ -0,0 +1,38 @@
+#!/bin/bash
+
+FILE_DIR="../database"
+VERSION_FILE="$FILE_DIR/version.json"
+source ./download_utils.sh
+
+# 检查 jq 是否已安装
+check_and_install jq
+
+# 数据库信息
+geo_lite_version=$(get_tag "PrxyHunter/GeoLite2")
+geo_lite_url="https://github.com/PrxyHunter/GeoLite2/releases/download/$geo_lite_version/GeoLite2-City.mmdb"
+
+ip2location_version=$(get_tag "renfei/ip2location")
+ip2location_url="https://github.com/renfei/ip2location/releases/download/$ip2location_version/IP2LOCATION-LITE-DB11.IPV6.BIN"
+
+qqwry_version=$(get_tag "metowolf/qqwry.dat")
+qqwry_url="https://cdn.jsdelivr.net/npm/qqwry.ipdb/qqwry.ipdb"
+
+declare -Ag docs
+
+docs[geo_lite_name]="GeoLite2-City.mmdb"
+docs[geo_lite_version]="$geo_lite_version"
+docs[geo_lite_url]="$geo_lite_url"
+
+docs[ip2location_name]="IP2LOCATION-LITE-DB11.IPV6.BIN"
+docs[ip2location_version]="$ip2location_version"
+docs[ip2location_url]="$ip2location_url"
+
+docs[qqwry_name]="qqwry.ipdb"
+docs[qqwry_version]="$qqwry_version"
+docs[qqwry_url]="$qqwry_url"
+
+# 主逻辑
+process_files
+echo "Update completed!"
+
+exit 0

+ 79 - 0
scripts/download_utils.sh

@@ -0,0 +1,79 @@
+#!/bin/bash
+
+function check_and_install() {
+  pkg=$1
+  if ! command -v ${pkg} >/dev/null 2>&1; then
+
+    # Ubuntu/Debian
+    if command -v apt-get >/dev/null 2>&1; then
+      sudo apt-get update
+      sudo apt-get install -y ${pkg}
+
+    # CentOS/RHEL
+    elif command -v yum >/dev/null 2>&1; then
+      sudo yum install -y epel-release
+      sudo yum install -y ${pkg}
+
+    # Fedora
+    elif command -v dnf >/dev/null 2>&1; then
+      sudo dnf install -y ${pkg}
+
+    # Arch Linux
+    elif command -v pacman >/dev/null 2>&1; then
+      sudo pacman -S ${pkg}
+
+    # openSUSE
+    elif command -v zypper >/dev/null 2>&1; then
+      sudo zypper install -y ${pkg}
+
+    else
+      echo -e "\e[31mUnable to install ${pkg}, unsupported Linux distro\e[0m"
+      exit 1
+    fi
+  fi
+}
+
+get_tag() {
+  curl -fsSL "https://api.github.com/repos/$1/releases/latest" | jq -r '.tag_name'
+}
+
+# 定义下载函数
+download_file() {
+  name=$1
+  version=$2
+  url=$3
+  local_version=$(jq -r ".[\"$name\"]" <$VERSION_FILE)
+
+  echo -e "\e[1;47;34m$name Version Info: 【本地版本】$local_version | 【最新版本】$version\e[0m"
+
+  if [ "$version" != "$local_version" ]; then
+    echo "Updating $name to $version"
+
+    # 下载
+    if ! curl -L -o "$FILE_DIR/$name" "$url"; then
+      echo -e "\e[31mFailed to download $name\e[0m"
+      return 1
+    fi
+
+    return 0
+  fi
+
+  return 0
+}
+
+process_files() {
+  json="{"
+  for doc in "${!docs[@]}"; do
+    if [[ $doc == *_name ]]; then
+      name=${docs[$doc]}
+      version=${docs[${doc/_name/_version}]}
+      url=${docs[${doc/_name/_url}]}
+
+      download_file "$name" "$version" "$url"
+      json+="\"$name\":\"$version\","
+    fi
+  done
+  json="${json%,}}"
+
+  echo "$json" >$VERSION_FILE
+}

+ 8 - 23
update.sh

@@ -1,23 +1,5 @@
-#!/usr/bin/env bash
-#检查系统
-check_sys() {
-  # shellcheck disable=SC2002
-  if [[ -f /etc/redhat-release ]]; then
-    release="centos"
-  elif cat /etc/issue | grep -q -E -i "debian"; then
-    release="debian"
-  elif cat /etc/issue | grep -q -E -i "ubuntu"; then
-    release="ubuntu"
-  elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
-    release="centos"
-  elif cat /proc/version | grep -q -E -i "debian"; then
-    release="debian"
-  elif cat /proc/version | grep -q -E -i "ubuntu"; then
-    release="ubuntu"
-  elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
-    release="centos"
-  fi
-}
+#!/bin/bash
+
 #检查composer是否安装
 #检查composer是否安装
 check_composer() {
 check_composer() {
   if [ ! -f "/usr/bin/composer" ]; then
   if [ ! -f "/usr/bin/composer" ]; then
@@ -36,10 +18,13 @@ set_permissions() {
   chmod -R 777 storage/
   chmod -R 777 storage/
 }
 }
 
 
-git fetch --all && git reset --hard origin/master && git pull
-check_sys
+git fetch -f
+git reset -q --hard origin/master
+git pull -q
 check_composer
 check_composer
 php artisan optimize:clear
 php artisan optimize:clear
 composer update
 composer update
 php artisan panel:update
 php artisan panel:update
-set_permissions
+set_permissions
+echo -e "\e[32mCheck For newest IP database files | 检测IP数据附件文件最新版本\e[0m"
+cd scripts/ && bash download_dbs.sh && cd ../