ToolsController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. Session::flash('errorMsg', '请选择要上传的文件');
  109. return Redirect::back();
  110. }
  111. $file = $request->file('uploadFile');
  112. // 只能上传JSON文件
  113. if ($file->getClientMimeType() !== 'application/json' || $file->getClientOriginalExtension() !== 'json') {
  114. Session::flash('errorMsg', '只允许上传JSON文件');
  115. return Redirect::back();
  116. }
  117. if (! $file->isValid()) {
  118. Session::flash('errorMsg', '产生未知错误,请重新上传');
  119. return Redirect::back();
  120. }
  121. $save_path = realpath(storage_path('uploads'));
  122. $new_name = md5($file->getClientOriginalExtension()).'.json';
  123. $file->move($save_path, $new_name);
  124. // 读取文件内容
  125. $data = file_get_contents($save_path.'/'.$new_name);
  126. $data = json_decode($data, true);
  127. if (! $data) {
  128. Session::flash('errorMsg', '内容格式解析异常,请上传符合SSR(R)配置规范的JSON文件');
  129. return Redirect::back();
  130. }
  131. try {
  132. DB::beginTransaction();
  133. foreach ($data as $user) {
  134. $obj = new User();
  135. $obj->username = $user->user;
  136. $obj->email = $user->user;
  137. $obj->password = '123456';
  138. $obj->port = $user->port;
  139. $obj->passwd = $user->passwd;
  140. $obj->vmess_id = $user->uuid;
  141. $obj->transfer_enable = $user->transfer_enable;
  142. $obj->method = $user->method;
  143. $obj->protocol = $user->protocol;
  144. $obj->obfs = $user->obfs;
  145. $obj->expired_at = '2099-01-01';
  146. $obj->reg_ip = IP::getClientIp();
  147. $obj->created_at = date('Y-m-d H:i:s');
  148. $obj->updated_at = date('Y-m-d H:i:s');
  149. $obj->save();
  150. }
  151. DB::commit();
  152. } catch (Exception $e) {
  153. DB::rollBack();
  154. Session::flash('errorMsg', '出错了,可能是导入的配置中有端口已经存在了');
  155. return Redirect::back();
  156. }
  157. Session::flash('successMsg', '导入成功');
  158. return Redirect::back();
  159. }
  160. return view('admin.tools.import');
  161. }
  162. // 日志分析
  163. public function analysis()
  164. {
  165. $file = storage_path('app/ssserver.log');
  166. if (! file_exists($file)) {
  167. Session::flash('analysisErrorMsg', $file.' 不存在,请先创建文件');
  168. return view('admin.tools.analysis');
  169. }
  170. $logs = $this->tail($file, 10000);
  171. if (false === $logs) {
  172. $view['urlList'] = [];
  173. } else {
  174. $url = [];
  175. foreach ($logs as $log) {
  176. if (str_contains($log, 'TCP connecting')) {
  177. continue;
  178. }
  179. preg_match('/TCP request (\w+\.){2}\w+/', $log, $tcp_matches);
  180. if (! empty($tcp_matches)) {
  181. $url[] = str_replace('TCP request ', '[TCP] ', $tcp_matches[0]);
  182. } else {
  183. preg_match(
  184. '/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)/',
  185. $log,
  186. $udp_matches
  187. );
  188. if (! empty($udp_matches)) {
  189. $url[] = str_replace('UDP data to ', '[UDP] ', $udp_matches[0]);
  190. }
  191. }
  192. }
  193. $view['urlList'] = array_unique($url);
  194. }
  195. return view('admin.tools.analysis', $view);
  196. }
  197. }