feat: 接入端到端 demo 与签名变体
- Flutter 桌面端通过 dart:ffi 调用 Rust Core cdylib - 实现 ECDH P-256 密钥协商、HKDF 子密钥派生、AES-256-GCM 控制帧 - Android 被控端用派生密钥做控制帧加密与媒体包 MAC - 新增 libopus JNI 骨架,默认回退 MediaCodec - 新增 aosp/zhanxun/neutral 签名变体与系统 sharedUserId - 本机独立信令服务与设置页开关 - 新增 .gitignore 排除所有编译产物与打包目录
This commit is contained in:
commit
31b9a52eb4
154 files changed
+13287
No files matched your search
@@ -0,0 +1,195 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func sampleDevice(id string) Device {
|
||||
return Device{
|
||||
DeviceID: id,
|
||||
Name: "Pixel",
|
||||
IP: "192.168.1.10",
|
||||
MediaPort: 41000,
|
||||
ControlPort: 41001,
|
||||
FP: "sha256:ab12",
|
||||
Cap: []string{"h264", "file"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterAndList(t *testing.T) {
|
||||
reg := New(45 * time.Second)
|
||||
if _, err := reg.Register(sampleDevice("a")); err != nil {
|
||||
t.Fatalf("register: %v", err)
|
||||
}
|
||||
if _, err := reg.Register(sampleDevice("b")); err != nil {
|
||||
t.Fatalf("register: %v", err)
|
||||
}
|
||||
devices := reg.List()
|
||||
if len(devices) != 2 {
|
||||
t.Fatalf("期望 2 台设备,实际 %d", len(devices))
|
||||
}
|
||||
if devices[0].DeviceID != "a" {
|
||||
t.Fatalf("列表未按 deviceId 排序: %+v", devices)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeartbeatAndExpiry(t *testing.T) {
|
||||
now := time.Now()
|
||||
reg := New(10 * time.Second)
|
||||
reg.now = func() time.Time { return now }
|
||||
|
||||
events, unsubscribe := reg.Subscribe()
|
||||
defer unsubscribe()
|
||||
if _, err := reg.Register(sampleDevice("a")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if event := <-events; event.Event != "online" {
|
||||
t.Fatalf("期望 online,实际 %s", event.Event)
|
||||
}
|
||||
|
||||
now = now.Add(8 * time.Second)
|
||||
if !reg.Heartbeat("a") {
|
||||
t.Fatal("心跳应成功")
|
||||
}
|
||||
if event := <-events; event.Event != "heartbeat" {
|
||||
t.Fatalf("期望 heartbeat,实际 %s", event.Event)
|
||||
}
|
||||
|
||||
now = now.Add(11 * time.Second)
|
||||
if devices := reg.List(); len(devices) != 0 {
|
||||
t.Fatalf("过期设备应被摘除,实际 %+v", devices)
|
||||
}
|
||||
if event := <-events; event.Event != "offline" {
|
||||
t.Fatalf("期望 offline,实际 %s", event.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRejectInvalidDevice(t *testing.T) {
|
||||
reg := New(time.Minute)
|
||||
device := sampleDevice("a")
|
||||
device.MediaPort = 0
|
||||
if _, err := reg.Register(device); err == nil {
|
||||
t.Fatal("非法端口应被拒绝")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user