NodeController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Helpers\DataChart;
  4. use App\Helpers\ProxyConfig;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Requests\Admin\NodeRequest;
  7. use App\Jobs\VNet\reloadNode;
  8. use App\Models\Country;
  9. use App\Models\Label;
  10. use App\Models\Level;
  11. use App\Models\Node;
  12. use App\Models\NodeCertificate;
  13. use App\Models\RuleGroup;
  14. use App\Utils\NetworkDetection;
  15. use Arr;
  16. use Exception;
  17. use Illuminate\Contracts\View\View;
  18. use Illuminate\Http\JsonResponse;
  19. use Illuminate\Http\RedirectResponse;
  20. use Illuminate\Http\Request;
  21. use Log;
  22. class NodeController extends Controller
  23. {
  24. use DataChart, ProxyConfig;
  25. public function index(Request $request): View
  26. { // 节点列表
  27. $query = Node::whereNull('relay_node_id')
  28. ->with([
  29. 'dailyDataFlows' => function ($query) {
  30. $query->whereBetween('created_at', [now()->startOfMonth(), now()]);
  31. },
  32. 'hourlyDataFlows' => function ($query) {
  33. $query->whereDate('created_at', now()->toDateString());
  34. },
  35. 'latestOnlineLog',
  36. 'latestHeartbeat',
  37. 'childNodes',
  38. ]);
  39. $request->whenFilled('status', function ($value) use ($query) {
  40. $query->where('status', $value);
  41. });
  42. $nodeList = $query->orderByDesc('sort')->orderBy('id')->paginate(15)->appends($request->except('page'))->through(function ($node) {
  43. // 预处理每个节点的数据
  44. $node->online_users = $node->latestOnlineLog?->online_user; // 在线人数
  45. // 计算流量总和
  46. $dailyTransfer = $node->dailyDataFlows->sum(fn ($item) => $item->u + $item->d);
  47. $hourlyTransfer = $node->hourlyDataFlows->sum(fn ($item) => $item->u + $item->d);
  48. $node->transfer = formatBytes($dailyTransfer + $hourlyTransfer); // 已产生流量
  49. $node_info = $node->latestHeartbeat; // 近期负载
  50. $node->isOnline = ! empty($node_info?->load);
  51. $node->load = $node_info?->load ?? false;
  52. $node->uptime = formatTime($node_info?->uptime);
  53. return $node;
  54. });
  55. return view('admin.node.index', compact('nodeList'));
  56. }
  57. public function store(NodeRequest $request): JsonResponse
  58. { // 添加节点
  59. try {
  60. if ($node = Node::create($this->nodeStore($request->validated()))) {
  61. if ($request->has('labels')) { // 生成节点标签
  62. $node->labels()->attach($request->input('labels'));
  63. }
  64. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]);
  65. }
  66. } catch (Exception $e) {
  67. Log::error(trans('common.error_action_item', ['action' => trans('common.add'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  68. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')]).', '.$e->getMessage()]);
  69. }
  70. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]);
  71. }
  72. public function create(): View
  73. {
  74. return view('admin.node.info', [
  75. 'nodes' => Node::orderBy('id')->pluck('id', 'name'),
  76. 'countries' => Country::orderBy('code')->get(),
  77. 'levels' => Level::orderBy('level')->pluck('name', 'level'),
  78. 'ruleGroups' => RuleGroup::orderBy('id')->pluck('name', 'id'),
  79. 'labels' => Label::orderByDesc('sort')->orderBy('id')->pluck('name', 'id'),
  80. 'certs' => NodeCertificate::orderBy('id')->pluck('domain', 'id'),
  81. ...$this->proxyConfigOptions(),
  82. ]);
  83. }
  84. private function nodeStore(array $info): array
  85. { // 添加节点信息
  86. switch ($info['type']) {
  87. case 0:
  88. $profile = ['method' => $info['method']];
  89. break;
  90. case 2:
  91. $profile = [
  92. 'method' => $info['v2_method'],
  93. 'v2_alter_id' => $info['v2_alter_id'],
  94. 'v2_net' => $info['v2_net'],
  95. 'v2_type' => $info['v2_type'],
  96. 'v2_host' => $info['v2_host'],
  97. 'v2_path' => $info['v2_path'],
  98. 'v2_tls' => $info['v2_tls'] ? 'tls' : '',
  99. 'v2_sni' => $info['v2_sni'],
  100. ];
  101. break;
  102. case 3:
  103. $profile = [
  104. 'allow_insecure' => false,
  105. ];
  106. break;
  107. case 1:
  108. case 4:
  109. $profile = [
  110. 'method' => $info['method'],
  111. 'protocol' => $info['protocol'],
  112. 'obfs' => $info['obfs'],
  113. 'obfs_param' => $info['obfs_param'],
  114. 'protocol_param' => $info['protocol_param'],
  115. 'passwd' => $info['passwd'],
  116. ];
  117. break;
  118. }
  119. $details = [
  120. 'next_renewal_date' => $info['next_renewal_date'],
  121. 'subscription_term' => $info['subscription_term'],
  122. 'renewal_cost' => $info['renewal_cost'],
  123. ];
  124. array_clean($details);
  125. return [
  126. 'type' => $info['type'],
  127. 'name' => $info['name'],
  128. 'country_code' => $info['country_code'],
  129. 'server' => $info['server'],
  130. 'ip' => $info['ip'],
  131. 'ipv6' => $info['ipv6'],
  132. 'level' => $info['level'],
  133. 'rule_group_id' => $info['rule_group_id'],
  134. 'speed_limit' => $info['speed_limit'],
  135. 'client_limit' => $info['client_limit'],
  136. 'details' => ! empty($details) ? $details : null,
  137. 'description' => $info['description'],
  138. 'profile' => $profile ?? [],
  139. 'traffic_rate' => $info['traffic_rate'],
  140. 'is_udp' => $info['is_udp'],
  141. 'is_display' => $info['is_display'],
  142. 'is_ddns' => $info['is_ddns'],
  143. 'relay_node_id' => $info['relay_node_id'],
  144. 'port' => $info['port'] ?? 0,
  145. 'push_port' => $info['push_port'],
  146. 'detection_type' => $info['detection_type'],
  147. 'sort' => $info['sort'],
  148. 'status' => $info['status'],
  149. ];
  150. }
  151. public function clone(Node $node): RedirectResponse
  152. { // 克隆节点
  153. $clone = [
  154. 'name' => $node->name.'_'.trans('admin.clone'),
  155. 'server' => null,
  156. ];
  157. if ($node->is_ddns) {
  158. $clone['ip'] = '1.1.1.1';
  159. $clone['is_ddns'] = 0;
  160. }
  161. $new = $node->replicate()->fill($clone);
  162. $new->save();
  163. return redirect(route('admin.node.edit', $new));
  164. }
  165. public function edit(Node $node): View
  166. { // 编辑节点页面
  167. $node->load('labels:id');
  168. $nodeArray = $node->toArray();
  169. return view('admin.node.info', [
  170. 'node' => array_merge(
  171. Arr::except($nodeArray, ['details', 'profile']),
  172. $nodeArray['details'] ?? [],
  173. $nodeArray['profile'] ?? [],
  174. ['labels' => $node->labels->pluck('id')->toArray()]// 将标签ID列表作为一维数组
  175. ),
  176. 'nodes' => Node::whereNotIn('id', [$node->id])->orderBy('id')->pluck('id', 'name'),
  177. 'countries' => Country::orderBy('code')->get(),
  178. 'levels' => Level::orderBy('level')->pluck('name', 'level'),
  179. 'ruleGroups' => RuleGroup::orderBy('id')->pluck('name', 'id'),
  180. 'labels' => Label::orderByDesc('sort')->orderBy('id')->pluck('name', 'id'),
  181. 'certs' => NodeCertificate::orderBy('id')->pluck('domain', 'id'),
  182. ...$this->proxyConfigOptions(),
  183. ]);
  184. }
  185. public function update(NodeRequest $request, Node $node): JsonResponse
  186. { // 编辑节点
  187. try {
  188. if ($node->update($this->nodeStore($request->validated()))) {
  189. // 更新节点标签
  190. $node->labels()->sync($request->input('labels'));
  191. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
  192. }
  193. } catch (Exception $e) {
  194. Log::error(trans('common.error_action_item', ['action' => trans('common.edit'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  195. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')]).', '.$e->getMessage()]);
  196. }
  197. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
  198. }
  199. public function destroy(Node $node): JsonResponse
  200. { // 删除节点
  201. try {
  202. if ($node->delete()) {
  203. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  204. }
  205. } catch (Exception $e) {
  206. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('model.node.attribute')]).': '.$e->getMessage());
  207. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
  208. }
  209. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  210. }
  211. public function checkNode(Node $node): JsonResponse
  212. { // 节点IP阻断检测
  213. foreach ($node->ips() as $ip) {
  214. $status = (new NetworkDetection)->networkStatus($ip, $node->port ?? 22);
  215. $data[$ip] = [trans("admin.network_status.{$status['icmp']}"), trans("admin.network_status.{$status['tcp']}")];
  216. }
  217. return response()->json(['status' => 'success', 'title' => '['.$node->name.'] '.trans('admin.node.connection_test'), 'message' => $data ?? []]);
  218. }
  219. public function refreshGeo(?int $id = null): JsonResponse
  220. { // 刷新节点地理位置
  221. $ret = false;
  222. if ($id) {
  223. $node = Node::findOrFail($id);
  224. $ret = $node->refresh_geo();
  225. } else {
  226. foreach (Node::whereStatus(1)->get() as $node) {
  227. $result = $node->refresh_geo();
  228. if ($result && ! $ret) {
  229. $ret = true;
  230. }
  231. }
  232. }
  233. if ($ret) {
  234. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.update')])]);
  235. }
  236. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.update')])]);
  237. }
  238. public function reload(?int $id = null): JsonResponse
  239. { // 重载节点
  240. $ret = false;
  241. if ($id) {
  242. $node = Node::findOrFail($id);
  243. $ret = (new reloadNode($node))->handle();
  244. } else {
  245. foreach (Node::whereStatus(1)->whereType(4)->get() as $node) {
  246. $result = (new reloadNode($node))->handle();
  247. if ($result && ! $ret) {
  248. $ret = true;
  249. }
  250. }
  251. }
  252. if ($ret) {
  253. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('admin.node.reload')])]);
  254. }
  255. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('admin.node.reload')])]);
  256. }
  257. public function nodeMonitor(Node $node): View
  258. { // 节点流量监控
  259. return view('admin.node.monitor', ['nodeName' => $node->name, 'nodeServer' => $node->server, ...$this->DataFlowChart($node->id, true)]);
  260. }
  261. }