feat: desktop-agent für windows, macos und linux
Go-Programm lc-agent: sendet neu kopierten Text als Clip ins Projekt "Zwischenablage" und übernimmt eigene Clips anderer Geräte live in die Zwischenablage. Von Passwort-Managern markierte Inhalte werden nie gesendet; Größengrenze, Ausschlussmuster, nur die letzten 50 eigenen Clips. Wiederholt beim Start, bis der Server erreichbar ist; beendet sich bei widerrufenem Token mit Exit-Code 3. Build für alle Plattformen per build.sh im Container, Autostart-Vorlagen für alle drei Systeme. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
db700fd003
commit
4aebac5336
23 files changed
+1894
-1
No files matched your search
@@ -0,0 +1,16 @@
|
||||
// Package clipboard reads and writes the system clipboard (text only) and detects
|
||||
// content that password managers mark as secret.
|
||||
package clipboard
|
||||
|
||||
// Content is the current clipboard text. Concealed is set when a password manager
|
||||
// marked it as secret; the text is then left empty and must never be sent anywhere.
|
||||
type Content struct {
|
||||
Text string
|
||||
Concealed bool
|
||||
}
|
||||
|
||||
// Clipboard is implemented per operating system (see New in the *_<os>.go files).
|
||||
type Clipboard interface {
|
||||
Read() (Content, error)
|
||||
Write(text string) error
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package clipboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// JXA script: returns the pasteboard's change count and, if it changed since the count
|
||||
// passed as argument, its types and text. One osascript call per poll, no cgo needed.
|
||||
const readScript = `
|
||||
ObjC.import('AppKit');
|
||||
function run(argv) {
|
||||
const pb = $.NSPasteboard.generalPasteboard;
|
||||
const count = Number(pb.changeCount);
|
||||
if (String(count) === argv[0]) return JSON.stringify({ count: count, same: true });
|
||||
const types = ObjC.deepUnwrap(pb.types) || [];
|
||||
const text = ObjC.unwrap(pb.stringForType($.NSPasteboardTypeString)) || '';
|
||||
return JSON.stringify({ count: count, types: types, text: text });
|
||||
}`
|
||||
|
||||
type darwinClipboard struct {
|
||||
lastCount int
|
||||
last Content
|
||||
}
|
||||
|
||||
func New() (Clipboard, error) {
|
||||
return &darwinClipboard{lastCount: -1}, nil
|
||||
}
|
||||
|
||||
func (c *darwinClipboard) Read() (Content, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
out, err := exec.CommandContext(ctx, "osascript", "-l", "JavaScript", "-e", readScript,
|
||||
strconv.Itoa(c.lastCount)).Output()
|
||||
if err != nil {
|
||||
return Content{}, err
|
||||
}
|
||||
var result struct {
|
||||
Count int `json:"count"`
|
||||
Same bool `json:"same"`
|
||||
Types []string `json:"types"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
if err := json.Unmarshal(out, &result); err != nil {
|
||||
return Content{}, err
|
||||
}
|
||||
if result.Same {
|
||||
return c.last, nil
|
||||
}
|
||||
c.lastCount = result.Count
|
||||
if isConcealed(result.Types) {
|
||||
c.last = Content{Concealed: true}
|
||||
} else {
|
||||
c.last = Content{Text: result.Text}
|
||||
}
|
||||
return c.last, nil
|
||||
}
|
||||
|
||||
func (c *darwinClipboard) Write(text string) error {
|
||||
cmd := exec.Command("pbcopy")
|
||||
// pbcopy only keeps non-ASCII text intact with a UTF-8 locale
|
||||
cmd.Env = append(os.Environ(), "LANG=en_US.UTF-8")
|
||||
cmd.Stdin = strings.NewReader(text)
|
||||
return cmd.Run()
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package clipboard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const commandTimeout = 3 * time.Second
|
||||
|
||||
// Linux has no clipboard API without a toolkit; the standard tools are used instead:
|
||||
// wl-clipboard on Wayland, xclip on X11.
|
||||
type linuxClipboard struct {
|
||||
listTypes []string
|
||||
read func(textType string) []string
|
||||
write []string
|
||||
}
|
||||
|
||||
// New picks Wayland or X11 depending on the session and the installed tools.
|
||||
func New() (Clipboard, error) {
|
||||
if os.Getenv("WAYLAND_DISPLAY") != "" && hasCommand("wl-paste") && hasCommand("wl-copy") {
|
||||
return &linuxClipboard{
|
||||
listTypes: []string{"wl-paste", "--list-types"},
|
||||
read: func(t string) []string { return []string{"wl-paste", "--no-newline", "--type", t} },
|
||||
write: []string{"wl-copy"},
|
||||
}, nil
|
||||
}
|
||||
if os.Getenv("DISPLAY") != "" && hasCommand("xclip") {
|
||||
return &linuxClipboard{
|
||||
listTypes: []string{"xclip", "-selection", "clipboard", "-t", "TARGETS", "-o"},
|
||||
read: func(t string) []string { return []string{"xclip", "-selection", "clipboard", "-t", t, "-o"} },
|
||||
write: []string{"xclip", "-selection", "clipboard", "-i"},
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.New("keine Zwischenablage gefunden: unter Wayland wl-clipboard, unter X11 xclip installieren")
|
||||
}
|
||||
|
||||
func hasCommand(name string) bool {
|
||||
_, err := exec.LookPath(name)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func run(args []string, stdin string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), commandTimeout)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
|
||||
if stdin != "" {
|
||||
cmd.Stdin = strings.NewReader(stdin)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
err := cmd.Run()
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
func (c *linuxClipboard) Read() (Content, error) {
|
||||
output, err := run(c.listTypes, "")
|
||||
if err != nil {
|
||||
// Empty clipboard: the tools exit with an error; not worth reporting
|
||||
return Content{}, nil
|
||||
}
|
||||
types := parseTypes(output)
|
||||
if isConcealed(types) {
|
||||
return Content{Concealed: true}, nil
|
||||
}
|
||||
textType := textType(types)
|
||||
if textType == "" {
|
||||
return Content{}, nil
|
||||
}
|
||||
text, err := run(c.read(textType), "")
|
||||
if err != nil {
|
||||
return Content{}, err
|
||||
}
|
||||
return Content{Text: text}, nil
|
||||
}
|
||||
|
||||
func (c *linuxClipboard) Write(text string) error {
|
||||
// xclip and wl-copy keep serving the selection in the background after we return
|
||||
cmd := exec.Command(c.write[0], c.write[1:]...)
|
||||
cmd.Stdin = strings.NewReader(text)
|
||||
return cmd.Run()
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package clipboard
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const (
|
||||
cfUnicodeText = 13
|
||||
gmemMoveable = 0x0002
|
||||
)
|
||||
|
||||
var (
|
||||
user32 = windows.NewLazySystemDLL("user32.dll")
|
||||
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
||||
openClipboard = user32.NewProc("OpenClipboard")
|
||||
closeClipboard = user32.NewProc("CloseClipboard")
|
||||
emptyClipboard = user32.NewProc("EmptyClipboard")
|
||||
getClipboardData = user32.NewProc("GetClipboardData")
|
||||
setClipboardData = user32.NewProc("SetClipboardData")
|
||||
isClipboardFormatAvailable = user32.NewProc("IsClipboardFormatAvailable")
|
||||
registerClipboardFormat = user32.NewProc("RegisterClipboardFormatW")
|
||||
globalAlloc = kernel32.NewProc("GlobalAlloc")
|
||||
globalFree = kernel32.NewProc("GlobalFree")
|
||||
globalLock = kernel32.NewProc("GlobalLock")
|
||||
globalUnlock = kernel32.NewProc("GlobalUnlock")
|
||||
)
|
||||
|
||||
// Formats that password managers and other tools set to keep content out of
|
||||
// clipboard history and sync tools (documented by Microsoft for cloud clipboard).
|
||||
var concealedFormats = []string{
|
||||
"ExcludeClipboardContentFromMonitorProcessing",
|
||||
"Clipboard Viewer Ignore",
|
||||
}
|
||||
|
||||
type windowsClipboard struct {
|
||||
concealed []uintptr
|
||||
}
|
||||
|
||||
func New() (Clipboard, error) {
|
||||
c := &windowsClipboard{}
|
||||
for _, name := range concealedFormats {
|
||||
ptr, err := windows.UTF16PtrFromString(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if id, _, _ := registerClipboardFormat.Call(uintptr(unsafe.Pointer(ptr))); id != 0 {
|
||||
c.concealed = append(c.concealed, id)
|
||||
}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// lockedMemory turns the address returned by GlobalLock into a pointer. The memory is
|
||||
// owned by Windows and locked, not part of the Go heap, so the garbage collector cannot
|
||||
// move it; reading the uintptr through a pointer keeps "go vet" from flagging the
|
||||
// (here intended) uintptr conversion.
|
||||
func lockedMemory(addr uintptr) unsafe.Pointer {
|
||||
return *(*unsafe.Pointer)(unsafe.Pointer(&addr))
|
||||
}
|
||||
|
||||
// open retries briefly: another program may hold the clipboard for a moment.
|
||||
func open() error {
|
||||
for i := 0; i < 10; i++ {
|
||||
if r, _, _ := openClipboard.Call(0); r != 0 {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
return errors.New("Zwischenablage ist belegt")
|
||||
}
|
||||
|
||||
func available(format uintptr) bool {
|
||||
r, _, _ := isClipboardFormatAvailable.Call(format)
|
||||
return r != 0
|
||||
}
|
||||
|
||||
func (c *windowsClipboard) Read() (Content, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
if err := open(); err != nil {
|
||||
return Content{}, err
|
||||
}
|
||||
defer closeClipboard.Call()
|
||||
|
||||
for _, format := range c.concealed {
|
||||
if available(format) {
|
||||
return Content{Concealed: true}, nil
|
||||
}
|
||||
}
|
||||
if !available(cfUnicodeText) {
|
||||
return Content{}, nil
|
||||
}
|
||||
handle, _, _ := getClipboardData.Call(cfUnicodeText)
|
||||
if handle == 0 {
|
||||
return Content{}, nil
|
||||
}
|
||||
ptr, _, _ := globalLock.Call(handle)
|
||||
if ptr == 0 {
|
||||
return Content{}, errors.New("Zwischenablage nicht lesbar")
|
||||
}
|
||||
defer globalUnlock.Call(handle)
|
||||
return Content{Text: windows.UTF16PtrToString((*uint16)(lockedMemory(ptr)))}, nil
|
||||
}
|
||||
|
||||
func (c *windowsClipboard) Write(text string) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
data, err := windows.UTF16FromString(text)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := open(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer closeClipboard.Call()
|
||||
emptyClipboard.Call()
|
||||
|
||||
size := uintptr(len(data) * 2)
|
||||
handle, _, _ := globalAlloc.Call(gmemMoveable, size)
|
||||
if handle == 0 {
|
||||
return errors.New("kein Speicher für die Zwischenablage")
|
||||
}
|
||||
ptr, _, _ := globalLock.Call(handle)
|
||||
if ptr == 0 {
|
||||
globalFree.Call(handle)
|
||||
return errors.New("kein Speicher für die Zwischenablage")
|
||||
}
|
||||
copy(unsafe.Slice((*uint16)(lockedMemory(ptr)), len(data)), data)
|
||||
globalUnlock.Call(handle)
|
||||
// On success the system owns the memory; only free it if the hand-over failed
|
||||
if r, _, _ := setClipboardData.Call(cfUnicodeText, handle); r == 0 {
|
||||
globalFree.Call(handle)
|
||||
return errors.New("Zwischenablage konnte nicht gesetzt werden")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package clipboard
|
||||
|
||||
import "strings"
|
||||
|
||||
// Markers set by password managers (KeePassXC, 1Password, Bitwarden, …) on Linux and macOS.
|
||||
// Windows uses registered clipboard formats instead (see clipboard_windows.go).
|
||||
var concealedTypes = map[string]bool{
|
||||
"x-kde-passwordManagerHint": true, // Linux (KDE convention, KeePassXC)
|
||||
"org.nspasteboard.ConcealedType": true, // macOS
|
||||
"org.nspasteboard.TransientType": true, // macOS: not meant to be kept
|
||||
"org.nspasteboard.AutoGeneratedType": true,
|
||||
}
|
||||
|
||||
// Text types offered by X11/Wayland clipboards, in order of preference.
|
||||
var textTypes = []string{"text/plain;charset=utf-8", "UTF8_STRING", "text/plain", "STRING", "TEXT"}
|
||||
|
||||
// parseTypes splits the output of "wl-paste --list-types" or "xclip -t TARGETS -o".
|
||||
func parseTypes(output string) []string {
|
||||
var types []string
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
if line = strings.TrimSpace(line); line != "" {
|
||||
types = append(types, line)
|
||||
}
|
||||
}
|
||||
return types
|
||||
}
|
||||
|
||||
func isConcealed(types []string) bool {
|
||||
for _, t := range types {
|
||||
if concealedTypes[t] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// textType returns the best text type on offer, or "" (e.g. only an image was copied).
|
||||
func textType(types []string) string {
|
||||
offered := make(map[string]bool, len(types))
|
||||
for _, t := range types {
|
||||
offered[t] = true
|
||||
}
|
||||
for _, t := range textTypes {
|
||||
if offered[t] {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package clipboard
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseTypes(t *testing.T) {
|
||||
got := parseTypes("TARGETS\n UTF8_STRING \n\nx-kde-passwordManagerHint\n")
|
||||
if len(got) != 3 || got[1] != "UTF8_STRING" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcealedAndTextType(t *testing.T) {
|
||||
keepass := []string{"TARGETS", "x-kde-passwordManagerHint", "UTF8_STRING"}
|
||||
if !isConcealed(keepass) {
|
||||
t.Fatal("KeePassXC hint not detected")
|
||||
}
|
||||
if isConcealed([]string{"UTF8_STRING", "text/plain"}) {
|
||||
t.Fatal("plain text treated as secret")
|
||||
}
|
||||
if got := textType([]string{"STRING", "text/plain;charset=utf-8"}); got != "text/plain;charset=utf-8" {
|
||||
t.Fatalf("preferred type %q", got)
|
||||
}
|
||||
if textType([]string{"image/png"}) != "" {
|
||||
t.Fatal("image treated as text")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user