ToolsController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Components\IP;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\User;
  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' => '请在左侧填入要反解析的SS(R)链接']);
  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' => '反解析成功']);
  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' => '请在左侧填入要转换的内容']);
  55. }
  56. // 校验格式
  57. $content = json_decode($content, true);
  58. if (empty($content->port_password)) {
  59. return Response::json(['status' => 'fail', 'message' => '转换失败:配置信息里缺少【port_password】字段,或者该字段为空']);
  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' => '转换成功']);
  83. }
  84. return view('admin.tools.convert');
  85. }
  86. // 下载转换好的JSON文件
  87. public function download(Request $request)
  88. {
  89. $type = $request->input('type');
  90. if (empty($type)) {
  91. exit('参数异常');
  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('文件不存在,请检查目录权限');
  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('请选择要上传的文件');
  109. }
  110. $file = $request->file('uploadFile');
  111. // 只能上传JSON文件
  112. if ($file->getClientMimeType() !== 'application/json' || $file->getClientOriginalExtension() !== 'json') {
  113. return Redirect::back()->withErrors('只允许上传JSON文件');
  114. }
  115. if (! $file->isValid()) {
  116. return Redirect::back()->withErrors('产生未知错误,请重新上传');
  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('内容格式解析异常,请上传符合SSR(R)配置规范的JSON文件');
  126. }
  127. try {
  128. DB::beginTransaction();
  129. foreach ($data as $user) {
  130. $obj = new User();
  131. $obj->username = $user->user;
  132. $obj->email = $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 = date('Y-m-d H:i:s');
  144. $obj->updated_at = date('Y-m-d H:i:s');
  145. $obj->save();
  146. }
  147. DB::commit();
  148. } catch (Exception $e) {
  149. DB::rollBack();
  150. return Redirect::back()->withErrors('出错了,可能是导入的配置中有端口已经存在了');
  151. }
  152. return Redirect::back()->with('successMsg', '导入成功');
  153. }
  154. return view('admin.tools.import');
  155. }
  156. // 日志分析
  157. public function analysis()
  158. {
  159. $file = storage_path('app/ssserver.log');
  160. if (! file_exists($file)) {
  161. Session::flash('analysisErrorMsg', $file.' 不存在,请先创建文件');
  162. return view('admin.tools.analysis');
  163. }
  164. $logs = $this->tail($file, 10000);
  165. if (false !== $logs) {
  166. foreach ($logs as $log) {
  167. if (str_contains($log, 'TCP connecting')) {
  168. continue;
  169. }
  170. preg_match('/TCP request (\w+\.){2}\w+/', $log, $tcp_matches);
  171. if (! empty($tcp_matches)) {
  172. $url[] = str_replace('TCP request ', '[TCP] ', $tcp_matches[0]);
  173. } else {
  174. preg_match(
  175. '/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)/',
  176. $log,
  177. $udp_matches
  178. );
  179. if (! empty($udp_matches)) {
  180. $url[] = str_replace('UDP data to ', '[UDP] ', $udp_matches[0]);
  181. }
  182. }
  183. }
  184. }
  185. return view('admin.tools.analysis', ['urlList' => array_unique($url ?? [])]);
  186. }
  187. }