From 53e817431c929a341deda753b3969bb2356c6196 Mon Sep 17 00:00:00 2001 From: GeekQu Date: Fri, 10 Jul 2020 03:24:16 +0000 Subject: [PATCH] chore(clash): update some fields --- app/Controllers/ConfController.php | 30 +++++++++++++++----- app/Controllers/LinkController.php | 16 +++++++++++ resources/conf/clash.tpl | 7 +++-- resources/conf/rule/ConnersHua_BacktoCN.yaml | 1 - resources/conf/rule/ConnersHua_Pro.yaml | 1 - resources/conf/rule/lhie1_Rule.yaml | 3 +- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/app/Controllers/ConfController.php b/app/Controllers/ConfController.php index 6e017a2a..6badd3aa 100644 --- a/app/Controllers/ConfController.php +++ b/app/Controllers/ConfController.php @@ -358,6 +358,17 @@ class ConfController extends BaseController */ public static function getClashConfs($User, $AllProxys, $Configs, $local = false) { + $confStr = [ + 'Proxy' => 'proxies', + 'ProxyGroup' => 'proxy-groups', + ]; + if (isset($_GET['newconf']) && (int) $_GET['newconf'] == 0) { + $confStr = [ + 'Proxy' => 'Proxy', + 'ProxyGroup' => 'Proxy Group', + ]; + } + if (isset($Configs['Proxy']) || count($Configs['Proxy']) != 0) { $tmpProxys = array_merge($AllProxys, $Configs['Proxy']); } else { @@ -369,14 +380,14 @@ class ConfController extends BaseController $Proxys[] = $Proxy; } $tmp = self::getClashConfGeneral($Configs['General']); - $tmp['Proxy'] = $Proxys; + $tmp[$confStr['Proxy']] = $Proxys; if (isset($Configs['ProxyGroup'])) { - $tmp['Proxy Group'] = self::getClashConfProxyGroup( + $tmp[$confStr['ProxyGroup']] = self::getClashConfProxyGroup( $AllProxys, $Configs['ProxyGroup'] ); } else { - $tmp['Proxy Group'] = self::getClashConfProxyGroup( + $tmp[$confStr['ProxyGroup']] = self::getClashConfProxyGroup( $AllProxys, $Configs['Proxy Group'] ); @@ -475,14 +486,19 @@ class ConfController extends BaseController */ public static function getClashConfRule($Rules, $local) { + $confStr = 'rules'; + if (isset($_GET['newconf']) && (int) $_GET['newconf'] == 0) { + $confStr = 'Rule'; + } + $return = ($confStr . ':' . PHP_EOL); + // 加载本地规则文件 if ($local) { $render = ConfRender::getTemplateRender(); - return $render->fetch(trim($Rules['source'])); + return $return . $render->fetch(trim($Rules['source'])); } // 加载远程规则文件 - $return = ('Rule:' . PHP_EOL); if (isset($Rules['source']) && $Rules['source'] != '') { $sourceURL = trim($Rules['source']); // 远程规则仅支持 github 以及 gitlab @@ -500,10 +516,10 @@ class ConfController extends BaseController } catch (ParseException $exception) { $sourceRule['error'] = printf('无法解析 YAML 字符串: %s', $exception->getMessage()); } - if (isset($sourceRule['error']) || !isset($sourceRule['Rule'])) { + if (isset($sourceRule['error']) || !isset($sourceRule[$confStr])) { $return .= $sourceContent; } else { - $return .= Yaml::dump($sourceRule['Rule'], 4, 2); + $return .= Yaml::dump($sourceRule[$confStr], 4, 2); } } } else { diff --git a/app/Controllers/LinkController.php b/app/Controllers/LinkController.php index 2e3fc6ac..90d500ab 100644 --- a/app/Controllers/LinkController.php +++ b/app/Controllers/LinkController.php @@ -964,9 +964,25 @@ class LinkController extends BaseController $ProxyGroups = ConfController::getClashProxyGroup2String($ProxyGroups); } + $confStr = [ + 'mode' => 'rule', + 'Proxy' => 'proxies', + 'ProxyGroup' => 'proxy-groups', + 'Rule' => 'rules', + ]; + if (isset($opts['newconf']) && (int) $opts['newconf'] == 0) { + $confStr = [ + 'mode' => 'Rule', + 'Proxy' => 'Proxy', + 'ProxyGroup' => 'Proxy Group', + 'Rule' => 'Rule', + ]; + } + $render = ConfRender::getTemplateRender(); $render->assign('user', $user) ->assign('userapiUrl', $userapiUrl) + ->assign('confStr', $confStr) ->assign('opts', $opts) ->assign('Proxys', $Proxys) ->assign('ProxyGroups', $ProxyGroups) diff --git a/resources/conf/clash.tpl b/resources/conf/clash.tpl index 54405cc5..c6ff166f 100644 --- a/resources/conf/clash.tpl +++ b/resources/conf/clash.tpl @@ -17,7 +17,7 @@ redir-port: {if array_key_exists("redir-port",$opts)}{$opts['redir-port']}{else} allow-lan: true # 规则模式:Rule(规则) / Global(全局代理)/ Direct(全局直连) -mode: Rule +mode: {$confStr['mode']} # 设置日志输出级别 (默认级别:silent,即不输出任何内容,以避免因日志内容过大而导致程序内存溢出)。 # 5 个级别:silent / info / warning / error / debug。级别越高日志输出量越大,越倾向于调试,若需要请自行开启。 @@ -56,15 +56,16 @@ secret: '{if array_key_exists("secret",$opts)}{$opts['secret']}{else}{/if}' # (2) 如果您不在乎可能解析到污染的结果,更加追求速度。请将 nameserver 列表的服务器插入至 fallback 列表内,并移除重复项。 {/if} -Proxy: +{$confStr['Proxy']}: {foreach $Proxys as $Proxy} - {json_encode($Proxy,320)} {/foreach} -Proxy Group: +{$confStr['ProxyGroup']}: {$ProxyGroups} # 规则 +{$confStr['Rule']}: {if $Profiles == 'ConnersHua_Pro'} {include file='rule/ConnersHua_Pro.yaml'} {elseif $Profiles == 'ConnersHua_BacktoCN'} diff --git a/resources/conf/rule/ConnersHua_BacktoCN.yaml b/resources/conf/rule/ConnersHua_BacktoCN.yaml index 1c089d91..f6220a85 100644 --- a/resources/conf/rule/ConnersHua_BacktoCN.yaml +++ b/resources/conf/rule/ConnersHua_BacktoCN.yaml @@ -1,4 +1,3 @@ -Rule: # (Video) # AcFun # USER-AGENT,AcFun*,PROXY diff --git a/resources/conf/rule/ConnersHua_Pro.yaml b/resources/conf/rule/ConnersHua_Pro.yaml index 9661fe2a..a46e6a83 100644 --- a/resources/conf/rule/ConnersHua_Pro.yaml +++ b/resources/conf/rule/ConnersHua_Pro.yaml @@ -1,4 +1,3 @@ -Rule: # Feeb - DOMAIN-KEYWORD,epochtimes,REJECT - DOMAIN-SUFFIX,881903.com,REJECT diff --git a/resources/conf/rule/lhie1_Rule.yaml b/resources/conf/rule/lhie1_Rule.yaml index a2aa90d3..cffba441 100644 --- a/resources/conf/rule/lhie1_Rule.yaml +++ b/resources/conf/rule/lhie1_Rule.yaml @@ -1,4 +1,3 @@ -Rule: # (GlobalTV) # > ABC - DOMAIN-SUFFIX,edgedatg.com,GlobalTV @@ -798,4 +797,4 @@ Rule: # GeoIP China - GEOIP,CN,Domestic -- MATCH,Others \ No newline at end of file +- MATCH,Others