ToolsController.php 9.3 KB

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