dd/app/Services/Freenode/Renderer.php

59 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Freenode;
use Symfony\Component\Yaml\Yaml;
final class Renderer
{
public static function singbox(array $nodes): string
{
$tags = array_column($nodes, 'tag');
$config = [
'log' => ['level' => 'warn'],
'dns' => ['servers' => [['type' => 'local', 'tag' => 'local']], 'final' => 'local'],
'inbounds' => [
['type' => 'tun', 'tag' => 'tun-in', 'address' => ['172.19.0.1/30'], 'auto_route' => true, 'strict_route' => true],
['type' => 'mixed', 'tag' => 'mixed-in', 'listen' => '127.0.0.1', 'listen_port' => 7891],
],
'outbounds' => array_merge([
['type' => 'selector', 'tag' => 'proxy', 'outbounds' => $tags ?: ['direct']],
['type' => 'direct', 'tag' => 'direct'],
], array_values($nodes)),
'route' => ['rules' => [
['action' => 'sniff'],
['protocol' => 'dns', 'action' => 'hijack-dns'],
['ip_is_private' => true, 'action' => 'route', 'outbound' => 'direct'],
['rule_set' => ['geoip-cn', 'geosite-cn'], 'action' => 'route', 'outbound' => 'direct'],
], 'rule_set' => [
['tag' => 'geoip-cn', 'type' => 'remote', 'format' => 'binary', 'url' => 'https://fastly.jsdelivr.net/gh/SagerNet/sing-geoip@rule-set/geoip-cn.srs', 'download_detour' => 'direct'],
['tag' => 'geosite-cn', 'type' => 'remote', 'format' => 'binary', 'url' => 'https://fastly.jsdelivr.net/gh/SagerNet/sing-geosite@rule-set/geosite-cn.srs', 'download_detour' => 'direct'],
], 'final' => 'proxy', 'default_domain_resolver' => 'local', 'auto_detect_interface' => true],
];
return json_encode($config, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
}
public static function clash(array $nodes, ?array $template = null): string
{
if ($template !== null) {
$template['proxies'] = array_values($nodes);
$names = array_column($nodes, 'name');
foreach ($template['proxy-groups'] as &$group) {
if (!in_array($group['name'], ['🍃 应用净化', '🛑 全球拦截', '🎯 全球直连'], true)) {
$group['proxies'] = array_values(array_unique(array_merge($group['proxies'] ?? [], $names)));
if (!$group['proxies']) $group['proxies'] = ['DIRECT'];
}
}
unset($group);
return Yaml::dump($template, 12, 2, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
}
return Yaml::dump([
'mixed-port' => 7890, 'allow-lan' => false, 'mode' => 'rule', 'log-level' => 'warning',
'proxies' => array_values($nodes),
'proxy-groups' => [['name' => 'PROXY', 'type' => 'select', 'proxies' => array_column($nodes, 'name') ?: ['DIRECT']]],
'rules' => ['IP-CIDR,10.0.0.0/8,DIRECT,no-resolve', 'IP-CIDR,172.16.0.0/12,DIRECT,no-resolve', 'IP-CIDR,192.168.0.0/16,DIRECT,no-resolve', 'MATCH,PROXY'],
], 12, 2, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
}
}