作者 zhl

Merge branch 'develop' of 47.244.231.31:zhl/globalso-v6 into develop

... ... @@ -3,18 +3,9 @@
namespace App\Http\Controllers\Aside\Devops;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Devops\ServerInformationLogic;
use App\Http\Requests\Aside\Devops\ServerInformationRequest;
use App\Models\Devops\ServerInformation;
use App\Models\Devops\ServerInformationLog;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class ServerInformationLogController extends BaseController
{
... ...
... ... @@ -25,8 +25,6 @@ class ServerInformationLogic extends BaseLogic
{
parent::__construct();
$this->model = new ServerInformation();
$this->param = $this->requestAll;
}
... ... @@ -41,23 +39,23 @@ class ServerInformationLogic extends BaseLogic
{
$request = $this->param ?? [];
$service = new ServerInformation();
$this->extracted( $request, $service );
if ( $this->checkIp( $service->ip ) ) {
return $this->fail( '服务器信息添加失败,ip已存在' );
$this->extracted($request, $service);
if ($this->checkIp($request['ip'])) {
return $this->fail('服务器信息添加失败,ip已存在');
}
DB::beginTransaction();
if ( $service->save() ) {
$original = $service->getOriginal();
$original['type'] = $request['type'];
if ($service->save()) {
$original = $service->getOriginal();
$original['type'] = $request['type'];
$original['belong_to'] = $request['belong_to'];
// 添加日志
$this->server_action_log( ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id );
$this->server_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id);
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail( '服务器信息添加失败' );
return $this->fail('服务器信息添加失败');
}
... ... @@ -69,53 +67,53 @@ class ServerInformationLogic extends BaseLogic
*/
public function update()
{
$service = $this->getService();
$fields_array = $service->FieldsArray();
$request = $this->param ?? [];
$original = $service->getOriginal();
$original['type'] = $service->ServiceStr( $original['type'] );
$original['belong_to'] = $service->BelongToStr( $original['belong_to'] );
$this->extracted( $request, $service, $original );
$service = $this->getService();
$fields_array = $service->FieldsArray();
$request = $this->param ?? [];
$original = $service->getOriginal();
$original['type'] = $service->ServiceStr($original['type']);
$original['belong_to'] = $service->BelongToStr($original['belong_to']);
$this->extracted($request, $service, $original);
// 检查ip是否存在
if ( $service->ip != $request['ip'] ) {
if ( $this->checkIp( $request['ip'] ) ) {
$this->fail( '服务器信息修改失败,ip已存在', Code::USER_ERROR );
if ($service->ip != $request['ip']) {
if ($this->checkIp($request['ip'])) {
$this->fail('服务器信息修改失败,ip已存在', Code::USER_ERROR);
}
}
DB::beginTransaction();
if ( $service->save() ) {
if ($service->save()) {
$revised = $service->getAttributes();
$diff = array_diff_assoc( $original, $revised );
unset( $diff['created_at'] );
unset( $diff['updated_at'] );
unset( $diff['deleted'] );
$diff = array_diff_assoc($original, $revised);
unset($diff['created_at']);
unset($diff['updated_at']);
unset($diff['deleted']);
$remarks = '';
if ( $diff ) {
if ($diff) {
$remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,修改内容为:';
foreach ( $diff as $key => $value ) {
foreach ($diff as $key => $value) {
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
}
} else {
$remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,无修改';
}
// 添加日志
$this->server_action_log( ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks );
$this->server_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail( '服务器信息修改失败' );
return $this->fail('服务器信息修改失败');
}
public function getService( int $deleted = ServerInformation::DELETED_NORMAL )
public function getService(int $deleted = ServerInformation::DELETED_NORMAL)
{
$id = $this->param['id'] ?? 0;
if ( !$id ) {
return $this->fail( 'ID不能为空' );
if (!$id) {
return $this->fail('ID不能为空');
}
$data = ServerInformation::query()->where( 'deleted', $deleted )->find( $id );
if ( !$data ) {
return $this->fail( '数据不存在!' );
$data = ServerInformation::query()->where('deleted', $deleted)->find($id);
if (!$data) {
return $this->fail('数据不存在!');
}
return $data;
}
... ... @@ -125,10 +123,10 @@ class ServerInformationLogic extends BaseLogic
* @param $ip
* @return bool
*/
public function checkIp( $ip )
public function checkIp($ip)
{
$usIp = ServerInformation::query()->where( 'ip', $ip )->first();
if ( $usIp ) {
$usIp = ServerInformation::query()->where('ip', $ip)->first();
if ($usIp) {
return true;
} else {
return false;
... ... @@ -142,15 +140,15 @@ class ServerInformationLogic extends BaseLogic
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function serverInfo( int $deleted = ServerInformation::DELETED_NORMAL )
public function serverInfo(int $deleted = ServerInformation::DELETED_NORMAL)
{
$id = $this->param['id'] ?? 0;
if ( !$id ) {
return $this->fail( 'ID不能为空' );
if (!$id) {
return $this->fail('ID不能为空');
}
$data = ServerInformation::query()->where( 'deleted', $deleted )->find( $id, [ 'type', 'ip', 'title', 'belong_to', 'ports', 'created_at', 'updated_at' ] );
if ( !$data ) {
return $this->fail( '数据不存在!' );
$data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'ports', 'created_at', 'updated_at']);
if (!$data) {
return $this->fail('数据不存在!');
}
return $data;
}
... ... @@ -161,10 +159,10 @@ class ServerInformationLogic extends BaseLogic
* @param array $original 原始数据
* @param array $revised 修改后数据
*/
public function server_action_log( int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '' )
public function server_action_log(int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
{
$log = new ServerInformationLog();
$this->log( $log, $action, $original, $revised, $remarks );
$this->log($log, $action, $original, $revised, $remarks);
}
/**
... ... @@ -172,14 +170,14 @@ class ServerInformationLogic extends BaseLogic
* @param $service
* @return void
*/
public function extracted( array $request, $service, array $original = [] )
public function extracted(array $request, $service, array $original = [])
{
$service->type = trim( $request['type'] ) ?? $original['type']; // 服务器类型
$service->ip = trim( $request['ip'] ) ?? $original['ip']; // 服务器ip
$service->title = trim( $request['title'] ) ?? $original['title']; // 服务器标题
$service->belong_to = trim( $request['belong_to'] ) ?? $original['belong_to']; // 服务器归属
$service->sshpass = trim( $request['sshpass'] ) ?? $original['sshpass']; // ssh密码
$service->ports = trim( $request['ports'] ) ?? $original['ports']; // ssh端口
$service->type = trim($request['type']) ?? $original['type']; // 服务器类型
$service->ip = trim($request['ip']) ?? $original['ip']; // 服务器ip
$service->title = trim($request['title']) ?? $original['title']; // 服务器标题
$service->belong_to = trim($request['belong_to']) ?? $original['belong_to']; // 服务器归属
$service->sshpass = trim($request['sshpass']) ?? $original['sshpass']; // ssh密码
$service->ports = trim($request['ports']) ?? $original['ports']; // ssh端口
}
/**
... ... @@ -188,27 +186,27 @@ class ServerInformationLogic extends BaseLogic
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function get_batch_update( $action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL )
public function get_batch_update($action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL)
{
$ids = $this->getIds();
$data = ServerInformation::query()->whereIn( 'id', $ids )->where( 'deleted', $deleted )->get();
$restore_ids = $data->pluck( 'id' )->toArray();
$actionArr = ServerInformationLog::actionArr();
$actionStr = $actionArr[$action];
if ( empty( $restore_ids ) ) {
$this->fail( $actionStr . '服务器信息不存在!', Code::USER_ERROR );
$ids = $this->getIds();
$data = ServerInformation::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
$restore_ids = $data->pluck('id')->toArray();
$actionArr = ServerInformationLog::actionArr();
$actionStr = $actionArr[$action];
if (empty($restore_ids)) {
$this->fail($actionStr . '服务器信息不存在!', Code::USER_ERROR);
}
$original = $data->toArray();
DB::beginTransaction();
try {
$update = $deleted == ServerInformation::DELETED_NORMAL ? ServerInformation::DELETED_DELETE : ServerInformation::DELETED_NORMAL;
ServerInformation::query()->whereIn( 'id', $restore_ids )->update( [ 'deleted' => $update ] );
$this->server_action_log( $action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode( ', ', $restore_ids ) );
ServerInformation::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
$this->server_action_log($action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode(', ', $restore_ids));
DB::commit();
return $this->success();
} catch ( \Exception $e ) {
} catch (\Exception $e) {
DB::rollBack();
return $this->fail( '服务器信息' . $actionStr . '失败', Code::USER_ERROR );
return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR);
}
}
... ... @@ -221,16 +219,16 @@ class ServerInformationLogic extends BaseLogic
public function getIds()
{
$id = $this->param['id'] ?? 0;
if ( !$id ) {
return $this->fail( '参数错误' );
if (!$id) {
return $this->fail('参数错误');
}
$ids = [];
if ( !is_array( $id ) ) {
$ids = explode( ',', $id );
if (!is_array($id)) {
$ids = explode(',', $id);
}
$ids = array_filter( $ids, 'intval' );
if ( empty( $ids ) ) {
return $this->fail( '参数错误' );
$ids = array_filter($ids, 'intval');
if (empty($ids)) {
return $this->fail('参数错误');
}
return $ids;
}
... ...
... ... @@ -49,9 +49,9 @@ class DomainInfoLogic extends BaseLogic
$this->extracted($request, $domain);
DB::beginTransaction();
if ($domain->save()) {
$original = $domain->getOriginal();
$original = $domain->getOriginal();
$original['belong_to'] = $request['belong_to'];
$original['status'] = $request['status'];
$original['status'] = $request['status'];
// 添加日志
$this->domain_action_log(DomainInfoLog::ACTION_ADD, $original, $original, '添加域名信息成功 - ID : ' . $domain->id);
DB::commit();
... ... @@ -70,11 +70,11 @@ class DomainInfoLogic extends BaseLogic
*/
public function update()
{
$domain = $this->getDomain();
$original = $domain->getOriginal();
$domain = $this->getDomain();
$original = $domain->getOriginal();
$original['belong_to'] = $domain->BelongToStr($original['belong_to']);
$original['status'] = $domain->StatusToStr($original['status']);
$request = $this->param;
$original['status'] = $domain->StatusToStr($original['status']);
$request = $this->param;
$this->extracted($request, $domain, $original);
// 检查ip是否存在
if ($domain->domain != $request['domain']) {
... ... @@ -85,8 +85,8 @@ class DomainInfoLogic extends BaseLogic
DB::beginTransaction();
if ($domain->save()) {
$fields_array = $domain->FieldsArray();
$revised = $domain->getAttributes();
$diff = array_diff_assoc($original, $revised);
$revised = $domain->getAttributes();
$diff = array_diff_assoc($original, $revised);
unset($diff['created_at']);
unset($diff['updated_at']);
unset($diff['deleted']);
... ... @@ -182,18 +182,15 @@ class DomainInfoLogic extends BaseLogic
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function extracted( array $request, $domain, array $original = [])
public function extracted(array $request, $domain, array $original = [])
{
$domain->domain = trim($request['domain']) ?? $original['domain'];
if (!$this->validateDomain($domain->domain)) {
$this->fail('域名格式错误');
}
$domain->belong_to = trim($request['belong_to']) ?? $original['belong_to']; // 域名归属 1 - 公司 2 - 客户
$domain->status = trim($request['status']) ?? $original['status']; // 域名状态 0 - 正常 1 - 关闭
$domain->domain_start_time = trim($request['domain_start_time']) ?? $original['domain_start_time']; // 域名开始时间
$domain->domain_end_time = trim($request['domain_end_time']) ?? $original['domain_end_time'];
$domain->domain = trim($request['domain']) ?? $original['domain'];
$domain->belong_to = trim($request['belong_to']) ?? $original['belong_to']; // 域名归属 1 - 公司 2 - 客户
$domain->status = trim($request['status']) ?? $original['status']; // 域名状态 0 - 正常 1 - 关闭
$domain->domain_start_time = trim($request['domain_start_time']) ?? $original['domain_start_time']; // 域名开始时间
$domain->domain_end_time = trim($request['domain_end_time']) ?? $original['domain_end_time'];
$domain->certificate_start_time = trim($request['certificate_start_time']) ?? $original['certificate_start_time']; // 证书开始时间
$domain->certificate_end_time = trim($request['certificate_end_time']) ?? $original['certificate_end_time']; // 证书结束时间
$domain->certificate_end_time = trim($request['certificate_end_time']) ?? $original['certificate_end_time']; // 证书结束时间
}
/**
... ... @@ -204,11 +201,11 @@ class DomainInfoLogic extends BaseLogic
*/
public function get_batch_update($action = DomainInfoLog::ACTION_DELETE, $deleted = DomainInfo::DELETED_NORMAL)
{
$ids = (new ServerInformationLogic())->getIds();
$data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
$ids = (new ServerInformationLogic())->getIds();
$data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
$restore_ids = $data->pluck('id')->toArray();
$actionArr = DomainInfoLog::actionArr();
$actionStr = $actionArr[$action];
$actionArr = DomainInfoLog::actionArr();
$actionStr = $actionArr[$action];
if (empty($restore_ids)) {
$this->fail($actionStr . '域名信息不存在!', Code::USER_ERROR);
}
... ... @@ -232,18 +229,18 @@ class DomainInfoLogic extends BaseLogic
*/
public function getDomainCertificateTime($domain)
{
$domain = trim($domain);
$data = [];
$domain = trim($domain);
$data = [];
$context = stream_context_create(['ssl' => ['capture_peer_cert' => true]]); // Notice: only 7.0.7+ supports this
$stream = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
$stream = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if ($stream) {
$params = stream_context_get_params($stream);
$params = stream_context_get_params($stream);
$peerCertificate = openssl_x509_parse($params['options']['ssl']['peer_certificate']);
if ($peerCertificate) {
$validFrom = date_create_from_format('U', $peerCertificate['validFrom_time_t']); // 有效期开始时间
$validTo = date_create_from_format('U', $peerCertificate['validTo_time_t']); // 有效期结束时间
$validFrom = date_create_from_format('U', $peerCertificate['validFrom_time_t']); // 有效期开始时间
$validTo = date_create_from_format('U', $peerCertificate['validTo_time_t']); // 有效期结束时间
$data['validFrom'] = $validFrom->format('Y-m-d H:i:s');
$data['validTo'] = $validTo->format('Y-m-d H:i:s');
$data['validTo'] = $validTo->format('Y-m-d H:i:s');
}
}
return $data;
... ... @@ -256,9 +253,9 @@ class DomainInfoLogic extends BaseLogic
public function getAllDomain()
{
return DomainInfo::query()->where('status', 1)
->where('deleted', DomainInfo::DELETED_NORMAL)
->orderBy('updated_at', 'desc')
->get();
->where('deleted', DomainInfo::DELETED_NORMAL)
->orderBy('updated_at', 'desc')
->get();
}
/**
... ... @@ -268,48 +265,18 @@ class DomainInfoLogic extends BaseLogic
public function getDomainTime($domain)
{
$conJson = file_get_contents("http://openai.waimaoq.com/v1/whois_api?domain={$domain}");
$conArr = json_decode($conJson, true);
$data = [];
$conArr = json_decode($conJson, true);
$data = [];
if ($conArr['code'] == 200) {
$con = $conArr['text'];
$data['domain'] = $domain;
$con = $conArr['text'];
$data['domain'] = $domain;
$data['validFrom'] = $con['creation_date'];
$data['validTo'] = $con['expiration_date'];
$data['validTo'] = $con['expiration_date'];
}
return $data;
}
/**
* 验证给定的值是否是有效的域名。
*
* @param mixed $value
* @return bool
*/
public function validateDomain($value)
{
// 从域中删除任何空间或路径。
$domain = preg_replace('/\s|\/.*$/', '', $value);
// 验证域是否至少包含一个句点。
if (strpos($domain, '.') === false) {
return false;
}
// 验证域是否以句点开始或结束。
if (strpos($domain, '.') === 0 || strrpos($domain, '.') === strlen($domain) - 1) {
return false;
}
// 验证域是否不包含无效字符。
if (!preg_match('/^[a-zA-Z0-9\-\.]+$/', $domain)) {
return false;
}
// 验证域是否具有有效的顶级域。
$tld = substr($domain, strrpos($domain, '.') + 1);
if (!in_array($tld, ['com', 'net', 'org'])) { // 如有必要,添加更多TLD。
return false;
}
return true;
}
/**
* 根据域名更新证书到期时间
* @param $id
* @param $updata
... ...
... ... @@ -16,9 +16,16 @@ class EnableCrossRequestMiddleware
public function handle($request, Closure $next)
{
$response = $next($request);
$response->header('Access-Control-Allow-Origin', '*');
$http_origin = "*";
if(isset($_SERVER['HTTP_ORIGIN'])){
$http_origin = $_SERVER['HTTP_ORIGIN'];
}
$response->header('Access-Control-Allow-Origin', $http_origin);
$response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Authorization');
if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') {
exit;
}
return $response;
// 指定允许其他域名访问
// $http_origin = "*";
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Http\Requests\Aside\Domain;
use App\Models\Aside\Domain\DomainInfo;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class DomainInfoRequest extends FormRequest
... ... @@ -25,11 +26,14 @@ class DomainInfoRequest extends FormRequest
*/
public function rules()
{
$domain = new DomainInfo();
Validator::extend('validate_domain', function ($attribute, $value, $parameters, $validator) {
return $this->validateDomain($value);
});
$domain = new DomainInfo();
$status_tot_keys = array_keys($domain->StatusToArray());
$belong_to_keys = array_keys($domain->BelongToArray());
$belong_to_keys = array_keys($domain->BelongToArray());
return [
'domain' => 'required|max:200',
'domain' => 'required|max:200|validate_domain',
'belong_to' => [
'required',
Rule::in($belong_to_keys)
... ... @@ -41,15 +45,46 @@ class DomainInfoRequest extends FormRequest
];
}
/**
* 验证给定的值是否是有效的域名。
*
* @param mixed $value
* @return bool
*/
public function validateDomain($value)
{
// 从域中删除任何空间或路径。
$domain = preg_replace('/\s|\/.*$/', '', $value);
// 验证域是否至少包含一个句点。
if (strpos($domain, '.') === false) {
return false;
}
// 验证域是否以句点开始或结束。
if (strpos($domain, '.') === 0 || strrpos($domain, '.') === strlen($domain) - 1) {
return false;
}
// 验证域是否不包含无效字符。
if (!preg_match('/^[a-zA-Z0-9\-\.]+$/', $domain)) {
return false;
}
// 验证域是否具有有效的顶级域。
$tld = substr($domain, strrpos($domain, '.') + 1);
if (!in_array($tld, ['com', 'net', 'org'])) { // 如有必要,添加更多TLD。
return false;
}
return true;
}
public function messages()
{
return [
'domain.required' => '域名不能为空',
'domain.max' => '域名长度不能超过200个字符',
'belong_to.required' => '域名所属不能为空',
'belong_to.in' => '域名所属参数错误',
'status.required' => '域名状态不能为空',
'status.in' => '域名状态参数错误',
'domain.required' => '域名不能为空',
'domain.max' => '域名长度不能超过200个字符',
'domain.validate_domain' => '域名格式不正确',
'belong_to.required' => '域名所属不能为空',
'belong_to.in' => '域名所属参数错误',
'status.required' => '域名状态不能为空',
'status.in' => '域名状态参数错误',
];
}
... ...
... ... @@ -2,9 +2,9 @@
namespace App\Models\Devops;
use Illuminate\Database\Eloquent\Model;
use App\Models\Base;
class ServerInformation extends Model
class ServerInformation extends Base
{
protected $table = 'gl_server_information';
... ...
... ... @@ -2,9 +2,9 @@
namespace App\Models\Devops;
use Illuminate\Database\Eloquent\Model;
use App\Models\Base;
class ServerInformationLog extends Model
class ServerInformationLog extends Base
{
protected $table = 'gl_server_information_log';
... ...
... ... @@ -68,16 +68,15 @@ class User extends Base
//TODO::验证验证码是否正确
$info = $this->read(['mobile'=>$param['mobile']],['*']);
}
if(empty($info)){
if($info === false){
return false;
}
//当前用户角色是否被禁用
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>0]);
if(empty($role_info)){
if($role_info === false){
return false;
}
//验证码登录
if(isset($info['token']) && !empty($info['token'])){
//清除上一次用户缓存
Cache::pull($info['token']);
... ...