From 865275c0658a8a60ad8dff91630f48038a1ea2ee Mon Sep 17 00:00:00 2001 From: net909 Date: Mon, 30 Dec 2024 18:00:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A5=BF=E9=83=A8=E6=95=B0?= =?UTF-8?q?=E7=A0=81=E8=99=9A=E6=8B=9F=E4=B8=BB=E6=9C=BA=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/DeployHelper.php | 37 ++++++++++ app/lib/deploy/west.php | 131 ++++++++++++++++++++++++++++++++++ app/utils/CheckUtils.php | 8 +-- app/view/system/proxyset.html | 2 +- 4 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 app/lib/deploy/west.php diff --git a/app/lib/DeployHelper.php b/app/lib/DeployHelper.php index 85b7c8b..f680d36 100644 --- a/app/lib/DeployHelper.php +++ b/app/lib/DeployHelper.php @@ -939,6 +939,43 @@ class DeployHelper ], ], ], + 'west' => [ + 'name' => '西部数码', + 'class' => 2, + 'icon' => 'west.ico', + 'note' => '支持部署到西部数码虚拟主机', + 'inputs' => [ + 'username' => [ + 'name' => '用户名', + 'type' => 'input', + 'placeholder' => '', + 'required' => true, + ], + 'api_password' => [ + 'name' => 'API密码', + 'type' => 'input', + 'placeholder' => '', + 'required' => true, + ], + 'proxy' => [ + 'name' => '使用代理服务器', + 'type' => 'radio', + 'options' => [ + '0' => '否', + '1' => '是', + ], + 'value' => '0' + ], + ], + 'taskinputs' => [ + 'sitename' => [ + 'name' => 'FTP账号', + 'type' => 'input', + 'placeholder' => '', + 'required' => true, + ], + ], + ], 'baishan' => [ 'name' => '白山云', 'class' => 2, diff --git a/app/lib/deploy/west.php b/app/lib/deploy/west.php new file mode 100644 index 0000000..c96b223 --- /dev/null +++ b/app/lib/deploy/west.php @@ -0,0 +1,131 @@ +username = $config['username']; + $this->api_password = $config['api_password']; + $this->proxy = $config['proxy'] == 1; + } + + public function check() + { + if (empty($this->username) || empty($this->api_password)) throw new Exception('用户名或API密码不能为空'); + $this->execute('/vhost/', ['act' => 'products']); + } + + public function deploy($fullchain, $privatekey, $config, &$info) + { + if (empty($config['sitename'])) throw new Exception('FTP账号不能为空'); + $params = [ + 'act' => 'vhostssl', + 'sitename' => $config['sitename'], + 'cmd' => 'info' + ]; + try { + $data = $this->execute('/vhost/', $params); + } catch (Exception $e) { + throw new Exception('获取虚拟主机SSL配置失败:' . $e->getMessage()); + } + + $params = [ + 'act' => 'vhostssl', + 'sitename' => $config['sitename'], + 'cmd' => 'import', + 'keycontent' => $privatekey, + 'certcontent' => $fullchain, + ]; + try { + $this->execute('/vhost/', $params); + } catch (Exception $e) { + throw new Exception('上传SSL证书失败:' . $e->getMessage()); + } + $this->log('SSL证书上传成功'); + + if (!isset($data['SSLEnabled']) || $data['SSLEnabled'] == 0) { + $params = [ + 'act' => 'vhostssl', + 'sitename' => $config['sitename'], + 'cmd' => 'openssl', + ]; + try { + $this->execute('/vhost/', $params); + } catch (Exception $e) { + throw new Exception('虚拟主机部署SSL失败:' . $e->getMessage()); + } + } else { + $params = [ + 'act' => 'vhostssl', + 'sitename' => $config['sitename'], + 'cmd' => 'info' + ]; + try { + $data = $this->execute('/vhost/', $params); + } catch (Exception $e) { + throw new Exception('获取虚拟主机SSL配置失败:' . $e->getMessage()); + } + if (!empty($data['sslcert']['ssl'])) { + foreach ($data['sslcert']['ssl'] as $domain => $row) { + if (!in_array($domain, $config['domainList'])) continue; + $params = [ + 'act' => 'vhostssl', + 'sitename' => $config['sitename'], + 'cmd' => 'clearsslcache', + 'sslid' => $row['sysid'], + 'dm' => $domain, + ]; + try { + $this->execute('/vhost/', $params); + $this->log('更新' . $domain . '证书缓存成功'); + } catch (Exception $e) { + $this->log('更新' . $domain . '证书缓存失败:' . $e->getMessage()); + } + } + } + } + $this->log('虚拟主机' . $config['sitename'] . '部署SSL成功'); + } + + private function execute($path, $params) + { + $params['username'] = $this->username; + $params['time'] = getMillisecond(); + $params['token'] = md5($this->username . $this->api_password . $params['time']); + $response = curl_client($this->baseUrl . $path, str_replace('+', '%20', http_build_query($params)), null, null, null, $this->proxy); + $response = mb_convert_encoding($response['body'], 'UTF-8', 'GBK'); + $arr = json_decode($response, true); + if ($arr) { + if ($arr['result'] == 200) { + return isset($arr['data']) ? $arr['data'] : []; + } else { + throw new Exception($arr['msg']); + } + } else { + throw new Exception('请求失败(httpCode=' . $response['code'] . ')'); + } + } + + public function setLogger($func) + { + $this->logger = $func; + } + + private function log($txt) + { + if ($this->logger) { + call_user_func($this->logger, $txt); + } + } +} diff --git a/app/utils/CheckUtils.php b/app/utils/CheckUtils.php index ea45536..91cac0d 100644 --- a/app/utils/CheckUtils.php +++ b/app/utils/CheckUtils.php @@ -76,7 +76,7 @@ class CheckUtils { if (!filter_var($target, FILTER_VALIDATE_IP) && checkDomain($target)) { $target = gethostbyname($target); - if (!$target) return ['status' => false, 'error' => 'DNS resolve failed', 'usetime' => 0]; + if (!$target) return ['status' => false, 'errmsg' => 'DNS resolve failed', 'usetime' => 0]; } $starttime = getMillisecond(); $fp = @fsockopen($target, $port, $errCode, $errStr, $timeout); @@ -93,13 +93,13 @@ class CheckUtils public static function ping($target) { - if (!function_exists('exec')) return ['status' => false, 'error' => 'exec函数不可用', 'usetime' => 0]; + if (!function_exists('exec')) return ['status' => false, 'errmsg' => 'exec函数不可用', 'usetime' => 0]; if (!filter_var($target, FILTER_VALIDATE_IP) && checkDomain($target)) { $target = gethostbyname($target); - if (!$target) return ['status' => false, 'error' => 'DNS resolve failed', 'usetime' => 0]; + if (!$target) return ['status' => false, 'errmsg' => 'DNS resolve failed', 'usetime' => 0]; } if (!filter_var($target, FILTER_VALIDATE_IP)) { - return ['status' => false, 'error' => 'Invalid IP address', 'usetime' => 0]; + return ['status' => false, 'errmsg' => 'Invalid IP address', 'usetime' => 0]; } $timeout = 1; exec('ping -c 1 -w '.$timeout.' '.$target.'', $output, $return_var); diff --git a/app/view/system/proxyset.html b/app/view/system/proxyset.html index f063f13..6e363fd 100644 --- a/app/view/system/proxyset.html +++ b/app/view/system/proxyset.html @@ -1,5 +1,5 @@ {extend name="common/layout" /} -{block name="title"}容灾切换代理设置{/block} +{block name="title"}代理设置{/block} {block name="main"}