作者 刘锟

兼容自建站项目

... ... @@ -9,7 +9,9 @@
namespace App\Console\Commands\Domain;
use App\Models\Devops\ServerConfig;
use App\Models\Project\CountryCustom;
use App\Models\Project\Project;
use Illuminate\Console\Command;
use App\Models\Domain\DomainInfo as DomainInfoModel;
... ... @@ -83,6 +85,7 @@ class DomainInfo extends Command
public function startUpdateCert()
{
$domainModel = new DomainInfoModel();
$projectModel = new Project();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $domainModel->where('status', '!=', 2)->where(function ($query) use ($end_day) {
$query->whereNull('certificate_end_time')->orWhere('certificate_end_time', '<', $end_day);
... ... @@ -95,8 +98,9 @@ class DomainInfo extends Command
$ssl['from'] && $data['certificate_start_time'] = $ssl['from'];
$ssl['to'] && $data['certificate_end_time'] = $ssl['to'];
if ($v['type'] == 1 && $ssl['to'] < $end_day) {
//申请免费证书
$project_info = $projectModel->read(['id' => $v['project_id']], ['serve_id']);
if ($v['type'] == 1 && $ssl['to'] < $end_day && $project_info['serve_id'] != ServerConfig::SELF_SITE_ID) {
//非自建站项目,申请免费证书
$this->updatePrivate($v);
$ssl_new = $this->updateDomainSsl($v['domain']);
... ... @@ -116,6 +120,7 @@ class DomainInfo extends Command
public function startUpdateAmpCert()
{
$domainModel = new DomainInfoModel();
$projectModel = new Project();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $domainModel->where('status', '!=', 2)->where('amp_status', 1)->where(function ($query) use ($end_day) {
$query->whereNull('amp_certificate_end_time')->orWhere('amp_certificate_end_time', '<', $end_day);
... ... @@ -137,8 +142,9 @@ class DomainInfo extends Command
$ssl['from'] && $data['amp_certificate_start_time'] = $ssl['from'];
$ssl['to'] && $data['amp_certificate_end_time'] = $ssl['to'];
if ($v['amp_type'] == 1 && $ssl['to'] < $end_day) {
//申请免费证书
$project_info = $projectModel->read(['id' => $v['project_id']], ['serve_id']);
if ($v['amp_type'] == 1 && $ssl['to'] < $end_day && $project_info['serve_id'] != ServerConfig::SELF_SITE_ID) {
//非自建站项目,申请免费证书
$this->updateAmpPrivate($v['domain']);
$ssl_new = $this->updateDomainSsl($v['domain']);
... ... @@ -283,9 +289,9 @@ class DomainInfo extends Command
],
]);
$stream = stream_socket_client('ssl://' . $domain . ':443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if($stream){
if ($stream) {
$remote_cert = stream_context_get_params($stream)['options']['ssl']['peer_certificate'];
if($remote_cert){
if ($remote_cert) {
$valid_from = date('Y-m-d H:i:s', openssl_x509_parse($remote_cert)['validFrom_time_t']);
$valid_to = date('Y-m-d H:i:s', openssl_x509_parse($remote_cert)['validTo_time_t']);
}
... ...
... ... @@ -10,9 +10,13 @@
namespace App\Http\Controllers\Bside\BCom;
use App\Enums\Common\Code;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Models\Com\Notify;
use App\Models\Com\UpdateNotify;
use App\Models\Com\UpdateProgress;
use App\Models\Devops\ServerConfig;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Country as CountryModel;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
... ... @@ -37,15 +41,66 @@ class CNoticeController extends BaseController
*/
public function sendNotify(Request $request)
{
$url = $this->user['domain'].'api/update_page/';
$param = [
'project_id' => $this->user['project_id'],
'type' => intval($request->input('type', 1)),
'route' => intval($request->input('page', 1)),
'url' => $request->input('url', []),
'language'=> $request->input('language', []),
];
http_post($url, json_encode($param));
$project_id = $this->user['project_id'];
$type = intval($request->input('type', 1));
$route = intval($request->input('page', 1));
$url = $request->input('url', []);
$language = $request->input('language', []);
$is_sitemap = intval($request->input('is_sitemap', 0));
//获取项目所在服务器
$project_model = new Project();
$project_info = $project_model->read(['id'=>$project_id],['serve_id']);
if($project_info && $project_info['serve_id'] == ServerConfig::SELF_SITE_ID){
//自建站服务器:如果项目已经上线,不请求C端接口,数据直接入库
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['project_id'=>$this->user['project_id']],['domain']);
if($domain_info){
//判断是否已有更新进行中
$notify_model = new Notify();
$data = [
'project_id' => $project_id,
'type' => $type,
'route' => $route,
'server_id' => ServerConfig::SELF_SITE_ID,
'status' => ['<',Notify::STATUS_FINISH_PAGE]
];
$notify = $notify_model->read($data,['id']);
if(!$notify){
$domain = $domain_info['domain'];
if($type == Notify::TYPE_AMP){
$domain_array = parse_url($domain);
$host = $domain_array['host'] ?? $domain_array['path'];
$host_array = explode('.',$host);
if(count($host_array) <= 2){
array_unshift($host_array,'m');
}else{
$host_array[0] = 'm';
}
$domain = implode('.',$host_array);
}
$data['data'] = Arr::a2s(['domain'=>$domain,'url'=>$url,'language'=>$language]);
$data['status'] = $is_sitemap == 1 ? Notify::STATUS_FINISH_PAGE : Notify::STATUS_INIT;
$notify_model->add($data);
}
}
}else{
//其他服务器:请求对应C端接口
$c_url = $this->user['domain'].'api/update_page/';
$param = [
'project_id' => $this->user['project_id'],
'type' => $type,
'route' => $route,
'url' => $url,
'language'=> $language,
'is_sitemap' => $is_sitemap
];
http_post($c_url, json_encode($param));
}
$this->response('更新中请稍后, 更新完成将会发送站内信通知更新结果!');
}
... ...
... ... @@ -266,6 +266,9 @@ class DomainInfoLogic extends BaseLogic
if($project_info['serve_id'] == 9){
$this->fail('请切换服务器,生成站点不能使用测试服务器');
}
if($project_info['serve_id'] == ServerConfig::SELF_SITE_ID){
$this->fail('自建站服务器无法生成站点');
}
//域名是否都已经解析
if(!empty($info['domain']) && !$this->check_cname($info['domain'], $server_info)){
$this->fail('域名' . $info['domain'] . '未解析至目标服务器');
... ...
... ... @@ -7,6 +7,7 @@ use App\Enums\Common\Common;
use App\Exceptions\BsideGlobalException;
use App\Http\Logic\Logic;
use App\Models\Com\UpdateNotify;
use App\Models\Devops\ServerConfig;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteDelete;
use App\Models\Service\Service;
... ... @@ -174,6 +175,10 @@ class BaseLogic extends Logic
$data['project_id'] = $this->user['project_id'];
$str = http_build_query($data);
$url = $this->user['domain'].'api/delHtml/?'.$str;
if(isset($this->project['serve_id']) && ($this->project['serve_id'] == ServerConfig::SELF_SITE_ID)){
//自建站服务器直接返回
return $this->success();
}
if(isset($this->project['serve_id']) && ($this->project['serve_id'] != 1)){//TODO::当前项目通知不过 ,跳过自动更新
exec('curl -k "'.$url.'" > /dev/null 2>&1 &');
}else{
... ...
... ... @@ -15,10 +15,11 @@ class Notify extends Base
const STATUS_FINISH_PAGE = 2;
/**
* 类型 1:主站, 2:小语种
* 类型 1:主站, 2:小语种, 3:amp
*/
const TYPE_MASTER = 1;
const TYPE_MINOR = 2;
const TYPE_AMP = 3;
/**
* 路由
... ...
... ... @@ -34,6 +34,8 @@ class ServerConfig extends Base
const STATUS_ONE = 1;
const SELF_SITE_ID = 8;//自建站服务器ID
/**
* @remark :获取数据用户名解密
* @name :getUserAttribute
... ...