Add lease-bound Remote key IPC and SDK validation
This commit is contained in:
1 parent
9ac33560dc
commit
792cac5d0b
12 files changed
+807
-9
No files matched your search
@@ -0,0 +1,91 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package tentaclewire
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const RemoteKeyQueueSize = 256
|
||||
const RemoteKeyQueueBytes = 256 * 1024
|
||||
|
||||
// RemoteKey identifies one logical, unshifted key, never an X keycode, Windows
|
||||
// VK, scan code, text to type or command to execute. Native ownership and
|
||||
// destination-layout resolution belong to a reader input owner.
|
||||
type RemoteKey struct {
|
||||
Name string `json:"name"`
|
||||
Pressed bool `json:"pressed"`
|
||||
}
|
||||
|
||||
type RemoteKeyRequest struct {
|
||||
RemoteOperation
|
||||
Key RemoteKey `json:"key"`
|
||||
}
|
||||
|
||||
func ValidRemoteKey(key RemoteKey) bool {
|
||||
name := key.Name
|
||||
if len(name) == 1 && (name[0] >= 'a' && name[0] <= 'z' || name[0] >= '0' && name[0] <= '9') {
|
||||
return true
|
||||
}
|
||||
if len(name) >= 2 && len(name) <= 3 && name[0] == 'F' {
|
||||
number, err := strconv.Atoi(name[1:])
|
||||
return err == nil && number >= 1 && number <= 24 && strconv.Itoa(number) == name[1:]
|
||||
}
|
||||
if len(name) == 4 && name[:3] == "KP_" && name[3] >= '0' && name[3] <= '9' {
|
||||
return true
|
||||
}
|
||||
switch name {
|
||||
case "BackSpace", "Tab", "Return", "Escape", "space", "Pause", "Print", "Menu",
|
||||
"Caps_Lock", "Num_Lock", "Scroll_Lock", "Shift_L", "Shift_R", "Control_L", "Control_R",
|
||||
"Alt_L", "Alt_R", "Super_L", "Super_R", "Page_Up", "Page_Down", "End", "Home",
|
||||
"Left", "Up", "Right", "Down", "Insert", "Delete",
|
||||
"KP_Begin", "KP_Enter", "KP_Page_Up", "KP_Page_Down", "KP_End", "KP_Home",
|
||||
"KP_Left", "KP_Up", "KP_Right", "KP_Down", "KP_Insert", "KP_Delete",
|
||||
"KP_Multiply", "KP_Add", "KP_Separator", "KP_Subtract", "KP_Decimal", "KP_Divide",
|
||||
"semicolon", "equal", "comma", "minus", "period", "slash", "grave", "bracketleft",
|
||||
"backslash", "bracketright", "apostrophe":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ValidRemoteKeyRequest(request RemoteKeyRequest) bool {
|
||||
return len(request.Lease) == 32 && request.Epoch != 0 && request.Sequence != 0 && ValidRemoteKey(request.Key)
|
||||
}
|
||||
|
||||
func (key *RemoteKey) UnmarshalJSON(data []byte) error {
|
||||
var fields map[string]json.RawMessage
|
||||
if DecodeStrict(data, &fields) != nil || len(fields) != 2 || fields["name"] == nil || fields["pressed"] == nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
var value struct {
|
||||
Name string `json:"name"`
|
||||
Pressed *bool `json:"pressed"`
|
||||
}
|
||||
if DecodeStrict(data, &value) != nil || value.Pressed == nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
result := RemoteKey{Name: value.Name, Pressed: *value.Pressed}
|
||||
if !ValidRemoteKey(result) {
|
||||
return ErrProtocol
|
||||
}
|
||||
*key = result
|
||||
return nil
|
||||
}
|
||||
|
||||
func (request *RemoteKeyRequest) UnmarshalJSON(data []byte) error {
|
||||
var fields map[string]json.RawMessage
|
||||
if DecodeStrict(data, &fields) != nil || len(fields) != 4 || fields["lease"] == nil || fields["epoch"] == nil || fields["sequence"] == nil || fields["key"] == nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
type plain RemoteKeyRequest
|
||||
var value plain
|
||||
if DecodeStrict(data, &value) != nil || !ValidRemoteKeyRequest(RemoteKeyRequest(value)) {
|
||||
return ErrProtocol
|
||||
}
|
||||
*request = RemoteKeyRequest(value)
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user