NodeController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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' => (bool) ($info['allow_insecure'] ?? false),
  121. ];
  122. break;
  123. case 5: // Hysteria2
  124. $profile = [
  125. 'obfs' => $info['obfs'] ?? null,
  126. 'obfs_param' => $info['obfs_param'] ?? null,
  127. 'upload_mbps' => $info['upload_mbps'] ?? null,
  128. 'download_mbps' => $info['download_mbps'] ?? null,
  129. 'ignore_client_bandwidth' => (bool) ($info['ignore_client_bandwidth'] ?? false),
  130. 'allow_insecure' => (bool) ($info['allow_insecure'] ?? false),
  131. 'ports' => $info['ports'] ?? null,
  132. ];
  133. break;
  134. case 1:
  135. case 4:
  136. $profile = [
  137. 'method' => $info['method'],
  138. 'protocol' => $info['protocol'],
  139. 'obfs' => $info['obfs'],
  140. 'obfs_param' => $info['obfs_param'] ?? null,
  141. 'protocol_param' => $info['protocol_param'] ?? null,
  142. 'passwd' => $info['passwd'] ?? null,
  143. ];
  144. break;
  145. }
  146. $details = [
  147. 'next_renewal_date' => $info['next_renewal_date'],
  148. 'subscription_term' => $info['subscription_term'],
  149. 'renewal_cost' => $info['renewal_cost'],
  150. ];
  151. array_clean($details);
  152. return [
  153. 'type' => $info['type'],
  154. 'name' => $info['name'],
  155. 'country_code' => $info['country_code'],
  156. 'server' => $info['server'],
  157. 'ip' => $info['ip'],
  158. 'ipv6' => $info['ipv6'],
  159. 'level' => $info['level'],
  160. 'rule_group_id' => $info['rule_group_id'],
  161. 'speed_limit' => $info['speed_limit'],
  162. 'client_limit' => $info['client_limit'],
  163. 'details' => ! empty($details) ? $details : null,
  164. 'description' => $info['description'],
  165. 'profile' => $profile ?? [],
  166. 'traffic_rate' => $info['traffic_rate'],
  167. 'is_udp' => $info['is_udp'],
  168. 'is_display' => $info['is_display'],
  169. 'is_ddns' => $info['is_ddns'],
  170. 'relay_node_id' => $info['relay_node_id'],
  171. 'port' => $info['port'] ?? 0,
  172. 'push_port' => $info['push_port'],
  173. 'detection_type' => $info['detection_type'],
  174. 'sort' => $info['sort'],
  175. 'status' => $info['status'],
  176. ];
  177. }
  178. public function clone(Node $node): RedirectResponse
  179. { // 克隆节点
  180. $clone = [
  181. 'name' => $node->name.'_'.trans('admin.clone'),
  182. 'server' => null,
  183. ];
  184. if ($node->is_ddns) {
  185. $clone['ip'] = '1.1.1.1';
  186. $clone['is_ddns'] = 0;
  187. }
  188. $new = $node->replicate()->fill($clone);
  189. $new->save();
  190. return redirect(route('admin.node.edit', $new));
  191. }
  192. public function edit(Node $node): View
  193. { // 编辑节点页面
  194. $node->load('labels:id');
  195. $nodeArray = $node->toArray();
  196. return view('admin.node.info', [
  197. 'node' => array_merge(
  198. Arr::except($nodeArray, ['details', 'profile']),
  199. $nodeArray['details'] ?? [],
  200. $nodeArray['profile'] ?? [],
  201. ['labels' => $node->labels->pluck('id')->toArray()]// 将标签ID列表作为一维数组
  202. ),
  203. 'nodes' => Node::whereNotIn('id', [$node->id])->orderBy('id')->pluck('id', 'name'),
  204. 'countries' => Country::orderBy('code')->get(),
  205. 'levels' => Level::orderBy('level')->pluck('name', 'level'),
  206. 'ruleGroups' => RuleGroup::orderBy('id')->pluck('name', 'id'),
  207. 'labels' => Label::orderByDesc('sort')->orderBy('id')->pluck('name', 'id'),
  208. 'certs' => NodeCertificate::orderBy('id')->pluck('domain', 'id'),
  209. ...$this->proxyConfigOptions(),
  210. ]);
  211. }
  212. public function update(NodeRequest $request, Node $node): JsonResponse
  213. { // 编辑节点
  214. // 获取验证后的数据
  215. $validatedData = $request->validated();
  216. // 构建操作清单
  217. $operationList = ['save_node_info', 'sync_labels', 'refresh_geo']; // 操作清单
  218. if (! ($validatedData['is_ddns'] ?? $node->is_ddns) && ($validatedData['server'] ?? $node->server) && sysConfig('ddns_mode')) { // 检查是否有DDNS相关变更
  219. $operationList[] = 'handle_ddns';
  220. }
  221. if ((int) ($validatedData['type'] ?? $node->type) === 4) { // 检查是否是VNET节点(可能需要重新加载)
  222. $operationList[] = 'reload_node';
  223. }
  224. // 发送操作清单
  225. broadcast(new NodeActions('update', ['list' => $operationList], $node->id));
  226. try {
  227. // 先尝试更新节点信息
  228. if ($node->update($this->nodeStore($validatedData))) {
  229. broadcast(new NodeActions('update', ['operation' => 'save_node_info', 'status' => 1], $node->id));
  230. // 如果没有字段变更,强制触发更新以确保 observer 被调用
  231. if (empty($node->getChanges())) {
  232. $node->touch();
  233. }
  234. // 同步节点标签
  235. $node->labels()->sync($request->input('labels'));
  236. broadcast(new NodeActions('update', ['operation' => 'sync_labels', 'status' => 1], $node->id));
  237. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
  238. }
  239. } catch (Exception $e) {
  240. Log::error(trans('common.error_action_item', ['action' => trans('common.edit'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  241. broadcast(new NodeActions('update', ['status' => 0, 'message' => $e->getMessage()], $node->id));
  242. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')]).', '.$e->getMessage()]);
  243. }
  244. broadcast(new NodeActions('update', ['status' => 0], $node->id));
  245. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
  246. }
  247. public function destroy(Node $node): JsonResponse
  248. { // 删除节点
  249. // 发送操作清单给前端
  250. $operationList = ['delete_node'];
  251. // 根据节点配置添加相应的操作项
  252. if ($node->server && sysConfig('ddns_mode')) {
  253. $operationList[] = 'handle_ddns';
  254. }
  255. broadcast(new NodeActions('delete', ['list' => $operationList], $node->id));
  256. try {
  257. // 删除节点
  258. if ($node->delete()) {
  259. broadcast(new NodeActions('delete', ['operation' => 'delete_node', 'status' => 1], $node->id));
  260. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  261. }
  262. } catch (Exception $e) {
  263. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  264. broadcast(new NodeActions('delete', ['status' => 0, 'message' => $e->getMessage()], $node->id));
  265. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
  266. }
  267. broadcast(new NodeActions('delete', ['status' => 0], $node->id));
  268. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  269. }
  270. public function checkNode(?Node $node = null): JsonResponse
  271. {
  272. // 获取节点集合并预加载IP信息
  273. $fields = ['id', 'name', 'is_ddns', 'server', 'ip', 'ipv6', 'port'];
  274. $nodes = ($node ? collect([$node]) : Node::whereStatus(1)->select($fields)->get())->map(function ($n) {
  275. return ['node' => $n, 'ips' => $n->ips()];
  276. });
  277. // 构建节点列表信息
  278. $nodeList = $nodes->mapWithKeys(function ($item) {
  279. return [$item['node']->id => ['name' => $item['node']->name, 'ips' => $item['ips']]];
  280. })->toArray();
  281. // 立即发送节点列表信息给前端
  282. broadcast(new NodeActions('check', ['list' => $nodeList], $node?->id));
  283. // 异步分发检测任务,提高响应速度
  284. $nodes->each(function ($item) use ($node) {
  285. dispatch(static function () use ($item, $node) {
  286. foreach ($item['ips'] as $ip) {
  287. $ret = ['ip' => $ip, 'icmp' => 4, 'tcp' => 4, 'node_id' => $item['node']->id, 'status' => 1];
  288. try {
  289. $status = NetworkDetection::networkStatus($ip, $item['node']->port ?? 22);
  290. $ret['icmp'] = $status['icmp'];
  291. $ret['tcp'] = $status['tcp'];
  292. } catch (Exception $e) {
  293. Log::error("节点 [{$item['node']->id}] IP [$ip] 检测失败: ".$e->getMessage());
  294. $ret += ['message' => $e->getMessage()];
  295. $ret['status'] = 0;
  296. }
  297. broadcast(new NodeActions('check', $ret, $node?->id));
  298. }
  299. });
  300. });
  301. return response()->json([
  302. 'status' => 'success',
  303. 'message' => trans('common.success_item', [
  304. 'attribute' => $node ? trans('admin.node.connection_test') : trans('admin.node.connection_test_all'),
  305. ]),
  306. ]);
  307. }
  308. public function refreshGeo(?Node $node = null): JsonResponse
  309. {
  310. $nodes = $node ? collect([$node]) : Node::whereStatus(1)->get();
  311. // 发送节点列表信息
  312. broadcast(new NodeActions('geo', ['list' => $nodes->pluck('name', 'id')], $node?->id));
  313. // 异步处理地理位置刷新
  314. $nodes->each(function ($n) use ($node) {
  315. dispatch(static function () use ($n, $node) {
  316. $ret = ['node_id' => $n->id, 'status' => 1];
  317. try {
  318. $ret += $n->refresh_geo();
  319. } catch (Exception $e) {
  320. Log::error("节点 [{$n->id}] 刷新地理位置失败: ".$e->getMessage());
  321. $ret += ['message' => $e->getMessage()];
  322. $ret['status'] = 0;
  323. }
  324. broadcast(new NodeActions('geo', $ret, $node?->id));
  325. });
  326. });
  327. return response()->json([
  328. 'status' => 'success',
  329. 'message' => trans('common.success_item', [
  330. 'attribute' => $node ? trans('admin.node.refresh_geo') : trans('admin.node.refresh_geo_all'),
  331. ]),
  332. ]);
  333. }
  334. public function reload(?Node $node = null): JsonResponse
  335. {
  336. $nodes = $node ? collect([$node]) : Node::whereStatus(1)->whereType(4)->get();
  337. // 发送节点列表信息
  338. broadcast(new NodeActions('reload', ['list' => $nodes->pluck('name', 'id')], $node?->id));
  339. // 异步处理节点重载
  340. $nodes->each(function ($n) use ($node) {
  341. dispatch(static function () use ($n, $node) {
  342. $ret = ['node_id' => $n->id, 'status' => 1];
  343. try {
  344. $ret = array_merge($ret, (new ReloadNode($n))->handle());
  345. if (count($ret['error'] ?? [])) {
  346. $ret['status'] = 0;
  347. }
  348. } catch (Exception $e) {
  349. Log::error("节点 [{$n->id}] 重载失败: ".$e->getMessage());
  350. $ret['message'] = $e->getMessage();
  351. $ret['status'] = 0;
  352. }
  353. broadcast(new NodeActions('reload', $ret, $node?->id));
  354. });
  355. });
  356. return response()->json([
  357. 'status' => 'success',
  358. 'message' => trans('common.success_item', [
  359. 'attribute' => $node ? trans('admin.node.reload') : trans('admin.node.reload_all'),
  360. ]),
  361. ]);
  362. }
  363. public function nodeMonitor(Node $node): View
  364. { // 节点流量监控
  365. return view('admin.node.monitor', ['nodeName' => $node->name, 'nodeServer' => $node->server, ...$this->DataFlowChart($node->id, true)]);
  366. }
  367. }