ToolsController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Components\Helpers;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\User;
  6. use DB;
  7. use Exception;
  8. use Hash;
  9. use Illuminate\Http\Request;
  10. use Redirect;
  11. use Response;
  12. use Session;
  13. class ToolsController extends Controller
  14. {
  15. // SS(R)链接反解析
  16. public function decompile(Request $request)
  17. {
  18. if ($request->isMethod('POST')) {
  19. $content = $request->input('content');
  20. if (empty($content)) {
  21. return Response::json(
  22. ['status' => 'fail', 'message' => '请在左侧填入要反解析的SS(R)链接']
  23. );
  24. }
  25. // 反解析处理
  26. $content = str_replace("\n", ",", $content);
  27. $content = explode(',', $content);
  28. $txt = '';
  29. foreach ($content as $item) {
  30. // 判断是SS还是SSR链接
  31. $str = '';
  32. if (str_contains($item, 'ssr://')) {
  33. $str = mb_substr($item, 6);
  34. } elseif (str_contains($item, 'ss://')) {
  35. $str = mb_substr($item, 5);
  36. }
  37. $txt .= "\r\n" . base64url_decode($str);
  38. }
  39. // 生成转换好的JSON文件
  40. file_put_contents(public_path('downloads/decompile.json'), $txt);
  41. return Response::json(
  42. ['status' => 'success', 'data' => $txt, 'message' => '反解析成功']
  43. );
  44. }
  45. return view('admin.tools.decompile');
  46. }
  47. // 格式转换(SS转SSR)
  48. public function convert(Request $request)
  49. {
  50. if ($request->isMethod('POST')) {
  51. $method = $request->input('method');
  52. $transfer_enable = $request->input('transfer_enable');
  53. $protocol = $request->input('protocol');
  54. $protocol_param = $request->input('protocol_param');
  55. $obfs = $request->input('obfs');
  56. $obfs_param = $request->input('obfs_param');
  57. $content = $request->input('content');
  58. if (empty($content)) {
  59. return Response::json(
  60. ['status' => 'fail', 'message' => '请在左侧填入要转换的内容']
  61. );
  62. }
  63. // 校验格式
  64. $content = json_decode($content, true);
  65. if (empty($content->port_password)) {
  66. return Response::json(
  67. [
  68. 'status' => 'fail',
  69. 'message' => '转换失败:配置信息里缺少【port_password】字段,或者该字段为空',
  70. ]
  71. );
  72. }
  73. // 转换成SSR格式JSON
  74. $data = [];
  75. foreach ($content->port_password as $port => $passwd) {
  76. $data[] = [
  77. 'u' => 0,
  78. 'd' => 0,
  79. 'enable' => 1,
  80. 'method' => $method,
  81. 'obfs' => $obfs,
  82. 'obfs_param' => empty($obfs_param) ? "" : $obfs_param,
  83. 'passwd' => $passwd,
  84. 'port' => $port,
  85. 'protocol' => $protocol,
  86. 'protocol_param' => empty($protocol_param) ? "" : $protocol_param,
  87. 'transfer_enable' => toGB($transfer_enable),
  88. 'user' => date('Ymd') . '_IMPORT_' . $port,
  89. ];
  90. }
  91. $json = json_encode($data);
  92. // 生成转换好的JSON文件
  93. file_put_contents(public_path('downloads/convert.json'), $json);
  94. return Response::json(
  95. ['status' => 'success', 'data' => $json, 'message' => '转换成功']
  96. );
  97. }
  98. // 加密方式、协议、混淆
  99. $view['methodList'] = Helpers::methodList();
  100. $view['protocolList'] = Helpers::protocolList();
  101. $view['obfsList'] = Helpers::obfsList();
  102. return view('admin.tools.convert', $view);
  103. }
  104. // 下载转换好的JSON文件
  105. public function download(Request $request)
  106. {
  107. $type = $request->input('type');
  108. if (empty($type)) {
  109. exit('参数异常');
  110. }
  111. if ($type == '1') {
  112. $filePath = public_path('downloads/convert.json');
  113. } else {
  114. $filePath = public_path('downloads/decompile.json');
  115. }
  116. if ( ! file_exists($filePath)) {
  117. exit('文件不存在,请检查目录权限');
  118. }
  119. return Response::download($filePath);
  120. }
  121. // 数据导入
  122. public function import(Request $request)
  123. {
  124. if ($request->isMethod('POST')) {
  125. if ( ! $request->hasFile('uploadFile')) {
  126. Session::flash('errorMsg', '请选择要上传的文件');
  127. return Redirect::back();
  128. }
  129. $file = $request->file('uploadFile');
  130. // 只能上传JSON文件
  131. if ($file->getClientMimeType(
  132. ) !== 'application/json' || $file->getClientOriginalExtension(
  133. ) !== 'json') {
  134. Session::flash('errorMsg', '只允许上传JSON文件');
  135. return Redirect::back();
  136. }
  137. if ( ! $file->isValid()) {
  138. Session::flash('errorMsg', '产生未知错误,请重新上传');
  139. return Redirect::back();
  140. }
  141. $save_path = realpath(storage_path('uploads'));
  142. $new_name = md5($file->getClientOriginalExtension()) . '.json';
  143. $file->move($save_path, $new_name);
  144. // 读取文件内容
  145. $data = file_get_contents($save_path . '/' . $new_name);
  146. $data = json_decode($data, true);
  147. if ( ! $data) {
  148. Session::flash('errorMsg', '内容格式解析异常,请上传符合SSR(R)配置规范的JSON文件');
  149. return Redirect::back();
  150. }
  151. try {
  152. DB::beginTransaction();
  153. foreach ($data as $user) {
  154. $obj = new User();
  155. $obj->username = $user->user;
  156. $obj->email = $user->user;
  157. $obj->password = Hash::make('123456');
  158. $obj->port = $user->port;
  159. $obj->passwd = $user->passwd;
  160. $obj->vmess_id = $user->uuid;
  161. $obj->transfer_enable = $user->transfer_enable;
  162. $obj->method = $user->method;
  163. $obj->protocol = $user->protocol;
  164. $obj->obfs = $user->obfs;
  165. $obj->expired_at = '2099-01-01';
  166. $obj->reg_ip = getClientIp();
  167. $obj->created_at = date('Y-m-d H:i:s');
  168. $obj->updated_at = date('Y-m-d H:i:s');
  169. $obj->save();
  170. }
  171. DB::commit();
  172. } catch (Exception $e) {
  173. DB::rollBack();
  174. Session::flash('errorMsg', '出错了,可能是导入的配置中有端口已经存在了');
  175. return Redirect::back();
  176. }
  177. Session::flash('successMsg', '导入成功');
  178. return Redirect::back();
  179. }
  180. return view('admin.tools.import');
  181. }
  182. // 日志分析
  183. public function analysis()
  184. {
  185. $file = storage_path('app/ssserver.log');
  186. if ( ! file_exists($file)) {
  187. Session::flash('analysisErrorMsg', $file . ' 不存在,请先创建文件');
  188. return view('admin.tools.analysis');
  189. }
  190. $logs = $this->tail($file, 10000);
  191. if (false === $logs) {
  192. $view['urlList'] = [];
  193. } else {
  194. $url = [];
  195. foreach ($logs as $log) {
  196. if (str_contains($log, 'TCP connecting')) {
  197. continue;
  198. }
  199. preg_match('/TCP request (\w+\.){2}\w+/', $log, $tcp_matches);
  200. if ( ! empty($tcp_matches)) {
  201. $url[] = str_replace(
  202. 'TCP request ',
  203. '[TCP] ',
  204. $tcp_matches[0]
  205. );
  206. } else {
  207. preg_match(
  208. '/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)/',
  209. $log,
  210. $udp_matches
  211. );
  212. if ( ! empty($udp_matches)) {
  213. $url[] = str_replace(
  214. 'UDP data to ',
  215. '[UDP] ',
  216. $udp_matches[0]
  217. );
  218. }
  219. }
  220. }
  221. $view['urlList'] = array_unique($url);
  222. }
  223. return view('admin.tools.analysis', $view);
  224. }
  225. }