Files
CloudHome-Ops/lib/config/prod_guard.dart

49 lines
1.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:io';
import 'app_config.dart';
/// 生产环境强制守卫。
///
/// 设计目标:本构建(APK)**永远无法进入生产模式**。
/// - 编译期常量 [kAllowProduction] 恒为 false;
/// - 运行期校验当前后端地址 host 不在生产拒绝名单中;
/// 任一命中即打印清晰报错并 [exit],阻断后续执行。
class ProductionGuard {
ProductionGuard._();
/// 编译期开关:绝不允许开启。
static const bool kAllowProduction = false;
/// 生产环境 host/IP 拒绝名单(精确匹配,区分大小写前已小写化)。
static const List<String> _productionHostDenyList = <String>[
'58.220.83.58', // 生产服务器 IP
'api.ainetv.com',
'ainetv.com',
];
/// 启动期执行:断言非生产,命中即失败退出。
///
/// 同时作为构建期守卫([assert] 在 debug/profile 构建抛错)与运行期守卫
/// (release 构建中 [assert] 被剥离,但 host 校验仍生效)。
static void enforce() {
// 编译期常量断言:若有人将 kAllowProduction 改为 true,调试构建立即崩溃。
assert(!kAllowProduction, '生产模式被编译期禁用 (kAllowProduction=false)');
// 解析当前后端地址并校验 host,禁止任何生产标识。
final full = AppConfig.apiFullBaseUrl;
final host = Uri.parse(full).host.toLowerCase();
if (_productionHostDenyList.contains(host) || host.contains('prod')) {
_failAndExit(
'检测到生产环境地址 host=$host(来自 $full),已强制阻断启动',
);
}
}
/// 打印清晰报错并退出进程,阻断后续执行。
static Never _failAndExit(String reason) {
// ignore: avoid_print
print('[PRODUCTION-GUARD] 启动被拒绝:$reason');
exit(1);
}
}