- Flutter 桌面端通过 dart:ffi 调用 Rust Core cdylib - 实现 ECDH P-256 密钥协商、HKDF 子密钥派生、AES-256-GCM 控制帧 - Android 被控端用派生密钥做控制帧加密与媒体包 MAC - 新增 libopus JNI 骨架,默认回退 MediaCodec - 新增 aosp/zhanxun/neutral 签名变体与系统 sharedUserId - 本机独立信令服务与设置页开关 - 新增 .gitignore 排除所有编译产物与打包目录
196 lines
4.3 KiB
Go
196 lines
4.3 KiB
Go
package registry
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Device struct {
|
|
DeviceID string `json:"deviceId"`
|
|
Name string `json:"name"`
|
|
IP string `json:"ip"`
|
|
MediaPort int `json:"mediaPort"`
|
|
ControlPort int `json:"controlPort"`
|
|
FP string `json:"fp"`
|
|
Cap []string `json:"cap"`
|
|
TTL int `json:"ttl,omitempty"`
|
|
Model string `json:"model,omitempty"`
|
|
Res string `json:"res,omitempty"`
|
|
Sig string `json:"sig,omitempty"`
|
|
LastSeen time.Time `json:"-"`
|
|
}
|
|
|
|
type Event struct {
|
|
Event string `json:"event"`
|
|
DeviceID string `json:"deviceId"`
|
|
Device *Device `json:"device,omitempty"`
|
|
}
|
|
|
|
type Registry struct {
|
|
mu sync.RWMutex
|
|
devices map[string]Device
|
|
subscribers map[chan Event]struct{}
|
|
expiry time.Duration
|
|
now func() time.Time
|
|
}
|
|
|
|
func New(expiry time.Duration) *Registry {
|
|
if expiry <= 0 {
|
|
expiry = 45 * time.Second
|
|
}
|
|
return &Registry{
|
|
devices: make(map[string]Device),
|
|
subscribers: make(map[chan Event]struct{}),
|
|
expiry: expiry,
|
|
now: time.Now,
|
|
}
|
|
}
|
|
|
|
func (r *Registry) Register(device Device) (Device, error) {
|
|
if err := validate(device); err != nil {
|
|
return Device{}, err
|
|
}
|
|
device.Cap = append([]string(nil), device.Cap...)
|
|
if device.TTL <= 0 {
|
|
device.TTL = 30
|
|
}
|
|
device.LastSeen = r.now()
|
|
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
_, existed := r.devices[device.DeviceID]
|
|
r.devices[device.DeviceID] = device
|
|
eventType := "online"
|
|
if existed {
|
|
eventType = "heartbeat"
|
|
}
|
|
stored := device
|
|
r.notifyLocked(Event{Event: eventType, DeviceID: device.DeviceID, Device: &stored})
|
|
return stored, nil
|
|
}
|
|
|
|
func (r *Registry) Heartbeat(deviceID string) bool {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
device, ok := r.devices[deviceID]
|
|
if !ok {
|
|
return false
|
|
}
|
|
if r.expired(device) {
|
|
delete(r.devices, deviceID)
|
|
r.notifyLocked(Event{Event: "offline", DeviceID: deviceID})
|
|
return false
|
|
}
|
|
device.LastSeen = r.now()
|
|
r.devices[deviceID] = device
|
|
r.notifyLocked(Event{Event: "heartbeat", DeviceID: deviceID})
|
|
return true
|
|
}
|
|
|
|
func (r *Registry) Unregister(deviceID string) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if _, ok := r.devices[deviceID]; !ok {
|
|
return
|
|
}
|
|
delete(r.devices, deviceID)
|
|
r.notifyLocked(Event{Event: "offline", DeviceID: deviceID})
|
|
}
|
|
|
|
func (r *Registry) Get(deviceID string) (Device, bool) {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
device, ok := r.devices[deviceID]
|
|
return device, ok
|
|
}
|
|
|
|
func (r *Registry) List() []Device {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.purgeLocked()
|
|
devices := make([]Device, 0, len(r.devices))
|
|
for _, device := range r.devices {
|
|
device.Cap = append([]string(nil), device.Cap...)
|
|
devices = append(devices, device)
|
|
}
|
|
sort.Slice(devices, func(i, j int) bool {
|
|
return devices[i].DeviceID < devices[j].DeviceID
|
|
})
|
|
return devices
|
|
}
|
|
|
|
func (r *Registry) Subscribe() (chan Event, func()) {
|
|
events := make(chan Event, 16)
|
|
r.mu.Lock()
|
|
r.subscribers[events] = struct{}{}
|
|
r.mu.Unlock()
|
|
return events, func() {
|
|
r.mu.Lock()
|
|
if _, ok := r.subscribers[events]; ok {
|
|
delete(r.subscribers, events)
|
|
close(events)
|
|
}
|
|
r.mu.Unlock()
|
|
}
|
|
}
|
|
|
|
func (r *Registry) StartJanitor(ctx context.Context, interval time.Duration) {
|
|
if interval <= 0 {
|
|
interval = 5 * time.Second
|
|
}
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
r.mu.Lock()
|
|
r.purgeLocked()
|
|
r.mu.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *Registry) expired(device Device) bool {
|
|
return r.now().Sub(device.LastSeen) > r.expiry
|
|
}
|
|
|
|
func (r *Registry) purgeLocked() {
|
|
for id, device := range r.devices {
|
|
if r.expired(device) {
|
|
delete(r.devices, id)
|
|
r.notifyLocked(Event{Event: "offline", DeviceID: id})
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *Registry) notifyLocked(event Event) {
|
|
for subscriber := range r.subscribers {
|
|
select {
|
|
case subscriber <- event:
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
func validate(device Device) error {
|
|
if strings.TrimSpace(device.DeviceID) == "" {
|
|
return fmt.Errorf("deviceId 不能为空")
|
|
}
|
|
if strings.TrimSpace(device.IP) == "" {
|
|
return fmt.Errorf("ip 不能为空")
|
|
}
|
|
if device.MediaPort < 1 || device.MediaPort > 65535 {
|
|
return fmt.Errorf("mediaPort 超出范围")
|
|
}
|
|
if device.ControlPort < 1 || device.ControlPort > 65535 {
|
|
return fmt.Errorf("controlPort 超出范围")
|
|
}
|
|
return nil
|
|
}
|