feat: mac发送完整支持4.1.13以上版本, 完善授权

This commit is contained in:
Wxw-Gu committed 2026-11-04 15:49:15 +08:00
1 parent 9db08dba1a
commit 8c162969ff
18 files changed
+1841 -238

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
/**
* macOS 设备授权接口的 wire 类型。
* 所有字段都由 `parseMacLicenseStatus` 运行时校验后才会交给上层使用。
*/
export type MacLicenseState =
| 'unlicensed' // no license for this device
| 'valid' // online check passed in this process
| 'cached' // local cache verified, not re-confirmed online
| 'expired'
| 'revoked'
| 'device_mismatch' // token belongs to a different device
| 'identity_error' // device key unusable
| 'server_unavailable' // network / 5xx; cache may still hold
| 'checking' // an online refresh is in flight
| 'unsupported' // host is running but has no /license endpoints (old build)
| 'runtime_missing' // host not reachable / runtime absent
| 'platform_unsupported' // not macOS native (Windows / OSS)
const MAC_LICENSE_STATES = new Set<MacLicenseState>([
'unlicensed',
'valid',
'cached',
'expired',
'revoked',
'device_mismatch',
'identity_error',
'server_unavailable',
'checking',
'unsupported',
'runtime_missing',
'platform_unsupported'
])
export interface MacLicenseStatus {
deviceId: string | null
state: MacLicenseState
authorized: boolean
source: 'online' | 'cache' | 'none'
features: string[]
lastCheckedAt: string | null
licenseExpiresAt: string | null
tokenExpiresAt: string | null
message: string
/** False when the running host predates the license endpoints (404). */
runtimeProtocolSupported?: boolean
/** Host-reported numeric TM_WECHAT_* code when a refresh failed. */
errorCode?: number | null
}
const DEVICE_ID_PATTERN = /^mac_[0-9a-f]{64}$/
function asOptionalString(value: unknown): string | null {
return typeof value === 'string' && value.length > 0 ? value : null
}
function asOptionalIsoDate(value: unknown): string | null {
if (typeof value !== 'string' || value.length === 0) return null
// Accept RFC3339 (what the native core emits); reject obvious garbage.
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(value)) return null
return value
}
function asStringArray(value: unknown): string[] {
if (!Array.isArray(value)) return []
return value.filter((entry): entry is string => typeof entry === 'string')
}
/**
* 把任意 JSON 校验成 MacLicenseStatus;无法确认的字段一律落到安全默认值。
* `authorized` 由 state 重算,伪造的 `authorized: true` 覆盖不了已撤销状态。
*/
export function parseMacLicenseStatus(raw: unknown): MacLicenseStatus {
const base: MacLicenseStatus = {
deviceId: null,
state: 'unlicensed',
authorized: false,
source: 'none',
features: [],
lastCheckedAt: null,
licenseExpiresAt: null,
tokenExpiresAt: null,
message: '当前设备暂无发送能力授权'
}
if (raw === null || typeof raw !== 'object') return base
const record = raw as Record<string, unknown>
const deviceId = asOptionalString(record.deviceId)
if (deviceId !== null && DEVICE_ID_PATTERN.test(deviceId)) {
base.deviceId = deviceId
}
const state = asOptionalString(record.state)
if (state !== null && MAC_LICENSE_STATES.has(state as MacLicenseState)) {
base.state = state as MacLicenseState
}
const source = asOptionalString(record.source)
if (source === 'online' || source === 'cache' || source === 'none') {
base.source = source
}
base.features = asStringArray(record.features)
base.lastCheckedAt = asOptionalIsoDate(record.lastCheckedAt)
base.licenseExpiresAt = asOptionalIsoDate(record.licenseExpiresAt)
base.tokenExpiresAt = asOptionalIsoDate(record.tokenExpiresAt)
const message = asOptionalString(record.message)
if (message !== null) base.message = message
if (typeof record.runtimeProtocolSupported === 'boolean') {
base.runtimeProtocolSupported = record.runtimeProtocolSupported
}
if (typeof record.errorCode === 'number' && Number.isFinite(record.errorCode)) {
base.errorCode = record.errorCode
}
/*
* authorized is DERIVED, not copied: only a small whitelist of states may
* present as authorized, and even then the payload's own boolean must agree.
* This keeps a forged {"authorized": true} from flipping a revoked state.
*/
const trustedStates: ReadonlySet<MacLicenseState> = new Set(['valid', 'cached'])
base.authorized = trustedStates.has(base.state) && record.authorized === true
return base
}
/**
* Build the "runtime missing / old host" status without a network round-trip.
* Used when the host is down or answered 404 on /license/status.
*/
export function offlineMacLicenseStatus(
state: 'runtime_missing' | 'unsupported' | 'platform_unsupported' | 'server_unavailable',
message: string
): MacLicenseStatus {
return {
deviceId: null,
state,
authorized: false,
source: 'none',
features: [],
lastCheckedAt: null,
licenseExpiresAt: null,
tokenExpiresAt: null,
message,
runtimeProtocolSupported: state === 'unsupported' ? false : undefined
}
}
@@ -15,6 +15,13 @@ export type MacWechatRuntimeLifecycle =
| 'crashed_unbound' // died while unbound — safe to auto-restart
| 'crashed_bound' // died while bound — WeChat keeps an orphan agent
export type MacWechatSendRuntimeAvailability = 'unavailable' | 'component_error' | 'available'
export interface MacWechatSendRuntimeAvailabilityResult {
state: MacWechatSendRuntimeAvailability
message: string
}
export type MacWechatHostRuntimeState =
| 'not_initialized'
| 'wechat_not_running'
@@ -0,0 +1,103 @@
import type { MacLicenseStatus } from './personal-wechat-mac-license'
import type { MacWechatSendRuntimeAvailabilityResult } from './personal-wechat-mac-runtime'
import type { PersonalWechatSenderStatus } from './personal-wechat'
export type MacSendPrimaryActionKind = 'none' | 'bind' | 'send'
export interface MacSendPrimaryAction {
label: string
disabled: boolean
loading: boolean
action: MacSendPrimaryActionKind
reason:
| 'runtime_unavailable'
| 'runtime_error'
| 'license_pending'
| 'license_invalid'
| 'license_feature_denied'
| 'binding'
| 'unbound'
| 'initializing'
| 'ready'
}
export function hasValidWechatLicense(license: MacLicenseStatus | null): boolean {
if (
!license ||
!license.authorized ||
(license.state !== 'valid' && license.state !== 'cached') ||
!license.features.includes('wechat')
) {
return false
}
const tokenExpiresAt = license.tokenExpiresAt ? Date.parse(license.tokenExpiresAt) : Number.NaN
return Number.isFinite(tokenExpiresAt) && tokenExpiresAt > Date.now()
}
export function resolveMacSendPrimaryAction(input: {
runtime: MacWechatSendRuntimeAvailabilityResult | null
license: MacLicenseStatus | null
senderStatus: PersonalWechatSenderStatus | null
binding: boolean
detecting: boolean
}): MacSendPrimaryAction {
const { runtime, license, senderStatus, binding, detecting } = input
const result = (
label: string,
disabled: boolean,
loading: boolean,
action: MacSendPrimaryActionKind,
reason: MacSendPrimaryAction['reason']
): MacSendPrimaryAction => ({ label, disabled, loading, action, reason })
if (!runtime || (detecting && !senderStatus)) {
return result('正在检查发送组件…', true, true, 'none', 'runtime_error')
}
if (runtime.state === 'unavailable') {
return result('当前版本没有微信发送能力', true, false, 'none', 'runtime_unavailable')
}
if (runtime.state === 'component_error') {
return result('发送组件缺失或异常', true, false, 'none', 'runtime_error')
}
if (license === null) {
return result('正在检测授权…', true, true, 'none', 'license_pending')
}
if (license.state === 'valid' || license.state === 'cached') {
if (license.authorized && !license.features.includes('wechat')) {
return result('未开通微信发送', true, false, 'none', 'license_feature_denied')
}
if (hasValidWechatLicense(license)) {
if (binding) return result('正在绑定…', true, true, 'none', 'binding')
const bound = Boolean(
senderStatus &&
(senderStatus.state === 'online' ||
Boolean(
senderStatus.wechatPid &&
senderStatus.boundWechatPid === senderStatus.wechatPid &&
senderStatus.attachReady &&
senderStatus.baseAddressReady
))
)
if (!bound) return result('绑定微信', false, false, 'bind', 'unbound')
if (senderStatus?.canSend) return result('开始发送', false, false, 'send', 'ready')
return result('正在准备发送能力…', true, detecting, 'none', 'initializing')
}
}
const bound = Boolean(
senderStatus &&
(senderStatus.state === 'online' ||
Boolean(
senderStatus.wechatPid &&
senderStatus.boundWechatPid === senderStatus.wechatPid &&
senderStatus.attachReady &&
senderStatus.baseAddressReady
))
)
return result(
bound ? '授权后可开始发送' : '授权后可绑定微信',
true,
false,
'none',
'license_invalid'
)
}