日志升级与缓存修复最终版

This commit is contained in:
root
2026-06-16 04:58:56 +08:00
parent bcb9f293a6
commit 6b3b99b0f1
61 changed files with 2405 additions and 333 deletions
+186 -12
View File
@@ -165,34 +165,208 @@ if (!function_exists('write_nt_list')) {
}
}
if (!function_exists('cloak_normalize_request_host')) {
/**
* 规范化请求 Host:小写、去端口、去首尾空白。
*
* @param string|null $host
*/
function cloak_normalize_request_host($host = null)
{
$host = strtolower(trim((string) ($host ?? ($_SERVER['HTTP_HOST'] ?? ''))));
if ($host === '') {
return '';
}
if ($host[0] === '[') {
$end = strpos($host, ']');
if ($end !== false) {
return substr($host, 0, $end + 1);
}
}
$colon = strpos($host, ':');
if ($colon !== false) {
$host = substr($host, 0, $colon);
}
return rtrim($host, '.');
}
}
if (!function_exists('cloak_host_is_ip')) {
/**
* @param string $host
*/
function cloak_host_is_ip($host)
{
$host = trim($host);
if ($host === '') {
return false;
}
if ($host[0] === '[' && substr($host, -1) === ']') {
return filter_var(substr($host, 1, -1), FILTER_VALIDATE_IP) !== false;
}
return filter_var($host, FILTER_VALIDATE_IP) !== false;
}
}
if (!function_exists('cloak_host_is_local')) {
/**
* localhost / 单标签主机名(不含点)视为 host-only Cookie。
*
* @param string $host
*/
function cloak_host_is_local($host)
{
$host = strtolower(trim($host));
if ($host === '' || strpos($host, '.') === false) {
return true;
}
return $host === 'localhost'
|| substr($host, -6) === '.local'
|| substr($host, -5) === '.test'
|| substr($host, -13) === '.localhost';
}
}
if (!function_exists('cloak_cookie_public_suffixes')) {
/**
* 常见多级公共后缀(co.uk / com.cn 等),避免 Cookie domain 算成 .co.uk。
*
* @return string[]
*/
function cloak_cookie_public_suffixes()
{
return [
'co.uk', 'org.uk', 'ac.uk', 'gov.uk', 'net.uk',
'com.cn', 'net.cn', 'org.cn', 'gov.cn',
'com.au', 'net.au', 'org.au', 'edu.au',
'co.jp', 'ne.jp', 'or.jp',
'com.br', 'com.mx', 'com.tw', 'com.hk', 'co.nz', 'co.kr', 'com.sg',
'com.tr', 'com.pl', 'com.ar', 'com.co', 'co.in', 'com.my',
];
}
}
if (!function_exists('cloak_normalize_cookie_domain_config')) {
/**
* 配置项 CLOAK_COOKIE_DOMAIN → Cookie 用 domain(带点前缀,如 .shili.buzz
*
* @param string $value
*/
function cloak_normalize_cookie_domain_config($value)
{
$value = strtolower(trim($value));
if ($value === '') {
return '';
}
$value = ltrim($value, '.');
if ($value === '' || cloak_host_is_ip($value) || cloak_host_is_local($value)) {
return '';
}
return '.' . $value;
}
}
if (!function_exists('cloak_registrable_domain')) {
/**
* 从 Host 推断可注册域(不含前导点),IP/localhost 返回空。
*
* @param string|null $host
*/
function cloak_registrable_domain($host = null)
{
if (defined('CLOAK_COOKIE_DOMAIN') && (string) CLOAK_COOKIE_DOMAIN !== '') {
$configured = cloak_normalize_cookie_domain_config((string) CLOAK_COOKIE_DOMAIN);
return $configured !== '' ? ltrim($configured, '.') : '';
}
$host = cloak_normalize_request_host($host);
if ($host === '' || cloak_host_is_ip($host) || cloak_host_is_local($host)) {
return '';
}
$parts = explode('.', $host);
$count = count($parts);
if ($count < 2) {
return '';
}
$suffix2 = $parts[$count - 2] . '.' . $parts[$count - 1];
if (in_array($suffix2, cloak_cookie_public_suffixes(), true)) {
if ($count < 3) {
return '';
}
return $parts[$count - 3] . '.' . $suffix2;
}
return $suffix2;
}
}
if (!function_exists('cloak_fingerprint_cookie_domain')) {
/**
* 指纹 Cookie 使用的跨子域 domain与 setCrossDomainCookie 一致)
* 指纹 / Session Cookie 跨子域 domain带点,如 .shili.buzz);IP/localhost 返回空(host-only)。
*/
function cloak_fingerprint_cookie_domain()
{
if (empty($_SERVER['HTTP_HOST'])) {
return '';
if (defined('CLOAK_COOKIE_DOMAIN') && (string) CLOAK_COOKIE_DOMAIN !== '') {
return cloak_normalize_cookie_domain_config((string) CLOAK_COOKIE_DOMAIN);
}
$host_names = explode('.', $_SERVER['HTTP_HOST']);
if (count($host_names) < 2) {
return '';
}
$registrable = cloak_registrable_domain();
return $registrable !== '' ? '.' . $registrable : '';
}
}
return '.' . $host_names[count($host_names) - 2] . '.' . $host_names[count($host_names) - 1];
if (!function_exists('cloak_fingerprint_js_cookie_options')) {
/**
* 前端 js-cookie 写入指纹 Cookie 的选项(与 PHP setcookie 策略一致)。
*
* @return array{path:string,expires:int,sameSite:string,domain?:string,secure?:bool}
*/
function cloak_fingerprint_js_cookie_options()
{
$opts = [
'path' => '/',
'expires' => 1,
'sameSite' => 'Lax',
];
$domain = cloak_fingerprint_cookie_domain();
if ($domain !== '') {
$opts['domain'] = $domain;
}
if (cloak_request_is_https()) {
$opts['secure'] = true;
}
return $opts;
}
}
if (!function_exists('cloak_set_cross_domain_cookie')) {
function cloak_set_cross_domain_cookie($name, $value, $expire)
{
$bottom_host_name = cloak_fingerprint_cookie_domain();
if ($bottom_host_name === '') {
setcookie($name, $value, $expire, '/');
$cookieDomain = cloak_fingerprint_cookie_domain();
$secure = cloak_request_is_https();
if (PHP_VERSION_ID >= 70300) {
$opts = [
'expires' => (int) $expire,
'path' => '/',
'secure' => $secure,
'httponly' => false,
'samesite' => 'Lax',
];
if ($cookieDomain !== '') {
$opts['domain'] = ltrim($cookieDomain, '.');
}
setcookie($name, $value, $opts);
return;
}
setcookie($name, $value, $expire, '/', $bottom_host_name);
if ($cookieDomain === '') {
setcookie($name, $value, (int) $expire, '/', '', $secure, false);
return;
}
setcookie($name, $value, (int) $expire, '/', $cookieDomain, $secure, false);
}
}