```php
<?php

declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| RFC 8484 DNS-over-HTTPS Proxy
|--------------------------------------------------------------------------
|
| Compatible with:
| - Windows 11
| - Android
| - Google TV
| - Linux
| - macOS
| - Chrome
| - Firefox
| - Edge
|
*/

$upstream = 'https://dns.nextdns.io/3c27f6';

/*
|--------------------------------------------------------------------------
| Parse Request
|--------------------------------------------------------------------------
*/

$request = null;

switch ($_SERVER['REQUEST_METHOD']) {

    case 'GET':

        $dns = $_GET['dns'] ?? '';

        if (empty($dns)) {
            http_response_code(400);
            exit('Missing dns parameter');
        }

        $dns = str_replace(
            ['-', '_'],
            ['+', '/'],
            $dns
        );

        $padding = strlen($dns) % 4;

        if ($padding > 0) {
            $dns .= str_repeat('=', 4 - $padding);
        }

        $request = base64_decode($dns);

        if ($request === false) {
            http_response_code(400);
            exit('Invalid dns parameter');
        }

        break;

    case 'POST':

        $request = file_get_contents('php://input');

        if (!$request) {
            http_response_code(400);
            exit('Empty request');
        }

        break;

    default:

        header('Allow: GET, POST');

        http_response_code(405);
        exit('Method Not Allowed');
}

/*
|--------------------------------------------------------------------------
| Forward To Upstream
|--------------------------------------------------------------------------
*/

$ch = curl_init($upstream);

curl_setopt_array($ch, [

    CURLOPT_POST => true,

    CURLOPT_POSTFIELDS => $request,

    CURLOPT_RETURNTRANSFER => true,

    CURLOPT_TIMEOUT => 10,

    CURLOPT_HTTPHEADER => [

        'Content-Type: application/dns-message',

        'Accept: application/dns-message'
    ]
]);

$response = curl_exec($ch);

$status = curl_getinfo(
    $ch,
    CURLINFO_HTTP_CODE
);

$error = curl_error($ch);

curl_close($ch);

if (
    $status !== 200 ||
    empty($response)
) {

    http_response_code(502);

    header(
        'Content-Type: application/json'
    );

    echo json_encode([
        'success' => false,
        'status' => $status,
        'error' => $error
    ]);

    exit;
}

/*
|--------------------------------------------------------------------------
| Return DNS Response
|--------------------------------------------------------------------------
*/

http_response_code(200);

header(
    'Content-Type: application/dns-message'
);

header(
    'Cache-Control: no-store'
);

header(
    'Access-Control-Allow-Origin: *'
);

echo $response;
```
