ToolsController.php 6.8 KB

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