1
0

NodeController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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\Node\CheckNodeIp;
  9. use App\Jobs\VNet\reloadNode;
  10. use App\Models\Country;
  11. use App\Models\Label;
  12. use App\Models\Level;
  13. use App\Models\Node;
  14. use App\Models\NodeCertificate;
  15. use App\Models\RuleGroup;
  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. try {
  61. if ($node = Node::create($this->nodeStore($request->validated()))) {
  62. if ($request->has('labels')) { // 生成节点标签
  63. $node->labels()->attach($request->input('labels'));
  64. }
  65. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]);
  66. }
  67. } catch (Exception $e) {
  68. Log::error(trans('common.error_action_item', ['action' => trans('common.add'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  69. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')]).', '.$e->getMessage()]);
  70. }
  71. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]);
  72. }
  73. public function create(): View
  74. {
  75. return view('admin.node.info', [
  76. 'nodes' => Node::orderBy('id')->pluck('id', 'name'),
  77. 'countries' => Country::orderBy('code')->get(),
  78. 'levels' => Level::orderBy('level')->pluck('name', 'level'),
  79. 'ruleGroups' => RuleGroup::orderBy('id')->pluck('name', 'id'),
  80. 'labels' => Label::orderByDesc('sort')->orderBy('id')->pluck('name', 'id'),
  81. 'certs' => NodeCertificate::orderBy('id')->pluck('domain', 'id'),
  82. ...$this->proxyConfigOptions(),
  83. ]);
  84. }
  85. private function nodeStore(array $info): array
  86. { // 添加节点信息
  87. switch ($info['type']) {
  88. case 0:
  89. $profile = ['method' => $info['method']];
  90. break;
  91. case 2:
  92. $profile = [
  93. 'method' => $info['v2_method'],
  94. 'v2_alter_id' => $info['v2_alter_id'],
  95. 'v2_net' => $info['v2_net'],
  96. 'v2_type' => $info['v2_type'],
  97. 'v2_host' => $info['v2_host'],
  98. 'v2_path' => $info['v2_path'],
  99. 'v2_tls' => $info['v2_tls'] ? 'tls' : '',
  100. 'v2_sni' => $info['v2_sni'],
  101. ];
  102. break;
  103. case 3:
  104. $profile = [
  105. 'allow_insecure' => false,
  106. ];
  107. break;
  108. case 1:
  109. case 4:
  110. $profile = [
  111. 'method' => $info['method'],
  112. 'protocol' => $info['protocol'],
  113. 'obfs' => $info['obfs'],
  114. 'obfs_param' => $info['obfs_param'],
  115. 'protocol_param' => $info['protocol_param'],
  116. 'passwd' => $info['passwd'],
  117. ];
  118. break;
  119. }
  120. $details = [
  121. 'next_renewal_date' => $info['next_renewal_date'],
  122. 'subscription_term' => $info['subscription_term'],
  123. 'renewal_cost' => $info['renewal_cost'],
  124. ];
  125. array_clean($details);
  126. return [
  127. 'type' => $info['type'],
  128. 'name' => $info['name'],
  129. 'country_code' => $info['country_code'],
  130. 'server' => $info['server'],
  131. 'ip' => $info['ip'],
  132. 'ipv6' => $info['ipv6'],
  133. 'level' => $info['level'],
  134. 'rule_group_id' => $info['rule_group_id'],
  135. 'speed_limit' => $info['speed_limit'],
  136. 'client_limit' => $info['client_limit'],
  137. 'details' => ! empty($details) ? $details : null,
  138. 'description' => $info['description'],
  139. 'profile' => $profile ?? [],
  140. 'traffic_rate' => $info['traffic_rate'],
  141. 'is_udp' => $info['is_udp'],
  142. 'is_display' => $info['is_display'],
  143. 'is_ddns' => $info['is_ddns'],
  144. 'relay_node_id' => $info['relay_node_id'],
  145. 'port' => $info['port'] ?? 0,
  146. 'push_port' => $info['push_port'],
  147. 'detection_type' => $info['detection_type'],
  148. 'sort' => $info['sort'],
  149. 'status' => $info['status'],
  150. ];
  151. }
  152. public function clone(Node $node): RedirectResponse
  153. { // 克隆节点
  154. $clone = [
  155. 'name' => $node->name.'_'.trans('admin.clone'),
  156. 'server' => null,
  157. ];
  158. if ($node->is_ddns) {
  159. $clone['ip'] = '1.1.1.1';
  160. $clone['is_ddns'] = 0;
  161. }
  162. $new = $node->replicate()->fill($clone);
  163. $new->save();
  164. return redirect(route('admin.node.edit', $new));
  165. }
  166. public function edit(Node $node): View
  167. { // 编辑节点页面
  168. $node->load('labels:id');
  169. $nodeArray = $node->toArray();
  170. return view('admin.node.info', [
  171. 'node' => array_merge(
  172. Arr::except($nodeArray, ['details', 'profile']),
  173. $nodeArray['details'] ?? [],
  174. $nodeArray['profile'] ?? [],
  175. ['labels' => $node->labels->pluck('id')->toArray()]// 将标签ID列表作为一维数组
  176. ),
  177. 'nodes' => Node::whereNotIn('id', [$node->id])->orderBy('id')->pluck('id', 'name'),
  178. 'countries' => Country::orderBy('code')->get(),
  179. 'levels' => Level::orderBy('level')->pluck('name', 'level'),
  180. 'ruleGroups' => RuleGroup::orderBy('id')->pluck('name', 'id'),
  181. 'labels' => Label::orderByDesc('sort')->orderBy('id')->pluck('name', 'id'),
  182. 'certs' => NodeCertificate::orderBy('id')->pluck('domain', 'id'),
  183. ...$this->proxyConfigOptions(),
  184. ]);
  185. }
  186. public function update(NodeRequest $request, Node $node): JsonResponse
  187. { // 编辑节点
  188. try {
  189. if ($node->update($this->nodeStore($request->validated()))) {
  190. // 更新节点标签
  191. $node->labels()->sync($request->input('labels'));
  192. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
  193. }
  194. } catch (Exception $e) {
  195. Log::error(trans('common.error_action_item', ['action' => trans('common.edit'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  196. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')]).', '.$e->getMessage()]);
  197. }
  198. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
  199. }
  200. public function destroy(Node $node): JsonResponse
  201. { // 删除节点
  202. try {
  203. if ($node->delete()) {
  204. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  205. }
  206. } catch (Exception $e) {
  207. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  208. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
  209. }
  210. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  211. }
  212. public function checkNode(?Node $node = null): JsonResponse
  213. {
  214. // 获取节点集合并预加载IP信息
  215. $fields = ['id', 'name', 'is_ddns', 'server', 'ip', 'ipv6', 'port'];
  216. $nodes = ($node ? collect([$node]) : Node::whereStatus(1)->select($fields)->get())->map(function ($n) {
  217. $n->ips = $n->ips();
  218. return $n;
  219. });
  220. // 构建节点列表信息
  221. $nodeList = $nodes->mapWithKeys(function ($n) {
  222. return [$n->id => ['name' => $n->name, 'ips' => $n->ips]];
  223. })->toArray();
  224. // 立即发送节点列表信息给前端
  225. broadcast(new NodeActions('check', ['nodeList' => $nodeList], $node?->id));
  226. // 异步分发检测任务,提高响应速度
  227. $nodes->each(function ($n) use ($node) {
  228. dispatch(new CheckNodeIp($n->id, $n->ips, $n->port ?? 22, $node?->id));
  229. });
  230. return response()->json([
  231. 'status' => 'success',
  232. 'message' => trans('common.success_item', [
  233. 'attribute' => $node ? trans('admin.node.connection_test') : trans('admin.node.connection_test_all'),
  234. ]),
  235. ]);
  236. }
  237. public function refreshGeo(?Node $node = null): JsonResponse
  238. {
  239. $nodes = $node ? collect([$node]) : Node::whereStatus(1)->get();
  240. // 发送节点列表信息
  241. broadcast(new NodeActions('geo', ['nodeList' => $nodes->pluck('name', 'id')], $node?->id));
  242. // 异步处理地理位置刷新
  243. $nodes->each(function ($n) use ($node) {
  244. dispatch(static function () use ($n, $node) {
  245. $ret = ['nodeId' => $n->id];
  246. try {
  247. $ret += $n->refresh_geo();
  248. } catch (Exception $e) {
  249. Log::error("节点 [{$n->id}] 刷新地理位置失败: ".$e->getMessage());
  250. $ret += ['error' => $e->getMessage()];
  251. }
  252. broadcast(new NodeActions('geo', $ret, $node?->id));
  253. });
  254. });
  255. return response()->json([
  256. 'status' => 'success',
  257. 'message' => trans('common.success_item', [
  258. 'attribute' => $node ? trans('admin.node.refresh_geo') : trans('admin.node.refresh_geo_all'),
  259. ]),
  260. ]);
  261. }
  262. public function reload(?Node $node = null): JsonResponse
  263. {
  264. $nodes = $node ? collect([$node]) : Node::whereStatus(1)->whereType(4)->get();
  265. // 发送节点列表信息
  266. broadcast(new NodeActions('reload', ['nodeList' => $nodes->pluck('name', 'id')], $node?->id));
  267. // 异步处理节点重载
  268. $nodes->each(function ($n) use ($node) {
  269. dispatch(static function () use ($n, $node) {
  270. $ret = ['nodeId' => $n->id];
  271. try {
  272. $ret += (new reloadNode($n))->handle();
  273. } catch (Exception $e) {
  274. Log::error("节点 [{$n->id}] 重载失败: ".$e->getMessage());
  275. $ret += ['error' => $e->getMessage()];
  276. }
  277. broadcast(new NodeActions('reload', $ret, $node?->id));
  278. });
  279. });
  280. return response()->json([
  281. 'status' => 'success',
  282. 'message' => trans('common.success_item', [
  283. 'attribute' => $node ? trans('admin.node.reload') : trans('admin.node.reload_all'),
  284. ]),
  285. ]);
  286. }
  287. public function nodeMonitor(Node $node): View
  288. { // 节点流量监控
  289. return view('admin.node.monitor', ['nodeName' => $node->name, 'nodeServer' => $node->server, ...$this->DataFlowChart($node->id, true)]);
  290. }
  291. }