<?php
// 获取订阅内容
$URL_MAP = array(
    'private' =>  'https://git.vsprit.com/shawenguan/Quantumult-X/raw/master/ResSub/private.snippet',
    'edgetunnel' =>  'https://edt2.530930.xyz/sub?token=39756bc6039d7841387718bc03961bbc',
    'ioshkj' =>  'https://git.vsprit.com/shawenguan/Quantumult-X/raw/master/ResSub/ioshkj.snippet',
    // https://yyds.emovpn.top/#/subscribe
    'emovpn' => 'https://api.e687699.top/api/v1/client/subscribe?token=d117717961314da13e57bd4bd7dadf9b',
    // https://www.freebb.me/#/dashboard
    'freeapp' => 'https://xb.freeapp.buzz/api/v1/client/subscribe?token=affc8f19821abf481d7f32ffacd8d216',
    // https://ikuuu.de/user
    'ikuuu' => 'https://fwkz2.no-mad-world.club/link/i3jImakgPfIGXlyn?clash=3&extend=1',
    'aiboboxx' => 'https://githubraw.com/aiboboxx/v2rayfree/main/README.md',
    'chengaopan' => 'https://githubraw.com/chengaopan/AutoMergePublicNodes/master/list_raw.txt',
    'Pawdroid' => 'https://githubraw.com/Pawdroid/Free-servers/main/README.md',
    // https://v2cross.com/1884.html
    'v2cross' => 'https://9527521.xyz/pubconfig/NdK69cBoC7XHP0Ij'
);

function getLatestSubUrl($key){
    global $URL_MAP;
    if (isset($URL_MAP[$key])) {
        return $URL_MAP[$key];
    }
    return null;
}

function getCommonSubUrl($key){
    return sprintf('https://git.vsprit.com/shawenguan/Quantumult-X/raw/master/ResSub/%s.snippet', $key);
}

function getUrlContent($url) {
    // 初始化cURL会话
    $ch = curl_init($url);

    // 设置cURL选项
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而非直接输出
    curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头部信息
    // 对于HTTPS URL，如果无有效证书，需要禁止cURL验证对等证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // 当遇到location跳转时，直接抓取跳转的页面
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    // 执行cURL会话
    $content = curl_exec($ch);

    // 检查是否有错误发生
    if(curl_errno($ch)) {
        return 'cURL Error: ' . curl_error($ch);
    }
    // 关闭cURL会话
    curl_close($ch);
    return $content;
}

$key = 'private';
if (isset($_REQUEST['k'])) {
    $key = $_REQUEST['k'];
}
$url = getLatestSubUrl($key);
if ($url !== null) {
    $content = getUrlContent($url);
    echo $content;
    return;
}

$url = getCommonSubUrl($key);
$content = getUrlContent($url);
if (stristr($content, 'Page Not Found') || stristr($content, '页面未找到')) {
    echo '404';
} else {
    echo $content;
}

?>