feat: 连接设备上限 backstop — 兜住"已登录的超限设备"(#16)
ci-pangolin / Lint — shellcheck (push) Has been cancelled
ci-pangolin / OpenAPI Sync Check (push) Has been cancelled
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Has been cancelled
ci-pangolin / Flutter — analyze + test (push) Has been cancelled
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Has been cancelled
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Has been cancelled
ci-pangolin / Go — build + test (push) Has been cancelled
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Has been cancelled
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Has been cancelled
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Has been cancelled

登录挡板只拦新登录,已登录的超限会话(如 pro 已连 5 台)永远不被提示 → 限制形同虚设。
补服务端连接卡点:超限账户连接直接拒,兜住已登录设备(下次一连即被拦,无需重登)。

服务端:
- Entitlement 加 MaxDevices(EntitlementForUser 查 p.max_devices;free 默认 1)
- NodeStore.CountActiveDevices(近 30d 活跃)+ SQLNodeStore/mock 实现
- ConnectNode:activeCount > MaxDevices → 403 {code:DEVICE_LIMIT_EXCEEDED, max_devices}

客户端:
- ConnectApiException 解析 code/max_devices(结构化错误体)
- _connect 遇 DEVICE_LIMIT_EXCEEDED → 置 deviceLimitProvider → 顶层路由弹「移除设备」页
  (设备列表走 devicesProvider),移除到上限内自动放行

后端 go test(store/nodes/httpapi/devices)全过;前端 analyze 干净 + 66 测试过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-01 22:11:13 +08:00
parent 8afe1f050c
commit 0a70a24cdb
5 changed files with 85 additions and 5 deletions
+26 -3
View File
@@ -16,14 +16,22 @@ class ConnectApiException implements Exception {
required this.statusCode,
required this.messageZh,
required this.messageEn,
this.code,
this.maxDevices,
});
final int statusCode;
final String messageZh;
final String messageEn;
/// 服务端错误码(如 DEVICE_LIMIT_EXCEEDED);网络/解析错误时为 null。
final String? code;
/// DEVICE_LIMIT_EXCEEDED 时服务端下发的套餐设备上限。
final int? maxDevices;
@override
String toString() => 'ConnectApiException($statusCode): $messageZh';
String toString() => 'ConnectApiException($statusCode${code != null ? '/$code' : ''}): $messageZh';
}
/// [ConnectApi] 封装 POST /v1/nodes/:id/connect 调用。
@@ -76,10 +84,25 @@ class ConnectApi {
}
if (response.statusCode != 200) {
// 解析结构化错误体(code/message/max_devices),失败则回退通用文案。
String? code;
int? maxDevices;
String? zh, en;
try {
final m = jsonDecode(response.body);
if (m is Map) {
code = m['code'] as String?;
maxDevices = (m['max_devices'] as num?)?.toInt();
zh = m['message_zh'] as String?;
en = m['message_en'] as String?;
}
} catch (_) {/* 非 JSON,用通用文案 */}
throw ConnectApiException(
statusCode: response.statusCode,
messageZh: '节点连接失败 (HTTP ${response.statusCode})',
messageEn: 'Connect failed (HTTP ${response.statusCode})',
code: code,
maxDevices: maxDevices,
messageZh: zh ?? '节点连接失败 (HTTP ${response.statusCode})',
messageEn: en ?? 'Connect failed (HTTP ${response.statusCode})',
);
}
@@ -16,6 +16,7 @@ import '../bridge/vpn_bridge_provider.dart';
import '../l10n/app_text.dart';
import '../models/node.dart';
import '../services/api_config.dart';
import '../services/auth_api.dart';
import '../services/connect_api.dart';
import '../services/device_identity.dart';
import 'app_providers.dart';
@@ -193,6 +194,14 @@ class ConnectionController extends StateNotifier<ConnectionState> {
// bridge.start() 不阻塞至连接建立;on 状态由 statusStream 回调驱动。
await _bridge.start(configJson);
} on ConnectApiException catch (e) {
// 设备数超限(backstop):弹「移除设备」页(顶层路由据 deviceLimitProvider 切屏),
// 兜住"已登录但超限"的设备——它们一连就被拦、被提示,无需重新登录。连接回 off。
if (e.code == 'DEVICE_LIMIT_EXCEEDED') {
_ref.read(deviceLimitProvider.notifier).state =
DeviceLimit(maxDevices: e.maxDevices ?? 0, devices: const []);
if (mounted) state = const ConnectionState(phase: VpnPhase.off);
return;
}
// 把后端/网络错误冒泡到 UI(原静默回 off,用户不知所以)。
if (mounted) state = ConnectionState(phase: VpnPhase.off, error: zh ? e.messageZh : e.messageEn);
} catch (e) {