NodeController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Events\NodeActions;
  4. use App\Helpers\DataChart;
  5. use App\Helpers\ProxyConfig;
  6. use App\Http\Controllers\Controller;
  7. use App\Http\Requests\Admin\NodeRequest;
  8. use App\Jobs\VNet\ReloadNode;
  9. use App\Models\Country;
  10. use App\Models\Label;
  11. use App\Models\Level;
  12. use App\Models\Node;
  13. use App\Models\NodeCertificate;
  14. use App\Models\RuleGroup;
  15. use App\Utils\NetworkDetection;
  16. use Arr;
  17. use Exception;
  18. use Illuminate\Contracts\View\View;
  19. use Illuminate\Http\JsonResponse;
  20. use Illuminate\Http\RedirectResponse;
  21. use Illuminate\Http\Request;
  22. use Log;
  23. class NodeController extends Controller
  24. {
  25. use DataChart, ProxyConfig;
  26. public function index(Request $request): View
  27. { // 节点列表
  28. $query = Node::whereNull('relay_node_id')
  29. ->with([
  30. 'dailyDataFlows' => function ($query) {
  31. $query->whereBetween('created_at', [now()->startOfMonth(), now()]);
  32. },
  33. 'hourlyDataFlows' => function ($query) {
  34. $query->whereDate('created_at', now()->toDateString());
  35. },
  36. 'latestOnlineLog',
  37. 'latestHeartbeat',
  38. 'childNodes',
  39. ]);
  40. $request->whenFilled('status', function ($value) use ($query) {
  41. $query->where('status', $value);
  42. });
  43. $nodeList = $query->orderByDesc('sort')->orderBy('id')->paginate(15)->appends($request->except('page'))->through(function ($node) {
  44. // 预处理每个节点的数据
  45. $node->online_users = $node->latestOnlineLog?->online_user; // 在线人数
  46. // 计算流量总和
  47. $dailyTransfer = $node->dailyDataFlows->sum(fn ($item) => $item->u + $item->d);
  48. $hourlyTransfer = $node->hourlyDataFlows->sum(fn ($item) => $item->u + $item->d);
  49. $node->transfer = formatBytes($dailyTransfer + $hourlyTransfer); // 已产生流量
  50. $node_info = $node->latestHeartbeat; // 近期负载
  51. $node->isOnline = ! empty($node_info?->load);
  52. $node->load = $node_info?->load ?? false;
  53. $node->uptime = formatTime($node_info?->uptime);
  54. return $node;
  55. });
  56. return view('admin.node.index', compact('nodeList'));
  57. }
  58. public function store(NodeRequest $request): JsonResponse
  59. { // 添加节点
  60. // 获取验证后的数据
  61. $validatedData = $request->validated();
  62. // 构建操作清单
  63. $operationList = ['save_node_info', 'create_auth', 'sync_labels', 'refresh_geo'];
  64. // 根据节点配置添加相应的操作项
  65. if (! ($validatedData['is_ddns'] ?? false) && ($validatedData['server'] ?? false) && sysConfig('ddns_mode')) {
  66. $operationList[] = 'handle_ddns';
  67. }
  68. // 发送操作清单
  69. broadcast(new NodeActions('create', ['list' => $operationList]));
  70. try {
  71. // 保存节点信息
  72. if ($node = Node::create($this->nodeStore($validatedData))) {
  73. broadcast(new NodeActions('create', ['operation' => 'save_node_info', 'status' => 1]));
  74. if ($request->has('labels')) { // 生成节点标签
  75. $node->labels()->attach($request->input('labels'));
  76. }
  77. broadcast(new NodeActions('create', ['operation' => 'sync_labels', 'status' => 1]));
  78. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]);
  79. }
  80. } catch (Exception $e) {
  81. Log::error(trans('common.error_action_item', ['action' => trans('common.add'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  82. broadcast(new NodeActions('create', ['status' => 0, 'message' => $e->getMessage()]));
  83. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')]).', '.$e->getMessage()]);
  84. }
  85. broadcast(new NodeActions('create', ['status' => 0]));
  86. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]);
  87. }
  88. public function create(): View
  89. {
  90. return view('admin.node.info', [
  91. 'nodes' => Node::orderBy('id')->pluck('id', 'name'),
  92. 'countries' => Country::orderBy('code')->get(),
  93. 'levels' => Level::orderBy('level')->pluck('name', 'level'),
  94. 'ruleGroups' => RuleGroup::orderBy('id')->pluck('name', 'id'),
  95. 'labels' => Label::orderByDesc('sort')->orderBy('id')->pluck('name', 'id'),
  96. 'certs' => NodeCertificate::orderBy('id')->pluck('domain', 'id'),
  97. ...$this->proxyConfigOptions(),
  98. ]);
  99. }
  100. private function nodeStore(array $info): array
  101. { // 添加节点信息
  102. switch ($info['type']) {
  103. case 0:
  104. $profile = ['method' => $info['method']];
  105. break;
  106. case 2:
  107. $profile = [
  108. 'method' => $info['v2_method'],
  109. 'v2_alter_id' => $info['v2_alter_id'],
  110. 'v2_net' => $info['v2_net'],
  111. 'v2_type' => $info['v2_type'],
  112. 'v2_host' => $info['v2_host'],
  113. 'v2_path' => $info['v2_path'],
  114. 'v2_tls' => $info['v2_tls'] ? 'tls' : '',
  115. 'v2_sni' => $info['v2_sni'],
  116. ];
  117. break;
  118. case 3:
  119. $profile = [
  120. 'allow_insecure' => false,
  121. ];
  122. break;
  123. case 1:
  124. case 4:
  125. $profile = [
  126. 'method' => $info['method'],
  127. 'protocol' => $info['protocol'],
  128. 'obfs' => $info['obfs'],
  129. 'obfs_param' => $info['obfs_param'],
  130. 'protocol_param' => $info['protocol_param'],
  131. 'passwd' => $info['passwd'],
  132. ];
  133. break;
  134. }
  135. $details = [
  136. 'next_renewal_date' => $info['next_renewal_date'],
  137. 'subscription_term' => $info['subscription_term'],
  138. 'renewal_cost' => $info['renewal_cost'],
  139. ];
  140. array_clean($details);
  141. return [
  142. 'type' => $info['type'],
  143. 'name' => $info['name'],
  144. 'country_code' => $info['country_code'],
  145. 'server' => $info['server'],
  146. 'ip' => $info['ip'],
  147. 'ipv6' => $info['ipv6'],
  148. 'level' => $info['level'],
  149. 'rule_group_id' => $info['rule_group_id'],
  150. 'speed_limit' => $info['speed_limit'],
  151. 'client_limit' => $info['client_limit'],
  152. 'details' => ! empty($details) ? $details : null,
  153. 'description' => $info['description'],
  154. 'profile' => $profile ?? [],
  155. 'traffic_rate' => $info['traffic_rate'],
  156. 'is_udp' => $info['is_udp'],
  157. 'is_display' => $info['is_display'],
  158. 'is_ddns' => $info['is_ddns'],
  159. 'relay_node_id' => $info['relay_node_id'],
  160. 'port' => $info['port'] ?? 0,
  161. 'push_port' => $info['push_port'],
  162. 'detection_type' => $info['detection_type'],
  163. 'sort' => $info['sort'],
  164. 'status' => $info['status'],
  165. ];
  166. }
  167. public function clone(Node $node): RedirectResponse
  168. { // 克隆节点
  169. $clone = [
  170. 'name' => $node->name.'_'.trans('admin.clone'),
  171. 'server' => null,
  172. ];
  173. if ($node->is_ddns) {
  174. $clone['ip'] = '1.1.1.1';
  175. $clone['is_ddns'] = 0;
  176. }
  177. $new = $node->replicate()->fill($clone);
  178. $new->save();
  179. return redirect(route('admin.node.edit', $new));
  180. }
  181. public function edit(Node $node): View
  182. { // 编辑节点页面
  183. $node->load('labels:id');
  184. $nodeArray = $node->toArray();
  185. return view('admin.node.info', [
  186. 'node' => array_merge(
  187. Arr::except($nodeArray, ['details', 'profile']),
  188. $nodeArray['details'] ?? [],
  189. $nodeArray['profile'] ?? [],
  190. ['labels' => $node->labels->pluck('id')->toArray()]// 将标签ID列表作为一维数组
  191. ),
  192. 'nodes' => Node::whereNotIn('id', [$node->id])->orderBy('id')->pluck('id', 'name'),
  193. 'countries' => Country::orderBy('code')->get(),
  194. 'levels' => Level::orderBy('level')->pluck('name', 'level'),
  195. 'ruleGroups' => RuleGroup::orderBy('id')->pluck('name', 'id'),
  196. 'labels' => Label::orderByDesc('sort')->orderBy('id')->pluck('name', 'id'),
  197. 'certs' => NodeCertificate::orderBy('id')->pluck('domain', 'id'),
  198. ...$this->proxyConfigOptions(),
  199. ]);
  200. }
  201. public function update(NodeRequest $request, Node $node): JsonResponse
  202. { // 编辑节点
  203. // 获取验证后的数据
  204. $validatedData = $request->validated();
  205. // 构建操作清单
  206. $operationList = ['save_node_info', 'sync_labels', 'refresh_geo']; // 操作清单
  207. if (! ($validatedData['is_ddns'] ?? $node->is_ddns) && ($validatedData['server'] ?? $node->server) && sysConfig('ddns_mode')) { // 检查是否有DDNS相关变更
  208. $operationList[] = 'handle_ddns';
  209. }
  210. if ((int) ($validatedData['type'] ?? $node->type) === 4) { // 检查是否是VNET节点(可能需要重新加载)
  211. $operationList[] = 'reload_node';
  212. }
  213. // 发送操作清单
  214. broadcast(new NodeActions('update', ['list' => $operationList], $node->id));
  215. try {
  216. // 先尝试更新节点信息
  217. if ($node->update($this->nodeStore($validatedData))) {
  218. broadcast(new NodeActions('update', ['operation' => 'save_node_info', 'status' => 1], $node->id));
  219. // 如果没有字段变更,强制触发更新以确保 observer 被调用
  220. if (empty($node->getChanges())) {
  221. $node->touch();
  222. }
  223. // 同步节点标签
  224. $node->labels()->sync($request->input('labels'));
  225. broadcast(new NodeActions('update', ['operation' => 'sync_labels', 'status' => 1], $node->id));
  226. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
  227. }
  228. } catch (Exception $e) {
  229. Log::error(trans('common.error_action_item', ['action' => trans('common.edit'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  230. broadcast(new NodeActions('update', ['status' => 0, 'message' => $e->getMessage()], $node->id));
  231. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')]).', '.$e->getMessage()]);
  232. }
  233. broadcast(new NodeActions('update', ['status' => 0], $node->id));
  234. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
  235. }
  236. public function destroy(Node $node): JsonResponse
  237. { // 删除节点
  238. // 发送操作清单给前端
  239. $operationList = ['delete_node'];
  240. // 根据节点配置添加相应的操作项
  241. if ($node->server && sysConfig('ddns_mode')) {
  242. $operationList[] = 'handle_ddns';
  243. }
  244. broadcast(new NodeActions('delete', ['list' => $operationList], $node->id));
  245. try {
  246. // 删除节点
  247. if ($node->delete()) {
  248. broadcast(new NodeActions('delete', ['operation' => 'delete_node', 'status' => 1], $node->id));
  249. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  250. }
  251. } catch (Exception $e) {
  252. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  253. broadcast(new NodeActions('delete', ['status' => 0, 'message' => $e->getMessage()], $node->id));
  254. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
  255. }
  256. broadcast(new NodeActions('delete', ['status' => 0], $node->id));
  257. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  258. }
  259. public function checkNode(?Node $node = null): JsonResponse
  260. {
  261. // 获取节点集合并预加载IP信息
  262. $fields = ['id', 'name', 'is_ddns', 'server', 'ip', 'ipv6', 'port'];
  263. $nodes = ($node ? collect([$node]) : Node::whereStatus(1)->select($fields)->get())->map(function ($n) {
  264. return ['node' => $n, 'ips' => $n->ips()];
  265. });
  266. // 构建节点列表信息
  267. $nodeList = $nodes->mapWithKeys(function ($item) {
  268. return [$item['node']->id => ['name' => $item['node']->name, 'ips' => $item['ips']]];
  269. })->toArray();
  270. // 立即发送节点列表信息给前端
  271. broadcast(new NodeActions('check', ['list' => $nodeList], $node?->id));
  272. // 异步分发检测任务,提高响应速度
  273. $nodes->each(function ($item) use ($node) {
  274. dispatch(static function () use ($item, $node) {
  275. foreach ($item['ips'] as $ip) {
  276. $ret = ['ip' => $ip, 'icmp' => 4, 'tcp' => 4, 'node_id' => $item['node']->id, 'status' => 1];
  277. try {
  278. $status = NetworkDetection::networkStatus($ip, $item['node']->port ?? 22);
  279. $ret['icmp'] = $status['icmp'];
  280. $ret['tcp'] = $status['tcp'];
  281. } catch (Exception $e) {
  282. Log::error("节点 [{$item['node']->id}] IP [$ip] 检测失败: ".$e->getMessage());
  283. $ret += ['message' => $e->getMessage()];
  284. $ret['status'] = 0;
  285. }
  286. broadcast(new NodeActions('check', $ret, $node?->id));
  287. }
  288. });
  289. });
  290. return response()->json([
  291. 'status' => 'success',
  292. 'message' => trans('common.success_item', [
  293. 'attribute' => $node ? trans('admin.node.connection_test') : trans('admin.node.connection_test_all'),
  294. ]),
  295. ]);
  296. }
  297. public function refreshGeo(?Node $node = null): JsonResponse
  298. {
  299. $nodes = $node ? collect([$node]) : Node::whereStatus(1)->get();
  300. // 发送节点列表信息
  301. broadcast(new NodeActions('geo', ['list' => $nodes->pluck('name', 'id')], $node?->id));
  302. // 异步处理地理位置刷新
  303. $nodes->each(function ($n) use ($node) {
  304. dispatch(static function () use ($n, $node) {
  305. $ret = ['node_id' => $n->id, 'status' => 1];
  306. try {
  307. $ret += $n->refresh_geo();
  308. } catch (Exception $e) {
  309. Log::error("节点 [{$n->id}] 刷新地理位置失败: ".$e->getMessage());
  310. $ret += ['message' => $e->getMessage()];
  311. $ret['status'] = 0;
  312. }
  313. broadcast(new NodeActions('geo', $ret, $node?->id));
  314. });
  315. });
  316. return response()->json([
  317. 'status' => 'success',
  318. 'message' => trans('common.success_item', [
  319. 'attribute' => $node ? trans('admin.node.refresh_geo') : trans('admin.node.refresh_geo_all'),
  320. ]),
  321. ]);
  322. }
  323. public function reload(?Node $node = null): JsonResponse
  324. {
  325. $nodes = $node ? collect([$node]) : Node::whereStatus(1)->whereType(4)->get();
  326. // 发送节点列表信息
  327. broadcast(new NodeActions('reload', ['list' => $nodes->pluck('name', 'id')], $node?->id));
  328. // 异步处理节点重载
  329. $nodes->each(function ($n) use ($node) {
  330. dispatch(static function () use ($n, $node) {
  331. $ret = ['node_id' => $n->id, 'status' => 1];
  332. try {
  333. $ret = array_merge($ret, (new ReloadNode($n))->handle());
  334. if (count($ret['error'] ?? [])) {
  335. $ret['status'] = 0;
  336. }
  337. } catch (Exception $e) {
  338. Log::error("节点 [{$n->id}] 重载失败: ".$e->getMessage());
  339. $ret['message'] = $e->getMessage();
  340. $ret['status'] = 0;
  341. }
  342. broadcast(new NodeActions('reload', $ret, $node?->id));
  343. });
  344. });
  345. return response()->json([
  346. 'status' => 'success',
  347. 'message' => trans('common.success_item', [
  348. 'attribute' => $node ? trans('admin.node.reload') : trans('admin.node.reload_all'),
  349. ]),
  350. ]);
  351. }
  352. public function nodeMonitor(Node $node): View
  353. { // 节点流量监控
  354. return view('admin.node.monitor', ['nodeName' => $node->name, 'nodeServer' => $node->server, ...$this->DataFlowChart($node->id, true)]);
  355. }
  356. }