ToolsController.php 9.1 KB

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