diff --git a/app/admin/controller/user/Ip.php b/app/admin/controller/user/Ip.php index 0474358..23674c3 100644 --- a/app/admin/controller/user/Ip.php +++ b/app/admin/controller/user/Ip.php @@ -2,6 +2,7 @@ namespace app\admin\controller\user; +use app\admin\service\IpService; use app\admin\validate\IpValidate; use app\common\controller\AdminController; use app\admin\model\Ip as IpModel; @@ -30,7 +31,7 @@ class Ip extends AdminController ->withoutField('delete_time') ->where($where) ->page($page, $limit) - ->order('id') + ->order('id desc') ->select() ->each(function ($item) { $item['expire_time'] = date("Y-m-d H:i:s", $item['expire_time']); @@ -56,6 +57,8 @@ class Ip extends AdminController $post = $this->request->post(); try { validate(IpValidate::class)->scene('add')->check($post); + $exists = IpValidate::checkExistsIp($post['ip'], $post['port']); + if ($exists !== true) throw new ValidateException($exists); $post['expire_time'] = strtotime($post['expire_time']); $save = $this->model->save($post); } catch (ValidateException $e) { @@ -79,6 +82,8 @@ class Ip extends AdminController $post = $this->request->post(); try { validate(IpValidate::class)->scene('edit')->check($post); + $exists = IpValidate::checkExistsIp($post['ip'], $post['port'], $post['id']); + if ($exists !== true) throw new ValidateException($exists); $post['expire_time'] = strtotime($post['expire_time']); $save = $row->save($post); } catch (ValidateException $e) { @@ -139,4 +144,17 @@ class Ip extends AdminController $this->success('保存成功'); } + /** + * 更新ip + */ + public function updateIp() + { + try { + $res = IpService::proxy(); + if (!$res) throw new \Exception(''); + } catch (\Exception $e) { + $this->error('更新失败'); + } + $this->success('更新成功'); + } } diff --git a/app/admin/model/Ip.php b/app/admin/model/Ip.php index 0cb62f2..0a632fb 100644 --- a/app/admin/model/Ip.php +++ b/app/admin/model/Ip.php @@ -2,6 +2,7 @@ namespace app\admin\model; +use app\admin\validate\IpValidate; use app\common\model\TimeModel; class Ip extends TimeModel @@ -10,6 +11,8 @@ class Ip extends TimeModel protected $defaultSoftDelete = '0'; + public static $saveField = ['ip', 'port', 'expire_time', 'city', 'isp', 'out_ip', 'status']; + public static function getPageList($param = []) { $page = $param['page'] ?? 1; @@ -24,4 +27,41 @@ class Ip extends TimeModel $list = self::where($where)->field($field)->order($order)->page($page, $limit)->select(); return ['data' => $list, 'count' => $count]; } + + /** + * 更新IP + * @param $params + * @return bool + */ + public static function updateIp($params): bool + { + if (empty($params) || !is_array($params)) return false; + + $is_true = false; + + + foreach ($params as $item) { + + foreach ($item as $key => $value) { + if (!in_array($key, self::$saveField)) { + unset($item[$key]); + } + } + + if (isset($item['expire_time'])) { + $item['expire_time'] = strtotime($item['expire_time']) ?: 0; + } + + if (!isset($item['ip']) && !isset($item['port'])) continue; + + $existsValidate = IpValidate::checkExistsIp($item['ip'], $item['port']); + + if ($existsValidate === true) { + $model = new self(); + $model->save($item); + $is_true = true; + } + } + return $is_true; + } } diff --git a/app/admin/service/IpService.php b/app/admin/service/IpService.php new file mode 100644 index 0000000..c64742c --- /dev/null +++ b/app/admin/service/IpService.php @@ -0,0 +1,70 @@ + $app_key, + // 指定套餐,默认不指定 + "pack" => "0", + // 获取代理IP数量 + "num" => "20", + // 代理协议 http协议: 1 + "xy" => "1", + // 格式 1:txt 2-json + "type" => "2", + // 支持自定义间隔符 + "lb" => "\r\n", + // 是否去重,默认不去重,不去重=0,去重=99 + "nr" => "0", + // 指定地区,默认全国,支持指定省或市,支持同时指定多个,英文符号 | 分隔 + // 全国: 0 + // 北京市: 110000 + // 北京市+江苏省+杭州市: 110000|320000|330100 + "area_id" => "0", + // + "isp" => 0, + ]; + + $data = self::curl($url, $params); + + return IpModel::updateIp($data); + } + + protected static function curl($url, $params) + { + $ch = curl_init(); + // 设置请求地址 + curl_setopt($ch, CURLOPT_URL, $url . http_build_query($params)); + // https请求 不验证证书和hosts + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + // 响应内容返回而不是直接输出 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + // 请求超时时间 秒 + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + // 发出网络请求 + $output = curl_exec($ch); + $result = json_decode($output, true); + if (isset($result['code'])) { + if ($result['code'] == 200) { + return $result['data']; + } + } + return []; + } + +} diff --git a/app/admin/validate/IpValidate.php b/app/admin/validate/IpValidate.php index 1382777..2cdd50f 100644 --- a/app/admin/validate/IpValidate.php +++ b/app/admin/validate/IpValidate.php @@ -2,6 +2,7 @@ namespace app\admin\validate; +use app\admin\model\Ip as IpModel; use think\Validate; class IpValidate extends Validate @@ -25,4 +26,23 @@ class IpValidate extends Validate 'edit' => ['id', 'ip', 'port', 'expire_time', 'city', 'isp', 'out_ip', 'status'] ]; + /** + * + * @param $ip + * @param $port + * @param int $id + * @return bool|string + */ + public static function checkExistsIp($ip, $port, int $id = 0) + { + $IpModel = new IpModel(); + $where = [['ip', '=', $ip], ['port', '=', $port]]; + if ($id > 0) $where[] = ['id', '!=', $id]; + try { + $existsRes = $IpModel->where($where)->find(); + return $existsRes ? 'IP及端口已存在' : true; + } catch (\Exception $e) { + return '验证失败'; + } + } } diff --git a/app/common/command/IpUpdate.php b/app/common/command/IpUpdate.php new file mode 100644 index 0000000..7e557cd --- /dev/null +++ b/app/common/command/IpUpdate.php @@ -0,0 +1,47 @@ +setName('ip_up') + ->setDescription('系统节点刷新服务'); + } + + protected function execute(Input $input, Output $output) + { + $output->writeln("========正在更新IP状态:=====" . date('Y-m-d H:i:s')); + $check = $this->update(); + $check !== true && $output->writeln("更新IP状态失败:" . $check); + $output->writeln("更新IP状态完成:" . date('Y-m-d H:i:s')); + } + + protected function update(): bool + { + $ipModel = new IpModel(); + $time = time(); + + $where = [['status', '=', 1], ['expire_time', '<', $time]]; + $idArr = $ipModel->where($where)->column('id'); + + if ($idArr) { + $res = $ipModel->where('id', 'in', $idArr)->save(['status' => 0]); + if (!$res) { + return '数据更新失败'; + } + } else { + return '占无修改数据'; + } + + return true; + } + +} diff --git a/config/console.php b/config/console.php index f5ab314..ef6f2b4 100644 --- a/config/console.php +++ b/config/console.php @@ -8,5 +8,6 @@ return [ 'curd' => 'app\common\command\Curd', 'node' => 'app\common\command\Node', 'OssStatic' => 'app\common\command\OssStatic', + 'ip_up' => 'app\common\command\IpUpdate', ], ]; diff --git a/public/static/admin/js/user/ip.js b/public/static/admin/js/user/ip.js index c95f943..1bc0913 100644 --- a/public/static/admin/js/user/ip.js +++ b/public/static/admin/js/user/ip.js @@ -22,11 +22,21 @@ define(["jquery", "easy-admin"], function ($, ea) { 'delete', [{ text: '导入', - title: '确定更新新节点?', + title: '确定导入新IP?', url: 'user.ip/import', method: 'request', auth: 'refresh', class: 'layui-btn layui-btn-success layui-btn-sm', + icon: 'fa fa-cloud-upload', + extend: 'data-table="' + init.table_render_id + '"', + }], + [{ + text: '更新', + title: '确定更新IP?', + url: 'user.ip/updateIp', + method: 'request', + auth: 'refresh', + class: 'layui-btn layui-btn-sm', icon: 'fa fa-hourglass', extend: 'data-table="' + init.table_render_id + '"', }]