mirror of
				https://github.com/v2board/v2board.git
				synced 2025-10-31 17:31:49 +08:00 
			
		
		
		
	update: add clash meta
This commit is contained in:
		
							
								
								
									
										176
									
								
								app/Http/Controllers/Client/Protocols/ClashMeta.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								app/Http/Controllers/Client/Protocols/ClashMeta.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,176 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Http\Controllers\Client\Protocols; | ||||
|  | ||||
| use App\Utils\Helper; | ||||
| use Symfony\Component\Yaml\Yaml; | ||||
|  | ||||
| class ClashMeta | ||||
| { | ||||
|     public $flag = 'clashmeta'; | ||||
|     private $servers; | ||||
|     private $user; | ||||
|  | ||||
|     public function __construct($user, $servers) | ||||
|     { | ||||
|         $this->user = $user; | ||||
|         $this->servers = $servers; | ||||
|     } | ||||
|  | ||||
|     public function handle() | ||||
|     { | ||||
|         $servers = $this->servers; | ||||
|         $user = $this->user; | ||||
|         $appName = config('v2board.app_name', 'V2Board'); | ||||
|         header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}"); | ||||
|         header('profile-update-interval: 24'); | ||||
|         header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($appName)); | ||||
|         $defaultConfig = base_path() . '/resources/rules/default.clash.yaml'; | ||||
|         $customConfig = base_path() . '/resources/rules/custom.clash.yaml'; | ||||
|         if (\File::exists($customConfig)) { | ||||
|             $config = Yaml::parseFile($customConfig); | ||||
|         } else { | ||||
|             $config = Yaml::parseFile($defaultConfig); | ||||
|         } | ||||
|         $proxy = []; | ||||
|         $proxies = []; | ||||
|  | ||||
|         foreach ($servers as $item) { | ||||
|             if ($item['type'] === 'shadowsocks') { | ||||
|                 array_push($proxy, self::buildShadowsocks($user['uuid'], $item)); | ||||
|                 array_push($proxies, $item['name']); | ||||
|             } | ||||
|             if ($item['type'] === 'v2ray') { | ||||
|                 array_push($proxy, self::buildVmess($user['uuid'], $item)); | ||||
|                 array_push($proxies, $item['name']); | ||||
|             } | ||||
|             if ($item['type'] === 'trojan') { | ||||
|                 array_push($proxy, self::buildTrojan($user['uuid'], $item)); | ||||
|                 array_push($proxies, $item['name']); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy); | ||||
|         foreach ($config['proxy-groups'] as $k => $v) { | ||||
|             if (!is_array($config['proxy-groups'][$k]['proxies'])) $config['proxy-groups'][$k]['proxies'] = []; | ||||
|             $isFilter = false; | ||||
|             foreach ($config['proxy-groups'][$k]['proxies'] as $src) { | ||||
|                 foreach ($proxies as $dst) { | ||||
|                     if (!$this->isRegex($src)) continue; | ||||
|                     $isFilter = true; | ||||
|                     $config['proxy-groups'][$k]['proxies'] = array_values(array_diff($config['proxy-groups'][$k]['proxies'], [$src])); | ||||
|                     if ($this->isMatch($src, $dst)) { | ||||
|                         array_push($config['proxy-groups'][$k]['proxies'], $dst); | ||||
|                     } | ||||
|                 } | ||||
|                 if ($isFilter) continue; | ||||
|             } | ||||
|             if ($isFilter) continue; | ||||
|             $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies); | ||||
|         } | ||||
|         // Force the current subscription domain to be a direct rule | ||||
|         $subsDomain = $_SERVER['SERVER_NAME']; | ||||
|         $subsDomainRule = "DOMAIN,{$subsDomain},DIRECT"; | ||||
|         array_unshift($config['rules'], $subsDomainRule); | ||||
|  | ||||
|         $yaml = Yaml::dump($config); | ||||
|         $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml); | ||||
|         return $yaml; | ||||
|     } | ||||
|  | ||||
|     public static function buildShadowsocks($password, $server) | ||||
|     { | ||||
|         if ($server['cipher'] === '2022-blake3-aes-128-gcm') { | ||||
|             $serverKey = Helper::getShadowsocksServerKey($server['created_at'], 16); | ||||
|             $userKey = Helper::uuidToBase64($password, 16); | ||||
|             $password = "{$serverKey}:{$userKey}"; | ||||
|         } | ||||
|         if ($server['cipher'] === '2022-blake3-aes-256-gcm') { | ||||
|             $serverKey = Helper::getShadowsocksServerKey($server['created_at'], 32); | ||||
|             $userKey = Helper::uuidToBase64($password, 32); | ||||
|             $password = "{$serverKey}:{$userKey}"; | ||||
|         } | ||||
|         $array = []; | ||||
|         $array['name'] = $server['name']; | ||||
|         $array['type'] = 'ss'; | ||||
|         $array['server'] = $server['host']; | ||||
|         $array['port'] = $server['port']; | ||||
|         $array['cipher'] = $server['cipher']; | ||||
|         $array['password'] = $password; | ||||
|         $array['udp'] = true; | ||||
|         return $array; | ||||
|     } | ||||
|  | ||||
|     public static function buildVmess($uuid, $server) | ||||
|     { | ||||
|         $array = []; | ||||
|         $array['name'] = $server['name']; | ||||
|         $array['type'] = 'vmess'; | ||||
|         $array['server'] = $server['host']; | ||||
|         $array['port'] = $server['port']; | ||||
|         $array['uuid'] = $uuid; | ||||
|         $array['alterId'] = 0; | ||||
|         $array['cipher'] = 'auto'; | ||||
|         $array['udp'] = true; | ||||
|  | ||||
|         if ($server['tls']) { | ||||
|             $array['tls'] = true; | ||||
|             if ($server['tlsSettings']) { | ||||
|                 $tlsSettings = $server['tlsSettings']; | ||||
|                 if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure'])) | ||||
|                     $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false); | ||||
|                 if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName'])) | ||||
|                     $array['servername'] = $tlsSettings['serverName']; | ||||
|             } | ||||
|         } | ||||
|         if ($server['network'] === 'ws') { | ||||
|             $array['network'] = 'ws'; | ||||
|             if ($server['networkSettings']) { | ||||
|                 $wsSettings = $server['networkSettings']; | ||||
|                 $array['ws-opts'] = []; | ||||
|                 if (isset($wsSettings['path']) && !empty($wsSettings['path'])) | ||||
|                     $array['ws-opts']['path'] = $wsSettings['path']; | ||||
|                 if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host'])) | ||||
|                     $array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']]; | ||||
|                 if (isset($wsSettings['path']) && !empty($wsSettings['path'])) | ||||
|                     $array['ws-path'] = $wsSettings['path']; | ||||
|                 if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host'])) | ||||
|                     $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']]; | ||||
|             } | ||||
|         } | ||||
|         if ($server['network'] === 'grpc') { | ||||
|             $array['network'] = 'grpc'; | ||||
|             if ($server['networkSettings']) { | ||||
|                 $grpcSettings = $server['networkSettings']; | ||||
|                 $array['grpc-opts'] = []; | ||||
|                 if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName']; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return $array; | ||||
|     } | ||||
|  | ||||
|     public static function buildTrojan($password, $server) | ||||
|     { | ||||
|         $array = []; | ||||
|         $array['name'] = $server['name']; | ||||
|         $array['type'] = 'trojan'; | ||||
|         $array['server'] = $server['host']; | ||||
|         $array['port'] = $server['port']; | ||||
|         $array['password'] = $password; | ||||
|         $array['udp'] = true; | ||||
|         if (!empty($server['server_name'])) $array['sni'] = $server['server_name']; | ||||
|         if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false); | ||||
|         return $array; | ||||
|     } | ||||
|  | ||||
|     private function isMatch($exp, $str) | ||||
|     { | ||||
|         return @preg_match($exp, $str); | ||||
|     } | ||||
|  | ||||
|     private function isRegex($exp) | ||||
|     { | ||||
|         return @preg_match($exp, null) !== false; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user