作者 lyh

gx

@@ -40,10 +40,12 @@ class DomainInfo extends Command @@ -40,10 +40,12 @@ class DomainInfo extends Command
40 $map = ['status'=>['!=',2]]; 40 $map = ['status'=>['!=',2]];
41 $list = $domainModel->list($map); 41 $list = $domainModel->list($map);
42 foreach ($list as $v){ 42 foreach ($list as $v){
  43 + if(empty($v['private_key']) || empty($v['private_cert'])){
43 //域名结束时间<2天时,重新生成 44 //域名结束时间<2天时,重新生成
44 - if($v['certificate_end_time'] > date('Y-m-d H:i:s',time()+ 24*3600)){ 45 + if(!empty($v['certificate_end_time']) && ($v['certificate_end_time'] > date('Y-m-d H:i:s',time() + 24*3600))){
45 $this->updatePrivate($v); 46 $this->updatePrivate($v);
46 } 47 }
  48 + }
47 $ssl = $this->updateDomainSsl($v['domain']); 49 $ssl = $this->updateDomainSsl($v['domain']);
48 $time = $this->updateDomain($v['domain']); 50 $time = $this->updateDomain($v['domain']);
49 $data = [ 51 $data = [
@@ -67,35 +69,76 @@ class DomainInfo extends Command @@ -67,35 +69,76 @@ class DomainInfo extends Command
67 */ 69 */
68 public function updatePrivate($param) 70 public function updatePrivate($param)
69 { 71 {
70 - $key = $param['private_key'];  
71 - $cert = $param['private_cert'];  
72 - if (empty($key) || empty($cert)){  
73 - $url = 'https://' . $v->domain. '/api/applySsl/';  
74 - }else{  
75 - $url = 'https://' . $v->domain. '/api/setSsl/';  
76 - }  
77 -  
78 - $extend_config = json_decode($v->extend_config, true);  
79 - $top_domain = Str::getTopDomain($v->domain);  
80 - if ((empty($extend_config) || empty($extend_config[0]['origin'])) && $v->id != 3) { 72 + $url = 'https://' . $param['domain']. '/api/applySsl/';
  73 + $extend_config = json_decode($param['extend_config'], true);
  74 + $top_domain = $this->getTopDomain($param['domain']);
  75 + if ((empty($extend_config) || empty($extend_config[0]['origin'])) && $param['id'] != 3) {
81 $extend_config = [ 76 $extend_config = [
82 - ['origin' => $top_domain, 'target' => $v->domain] 77 + ['origin' => $top_domain, 'target' => $param['domain']]
83 ]; 78 ];
84 } 79 }
85 $param = [ 80 $param = [
86 - 'project_id' => $k, 81 + 'project_id' => $param['project_id'],
87 'type' => 1, 82 'type' => 1,
88 'route' => 1, 83 'route' => 1,
89 - "domain" =>$v->domain, 84 + "domain" =>$param['domain'],
90 "rewrite"=> $extend_config, 85 "rewrite"=> $extend_config,
91 'other_domain' => [$top_domain, '*.' . $top_domain], 86 'other_domain' => [$top_domain, '*.' . $top_domain],
92 - 'private_key' => $key,  
93 - 'cert' => $cert 87 + 'private_key' => '',
  88 + 'cert' => ''
94 ]; 89 ];
95 - $result = app(ToolRepository::class)->curlRequest($url, $param);  
96 - Log::info('domain id: ' . $v->id . ', domain: ' . $v->domain . ', result: ' . var_export($result, true));  
97 - var_dump($result); 90 + $result = $this->curlRequest($url, $param);
  91 + Log::info('domain id: ' . $param['id'] . ', domain: ' . $param['domain'] . ', result: ' . var_export($result, true));
  92 + }
  93 +
  94 + public static function getTopDomain ($url) {
  95 + $url = strtolower($url); //首先转成小写
  96 + $url = mb_ereg_replace('^( | )+', '', trim($url));
  97 + $url = mb_ereg_replace('( | )+$', '', $url);
  98 + if (!preg_match('/^(http:\/\/|https)/', $url)) {
  99 + $url = "https://".$url;
  100 + }
  101 + $hosts = parse_url($url);
  102 + $host = $hosts['host'] ?? '';
  103 + //查看是几级域名
  104 + $data = explode('.', $host);
  105 + $n = count($data);
  106 + if ($n < 2) {
  107 + return $host;
  108 + }
  109 + //判断是否是双后缀
  110 + $preg = '/[\w].+\.(com|net|org|gov|edu|co|ne)\.[\w]/';
  111 + if (($n > 2) && preg_match($preg, $host)) {
  112 + //双后缀取后3位
  113 + $host = $data[$n - 3].'.'.$data[$n - 2].'.'.$data[$n - 1];
  114 + } else {
  115 + //非双后缀取后两位
  116 + $host = $data[$n - 2].'.'.$data[$n - 1];
  117 + }
  118 + return $host;
  119 + }
98 120
  121 + public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 60)
  122 + {
  123 + $ch = curl_init();
  124 + curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
  125 + curl_setopt($ch, CURLOPT_URL, $url);
  126 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  127 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  128 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  129 + if ($data)
  130 + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  131 + curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
  132 + 'Expect:',
  133 + 'Content-type: application/json',
  134 + 'Accept: application/json',
  135 + ], $header)
  136 + );
  137 + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  138 + $response = curl_exec($ch);
  139 + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  140 + curl_close($ch);
  141 + return [$code, $response];
99 } 142 }
100 143
101 /** 144 /**