diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 0f8ade62..db7316c2 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -476,19 +476,19 @@ class UserController extends BaseController } $array_node = array(); - $array_node['id']=$node->id; - $array_node['class']=$node->node_class; - $array_node['name']=$node->name; + $array_node['id'] = $node->id; + $array_node['class'] = $node->node_class; + $array_node['name'] = $node->name; if ($node->sort == 13) { $server = Tools::ssv2Array($node->server); $array_node['server'] = $server['add']; } else { $array_node['server'] = $node->server; } - $array_node['sort']=$node->sort; - $array_node['info']=$node->info; - $array_node['mu_only']=$node->mu_only; - $array_node['group']=$node->node_group; + $array_node['sort'] = $node->sort; + $array_node['info'] = $node->info; + $array_node['mu_only'] = $node->mu_only; + $array_node['group'] = $node->node_group; $array_node['raw_node'] = $node; $regex = Config::get('flag_regex'); @@ -519,7 +519,7 @@ class UserController extends BaseController // 0: new node; -1: offline; 1: online $node_heartbeat = $node->node_heartbeat + 300; $array_node['online'] = -1; - if (!in_array($sort, array(0, 7, 8, 10, 11, 12, 13)) || $node_heartbeat == 300 ) { + if (!in_array($sort, array(0, 7, 8, 10, 11, 12, 13)) || $node_heartbeat == 300) { $array_node['online'] = 0; } elseif ($node_heartbeat > time()) { $array_node['online'] = 1; @@ -533,8 +533,8 @@ class UserController extends BaseController } } - $array_node['traffic_used'] = (int)Tools::flowToGB($node->node_bandwidth); - $array_node['traffic_limit'] = (int)Tools::flowToGB($node->node_bandwidth_limit); + $array_node['traffic_used'] = (int) Tools::flowToGB($node->node_bandwidth); + $array_node['traffic_limit'] = (int) Tools::flowToGB($node->node_bandwidth_limit); if ($node->node_speedlimit == 0.0) { $array_node['bandwidth'] = 0; } elseif ($node->node_speedlimit >= 1024.00) { @@ -556,7 +556,7 @@ class UserController extends BaseController ->assign('user', $user) ->registerClass('URL', URL::class) ->display('user/node.tpl'); - } + } public function node_old($request, $response, $args) @@ -572,7 +572,7 @@ class UserController extends BaseController $node_prefix = array(); $node_flag_file = array(); $node_method = array(); - $a = 0;//命名的什么JB变量 + $a = 0; //命名的什么JB变量 $node_order = array(); $node_alive = array(); $node_prealive = array(); @@ -628,9 +628,9 @@ class UserController extends BaseController } if ($node->node_bandwidth_limit == 0) { - $node_bandwidth[$name_cheif] = (int)($node->node_bandwidth / 1024 / 1024 / 1024) . ' GB 已用'; + $node_bandwidth[$name_cheif] = (int) ($node->node_bandwidth / 1024 / 1024 / 1024) . ' GB 已用'; } else { - $node_bandwidth[$name_cheif] = (int)($node->node_bandwidth / 1024 / 1024 / 1024) . ' GB / ' . (int)($node->node_bandwidth_limit / 1024 / 1024 / 1024) . ' GB - ' . $node->bandwidthlimit_resetday . ' 日重置'; + $node_bandwidth[$name_cheif] = (int) ($node->node_bandwidth / 1024 / 1024 / 1024) . ' GB / ' . (int) ($node->node_bandwidth_limit / 1024 / 1024 / 1024) . ' GB - ' . $node->bandwidthlimit_resetday . ' 日重置'; } if ($node_tempalive != '暂无数据') { @@ -649,7 +649,7 @@ class UserController extends BaseController $nodeLoad = $node->getNodeLoad(); if (isset($nodeLoad[0]['load'])) { - $node_latestload[$name_cheif] = ((float)(explode(' ', $nodeLoad[0]['load']))[0]) * 100; + $node_latestload[$name_cheif] = ((float) (explode(' ', $nodeLoad[0]['load']))[0]) * 100; } else { $node_latestload[$name_cheif] = null; } @@ -664,8 +664,8 @@ class UserController extends BaseController } } } - $node_prefix = (object)$node_prefix; - $node_order = (object)$node_order; + $node_prefix = (object) $node_prefix; + $node_order = (object) $node_order; $tools = new Tools(); return $this->view()->assign('relay_rules', $relay_rules)->assign('node_class', $node_class)->assign('node_isv6', $node_isv6)->assign('tools', $tools)->assign('node_method', $node_method)->assign('node_muport', $node_muport)->assign('node_bandwidth', $node_bandwidth)->assign('node_heartbeat', $node_heartbeat)->assign('node_prefix', $node_prefix)->assign('node_flag_file', $node_flag_file)->assign('node_prealive', $node_prealive)->assign('node_order', $node_order)->assign('user', $user)->assign('node_alive', $node_alive)->assign('node_latestload', $node_latestload)->registerClass('URL', URL::class)->display('user/node.tpl'); } @@ -752,11 +752,11 @@ class UserController extends BaseController foreach ($totallogin as $single) { //if(isset($useripcount[$single->userid])) { - if (!isset($userloginip[$single->ip])) { - //$useripcount[$single->userid]=$useripcount[$single->userid]+1; - $location = $iplocation->getlocation($single->ip); - $userloginip[$single->ip] = iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); - } + if (!isset($userloginip[$single->ip])) { + //$useripcount[$single->userid]=$useripcount[$single->userid]+1; + $location = $iplocation->getlocation($single->ip); + $userloginip[$single->ip] = iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); + } } } @@ -765,16 +765,16 @@ class UserController extends BaseController { $single->ip = Tools::getRealIp($single->ip); $is_node = Node::where('node_ip', $single->ip)->first(); - if ($is_node) { - continue; - } + if ($is_node) { + continue; + } - if (!isset($userip[$single->ip])) { - //$useripcount[$single->userid]=$useripcount[$single->userid]+1; - $location = $iplocation->getlocation($single->ip); - $userip[$single->ip] = iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); - } + if (!isset($userip[$single->ip])) { + //$useripcount[$single->userid]=$useripcount[$single->userid]+1; + $location = $iplocation->getlocation($single->ip); + $userip[$single->ip] = iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); + } } } @@ -1245,8 +1245,7 @@ class UserController extends BaseController try { Mail::send($to, $subject, 'news/warn.tpl', [ 'user' => $user, 'text' => $text - ], [ - ]); + ], []); } catch (Exception $e) { echo $e->getMessage(); } @@ -1262,11 +1261,11 @@ class UserController extends BaseController ) ); $opts = array('http' => - array( - 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'content' => $postdata - )); + array( + 'method' => 'POST', + 'header' => 'Content-type: application/x-www-form-urlencoded', + 'content' => $postdata + )); $context = stream_context_create($opts); file_get_contents('https://sc.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context); } @@ -1312,8 +1311,7 @@ class UserController extends BaseController try { Mail::send($to, $subject, 'news/warn.tpl', [ 'user' => $user, 'text' => $text - ], [ - ]); + ], []); } catch (Exception $e) { echo $e->getMessage(); } @@ -1328,11 +1326,11 @@ class UserController extends BaseController ) ); $opts = array('http' => - array( - 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'content' => $postdata - )); + array( + 'method' => 'POST', + 'header' => 'Content-type: application/x-www-form-urlencoded', + 'content' => $postdata + )); $context = stream_context_create($opts); file_get_contents('https://sc.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context); $useScFtqq = Config::get('ScFtqq_SCKEY'); @@ -1347,8 +1345,7 @@ class UserController extends BaseController try { Mail::send($to, $subject, 'news/warn.tpl', [ 'user' => $user, 'text' => $text - ], [ - ]); + ], []); } catch (Exception $e) { echo $e->getMessage(); } @@ -1363,11 +1360,11 @@ class UserController extends BaseController ) ); $opts = array('http' => - array( - 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'content' => $postdata - )); + array( + 'method' => 'POST', + 'header' => 'Content-type: application/x-www-form-urlencoded', + 'content' => $postdata + )); $context = stream_context_create($opts); file_get_contents('https://sc.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context); } @@ -1887,20 +1884,20 @@ class UserController extends BaseController $return = ''; switch ($type) { case 'ss': - $return .= URL::getAllUrl($user, 0, 1).PHP_EOL; - break; + $return .= URL::getAllUrl($user, 0, 1) . PHP_EOL; + break; case 'ssr': - $return .= URL::getAllUrl($user, 0, 0).PHP_EOL; - break; + $return .= URL::getAllUrl($user, 0, 0) . PHP_EOL; + break; case 'ssd': - $return .= URL::getAllSSDUrl($user).PHP_EOL; - break; + $return .= URL::getAllSSDUrl($user) . PHP_EOL; + break; case 'v2ray': - $return .= URL::getAllVMessUrl($user).PHP_EOL; - break; + $return .= URL::getAllVMessUrl($user) . PHP_EOL; + break; default: $return .= '悟空别闹!'; - break; + break; } $newResponse = $response->withHeader('Content-type', ' application/octet-stream; charset=utf-8')->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate')->withHeader('Content-Disposition', ' attachment; filename=node.txt'); $newResponse->getBody()->write($return); @@ -1922,20 +1919,38 @@ class UserController extends BaseController { $zipArc = new \ZipArchive(); $user_token = LinkController::GenerateSSRSubCode($this->user->id, 0); - if ($request->getQueryParams()['type'] == 'ssr-win') { - $file = '../storage/ssr_client_' . $user_token . '.zip'; - $zipArc->open($file, \ZipArchive::CREATE); - $zipArc->addFromString ('gui-config.json', LinkController::getSSRPcConf($this->user)); - $zipArc->addGlob('../resources/clients/ssr-win/*', GLOB_BRACE, ['add_path' => '/', 'remove_all_path' => true]); - $zipArc->close(); - $newResponse = $response->withHeader('Content-type', ' application/octet-stream')->withHeader('Content-Disposition', ' attachment; filename=ssr-win.zip'); - $newResponse->getBody()->write(file_get_contents($file)); - unlink($file); - } else { - return 'gg'; + $type = trim($request->getQueryParams()['type']); + // 临时文件存放路径 + $temp_file_path = '../storage/'; + // 客户端文件存放路径 + $client_path = '../resources/clients/'; + switch ($type) { + case 'ssr-win': + $temp_file_path .= $type . '_' . $user_token . '.zip'; + $user_config_file_name = 'gui-config.json'; + $content = LinkController::getSSRPcConf($this->user); + $client_path .= $type . '/'; + break; + default: + return 'gg'; + } + // 文件存在则先删除 + if (is_file($temp_file_path)) { + unlink($temp_file_path); } + // 超链接文件内容 + $site_url_content = '[InternetShortcut]' . PHP_EOL . 'URL=' . Config::get('baseUrl'); + // 创建 zip 并添加内容 + $zipArc->open($temp_file_path, \ZipArchive::CREATE); + $zipArc->addFromString($user_config_file_name, $content); + $zipArc->addFromString('点击访问_' . Config::get('appName') . '.url', $site_url_content); + Tools::folderToZip($client_path, $zipArc, strlen($client_path)); + $zipArc->close(); + + $newResponse = $response->withHeader('Content-type', ' application/octet-stream')->withHeader('Content-Disposition', ' attachment; filename=' . $type . '.zip'); + $newResponse->getBody()->write(file_get_contents($temp_file_path)); + unlink($temp_file_path); return $newResponse; } - } diff --git a/app/Utils/Tools.php b/app/Utils/Tools.php index 6dfd214c..34557194 100644 --- a/app/Utils/Tools.php +++ b/app/Utils/Tools.php @@ -450,10 +450,9 @@ class Tools if ($server[1] == '0' || $server[1] == '') { $item['port'] = 443; } else { - $item['port'] = (int)$server[1]; - + $item['port'] = (int) $server[1]; } - $item['aid'] = (int)$server[2]; + $item['aid'] = (int) $server[2]; $item['net'] = 'tcp'; $item['type'] = 'none'; if (count($server) >= 4) { @@ -480,7 +479,7 @@ class Tools unset($item['server']); } if (array_key_exists('outside_port', $item)) { - $item['port'] = (int)$item['outside_port']; + $item['port'] = (int) $item['outside_port']; unset($item['outside_port']); } if (isset($item['inside_port'])) { @@ -509,7 +508,7 @@ class Tools if ($server[1] == '0' || $server[1] == '') { $item['port'] = 443; } else { - $item['port'] = (int)$server[1]; + $item['port'] = (int) $server[1]; } if (count($server) >= 4) { $item['net'] = $server[3]; @@ -531,7 +530,7 @@ class Tools unset($item['server']); } if (array_key_exists('outside_port', $item)) { - $item['port'] = (int)$item['outside_port']; + $item['port'] = (int) $item['outside_port']; unset($item['outside_port']); } } @@ -768,6 +767,29 @@ class Tools : ($done['emoji'] . ' ' . $Name)); } - - + /** + * Add files and sub-directories in a folder to zip file. + * @param string $folder + * @param ZipArchive $zipFile + * @param int $exclusiveLength Number of text to be exclusived from the file path. + */ + public static function folderToZip($folder, &$zipFile, $exclusiveLength) + { + $handle = opendir($folder); + while (false !== $f = readdir($handle)) { + if ($f != '.' && $f != '..') { + $filePath = "$folder/$f"; + // Remove prefix from file path before add to zip. + $localPath = substr($filePath, $exclusiveLength); + if (is_file($filePath)) { + $zipFile->addFile($filePath, $localPath); + } elseif (is_dir($filePath)) { + // Add sub-directory. + $zipFile->addEmptyDir($localPath); + self::folderToZip($filePath, $zipFile, $exclusiveLength); + } + } + } + closedir($handle); + } }