diff --git a/app/command/Dmtask.php b/app/command/Dmtask.php index b602823..618f4a8 100644 --- a/app/command/Dmtask.php +++ b/app/command/Dmtask.php @@ -10,6 +10,7 @@ use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use think\facade\Db; +use think\facade\Config; use app\lib\TaskRunner; class Dmtask extends Command @@ -23,6 +24,9 @@ class Dmtask extends Command protected function execute(Input $input, Output $output) { + $res = Db::name('config')->cache('configs',0)->column('value','key'); + Config::set($res, 'sys'); + config_set('run_error', ''); if(!extension_loaded('swoole')){ $output->writeln('[Error] 未安装Swoole扩展'); diff --git a/app/command/Opiptask.php b/app/command/Opiptask.php index 1a40b3c..e227a56 100644 --- a/app/command/Opiptask.php +++ b/app/command/Opiptask.php @@ -10,6 +10,7 @@ use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use think\facade\Db; +use think\facade\Config; use app\lib\OptimizeService; class Opiptask extends Command @@ -23,6 +24,9 @@ class Opiptask extends Command protected function execute(Input $input, Output $output) { + $res = Db::name('config')->cache('configs',0)->column('value','key'); + Config::set($res, 'sys'); + (new OptimizeService())->execute(); } } diff --git a/app/controller/Dmonitor.php b/app/controller/Dmonitor.php index 9430a43..a3c0344 100644 --- a/app/controller/Dmonitor.php +++ b/app/controller/Dmonitor.php @@ -86,6 +86,7 @@ class Dmonitor extends BaseController 'frequency' => input('post.frequency/d'), 'cycle' => input('post.cycle/d'), 'timeout' => input('post.timeout/d'), + 'proxy' => input('post.proxy/d'), 'remark' => input('post.remark', null, 'trim'), 'recordinfo' => input('post.recordinfo', null, 'trim'), 'addtime' => time(), @@ -121,6 +122,7 @@ class Dmonitor extends BaseController 'frequency' => input('post.frequency/d'), 'cycle' => input('post.cycle/d'), 'timeout' => input('post.timeout/d'), + 'proxy' => input('post.proxy/d'), 'remark' => input('post.remark', null, 'trim'), 'recordinfo' => input('post.recordinfo', null, 'trim'), ]; @@ -233,6 +235,23 @@ class Dmonitor extends BaseController return View::fetch(); } + public function proxyset() + { + if(!checkPermission(2)) return $this->alert('error', '无权限'); + if(request()->isPost()){ + $params = input('post.'); + foreach ($params as $key=>$value){ + if (empty($key)) { + continue; + } + config_set($key, $value); + Cache::delete('configs'); + } + return json(['code'=>0, 'msg'=>'succ']); + } + return View::fetch(); + } + public function mailtest() { if(!checkPermission(2)) return $this->alert('error', '无权限'); @@ -246,6 +265,21 @@ class Dmonitor extends BaseController } } + public function tgbottest() + { + if(!checkPermission(2)) return $this->alert('error', '无权限'); + $tgbot_token = config_get('tgbot_token'); + $tgbot_chatid = config_get('tgbot_chatid'); + if(empty($tgbot_token) || empty($tgbot_chatid)) return json(['code'=>-1, 'msg'=>'请先保存设置']); + $content = "消息发送测试\n\n这是一封测试消息!\n\n来自:".request()->root(true); + $result = \app\lib\MsgNotice::send_telegram_bot($content); + if($result === true){ + return json(['code'=>0, 'msg'=>'消息发送成功!']); + }else{ + return json(['code'=>-1, 'msg'=>'消息发送失败!'.$result]); + } + } + public function clean() { if(!checkPermission(2)) return $this->alert('error', '无权限'); diff --git a/app/lib/CheckUtils.php b/app/lib/CheckUtils.php index 6aebc31..2b4ba21 100644 --- a/app/lib/CheckUtils.php +++ b/app/lib/CheckUtils.php @@ -4,7 +4,7 @@ namespace app\lib; class CheckUtils { - public static function curl($url, $timeout, $ip = null) + public static function curl($url, $timeout, $ip = null, $proxy = false) { $status = true; $errmsg = null; @@ -19,6 +19,28 @@ class CheckUtils } } $ch = curl_init(); + if($proxy){ + $proxy_server = config_get('proxy_server'); + $proxy_port = intval(config_get('proxy_port')); + $proxy_userpwd = config_get('proxy_user').':'.config_get('proxy_pwd'); + $proxy_type = config_get('proxy_type'); + if($proxy_type == 'https'){ + $proxy_type = CURLPROXY_HTTPS; + }elseif($proxy_type == 'sock4'){ + $proxy_type = CURLPROXY_SOCKS4; + }elseif($proxy_type == 'sock5'){ + $proxy_type = CURLPROXY_SOCKS5; + }else{ + $proxy_type = CURLPROXY_HTTP; + } + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); + curl_setopt($ch, CURLOPT_PROXY, $proxy_server); + curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port); + if($proxy_userpwd != ':'){ + curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_userpwd); + } + curl_setopt($ch, CURLOPT_PROXYTYPE, $proxy_type); + } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); diff --git a/app/lib/DnsHelper.php b/app/lib/DnsHelper.php index 006673e..7c3b673 100644 --- a/app/lib/DnsHelper.php +++ b/app/lib/DnsHelper.php @@ -65,7 +65,7 @@ class DnsHelper 'name' => 'Cloudflare', 'config' => [ 'ak' => '邮箱地址', - 'sk' => 'API密钥' + 'sk' => 'API密钥/令牌' ], 'remark' => 2, 'status' => false, diff --git a/app/lib/MsgNotice.php b/app/lib/MsgNotice.php index 78b60ee..34dd722 100644 --- a/app/lib/MsgNotice.php +++ b/app/lib/MsgNotice.php @@ -41,8 +41,13 @@ class MsgNotice self::send_mail($mail_name, $mail_title, $mail_content); } if(config_get('notice_wxtpl') == 1){ - $mail_content = str_replace(['
', '', ''], ["\n\n", '**', '**'], $mail_content); - self::send_wechat_tplmsg($mail_title, $mail_content); + $content = str_replace(['
', '', ''], ["\n\n", '**', '**'], $mail_content); + self::send_wechat_tplmsg($mail_title, $content); + } + if(config_get('notice_tgbot') == 1){ + $content = str_replace('
', "\n", $mail_content); + $content = "".$mail_title."\n".$content; + self::send_telegram_bot($content); } } @@ -102,4 +107,61 @@ class MsgNotice return $arr['msg']; } } + + public static function send_telegram_bot($content){ + $tgbot_token = config_get('tgbot_token'); + $tgbot_chatid = config_get('tgbot_chatid'); + if(!$tgbot_token||!$tgbot_chatid)return false; + $url = 'https://api.telegram.org/bot'.$tgbot_token.'/sendMessage'; + $post = ['chat_id'=>$tgbot_chatid, 'text'=>$content, 'parse_mode'=>'HTML']; + $result = self::telegram_curl($url, http_build_query($post)); + $arr = json_decode($result, true); + if(isset($arr['ok']) && $arr['ok']==true){ + return true; + }else{ + return $arr['description']; + } + } + + private static function telegram_curl($url, $post){ + $ch = curl_init(); + if(config_get('tgbot_proxy') == 1){ + $proxy_server = config_get('proxy_server'); + $proxy_port = intval(config_get('proxy_port')); + $proxy_userpwd = config_get('proxy_user').':'.config_get('proxy_pwd'); + $proxy_type = config_get('proxy_type'); + if($proxy_type == 'https'){ + $proxy_type = CURLPROXY_HTTPS; + }elseif($proxy_type == 'sock4'){ + $proxy_type = CURLPROXY_SOCKS4; + }elseif($proxy_type == 'sock5'){ + $proxy_type = CURLPROXY_SOCKS5; + }else{ + $proxy_type = CURLPROXY_HTTP; + } + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); + curl_setopt($ch, CURLOPT_PROXY, $proxy_server); + curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port); + if($proxy_userpwd != ':'){ + curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_userpwd); + } + curl_setopt($ch, CURLOPT_PROXYTYPE, $proxy_type); + } + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + $httpheader[] = "Accept: */*"; + $httpheader[] = "Accept-Encoding: gzip,deflate,sdch"; + $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8"; + $httpheader[] = "Connection: close"; + curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post); + curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0"); + curl_setopt($ch, CURLOPT_ENCODING, "gzip"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $ret = curl_exec($ch); + curl_close($ch); + return $ret; + } } \ No newline at end of file diff --git a/app/lib/TaskRunner.php b/app/lib/TaskRunner.php index 7925bc8..8cef5b0 100644 --- a/app/lib/TaskRunner.php +++ b/app/lib/TaskRunner.php @@ -39,7 +39,7 @@ class TaskRunner } }else{ if($row['checktype'] == 2){ - $result = CheckUtils::curl($row['checkurl'], $row['timeout'], $row['main_value']); + $result = CheckUtils::curl($row['checkurl'], $row['timeout'], $row['main_value'], $row['proxy'] == 1); }else if($row['checktype'] == 1){ $result = CheckUtils::tcp($row['main_value'], $row['tcpport'], $row['timeout']); }else{ diff --git a/app/lib/dns/cloudflare.php b/app/lib/dns/cloudflare.php index 02a983a..2ad2fab 100644 --- a/app/lib/dns/cloudflare.php +++ b/app/lib/dns/cloudflare.php @@ -178,10 +178,16 @@ class cloudflare implements DnsInterface { private function send_reuqest($method, $path, $params = null){ $url = $this->baseUrl . $path; - $headers = [ - 'X-Auth-Email: '.$this->Email, - 'X-Auth-Key: '.$this->ApiKey, - ]; + if(preg_match('/^[0-9a-z]+$/i',$this->ApiKey)){ + $headers = [ + 'X-Auth-Email: '.$this->Email, + 'X-Auth-Key: '.$this->ApiKey, + ]; + }else{ + $headers = [ + 'Authorization: Bearer '.$this->ApiKey, + ]; + } $body = ''; if ($method == 'GET' || $method == 'DELETE') { diff --git a/app/sql/install.sql b/app/sql/install.sql index 2672ee3..2a80969 100644 --- a/app/sql/install.sql +++ b/app/sql/install.sql @@ -5,7 +5,7 @@ CREATE TABLE `dnsmgr_config` ( PRIMARY KEY (`key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -INSERT INTO `dnsmgr_config` VALUES ('version', '1007'); +INSERT INTO `dnsmgr_config` VALUES ('version', '1011'); INSERT INTO `dnsmgr_config` VALUES ('notice_mail', '0'); INSERT INTO `dnsmgr_config` VALUES ('notice_wxtpl', '0'); INSERT INTO `dnsmgr_config` VALUES ('mail_smtp', 'smtp.qq.com'); @@ -92,6 +92,7 @@ CREATE TABLE `dnsmgr_dmtask` ( `cycle` tinyint(5) NOT NULL DEFAULT 3, `timeout` tinyint(5) NOT NULL DEFAULT 2, `remark` varchar(100) DEFAULT NULL, + `proxy` tinyint(1) NOT NULL DEFAULT 0, `addtime` int(11) NOT NULL DEFAULT 0, `checktime` int(11) NOT NULL DEFAULT 0, `checknexttime` int(11) NOT NULL DEFAULT 0, diff --git a/app/sql/update.sql b/app/sql/update.sql index 1b82512..958a764 100644 --- a/app/sql/update.sql +++ b/app/sql/update.sql @@ -62,4 +62,7 @@ CREATE TABLE IF NOT EXISTS `dnsmgr_optimizeip` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ALTER TABLE `dnsmgr_domain` -ADD COLUMN `remark` varchar(100) DEFAULT NULL; \ No newline at end of file +ADD COLUMN `remark` varchar(100) DEFAULT NULL; + +ALTER TABLE `dnsmgr_dmtask` +ADD COLUMN `proxy` tinyint(1) NOT NULL DEFAULT 0; \ No newline at end of file diff --git a/app/view/common/layout.html b/app/view/common/layout.html index efeb3bd..40cdc15 100644 --- a/app/view/common/layout.html +++ b/app/view/common/layout.html @@ -107,7 +107,7 @@ 域名管理 {if request()->user['level'] eq 2} -
  • +
  • 容灾切换 @@ -119,6 +119,7 @@
  • 运行概览
  • 切换策略
  • 通知设置
  • +
  • 代理设置
  • diff --git a/app/view/dmonitor/noticeset.html b/app/view/dmonitor/noticeset.html index 4d82ac6..107ea70 100644 --- a/app/view/dmonitor/noticeset.html +++ b/app/view/dmonitor/noticeset.html @@ -15,6 +15,10 @@
    +
    + +
    +
    @@ -99,6 +103,34 @@ WxPusher:点此进入 ,注册并且创建应用 -> 将appToken填写到上方输入框 -> 扫码关注应用 -> 在用户列表查看自己的UID填写到上方输入框
    +
    +

    Telegram机器人接口设置

    +
    +
    +
    + +
    +
    +
    + +
    +
    +
    + +
    +
    +
    + +
    +
    +
    + +
    {/block} @@ -129,7 +161,7 @@ function saveSetting(obj){ success : function(data) { layer.close(ii); if(data.code == 0){ - layer.alert('设置保存成功!', { + layer.alert('设置保存成功!
    重启检测进程或容器后生效', { icon: 1, closeBtn: false }, function(){ @@ -166,5 +198,25 @@ function mailtest(){ } }); } +function tgbottest(){ + var ii = layer.load(2, {shade:[0.1,'#fff']}); + $.ajax({ + type : 'GET', + url : '/dmonitor/tgbottest', + dataType : 'json', + success : function(data) { + layer.close(ii); + if(data.code == 0){ + layer.alert(data.msg, {icon: 1}); + }else{ + layer.alert(data.msg, {icon: 2}) + } + }, + error:function(data){ + layer.close(ii); + layer.msg('服务器错误'); + } + }); +} {/block} \ No newline at end of file diff --git a/app/view/dmonitor/proxyset.html b/app/view/dmonitor/proxyset.html new file mode 100644 index 0000000..119a3f3 --- /dev/null +++ b/app/view/dmonitor/proxyset.html @@ -0,0 +1,129 @@ +{extend name="common/layout" /} +{block name="title"}容灾切换代理设置{/block} +{block name="main"} +
    +
    +
    +

    代理服务器设置

    +
    +
    +
    + +
    +

    +
    + +
    +

    +
    + +
    +

    +
    + +
    +

    +
    + +
    +

    +
    +
    +
    +
    +
    +
    +
    +
    +{/block} +{block name="script"} + + +{/block} \ No newline at end of file diff --git a/app/view/dmonitor/taskform.html b/app/view/dmonitor/taskform.html index dd6541c..d1bc22a 100644 --- a/app/view/dmonitor/taskform.html +++ b/app/view/dmonitor/taskform.html @@ -70,6 +70,17 @@ +
    + +
    + + +
    +
    @@ -141,6 +152,7 @@ new Vue({ frequency: 5, timeout: 2, cycle: 3, + proxy: 0, }, recordList: [], typeList: [ diff --git a/config/app.php b/config/app.php index e7b82b9..6527ef6 100644 --- a/config/app.php +++ b/config/app.php @@ -33,5 +33,5 @@ return [ 'version' => '1011', - 'dbversion' => '1007' + 'dbversion' => '1011' ]; diff --git a/route/app.php b/route/app.php index 6a4ea27..992a09e 100644 --- a/route/app.php +++ b/route/app.php @@ -69,7 +69,9 @@ Route::group(function () { Route::any('/dmonitor/task/:action', 'dmonitor/taskform'); Route::get('/dmonitor/task', 'dmonitor/task'); Route::any('/dmonitor/noticeset', 'dmonitor/noticeset'); + Route::any('/dmonitor/proxyset', 'dmonitor/proxyset'); Route::get('/dmonitor/mailtest', 'dmonitor/mailtest'); + Route::get('/dmonitor/tgbottest', 'dmonitor/tgbottest'); Route::post('/dmonitor/clean', 'dmonitor/clean'); Route::any('/optimizeip/opipset', 'optimizeip/opipset');