Skip to content

Commit

Permalink
新增数据库文件的更新逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhao.binyan committed Jul 25, 2019
1 parent 24c203c commit 10c90df
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -12,7 +12,7 @@

## 数据库文件更新日期

2018年3月25日更新,数据库为2018年3月20日版本
2019年7月25日更新,数据库为2018年7月20日版本

## 使用说明

Expand Down Expand Up @@ -65,7 +65,17 @@ echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
{"ip":"60.195.153.98","country":"中国","province":"北京","city":"顺义区","county":"","isp":"","area":"中国北京顺义区后沙峪金龙网吧"}
```

## 自己手动更新数据库
## 更新数据库

### 在线直接更新

更新到源码目录
`php ~/bin/update-ip.php`

更新到指定目录
`php ~/bin/update-ip.php -d=/tmp`

### 【或者】自己手动更新数据库

1,http://www.cz88.net/fox/ipdat.shtml
下载数据库程序(Windows 环境),执行完毕后,即可在程序安装目录找到数据库文件 qqwry.dat
Expand All @@ -84,6 +94,8 @@ echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
```
IP 地理位置查询类
2019-07-25 赵彬言 1,增加自动更新功能,参考 https://blog.shuax.com/archives/QQWryUpdate.html 感谢 https://github.com/itbdw/ip-database/issues/10
2017-09-12 赵彬言 1,缩减返回数据,去掉字段 remark smallarea beginip endip
2,将调用改为单例模式,保证只读取一次文件
3,修复 bug,直接将返回 gbk 编码内容转为 utf-8,移除编码隐患
Expand Down
84 changes: 84 additions & 0 deletions bin/update-ip.php
@@ -0,0 +1,84 @@
<?php
/**
* Created by PhpStorm.
* User: zhao.binyan
* Date: 2019/7/25
* Time: 下午2:24
*/

/*
纯真数据库自动更新原理实现
www.shuax.com 2014.03.27
*/

/**
* 示例
* `php ~/bin/update-ip.php`
*
* 更新到指定目录
* `php ~/bin/update-ip.php -d=/tmp`
*/

date_default_timezone_set("PRC");

//可设置为服务器特定目录,单独,避免组件升级互相影响
$dir = dirname(__DIR__) . "/src";
$option = getopt("d::");
if (isset($option['d'])) {
if (!is_readable($option['d'])) {
die("bad param, dir not readable " . $option['d']);
}
$dir = $option['d'];
}

$stime = microtime(true);

echo "开始准备更新数据库" . date("Y-m-d H:i:s");
echo "\n";

$copywrite = file_get_contents("http://update.cz88.net/ip/copywrite.rar");

if (!$copywrite) {
$download_spend = $qqwry_time - $stime;
die("copywrite.rar 下载失败 " . sprintf("下载耗时%s", $download_spend));
}

$qqwry = file_get_contents("http://update.cz88.net/ip/qqwry.rar");
$qqwry_time = microtime(true);

if (!$qqwry) {
$download_spend = $qqwry_time - $stime;
die("qqwry.rar 下载失败 " . sprintf("下载耗时%s", $download_spend));
}

$key = unpack("V6", $copywrite)[6];
for ($i = 0; $i < 0x200; $i++) {
$key *= 0x805;
$key++;
$key = $key & 0xFF;
$qqwry[$i] = chr(ord($qqwry[$i]) ^ $key);
}
$qqwry = gzuncompress($qqwry);
$unzip_time = microtime(true);

$download_spend = $qqwry_time - $stime;
$unzip_spend = $unzip_time - $qqwry_time;

if (!$qqwry) {
die("gzip 解压缩失败 " . sprintf("下载耗时%s,解压耗时%s", $download_spend, $unzip_spend));
}

$tmp_file = $dir . '/' . 'qqwry.dat.bak';
$online_file = $dir . '/' . 'qqwry.dat';

if (file_put_contents($tmp_file, $qqwry)) {
$put_time = microtime(true);
$put_spend = $put_time - $unzip_time;
copy($online_file, $online_file.'.online.bak');
copy($tmp_file, $online_file);

$copy_spend = microtime(true) - $put_time;
die("更新成功 " . sprintf("下载耗时%s,解压耗时%s,写入耗时%s,复制耗时%s", $download_spend, $unzip_spend, $put_spend, $copy_spend));
} else {
die("更新失败 " . sprintf("下载耗时%s,解压耗时%s", $download_spend, $unzip_spend));
}
Binary file modified src/qqwry.dat 100755 → 100644
Binary file not shown.

0 comments on commit 10c90df

Please sign in to comment.