Authorize data sync on connected agent registration

This commit is contained in:
2026-09-22 16:47:52 +08:00
parent 49c07cca30
commit e26535781d
17 changed files with 194 additions and 110 deletions
+9 -53
View File
@@ -392,11 +392,6 @@ internal static class ServiceSettingsEditor
throw new InvalidDataException("ListenUrl is invalid.");
static string? Optional(TextBox box) => string.IsNullOrWhiteSpace(box.Text) ? null : box.Text.Trim();
static string[] Lines(TextBox box) => box.Lines
.Select(line => line.Trim())
.Where(line => line.Length > 0)
.Distinct(StringComparer.Ordinal)
.ToArray();
var host = uri.Host.Trim('[', ']');
var token = current.AccessToken ?? ServiceOptions.GenerateToken();
@@ -503,13 +498,14 @@ internal static class ServiceSettingsEditor
var reportingTitle = new Label
{
Left = 18, Top = 416, Width = 620, Height = 24,
Text = "远程读取白名单(不填写不会默认放行"
Text = "远程读取授权(Agent 连接后自动授予已验证账号和会话"
};
var reportingEnabled = new CheckBox
{
Left = 18, Top = 442, Width = 620,
Text = "Reporting 数据读取",
Checked = reporting.Enabled
Text = "使Agent 连接授权读取(无需单独确认)",
Checked = true,
Enabled = false
};
var reportingAccountEnabled = new CheckBox
{
@@ -544,7 +540,7 @@ internal static class ServiceSettingsEditor
var reportingNote = new Label
{
Left = 18, Top = 674, Width = 620, Height = 36,
Text = "每行一个 chatId通讯录/会话读取只返回白名单范围。"
Text = "连接成功即授权当前已验证账号的通讯录会话和消息读取;下方旧范围仅为兼容显示。"
};
remoteTab.Controls.AddRange([
remoteEnabled, remoteAddressLabel, remoteAddressBox, remoteNodeLabel, remoteNodeBox,
@@ -569,7 +565,7 @@ internal static class ServiceSettingsEditor
{
foreach (var control in remoteInputs) control.Enabled = remoteEnabled.Checked;
reportingEnabled.Enabled = remoteEnabled.Checked;
var reportingInputsEnabled = remoteEnabled.Checked && reportingEnabled.Checked;
var reportingInputsEnabled = false;
foreach (var control in reportingInputs) control.Enabled = reportingInputsEnabled;
}
remoteEnabled.CheckedChanged += (_, _) => SetRemoteEnabled();
@@ -608,49 +604,9 @@ internal static class ServiceSettingsEditor
AllowInsecureHttp = allowInsecureHttp.Checked
}
: null;
var reportingOptions = reporting;
if (remoteEnabled.Checked && reportingEnabled.Checked)
{
var accountId = Optional(reportingAccountBox);
if (accountId is null)
{
MessageBox.Show("启用 Reporting 时必须填写账号 ID。", "WxAgent 设置无效", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var allowedChats = Lines(groupChatsBox).Select(chatId => new AllowedChat
{
Type = ReportingChatType.Group,
ChatId = chatId,
Enabled = true,
IdentityVerified = reportingIdentityConfirmed.Checked
}).Concat(Lines(privateChatsBox).Select(chatId => new AllowedChat
{
Type = ReportingChatType.Private,
ChatId = chatId,
Enabled = true,
IdentityVerified = reportingIdentityConfirmed.Checked
})).ToArray();
var account = new AccountReportingConfig
{
AccountId = accountId,
Enabled = reportingAccountEnabled.Checked,
AllowedChats = allowedChats
};
var accounts = reporting.Accounts
.Where(existing => !string.Equals(existing.AccountId, accountId, StringComparison.Ordinal))
.Append(account)
.ToArray();
reportingOptions = reporting with
{
Enabled = true,
ConfigVersion = checked(reporting.ConfigVersion + 1),
Accounts = accounts
};
}
else if (remoteEnabled.Checked && !reportingEnabled.Checked)
{
reportingOptions = reporting with { Enabled = false };
}
var reportingOptions = remoteEnabled.Checked
? reporting with { Enabled = true, ConfigVersion = checked(Math.Max(1, reporting.ConfigVersion) + 1) }
: reporting;
var edited = new ServiceOptions
{
ListenUrl = $"http://{formattedHost}:{portBox.Value}",