<?php
$configFile = __DIR__ . '/redirects.json';
if (!file_exists($configFile)) {
    http_response_code(404);
    exit("Redirect config not found.");
}

$config = json_decode(file_get_contents($configFile), true);

// 获取当前访问域名（去掉 www）
$host = $_SERVER['HTTP_HOST'];
$host = preg_replace('/^www\./i', '', $host);

// 判断是否存在对应的iframe目标
if (!isset($config[$host])) {
    http_response_code(404);
    echo "No iframe rule for domain: $host";
    exit;
}

$target = $config[$host];
?>
<!--<!DOCTYPE html>-->
<!--<html>-->
<!--<head>-->
<!--<meta charset="utf-8">-->
<!--<title><?= htmlspecialchars($host) ?> 内容页面</title>-->
<!--<style>-->
<!--html, body {-->
<!--    margin: 0;-->
<!--    padding: 0;-->
<!--    height: 100%;-->
<!--    overflow: hidden;-->
<!--}-->
<!--iframe {-->
<!--    width: 100%;-->
<!--    height: 100%;-->
<!--    border: none;-->
<!--}-->
<!--</style>-->
<!--</head>-->
<!--<body>-->
<!--<iframe src="<?= htmlspecialchars($target) ?>" allowfullscreen></iframe>-->
<!--</body>-->
<!--</html>-->




<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover">
    <script>
        (function(doc, win) {
            var docEl = doc.documentElement;
            var timer = null;

            function setRootFont() {
                var width = docEl.clientWidth || win.innerWidth || 0;

                if (!width) {
                    timer = win.setTimeout(setRootFont, 50);
                    return;
                }

                docEl.style.fontSize = (width / 20) + 'px';
            }

            function resetRootFont() {
                if (timer) {
                    win.clearTimeout(timer);
                }
                timer = win.setTimeout(setRootFont, 50);
            }

            setRootFont();
            win.addEventListener('resize', resetRootFont, false);
            win.addEventListener('orientationchange', resetRootFont, false);
            win.addEventListener('pageshow', setRootFont, false);
        })(document, window);
    </script>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            overflow: hidden;
            background: #fff;
        }

        #frame3d {
            position: fixed;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            inset: 0;
            display: block;
            width: 100%;
            height: 100vh;
            height: 100dvh;
            border: 0;
            overflow: auto;
            -webkit-overflow-scrolling: touch;
        }
    </style>
</head>
<body>
    <iframe id="frame3d" name="frame3d" frameborder="0" scrolling="auto"></iframe>
    <script>
        (function(win, doc) {
            var host = win.location.host;
            var frame = doc.getElementById('frame3d');

            if (host && frame) {
                frame.src = "<?php echo htmlspecialchars($target);?>";
            }
        })(window, document);
    </script>
</body>
</html>

