n/haoyihaou/1panel · 2 个月前
注册 登录
n/haoyihao · 2 个月前 · 已编辑 · 979 次浏览
u/1panel黄金

【存档】免费白嫖日本/韩国原生家宽节点!基于 Serv00 + PHP 打造专属 Mihomo 订阅接口

原帖发在内版被系统过期自动隐藏了,看到有人还需要,就自己创建节点重新发表一次,不喜直接划走

之前薅的 Serv00 免费vps服务器一直闲着不知道干点儿啥
不记得在哪里看到的, VPNGate 提供海量日本/韩国志愿家宽节点
VPNGate 官方仅提供原始 CSV 和单个 .ovpn 格式,无法直接给 Mihomo 使用
因此需要一个自动化转换层,这不就有了嘛,终于可以让 Serv00 派上用场了

废话不多说,直接上代码,保存为 openvpn.php,上传到 Serv00 服务器上即可使用

<?php
// 屏蔽提示信息并设置脚本响应头/超时限制
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', 0);
set_time_limit(60);

// ================= 路由与密钥保护设置 =================
const AUTH_TOKEN = "JJmi1vwutFHfssyH8Ym88NQNp2pZQ6Lo";

$requestToken = $_GET['token'] ?? '';

if ($requestToken !== AUTH_TOKEN) {
    http_response_code(401);
    header("Content-Type: text/plain; charset=utf-8");
    echo "Unauthorized: 密钥错误或未提供";
    exit;
}
// =====================================================

$cacheFile = __DIR__ . '/vpngate_cache.yaml';
$expireSeconds = 3600; // 缓存 1 小时
$vpngateUrl = "https://www.vpngate.net/api/iphone/";

try {
    // 1. 检查本地文件缓存
    if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $expireSeconds)) {
        $cachedYaml = file_get_contents($cacheFile);
        if ($cachedYaml !== false) {
            header("Content-Type: text/yaml; charset=utf-8");
            header('Content-Disposition: inline; filename=openvpn.yaml');
            header("Access-Control-Allow-Origin: *");
            header("X-Cache-Status: HIT");
            echo $cachedYaml;
            exit;
        }
    }

    // 2. 缓存未命中,获取节点
    $nodes = fetchAndParseVPNGate($vpngateUrl);

    if (empty($nodes)) {
        http_response_code(404);
        header("Content-Type: text/plain; charset=utf-8");
        echo "未解析到有效的 VPNGate 节点";
        exit;
    }

    // 3. 手动转换为 YAML 字符串
    $yamlData = dumpToYaml($nodes);

    // 4. 写入本地文件缓存
    @file_put_contents($cacheFile, $yamlData, LOCK_EX);

    // 5. 返回生成的 YAML
    header("Content-Type: text/yaml; charset=utf-8");
    header('Content-Disposition: inline; filename="openvpn.yaml"');
    header("Access-Control-Allow-Origin: *");
    header("X-Cache-Status: MISS");
    echo $yamlData;

} catch (Exception $err) {
    http_response_code(500);
    header("Content-Type: text/plain; charset=utf-8");
    echo "处理失败: " . $err->getMessage();
}

/**
 * 手动将节点对象数组转为标准 Mihomo YAML 格式
 */
function dumpToYaml(array $nodes): string {
    $yaml = "proxies:\n";
    foreach ($nodes as $node) {
        $yaml .= '  - name: "' . escapeYamlString($node['name']) . '"' . "\n";
        $yaml .= "    type: {$node['type']}\n";
        $yaml .= "    server: {$node['server']}\n";
        $yaml .= "    port: {$node['port']}\n";
        $yaml .= "    proto: {$node['proto']}\n";

        if (!empty($node['cipher'])) $yaml .= "    cipher: {$node['cipher']}\n";
        if (!empty($node['auth']))   $yaml .= "    auth: {$node['auth']}\n";

        // 针对长文本证书(ca/cert/key),采用 YAML 多行缩进写法 (|+ 格式)
        if (!empty($node['ca']))   $yaml .= "    ca: |+\n" . indentText($node['ca'], 6) . "\n";
        if (!empty($node['cert'])) $yaml .= "    cert: |+\n" . indentText($node['cert'], 6) . "\n";
        if (!empty($node['key']))  $yaml .= "    key: |+\n" . indentText($node['key'], 6) . "\n";
    }
    return $yaml;
}

function escapeYamlString(string $str): string {
    return str_replace(['\\', '"'], ['\\\\', '\"'], $str);
}

function indentText(string $text, int $spaces): string {
    $prefix = str_repeat(' ', $spaces);
    $lines = explode("\n", $text);
    $indented = array_map(fn($line) => $prefix . $line, $lines);
    return implode("\n", $indented);
}

/**
 * 获取并解析 VPNGate CSV 数据
 */
function fetchAndParseVPNGate(string $url): array {
    $options = [
        "http" => [
            "method" => "GET",
            "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36\r\n" .
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n",
            "timeout" => 15
        ]
    ];
    $context = stream_context_create($options);
    $text = @file_get_contents($url, false, $context);

    if ($text === false) {
        throw new Exception("抓取 VPNGate 数据失败,请检查网络或服务器配置");
    }

    $lines = preg_split('/\r?\n/', $text);
    $nodes = [];

    foreach ($lines as $line) {
        $line = trim($line);
        if ($line === '') continue;

        $record = explode(',', $line);
        if (empty($record)) continue;

        $firstCol = trim($record[0]);
        if (str_starts_with($firstCol, '#') || str_starts_with($firstCol, '*')) {
            continue;
        }

        $lastCol = trim(end($record));
        if (empty($lastCol)) continue;

        $ovpnText = base64_decode($lastCol, true);
        if ($ovpnText === false) continue;

        $nodeName = "VPNGate-" . $record[0];
        if (isset($record[6]) && !empty(trim($record[6]))) {
            $nodeName = trim($record[6]) . "-" . $record[0];
        }

        if (str_contains($nodeName, "public")) {
            continue;
        }

        $node = convertOvpnToMihomo($nodeName, $ovpnText);

        if ($node && isset($node['proto']) && $node['proto'] === "udp") {
            continue;
        }

        if ($node && !empty($node['server'])) {
            $nodes[] = $node;
        }
    }

    return $nodes;
}

/**
 * 解析 .ovpn 文本并提取参数
 */
function convertOvpnToMihomo(string $name, string $ovpn): ?array {
    $node = [
        'name' => $name,
        'type' => "openvpn"
    ];

    $lines = explode("\n", $ovpn);
    foreach ($lines as $line) {
        $line = trim($line);

        if (preg_match('/^\s*remote\s+([^\s]+)\s+(\d+)/i', $line, $matchRemote)) {
            $node['server'] = $matchRemote[1];
            $node['port'] = (int)$matchRemote[2];
        }

        if (preg_match('/^\s*proto\s+(tcp|udp|tcp-client|udp-client)/i', $line, $matchProto)) {
            if (str_starts_with(strtolower($matchProto[1]), "tcp")) {
                $node['proto'] = "tcp";
            } else {
                $node['proto'] = "udp";
            }
        }

        if (preg_match('/^\s*cipher\s+([^\s]+)/i', $line, $matchCipher)) {
            $node['cipher'] = $matchCipher[1];
        }

        if (preg_match('/^\s*auth\s+([^\s]+)/i', $line, $matchAuth)) {
            $node['auth'] = $matchAuth[1];
        }
    }

    $ca = extractXMLBlock($ovpn, "ca");
    if (!empty($ca)) $node['ca'] = $ca;

    $cert = extractXMLBlock($ovpn, "cert");
    if (!empty($cert)) $node['cert'] = $cert;

    $key = extractXMLBlock($ovpn, "key");
    if (!empty($key)) $node['key'] = $key;

    if (empty($node['server'])) {
        return null;
    }

    if (empty($node['proto'])) {
        $node['proto'] = "udp";
    }

    return $node;
}

function extractXMLBlock(string $content, string $tag): string {
    $pattern = "/<" . preg_quote($tag, '/') . ">([\s\S]*?)<\/" . preg_quote($tag, '/') . ">/i";
    if (preg_match($pattern, $content, $match) && !empty($match[1])) {
        return trim($match[1]);
    }
    return "";
}

当然这些家宽节点在国内大概率无法直连
这就需要自己找个前置节点,至于去哪里搞到线路好还便宜的节点那是 另外一篇帖子了,这里不展开讲
找好前置节点后,配置好规则分组,导入到自己熟悉的客户端就可以使用了,你的mihomo配置文件大概长这样

proxies:
    - 你的前置节点配置

proxy-providers:
    provider1:
        interval: 3600
        override:
            dialer-proxy: 你的前置节点名称
        path: ./provider/openvpn.yaml
        type: http
        url: https://789.nx.kg/openvpn.php?token=JJmi1vwutFHfssyH8Ym88NQNp2pZQ6Lo

我比较懒,就不写规则分组了,将以上配置直接导入到 Pandora-Box 可直接使用,这个客户端直接支持不写规则分组导入节点或者配置

最终效果如下图
节点

家宽节点质量如下图
ping0.cc

至于网速嘛,这得看你前置节点
速度

最后,眼尖的你可能发现我的Serv00提供的订阅地址了

https://789.nx.kg/openvpn.php?token=JJmi1vwutFHfssyH8Ym88NQNp2pZQ6Lo

拿去拿去

题外话:这种方法不是使用 Serv00 搭建节点,不会封号,放心

24
45
登录后参与讨论。

45 条回复

排序
  1. duyouqian 白银 2 个月前

    插眼+1

    4
  2. DaniG 黄金 3 YEARS 2 个月前

    感觉很不错

    3
  3. Yang-iyu 白银 2 个月前

    大佬牛逼,我插眼+1

    2
  4. xiaoyi669 💬 钻石 钻石会员 2 个月前

    参与留存一下

    2
  5. huwei 黄金 2 个月前

    感谢大佬

    2
  6. xxooxx 青铜 2 个月前

    openvpn的格式,好像很多都不支持。

    2
  7. 1panel 黄金 楼主 2 个月前

    基于 mihomo 和 sing-box 最新内核的客户端都是支持的

    2
  8. shine1991 黄金 3 YEARS 2 个月前

    插眼等有serv00

    2
  9. 1panel 黄金 楼主 2 个月前

    没有serv00也行,可以找ai改写成cloudflare的works脚本,部署也是一样的

    2
  10. shine1991 黄金 3 YEARS 2 个月前

    谢谢佬提醒我试试看 :xhj16:

    0
  11. shine1991 黄金 3 YEARS 2 个月前

    用AI修改完成。把token添加到环境变量使用感谢大佬

    1
  12. newbietan 黄金 3 YEARS 2 个月前

    可以,很强,可惜没serv00账号

    2
  13. 1panel 黄金 楼主 2 个月前

    没有serv00也行,可以找ai改写成cloudflare的works脚本,去部署也可以,比serv00响应更快

    0
  14. yorushika 钻石 2 个月前

    大佬牛逼

    2
  15. QAQsmile 白银 2 个月前

    插眼

    1
  16. leexore 白银 2 个月前

    感谢楼主,只看不用

    1
  17. xiaozx 💤 黄金 3 YEARS 2 个月前

    感谢分享!

    1
  18. qwang 黄金 3 YEARS 2 个月前

    插眼

    1
  19. Make1 黄金 3 YEARS 2 个月前

    可惜了,QX无法使用

    1
  20. smzdm2025 白银 2 个月前

    可以,有空搭建一个

    1
  21. jeco 黄金 2 个月前

    哇大佬牛逼啊,这个必须收藏了!

    1
  22. 906964928 青铜 2 个月前

    支持一波

    1
  23. hankswang01 黄金 不离不弃 2 个月前

    不明觉厉啊

    1
  24. nahear 钻石 2 个月前

    不错不错 :xhj003:

    1
  25. thingEvery 黄金 2 YEARS 2 个月前

    终于又看见了,赶紧收藏 :xhj15:

    1
  26. yybbtech 🎧 黄金 2 个月前

    这回必须插眼了

    0
  27. for 黄金 2 个月前

    插眼,留着备用。大佬牛皮

    0
  28. IPLAY4LOVE 黄金 2 个月前

    已经点赞收藏了 ,大佬太强了,赶紧学习一下

    0
  29. 906964928 青铜 2 个月前

    大佬你提供的订阅地址哪些手机端app可以使用,试了FL clash,karing都不行

    0
  30. rzo 白银 3 YEARS 2 个月前

    FLclash可以,不是直接用的。你要先有个flclash可以用的配置,然后在proxy-providers中配置链式代理

    0
  31. 906964928 青铜 2 个月前

    你看我配置的有问题吗,我导入FL clash提示有错误

    0
  32. rzo 白银 3 YEARS 2 个月前

    dialer-proxy:空格,冒号后面要有个空格

    0
  33. 906964928 青铜 2 个月前

    这是提示错误

    0
  34. mu1997 黄金 2 个月前

    没看太懂
    学习一下

    0
  35. 906964928 青铜 2 个月前

    image
    实测只有 Pandora-Box能把节点加载出来,其他代理软件不行,虽然延迟有点高,但是速度还行

    0
  36. 1panel 黄金 楼主 2 个月前

    其他代理软件也可以加载,需要再添加规则分组,完整的订阅文件才行

    0
  37. liuckv 黄金 不离不弃 2 个月前

    v2用不了,可惜

    0
  38. Romar_Maulion 白银 2 个月前

    奇怪,为啥我检测有延迟,连接就没网

    0
  39. 1panel 黄金 楼主 2 个月前

    开了多个代理软件吗

    0
  40. Romar_Maulion 白银 2 个月前

    用的flclash,后面改成flyclash就可以了,晚上速度好像有点慢

    0
  41. 1panel 黄金 楼主 2 个月前

    这得看你前置代理,前置牛逼速度才能拉起来
    前置

    0
  42. 继续这个对话 →
  43. wj594774 白银 3 YEARS 2 个月前

    牛蛙

    0
  44. ganggang0511 青铜 2 个月前

    谢谢分享。。

    0