Skip to content

Commit

Permalink
1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
netcccyun committed Jul 7, 2024
1 parent bf6a091 commit 57a237f
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 14 deletions.
4 changes: 4 additions & 0 deletions app/command/Dmtask.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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
Expand All @@ -23,6 +24,9 @@ protected function configure()

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扩展');
Expand Down
4 changes: 4 additions & 0 deletions app/command/Opiptask.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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
Expand All @@ -23,6 +24,9 @@ protected function configure()

protected function execute(Input $input, Output $output)
{
$res = Db::name('config')->cache('configs',0)->column('value','key');
Config::set($res, 'sys');

(new OptimizeService())->execute();
}
}
34 changes: 34 additions & 0 deletions app/controller/Dmonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function taskform()
'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(),
Expand Down Expand Up @@ -121,6 +122,7 @@ public function taskform()
'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'),
];
Expand Down Expand Up @@ -233,6 +235,23 @@ public function noticeset()
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', '无权限');
Expand All @@ -246,6 +265,21 @@ public function mailtest()
}
}

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 = "<strong>消息发送测试</strong>\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', '无权限');
Expand Down
24 changes: 23 additions & 1 deletion app/lib/CheckUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class CheckUtils
{
public static function curl($url, $timeout, $ip = null)
public static function curl($url, $timeout, $ip = null, $proxy = false)
{
$status = true;
$errmsg = null;
Expand All @@ -19,6 +19,28 @@ public static function curl($url, $timeout, $ip = null)
}
}
$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);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/DnsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DnsHelper
'name' => 'Cloudflare',
'config' => [
'ak' => '邮箱地址',
'sk' => 'API密钥'
'sk' => 'API密钥/令牌'
],
'remark' => 2,
'status' => false,
Expand Down
66 changes: 64 additions & 2 deletions app/lib/MsgNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public static function send($action, $task, $result)
self::send_mail($mail_name, $mail_title, $mail_content);
}
if(config_get('notice_wxtpl') == 1){
$mail_content = str_replace(['<br/>', '<b>', '</b>'], ["\n\n", '**', '**'], $mail_content);
self::send_wechat_tplmsg($mail_title, $mail_content);
$content = str_replace(['<br/>', '<b>', '</b>'], ["\n\n", '**', '**'], $mail_content);
self::send_wechat_tplmsg($mail_title, $content);
}
if(config_get('notice_tgbot') == 1){
$content = str_replace('<br/>', "\n", $mail_content);
$content = "<strong>".$mail_title."</strong>\n".$content;
self::send_telegram_bot($content);
}
}

Expand Down Expand Up @@ -102,4 +107,61 @@ public static function send_wechat_tplmsg($title, $content){
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;
}
}
2 changes: 1 addition & 1 deletion app/lib/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function execute($row)
}
}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{
Expand Down
14 changes: 10 additions & 4 deletions app/lib/dns/cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,16 @@ private function convertType($type){
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') {
Expand Down
3 changes: 2 additions & 1 deletion app/sql/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion app/sql/update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
ADD COLUMN `remark` varchar(100) DEFAULT NULL;

ALTER TABLE `dnsmgr_dmtask`
ADD COLUMN `proxy` tinyint(1) NOT NULL DEFAULT 0;
3 changes: 2 additions & 1 deletion app/view/common/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<a href="/domain"><i class="fa fa-list-ul fa-fw"></i> <span>域名管理</span></a>
</li>
{if request()->user['level'] eq 2}
<li class="treeview {:checkIfActive('overview,task,noticeset,taskinfo,taskform')}">
<li class="treeview {:checkIfActive('overview,task,noticeset,taskinfo,taskform,proxyset')}">
<a href="javascript:;">
<i class="fa fa-heartbeat fa-fw"></i>
<span>容灾切换</span>
Expand All @@ -119,6 +119,7 @@
<li><a href="/dmonitor/overview"><i class="fa fa-circle-o"></i> 运行概览</a></li>
<li><a href="/dmonitor/task"><i class="fa fa-circle-o"></i> 切换策略</a></li>
<li><a href="/dmonitor/noticeset"><i class="fa fa-circle-o"></i> 通知设置</a></li>
<li><a href="/dmonitor/proxyset"><i class="fa fa-circle-o"></i> 代理设置</a></li>
</ul>
</li>
<li class="treeview {:checkIfActive('opipset,opiplist,opipform')}">
Expand Down
54 changes: 53 additions & 1 deletion app/view/dmonitor/noticeset.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<label class="col-sm-3 control-label">微信公众号通知</label>
<div class="col-sm-9"><select class="form-control" name="notice_wxtpl" default="{:config_get('notice_wxtpl')}"><option value="0">关闭</option><option value="1">开启</option></select></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Telegram机器人通知</label>
<div class="col-sm-9"><select class="form-control" name="notice_tgbot" default="{:config_get('notice_tgbot')}"><option value="0">关闭</option><option value="1">开启</option></select></div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9"><input type="submit" name="submit" value="保存" class="btn btn-primary btn-block"/></div>
</div>
Expand Down Expand Up @@ -99,6 +103,34 @@
<b>WxPusher:</b><a href="https://wxpusher.zjiecode.com/admin/" target="_blank" rel="noopener noreferrer">点此进入</a> ,注册并且创建应用 -> 将appToken填写到上方输入框 -> 扫码关注应用 -> 在用户列表查看自己的UID填写到上方输入框<br/>
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading"><h3 class="panel-title">Telegram机器人接口设置</h3></div>
<div class="panel-body">
<form onsubmit="return saveSetting(this)" method="post" class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-3 control-label">Token</label>
<div class="col-sm-9"><input type="text" name="tgbot_token" value="{:config_get('tgbot_token')}" class="form-control"/></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Chat Id</label>
<div class="col-sm-9"><input type="text" name="tgbot_chatid" value="{:config_get('tgbot_chatid')}" class="form-control"/></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">使用代理服务器</label>
<div class="col-sm-9"><select class="form-control" name="tgbot_proxy" default="{:config_get('tgbot_proxy')}"><option value="0"></option><option value="1"></option></select></div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<input type="submit" name="submit" value="保存" class="btn btn-primary btn-block"/>
<a href="javascript:tgbottest()" class="btn btn-default btn-block">发送测试消息</a>
</div>
</div>
</form>
</div>
<div class="panel-footer">
<a href="https://t.me/BotFather" target="_blank" rel="noopener noreferrer">@BotFather</a>对话,使用/newbot命令创建一个新的机器人,根据提示输入机器人的名称和用户名,可得到Token,或使用/mybots命令查看已创建的机器人;与<a href="https://t.me/getmyid_bot" target="_blank" rel="noopener noreferrer">@getmyid_bot</a>对话,可得到Chat Id<br/>
</div>
</div>
</div>
</div>
{/block}
Expand Down Expand Up @@ -129,7 +161,7 @@
success : function(data) {
layer.close(ii);
if(data.code == 0){
layer.alert('设置保存成功!', {
layer.alert('设置保存成功!<br/>重启检测进程或容器后生效', {
icon: 1,
closeBtn: false
}, function(){
Expand Down Expand Up @@ -166,5 +198,25 @@
}
});
}
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('服务器错误');
}
});
}
</script>
{/block}
Loading

0 comments on commit 57a237f

Please sign in to comment.