Files
EasyDesignCollection/lib/license/license_service.dart
T
呆头鹅 5d0238260f feat(V1.1.1): 软件授权(license)——MAC 绑定 .lic 校验,未授权禁用导出
- 新增 lib/license/license_service.dart:读取 exe 同级 license/*.lic,按算法由本机 MAC 计算期望授权串比对
- 算法:MAC 去分隔符 → A=01..Z=26(数字 00..09)→ 末尾加 26 → 整串倒序
- 未授权时「导出 SVG / PNG」置灰,并在导出入口二次拦截
- 关于页:显示授权状态、本机 MAC、授权申请邮箱 liuyu65@126.com,并可「提交授权申请」(唤起邮件客户端发送 MAC+发件人信息)
- 新增 license/ 目录(含说明);构建脚本把 license/ 复制到 dist/
- 新增 tools/keygen(Win32 图形界面 keygen.exe + 构建脚本)用于生成 .lic
- 版本 1.1.1+3;新增授权算法单元测试;重新构建 dist
2026-09-19 11:08:39 +08:00

159 lines
5.3 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.
/// 软件授权(License)服务。
///
/// 授权模型:
/// - 应用启动时读取可执行文件同级目录下的 `license/*.lic`(每个文件一行文本);
/// - 由**本机 MAC 地址**按固定算法算出期望的授权串,与 .lic 内容比对;
/// - 有效则可导出 SVG / PNG,无效则导出按钮置灰;
/// - 关于页展示授权状态、本机 MAC,并提供「提交授权申请」。
///
/// 授权算法(与 keygen 保持一致):
/// 1. MAC `AA-BB-CC-DD-EE-FF` → 去掉横线 / 空格 → `AABBCCDDEEFF`;
/// 2. 每个字符按 `A=01 … Z=26`(数字 `0..9` 记作 `00..09`)转两位十进制;
/// 3. 尾部追加固定串 `26`;
/// 4. 整串倒序,即为 .lic 文件内容。
///
/// 例:`AABBCCDDEEFF` → `010102020303040405050606` → 加 `26`
/// → `01010202030304040505060626` → 倒序 → `62606050504040303020201010`。
library;
import 'dart:io';
/// 授权状态快照。
class LicenseInfo {
/// 构造授权信息。
const LicenseInfo({required this.mac, required this.valid, this.filePath, this.content});
/// 本机主 MAC 地址(形如 `AA-BB-CC-DD-EE-FF`);读取失败为 null。
final String? mac;
/// 是否存在有效授权。
final bool valid;
/// 命中的 .lic 文件路径(若有)。
final String? filePath;
/// 命中的 .lic 文件内容(若有)。
final String? content;
/// 期望的授权串(由本机 MAC 计算);无法获取 MAC 时为 null。
String? get expected => mac == null ? null : LicenseService.encodeMac(mac!);
}
/// 授权服务(单例)。
class LicenseService {
LicenseService._();
/// 单例。
static final LicenseService instance = LicenseService._();
LicenseInfo _info = const LicenseInfo(mac: null, valid: false);
/// 最近一次的授权信息。
LicenseInfo get info => _info;
/// 是否已授权。
bool get licensed => _info.valid;
/// license 目录(可执行文件同级目录下的 `license`)。
Directory get licenseDir => Directory('${Directory.current.path}${Platform.pathSeparator}license');
/// 初始化:读取 MAC 并校验 `license/*.lic`。
Future<LicenseInfo> init() async {
final String? mac = await _readMac();
bool valid = false;
String? hitPath;
String? hitContent;
final Directory dir = licenseDir;
if (dir.existsSync()) {
final List<File> files = dir
.listSync()
.whereType<File>()
.where((File f) => f.path.toLowerCase().endsWith('.lic'))
.toList()
..sort((File a, File b) => a.path.compareTo(b.path));
final String? expected = mac == null ? null : encodeMac(mac);
for (final File f in files) {
String content;
try {
content = f.readAsStringSync().trim();
} catch (_) {
continue;
}
if (expected != null && content == expected) {
valid = true;
hitPath = f.path;
hitContent = content;
break;
}
}
}
_info = LicenseInfo(mac: mac, valid: valid, filePath: hitPath, content: hitContent);
return _info;
}
/// 读取本机第一个 MAC 地址(通过系统 `getmac`)。
Future<String?> _readMac() async {
try {
final ProcessResult r = await Process.run(
'getmac',
<String>['/fo', 'csv', '/nh'],
runInShell: true,
);
final String out = '${r.stdout}';
final Match? m = RegExp(r'([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2}').firstMatch(out);
if (m == null) return null;
return m.group(0)!.toUpperCase().replaceAll(':', '-');
} catch (_) {
return null;
}
}
/// 按授权算法,由 [mac] 计算期望的授权串(.lic 内容)。
static String encodeMac(String mac) {
final String clean = mac.replaceAll(RegExp(r'[^0-9A-Za-z]'), '').toUpperCase();
final StringBuffer sb = StringBuffer();
for (final int code in clean.codeUnits) {
int v;
if (code >= 0x30 && code <= 0x39) {
v = code - 0x30; // '0'..'9' -> 0..9
} else if (code >= 0x41 && code <= 0x5A) {
v = code - 0x40; // 'A'..'Z' -> 1..26
} else {
continue;
}
sb.write(v.toString().padLeft(2, '0'));
}
sb.write('26');
final String fwd = sb.toString();
return String.fromCharCodes(fwd.codeUnits.reversed);
}
/// 提交授权申请:通过网络(默认邮件客户端)把 MAC 与发件人信息发送到指定邮箱。
///
/// 返回 true 表示已成功唤起邮件客户端。
Future<bool> submitApplication({required String sender, String? note}) async {
const String to = 'liuyu65@126.com';
final String mac = _info.mac ?? '(读取失败)';
final String subject = Uri.encodeComponent('时序图设计器 V1.1.1 授权申请');
final String body = Uri.encodeComponent(
'MAC 地址:$mac\n'
'发件人信息:$sender\n'
'备注:${note ?? '(无)'}\n'
'软件版本:V1.1.1\n',
);
final String url = 'mailto:$to?subject=$subject&body=$body';
try {
await Process.run('rundll32', <String>['url.dll,FileProtocolHandler', url]);
return true;
} catch (_) {
try {
await Process.run('cmd', <String>['/c', 'start', '', url], runInShell: true);
return true;
} catch (_) {
return false;
}
}
}
}