NodeController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Helpers;
  4. use App\Components\NetworkDetection;
  5. use App\Models\Country;
  6. use App\Models\Label;
  7. use App\Models\Level;
  8. use App\Models\NodeAuth;
  9. use App\Models\NodeCertificate;
  10. use App\Models\NodeRule;
  11. use App\Models\RuleGroup;
  12. use App\Models\SsNode;
  13. use App\Models\SsNodeInfo;
  14. use App\Models\SsNodeLabel;
  15. use App\Models\SsNodeOnlineLog;
  16. use App\Models\SsNodePing;
  17. use App\Models\SsNodeTrafficDaily;
  18. use App\Models\SsNodeTrafficHourly;
  19. use App\Models\UserTrafficDaily;
  20. use App\Models\UserTrafficHourly;
  21. use App\Models\UserTrafficLog;
  22. use DB;
  23. use Exception;
  24. use Illuminate\Http\JsonResponse;
  25. use Illuminate\Http\Request;
  26. use Log;
  27. use Redirect;
  28. use Response;
  29. use Session;
  30. use Validator;
  31. class NodeController extends Controller {
  32. // 节点列表
  33. public function nodeList(Request $request): \Illuminate\Http\Response {
  34. $status = $request->input('status');
  35. $query = SsNode::query();
  36. if(isset($status)){
  37. $query->whereStatus($status);
  38. }
  39. $nodeList = $query->orderByDesc('status')->orderBy('id')->paginate(15)->appends($request->except('page'));
  40. foreach($nodeList as $node){
  41. // 在线人数
  42. $online_log = SsNodeOnlineLog::query()
  43. ->whereNodeId($node->id)
  44. ->where('log_time', '>=', strtotime("-5 minutes"))
  45. ->orderByDesc('id')
  46. ->first();
  47. $node->online_users = empty($online_log)? 0 : $online_log->online_user;
  48. // 已产生流量
  49. $totalTraffic = SsNodeTrafficDaily::query()->whereNodeId($node->id)->sum('total');
  50. $node->transfer = flowAutoShow($totalTraffic);
  51. // 负载(10分钟以内)
  52. $node_info = SsNodeInfo::query()
  53. ->whereNodeId($node->id)
  54. ->where('log_time', '>=', strtotime("-10 minutes"))
  55. ->orderByDesc('id')
  56. ->first();
  57. $node->isOnline = empty($node_info) || empty($node_info->load)? 0 : 1;
  58. $node->load = $node->isOnline? $node_info->load : '离线';
  59. $node->uptime = empty($node_info)? 0 : seconds2time($node_info->uptime);
  60. }
  61. $view['nodeList'] = $nodeList;
  62. return Response::view('admin.node.nodeList', $view);
  63. }
  64. public function checkNode(Request $request): JsonResponse {
  65. $id = $request->input('id');
  66. $node = SsNode::query()->whereId($id)->first();
  67. // 使用DDNS的node先获取ipv4地址
  68. if($node->is_ddns){
  69. $ip = gethostbyname($node->server);
  70. if(strcmp($ip, $node->server) != 0){
  71. $node->ip = $ip;
  72. }else{
  73. return Response::json(['status' => 'fail', 'title' => 'IP获取错误', 'message' => $node->name.'IP获取失败']);
  74. }
  75. }
  76. $data[0] = NetworkDetection::networkCheck($node->ip, true); //ICMP
  77. $data[1] = NetworkDetection::networkCheck($node->ip, false, $node->single? $node->port : null); //TCP
  78. return Response::json(['status' => 'success', 'title' => '['.$node->name.']阻断信息', 'message' => $data]);
  79. }
  80. // 添加节点
  81. public function addNode(Request $request) {
  82. if($request->isMethod('POST')){
  83. $validator = $this->nodeValidation($request);
  84. if($validator){
  85. return $validator;
  86. }
  87. // TODO:判断是否已存在绑定了相同域名的节点,提示是否要强制替换,或者不提示之前强制将其他节点的绑定域名置为空,然后发起域名绑定请求,或者请求进入队列
  88. try{
  89. DB::beginTransaction();
  90. $node = new SsNode();
  91. $node->type = $request->input('type');
  92. $node->name = $request->input('name');
  93. $node->country_code = $request->input('country_code');
  94. $node->server = $request->input('server');
  95. $node->ip = $request->input('ip');
  96. $node->ipv6 = $request->input('ipv6');
  97. $node->relay_server = $request->input('relay_server');
  98. $node->relay_port = $request->input('relay_port');
  99. $node->level = $request->input('level');
  100. $node->speed_limit = intval($request->input('speed_limit')) * Mbps;
  101. $node->client_limit = $request->input('client_limit');
  102. $node->description = $request->input('description');
  103. $node->method = $request->input('method');
  104. $node->protocol = $request->input('protocol');
  105. $node->protocol_param = $request->input('protocol_param');
  106. $node->obfs = $request->input('obfs');
  107. $node->obfs_param = $request->input('obfs_param');
  108. $node->traffic_rate = $request->input('traffic_rate');
  109. $node->is_subscribe = intval($request->input('is_subscribe'));
  110. $node->is_ddns = intval($request->input('is_ddns'));
  111. $node->is_relay = intval($request->input('is_relay'));
  112. $node->is_udp = intval($request->input('is_udp'));
  113. $node->push_port = $request->input('push_port');
  114. $node->detection_type = $request->input('detection_type');
  115. $node->compatible = intval($request->input('compatible'));
  116. $node->single = intval($request->input('single'));
  117. $node->port = $request->input('port');
  118. $node->passwd = $request->input('passwd');
  119. $node->sort = $request->input('sort');
  120. $node->status = intval($request->input('status'));
  121. $node->v2_alter_id = $request->input('v2_alter_id');
  122. $node->v2_port = $request->input('v2_port');
  123. $node->v2_method = $request->input('v2_method');
  124. $node->v2_net = $request->input('v2_net');
  125. $node->v2_type = $request->input('v2_type');
  126. $node->v2_host = $request->input('v2_host')?: '';
  127. $node->v2_path = $request->input('v2_path');
  128. $node->v2_tls = intval($request->input('v2_tls'));
  129. $node->tls_provider = $request->input('tls_provider');
  130. $node->save();
  131. DB::commit();
  132. // 生成节点标签
  133. $this->makeLabels($node->id, $request->input('labels'));
  134. return Response::json(['status' => 'success', 'message' => '添加成功']);
  135. }catch(Exception $e){
  136. DB::rollBack();
  137. Log::error('添加节点信息异常:'.$e->getMessage());
  138. return Response::json(['status' => 'fail', 'message' => '添加失败:'.$e->getMessage()]);
  139. }
  140. }else{
  141. $view['method_list'] = Helpers::methodList();
  142. $view['protocol_list'] = Helpers::protocolList();
  143. $view['obfs_list'] = Helpers::obfsList();
  144. $view['country_list'] = Country::query()->orderBy('code')->get();
  145. $view['level_list'] = Level::query()->orderBy('level')->get();
  146. $view['label_list'] = Label::query()->orderByDesc('sort')->orderBy('id')->get();
  147. $view['dv_list'] = NodeCertificate::query()->orderBy('id')->get();
  148. return Response::view('admin.node.nodeInfo', $view);
  149. }
  150. }
  151. // 节点信息验证
  152. private function nodeValidation(Request $request) {
  153. if($request->input('server')){
  154. $domain = $request->input('server');
  155. $domain = explode('.', $domain);
  156. $domainSuffix = end($domain); // 取得域名后缀
  157. if(!in_array($domainSuffix, config('domains'), true)){
  158. return Response::json(['status' => 'fail', 'message' => '绑定域名不合法']);
  159. }
  160. }
  161. $validator = Validator::make($request->all(), [
  162. 'type' => 'required|between:1,3',
  163. 'name' => 'required',
  164. 'country_code' => 'required',
  165. 'server' => 'required_if:is_ddns,1',
  166. 'push_port' => 'numeric|between:0,65535',
  167. 'traffic_rate' => 'required|numeric|min:0',
  168. 'level' => 'required|numeric|between:0,255',
  169. 'speed_limit' => 'required|numeric|min:0',
  170. 'client_limit' => 'required|numeric|min:0',
  171. 'port' => 'nullable|numeric|between:0,65535',
  172. 'ip' => 'ipv4',
  173. 'ipv6' => 'nullable|ipv6',
  174. 'relay_server' => 'required_if:is_relay,1',
  175. 'relay_port' => 'required_if:is_relay,1|numeric|between:0,65535',
  176. 'method' => 'required_if:type,1',
  177. 'protocol' => 'required_if:type,1',
  178. 'obfs' => 'required_if:type,1',
  179. 'is_subscribe' => 'boolean',
  180. 'is_ddns' => 'boolean',
  181. 'is_relay' => 'boolean',
  182. 'is_udp' => 'boolean',
  183. 'detection_type' => 'between:0,3',
  184. 'compatible' => 'boolean',
  185. 'single' => 'boolean',
  186. 'sort' => 'required|numeric|between:0,255',
  187. 'status' => 'boolean',
  188. 'v2_alter_id' => 'required_if:type,2|numeric|between:0,65535',
  189. 'v2_port' => 'required_if:type,2|numeric|between:0,65535',
  190. 'v2_method' => 'required_if:type,2',
  191. 'v2_net' => 'required_if:type,2',
  192. 'v2_type' => 'required_if:type,2',
  193. 'v2_tls' => 'boolean'
  194. ], [
  195. 'server.required_unless' => '开启DDNS, 域名不能为空',
  196. ]);
  197. if($validator->fails()){
  198. return Response::json(['status' => 'fail', 'message' => $validator->errors()->all()]);
  199. }
  200. return false;
  201. }
  202. // 生成节点标签
  203. private function makeLabels($nodeId, $labels): void {
  204. // 先删除所有该节点的标签
  205. SsNodeLabel::query()->whereNodeId($nodeId)->delete();
  206. if(!empty($labels) && is_array($labels)){
  207. foreach($labels as $label){
  208. $nodeLabel = new SsNodeLabel();
  209. $nodeLabel->node_id = $nodeId;
  210. $nodeLabel->label_id = $label;
  211. $nodeLabel->save();
  212. }
  213. }
  214. }
  215. // 编辑节点
  216. public function editNode(Request $request) {
  217. $id = $request->input('id');
  218. if($request->isMethod('POST')){
  219. $validator = $this->nodeValidation($request);
  220. if($validator){
  221. return $validator;
  222. }
  223. try{
  224. DB::beginTransaction();
  225. $data = [
  226. 'type' => $request->input('type'),
  227. 'name' => $request->input('name'),
  228. 'country_code' => $request->input('country_code'),
  229. 'server' => $request->input('server'),
  230. 'ip' => $request->input('ip'),
  231. 'ipv6' => $request->input('ipv6'),
  232. 'relay_server' => $request->input('relay_server'),
  233. 'relay_port' => $request->input('relay_port'),
  234. 'level' => $request->input('level'),
  235. 'speed_limit' => intval($request->input('speed_limit')) * Mbps,
  236. 'client_limit' => $request->input('client_limit'),
  237. 'description' => $request->input('description'),
  238. 'method' => $request->input('method'),
  239. 'protocol' => $request->input('protocol'),
  240. 'protocol_param' => $request->input('protocol_param'),
  241. 'obfs' => $request->input('obfs'),
  242. 'obfs_param' => $request->input('obfs_param'),
  243. 'traffic_rate' => $request->input('traffic_rate'),
  244. 'is_subscribe' => intval($request->input('is_subscribe')),
  245. 'is_ddns' => intval($request->input('is_ddns')),
  246. 'is_relay' => intval($request->input('is_relay')),
  247. 'is_udp' => intval($request->input('is_udp')),
  248. 'push_port' => $request->input('push_port'),
  249. 'detection_type' => $request->input('detection_type'),
  250. 'compatible' => intval($request->input('compatible')),
  251. 'single' => intval($request->input('single')),
  252. 'port' => $request->input('port'),
  253. 'passwd' => $request->input('passwd'),
  254. 'sort' => $request->input('sort'),
  255. 'status' => intval($request->input('status')),
  256. 'v2_alter_id' => $request->input('v2_alter_id'),
  257. 'v2_port' => $request->input('v2_port'),
  258. 'v2_method' => $request->input('v2_method'),
  259. 'v2_net' => $request->input('v2_net'),
  260. 'v2_type' => $request->input('v2_type'),
  261. 'v2_host' => $request->input('v2_host')?: '',
  262. 'v2_path' => $request->input('v2_path'),
  263. 'v2_tls' => intval($request->input('v2_tls')),
  264. 'tls_provider' => $request->input('tls_provider')
  265. ];
  266. // 生成节点标签
  267. $this->makeLabels($id, $request->input('labels'));
  268. SsNode::query()->whereId($id)->update($data);
  269. // TODO:更新节点绑定的域名DNS(将节点IP更新到域名DNS 的A记录)
  270. DB::commit();
  271. return Response::json(['status' => 'success', 'message' => '编辑成功']);
  272. }catch(Exception $e){
  273. DB::rollBack();
  274. Log::error('编辑节点信息异常:'.$e->getMessage());
  275. return Response::json(['status' => 'fail', 'message' => '编辑失败:'.$e->getMessage()]);
  276. }
  277. }else{
  278. $node = SsNode::query()->with(['label'])->whereId($id)->first();
  279. if($node){
  280. $node->labels = $node->label->pluck('label_id');
  281. }
  282. $view['node'] = $node;
  283. $view['method_list'] = Helpers::methodList();
  284. $view['protocol_list'] = Helpers::protocolList();
  285. $view['obfs_list'] = Helpers::obfsList();
  286. $view['country_list'] = Country::query()->orderBy('code')->get();
  287. $view['level_list'] = Level::query()->orderBy('level')->get();
  288. $view['label_list'] = Label::query()->orderByDesc('sort')->orderBy('id')->get();
  289. $view['dv_list'] = NodeCertificate::query()->orderBy('id')->get();
  290. return view('admin.node.nodeInfo', $view)->with(compact('node'));
  291. }
  292. }
  293. // 删除节点
  294. public function delNode(Request $request): ?JsonResponse {
  295. $id = $request->input('id');
  296. $node = SsNode::query()->whereId($id)->first();
  297. if(!$node){
  298. return Response::json(['status' => 'fail', 'data' => '', 'message' => '节点不存在,请重试']);
  299. }
  300. try{
  301. DB::beginTransaction();
  302. // 删除分组关联、节点标签、节点相关日志
  303. SsNode::query()->whereId($id)->delete();
  304. SsNodeLabel::query()->whereNodeId($id)->delete();
  305. SsNodeInfo::query()->whereNodeId($id)->delete();
  306. SsNodeOnlineLog::query()->whereNodeId($id)->delete();
  307. SsNodeTrafficDaily::query()->whereNodeId($id)->delete();
  308. SsNodeTrafficHourly::query()->whereNodeId($id)->delete();
  309. SsNodePing::query()->whereNodeId($id)->delete();
  310. UserTrafficDaily::query()->whereNodeId($id)->delete();
  311. UserTrafficHourly::query()->whereNodeId($id)->delete();
  312. UserTrafficLog::query()->whereNodeId($id)->delete();
  313. NodeAuth::query()->whereNodeId($id)->delete();
  314. NodeRule::query()->whereNodeId($id)->delete();
  315. $RuleGroupList = RuleGroup::query()->get();
  316. foreach($RuleGroupList as $RuleGroup){
  317. $nodes = explode(',', $RuleGroup->nodes);
  318. if(in_array($id, $nodes, true)){
  319. $nodes = implode(',', array_diff($nodes, [$id]));
  320. RuleGroup::query()->whereId($RuleGroup->id)->update(['nodes' => $nodes]);
  321. }
  322. }
  323. DB::commit();
  324. return Response::json(['status' => 'success', 'data' => '', 'message' => '删除成功']);
  325. }catch(Exception $e){
  326. DB::rollBack();
  327. Log::error('删除节点信息异常:'.$e->getMessage());
  328. return Response::json(['status' => 'fail', 'data' => '', 'message' => '删除失败:'.$e->getMessage()]);
  329. }
  330. }
  331. // 节点流量监控
  332. public function nodeMonitor(Request $request) {
  333. $node_id = $request->input('id');
  334. $node = SsNode::query()->whereId($node_id)->orderByDesc('sort')->first();
  335. if(!$node){
  336. Session::flash('errorMsg', '节点不存在,请重试');
  337. return Redirect::back();
  338. }
  339. // 查看流量
  340. $dailyData = [];
  341. $hourlyData = [];
  342. // 节点一个月内的流量
  343. $nodeTrafficDaily = SsNodeTrafficDaily::query()
  344. ->with(['info'])
  345. ->whereNodeId($node->id)
  346. ->where('created_at', '>=', date('Y-m'))
  347. ->orderBy('created_at')
  348. ->pluck('total')
  349. ->toArray();
  350. $dailyTotal = date('d') - 1;//今天不算,减一
  351. $dailyCount = count($nodeTrafficDaily);
  352. for($x = 0; $x < ($dailyTotal - $dailyCount); $x++){
  353. $dailyData[$x] = 0;
  354. }
  355. for($x = ($dailyTotal - $dailyCount); $x < $dailyTotal; $x++){
  356. $dailyData[$x] = round($nodeTrafficDaily[$x - ($dailyTotal - $dailyCount)] / GB, 3);
  357. }
  358. // 节点一天内的流量
  359. $nodeTrafficHourly = SsNodeTrafficHourly::query()
  360. ->with(['info'])
  361. ->whereNodeId($node->id)
  362. ->where('created_at', '>=', date('Y-m-d'))
  363. ->orderBy('created_at')
  364. ->pluck('total')
  365. ->toArray();
  366. $hourlyTotal = date('H');
  367. $hourlyCount = count($nodeTrafficHourly);
  368. for($x = 0; $x < ($hourlyTotal - $hourlyCount); $x++){
  369. $hourlyData[$x] = 0;
  370. }
  371. for($x = ($hourlyTotal - $hourlyCount); $x < $hourlyTotal; $x++){
  372. $hourlyData[$x] = round($nodeTrafficHourly[$x - ($hourlyTotal - $hourlyCount)] / GB, 3);
  373. }
  374. $view['trafficDaily'] = ['nodeName' => $node->name, 'dailyData' => json_encode($dailyData)];
  375. $view['trafficHourly'] = ['nodeName' => $node->name, 'hourlyData' => json_encode($hourlyData)];
  376. // 本月天数数据
  377. $monthDays = [];
  378. for($i = 1; $i <= date("d"); $i++){
  379. $monthDays[] = $i;
  380. }
  381. // 本日小时数据
  382. $dayHours = [];
  383. for($i = 1; $i <= date("H"); $i++){
  384. $dayHours[] = $i;
  385. }
  386. $view['nodeName'] = $node->name;
  387. $view['nodeServer'] = $node->server;
  388. $view['monthDays'] = json_encode($monthDays);
  389. $view['dayHours'] = json_encode($dayHours);
  390. return Response::view('admin.node.nodeMonitor', $view);
  391. }
  392. // Ping节点延迟
  393. public function pingNode(Request $request): ?JsonResponse {
  394. $node = SsNode::query()->whereId($request->input('id'))->first();
  395. if(!$node){
  396. return Response::json(['status' => 'fail', 'message' => '节点不存在,请重试']);
  397. }
  398. $result = NetworkDetection::ping($node->is_ddns? $node->server : $node->ip);
  399. if($result){
  400. $data[0] = $result['China Telecom']['time']?: '无';
  401. $data[1] = $result['China Unicom']['time']?: '无';
  402. $data[2] = $result['China Mobile']['time']?: '无';
  403. $data[3] = $result['Hong Kong']['time']?: '无';
  404. return Response::json(['status' => 'success', 'message' => $data]);
  405. }
  406. return Response::json(['status' => 'fail', 'message' => 'Ping访问失败']);
  407. }
  408. // Ping节点延迟日志
  409. public function pingLog(Request $request): \Illuminate\Http\Response {
  410. $node_id = $request->input('nodeId');
  411. $query = SsNodePing::query();
  412. if(isset($node_id)){
  413. $query->whereNodeId($node_id);
  414. }
  415. $view['nodeList'] = SsNode::query()->orderBy('id')->get();
  416. $view['pingLogs'] = $query->orderBy('id')->paginate(15)->appends($request->except('page'));
  417. return Response::view('admin.logs.nodePingLog', $view);
  418. }
  419. // 节点授权列表
  420. public function authList(Request $request): \Illuminate\Http\Response {
  421. $view['list'] = NodeAuth::query()->orderBy('id')->paginate(15)->appends($request->except('page'));
  422. return Response::view('admin.node.authList', $view);
  423. }
  424. // 添加节点授权
  425. public function addAuth(): JsonResponse {
  426. $nodeArray = SsNode::query()->whereStatus(1)->orderBy('id')->pluck('id')->toArray();
  427. $authArray = NodeAuth::query()->orderBy('id')->pluck('node_id')->toArray();
  428. if($nodeArray == $authArray){
  429. return Response::json(['status' => 'success', 'message' => '没有需要生成授权的节点']);
  430. }
  431. foreach(array_diff($nodeArray, $authArray) as $nodeId){
  432. $obj = new NodeAuth();
  433. $obj->node_id = $nodeId;
  434. $obj->key = makeRandStr(16);
  435. $obj->secret = makeRandStr(8);
  436. $obj->save();
  437. }
  438. return Response::json(['status' => 'success', 'message' => '生成成功']);
  439. }
  440. // 删除节点授权
  441. public function delAuth(Request $request): JsonResponse {
  442. try{
  443. NodeAuth::query()->whereId($request->input('id'))->delete();
  444. }catch(Exception $e){
  445. return Response::json(['status' => 'fail', 'message' => '错误:'.var_export($e, true)]);
  446. }
  447. return Response::json(['status' => 'success', 'message' => '操作成功']);
  448. }
  449. // 重置节点授权
  450. public function refreshAuth(Request $request): ?JsonResponse {
  451. $ret = NodeAuth::query()->whereId($request->input('id'))->update([
  452. 'key' => makeRandStr(16),
  453. 'secret' => makeRandStr(8)
  454. ]);
  455. if($ret){
  456. return Response::json(['status' => 'success', 'message' => '操作成功']);
  457. }
  458. return Response::json(['status' => 'fail', 'message' => '操作失败']);
  459. }
  460. // 域名证书列表
  461. public function certificateList(Request $request): \Illuminate\Http\Response {
  462. $DvList = NodeCertificate::query()->orderBy('id')->paginate(15)->appends($request->except('page'));
  463. foreach($DvList as $Dv){
  464. if($Dv->key && $Dv->pem){
  465. $DvInfo = openssl_x509_parse($Dv->pem);
  466. //dd($DvInfo);
  467. $Dv->issuer = $DvInfo['issuer']['O'];
  468. $Dv->from = $DvInfo['validFrom_time_t']? date('Y-m-d', $DvInfo['validFrom_time_t']) : null;
  469. $Dv->to = $DvInfo['validTo']? date('Y-m-d', $DvInfo['validTo_time_t']) : null;
  470. }
  471. }
  472. $view['list'] = $DvList;
  473. return Response::view('admin.node.certificateList', $view);
  474. }
  475. // 添加域名证书
  476. public function addCertificate(Request $request) {
  477. if($request->isMethod('POST')){
  478. $obj = new NodeCertificate();
  479. $obj->domain = $request->input('domain');
  480. $obj->key = str_replace(["\r", "\n"], '', $request->input('key'));
  481. $obj->pem = str_replace(["\r", "\n"], '', $request->input('pem'));
  482. $obj->save();
  483. if($obj->id){
  484. return Response::json(['status' => 'success', 'message' => '生成成功']);
  485. }
  486. return Response::json(['status' => 'fail', 'message' => '生成失败']);
  487. }
  488. return Response::view('admin.node.certificateInfo');
  489. }
  490. // 编辑域名证书
  491. public function editCertificate(Request $request) {
  492. $Dv = NodeCertificate::query()->find($request->input('id'));
  493. if($request->isMethod('POST')){
  494. if($Dv){
  495. $ret = NodeCertificate::query()->update([
  496. 'domain' => $request->input('domain'),
  497. 'key' => $request->input('key'),
  498. 'pem' => $request->input('pem')
  499. ]);
  500. if($ret){
  501. return Response::json(['status' => 'success', 'message' => '修改成功']);
  502. }
  503. }
  504. return Response::json(['status' => 'fail', 'message' => '修改失败']);
  505. }
  506. $view['Dv'] = $Dv;
  507. return Response::view('admin.node.certificateInfo', $view);
  508. }
  509. // 删除域名证书
  510. public function delCertificate(Request $request): JsonResponse {
  511. try{
  512. NodeCertificate::query()->whereId($request->input('id'))->delete();
  513. }catch(Exception $e){
  514. return Response::json(['status' => 'fail', 'message' => '错误:'.var_export($e, true)]);
  515. }
  516. return Response::json(['status' => 'success', 'message' => '操作成功']);
  517. }
  518. }