added other dsconnect settings

This commit is contained in:
2026-02-19 11:13:14 +01:00
parent 019c60689e
commit 09af99946e
13 changed files with 204 additions and 35 deletions
+28 -5
View File
@@ -7,6 +7,7 @@ import (
"sync"
"time"
"github.com/duffy/usb-server/internal/config"
"github.com/duffy/usb-server/internal/protocol"
"github.com/duffy/usb-server/internal/usb"
"github.com/duffy/usb-server/internal/usbip"
@@ -16,6 +17,7 @@ import (
// ShareManager handles sharing USB devices
type ShareManager struct {
client *Client
cfg *config.Config
mu sync.RWMutex
devices []usb.Device
active map[string]*activeShare // busID -> active share
@@ -38,9 +40,10 @@ type shareTunnel struct {
}
// NewShareManager creates a share manager
func NewShareManager(client *Client) *ShareManager {
func NewShareManager(client *Client, cfg *config.Config) *ShareManager {
sm := &ShareManager{
client: client,
cfg: cfg,
active: make(map[string]*activeShare),
tunnels: make(map[string]*shareTunnel),
}
@@ -50,6 +53,7 @@ func NewShareManager(client *Client) *ShareManager {
client.OnReleaseDevice = sm.handleReleaseDevice
client.OnTunnelData = sm.handleTunnelData
client.OnClientLeft = sm.handleClientLeft
client.OnForceRelease = sm.handleForceRelease
return sm
}
@@ -129,10 +133,11 @@ func (sm *ShareManager) broadcastDeviceList() {
}
msg := &protocol.DeviceList{
Type: protocol.MsgDeviceList,
ClientID: sm.client.ID(),
ClientName: sm.client.Config().Name,
Devices: protoDevices,
Type: protocol.MsgDeviceList,
ClientID: sm.client.ID(),
ClientName: sm.client.Config().Name,
Devices: protoDevices,
AllowForceDetach: sm.cfg.AllowForceDetach,
}
sm.client.SendJSON(msg)
@@ -312,6 +317,24 @@ func (sm *ShareManager) handleReleaseDevice(busID, fromClient string) {
sm.broadcastDeviceList()
}
func (sm *ShareManager) handleForceRelease(targetClient, fromClient, busID string) {
if !sm.cfg.AllowForceDetach {
log.Printf("[share] force-release denied for %s (not allowed by config)", busID)
return
}
sm.mu.RLock()
share, exists := sm.active[busID]
sm.mu.RUnlock()
if !exists {
return
}
log.Printf("[share] force-releasing %s (requested by %s, was used by %s)", busID, fromClient[:8], share.usedBy[:8])
sm.handleReleaseDevice(busID, share.usedBy)
}
func (sm *ShareManager) handleClientLeft(msg *protocol.ClientLeft) {
sm.mu.RLock()
var toRelease []string