ToolsController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use App\Utils\IP;
  6. use DB;
  7. use Exception;
  8. use Illuminate\Contracts\View\View;
  9. use Illuminate\Http\JsonResponse;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Log;
  13. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  14. class ToolsController extends Controller
  15. {
  16. public function decompile(Request $request): JsonResponse|View
  17. { // SS(R)链接反解析
  18. if ($request->isMethod('POST')) {
  19. $content = $request->input('content');
  20. if (empty($content)) {
  21. return response()->json(['status' => 'fail', 'message' => trans('admin.tools.decompile.content_placeholder')]);
  22. }
  23. // 反解析处理
  24. $content = str_replace("\n", ',', $content);
  25. $content = explode(',', $content);
  26. $txt = '';
  27. foreach ($content as $item) {
  28. // 判断是SS还是SSR链接
  29. $str = '';
  30. if (str_contains($item, 'ssr://')) {
  31. $str = mb_substr($item, 6);
  32. } elseif (str_contains($item, 'ss://')) {
  33. $str = mb_substr($item, 5);
  34. }
  35. $txt .= "\r\n".base64url_decode($str);
  36. }
  37. // 生成转换好的JSON文件
  38. //file_put_contents(public_path('downloads/decompile.json'), $txt);
  39. return response()->json(['status' => 'success', 'data' => $txt, 'message' => trans('common.success_item', ['attribute' => trans('admin.tools.decompile.attribute')])]);
  40. }
  41. return view('admin.tools.decompile');
  42. }
  43. public function convert(Request $request): JsonResponse|View
  44. { // 格式转换(SS转SSR)
  45. if ($request->isMethod('POST')) {
  46. $method = $request->input('method');
  47. $transfer_enable = $request->input('transfer_enable');
  48. $protocol = $request->input('protocol');
  49. $protocol_param = $request->input('protocol_param');
  50. $obfs = $request->input('obfs');
  51. $obfs_param = $request->input('obfs_param');
  52. $content = $request->input('content');
  53. if (empty($content)) {
  54. return response()->json(['status' => 'fail', 'message' => trans('admin.tools.convert.content_placeholder')]);
  55. }
  56. // 校验格式
  57. $content = json_decode($content, true);
  58. if (empty($content->port_password)) {
  59. return response()->json(['status' => 'fail', 'message' => trans('admin.tools.convert.missing_error')]);
  60. }
  61. // 转换成SSR格式JSON
  62. $data = [];
  63. foreach ($content->port_password as $port => $passwd) {
  64. $data[] = [
  65. 'u' => 0,
  66. 'd' => 0,
  67. 'enable' => 1,
  68. 'method' => $method,
  69. 'obfs' => $obfs,
  70. 'obfs_param' => empty($obfs_param) ? '' : $obfs_param,
  71. 'passwd' => $passwd,
  72. 'port' => $port,
  73. 'protocol' => $protocol,
  74. 'protocol_param' => empty($protocol_param) ? '' : $protocol_param,
  75. 'transfer_enable' => $transfer_enable,
  76. 'user' => date('Ymd').'_IMPORT_'.$port,
  77. ];
  78. }
  79. $json = json_encode($data);
  80. // 生成转换好的JSON文件
  81. file_put_contents(public_path('downloads/convert.json'), $json);
  82. return response()->json(['status' => 'success', 'data' => $json, 'message' => trans('common.success_item', ['attribute' => trans('common.convert')])]);
  83. }
  84. return view('admin.tools.convert');
  85. }
  86. public function download(Request $request): BinaryFileResponse
  87. { // 下载转换好的JSON文件
  88. $type = (int) $request->input('type');
  89. if (empty($type)) {
  90. abort(trans('admin.tools.convert.params_unknown'));
  91. }
  92. if ($type === 1) {
  93. $filePath = public_path('downloads/convert.json');
  94. } else {
  95. $filePath = public_path('downloads/decompile.json');
  96. }
  97. if (! file_exists($filePath)) {
  98. abort(trans('admin.tools.convert.file_missing'));
  99. }
  100. return response()->download($filePath);
  101. }
  102. public function import(Request $request): RedirectResponse|View
  103. { // 数据导入
  104. if ($request->isMethod('POST')) {
  105. if (! $request->hasFile('uploadFile')) {
  106. return redirect()->back()->withErrors(trans('admin.tools.import.file_required'));
  107. }
  108. $file = $request->file('uploadFile');
  109. // 只能上传JSON文件
  110. if ($file->getClientMimeType() !== 'application/json' || $file->getClientOriginalExtension() !== 'json') {
  111. return redirect()->back()->withErrors(trans('admin.tools.import.file_type_error', ['type' => 'JSON']));
  112. }
  113. if (! $file->isValid()) {
  114. return redirect()->back()->withErrors(trans('admin.tools.import.file_error'));
  115. }
  116. $save_path = realpath(storage_path('uploads'));
  117. $new_name = md5($file->getClientOriginalExtension()).'.json';
  118. $file->move($save_path, $new_name);
  119. // 读取文件内容
  120. $data = file_get_contents($save_path.'/'.$new_name);
  121. $data = json_decode($data, true);
  122. if (! $data) {
  123. return redirect()->back()->withErrors(trans('admin.tools.import.format_error', ['type' => 'JSON']));
  124. }
  125. try {
  126. DB::beginTransaction();
  127. foreach ($data as $user) {
  128. $obj = new User;
  129. $obj->nickname = $user->user;
  130. $obj->username = $user->user;
  131. $obj->password = '123456';
  132. $obj->port = $user->port;
  133. $obj->passwd = $user->passwd;
  134. $obj->vmess_id = $user->uuid;
  135. $obj->transfer_enable = $user->transfer_enable;
  136. $obj->method = $user->method;
  137. $obj->protocol = $user->protocol;
  138. $obj->obfs = $user->obfs;
  139. $obj->expired_at = '2099-01-01';
  140. $obj->reg_ip = IP::getClientIp();
  141. $obj->created_at = now();
  142. $obj->updated_at = now();
  143. $obj->save();
  144. }
  145. DB::commit();
  146. } catch (Exception $e) {
  147. DB::rollBack();
  148. Log::error(trans('common.error_action_item', ['action' => trans('common.import'), 'attribute' => trans('admin.menu.tools.import')]).': '.$e->getMessage());
  149. return redirect()->back()->withErrors(trans('common.failed_item', ['attribute' => trans('common.import')]).', '.$e->getMessage());
  150. }
  151. return redirect()->back()->with('successMsg', trans('common.success_item', ['attribute' => trans('common.import')]));
  152. }
  153. return view('admin.tools.import');
  154. }
  155. public function analysis(): View
  156. { // 日志分析
  157. $file = storage_path('app/ssserver.log');
  158. if (! file_exists($file)) {
  159. session()->flash('analysisErrorMsg', trans('admin.tools.analysis.file_missing', ['file_name' => $file]));
  160. return view('admin.tools.analysis');
  161. }
  162. $logs = $this->tail($file, 10000);
  163. if ($logs) {
  164. foreach ($logs as $log) {
  165. if (str_contains($log, 'TCP connecting')) {
  166. continue;
  167. }
  168. preg_match('/TCP request (\w+\.){2}\w+/', $log, $tcp_matches);
  169. if (! empty($tcp_matches)) {
  170. $url[] = str_replace('TCP request ', '[TCP] ', $tcp_matches[0]);
  171. } else {
  172. preg_match(
  173. '/UDP data to (25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)/',
  174. $log,
  175. $udp_matches
  176. );
  177. if (! empty($udp_matches)) {
  178. $url[] = str_replace('UDP data to ', '[UDP] ', $udp_matches[0]);
  179. }
  180. }
  181. }
  182. }
  183. return view('admin.tools.analysis', ['urlList' => array_unique($url ?? [])]);
  184. }
  185. private function tail(string $file, int $n, int $base = 5): array|false
  186. { // 类似Linux中的tail命令
  187. $fileLines = $this->countLine($file);
  188. if ($fileLines < 15000) {
  189. return false;
  190. }
  191. $fp = fopen($file, 'rb+');
  192. assert($n > 0);
  193. $pos = $n + 1;
  194. $lines = [];
  195. $counts = 0;
  196. while ($counts <= $n) {
  197. try {
  198. fseek($fp, -$pos, SEEK_END);
  199. } catch (Exception) {
  200. break;
  201. }
  202. $pos *= $base;
  203. while (! feof($fp)) {
  204. array_unshift($lines, fgets($fp));
  205. $counts++;
  206. }
  207. }
  208. return array_slice($lines, 0, $n);
  209. }
  210. private function countLine(string $file): int
  211. { // 计算文件行数
  212. $fp = fopen($file, 'rb');
  213. $i = 0;
  214. while (! feof($fp)) {
  215. //每次读取2M
  216. if ($data = fread($fp, 1024 * 1024 * 2)) {
  217. //计算读取到的行数
  218. $num = substr_count($data, "\n");
  219. $i += $num;
  220. }
  221. }
  222. fclose($fp);
  223. return $i;
  224. }
  225. }