Browse Source

ip接口倒序输出、自动更新禁用、一键更新ip

master
wanghongjun 1 year ago
parent
commit
33a3899c72
  1. 20
      app/admin/controller/user/Ip.php
  2. 40
      app/admin/model/Ip.php
  3. 70
      app/admin/service/IpService.php
  4. 20
      app/admin/validate/IpValidate.php
  5. 47
      app/common/command/IpUpdate.php
  6. 1
      config/console.php
  7. 12
      public/static/admin/js/user/ip.js

20
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('更新成功');
}
}

40
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;
}
}

70
app/admin/service/IpService.php

@ -0,0 +1,70 @@
<?php
namespace app\admin\service;
use app\admin\model\Ip as IpModel;
class IpService
{
/**
* 发起请求获取IP
* @return bool
*/
public static function proxy(): bool
{
$app_key = "50f95964abbb4b10f9b5e4f1075149d0";
$url = "https://api.wandoudl.com/api/ip/list-v2?";
$params = [
// 账户密钥
"app_key" => $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 [];
}
}

20
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 '验证失败';
}
}
}

47
app/common/command/IpUpdate.php

@ -0,0 +1,47 @@
<?php
namespace app\common\command;
use app\admin\model\Ip as IpModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class IpUpdate extends Command
{
protected function configure()
{
$this->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;
}
}

1
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',
],
];

12
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 + '"',
}]

Loading…
Cancel
Save