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:
dbildhauerandClaude Opus 5.5 committed 2026-10-25 15:11:40 +01:00
1 parent db700fd003
commit 4aebac5336
23 files changed
+1894 -1

No files matched your search

+139
View File
@@ -0,0 +1,139 @@
// Package config loads and stores the agent configuration (server, token, options).
package config
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
)
// Direction controls which way the clipboard is synchronised.
type Direction string
const (
Both Direction = "both"
Send Direction = "send"
Receive Direction = "receive"
)
// Config is stored as JSON; the file contains the token and is only readable by the user.
type Config struct {
Server string `json:"server"`
Token string `json:"token"`
Direction Direction `json:"direction"`
// Project for clips sent by the agent; created if missing. Empty: no project.
Project string `json:"project"`
// Number of own clips kept on the server; older ones are deleted by the agent.
Keep int `json:"keep"`
MaxChars int `json:"max_chars"`
Exclude []string `json:"exclude"`
PollMs int `json:"poll_ms"`
}
// Defaults returns the configuration used for missing fields.
func Defaults() Config {
return Config{
Direction: Both,
Project: "Zwischenablage",
Keep: 50,
MaxChars: 100_000,
Exclude: []string{},
PollMs: 700,
}
}
// DefaultPath is <user config dir>/lan-clipboard/agent.json.
func DefaultPath() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "lan-clipboard", "agent.json"), nil
}
// StatePath is the file next to the config that remembers the agent's own clips.
func StatePath(configPath string) string {
return filepath.Join(filepath.Dir(configPath), "agent-state.json")
}
// Load reads the config file on top of the defaults and validates it.
func Load(path string) (Config, error) {
cfg := Defaults()
data, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return cfg, fmt.Errorf("keine Konfiguration unter %s – zuerst \"lc-agent setup\" ausführen", path)
}
return cfg, err
}
if err := json.Unmarshal(data, &cfg); err != nil {
return cfg, fmt.Errorf("%s: %w", path, err)
}
return cfg, cfg.Validate()
}
// Save writes the config atomically with permissions 0600 (directory 0700).
func Save(path string, cfg Config) error {
if err := cfg.Validate(); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, append(data, '\n'), 0o600); err != nil {
return err
}
return os.Rename(tmp, path)
}
// Validate checks all fields; the token is only sent over HTTPS (or to localhost).
func (c Config) Validate() error {
u, err := url.Parse(c.Server)
if err != nil || u.Host == "" {
return fmt.Errorf("ungültige Server-Adresse %q", c.Server)
}
local := u.Hostname() == "localhost" || u.Hostname() == "127.0.0.1"
if u.Scheme != "https" && !(u.Scheme == "http" && local) {
return errors.New("die Server-Adresse muss mit https:// beginnen")
}
if !strings.HasPrefix(c.Token, "lc_") {
return errors.New("der Token muss mit lc_ beginnen (unter \"Konto\" → \"Geräte-Tokens\" erstellen)")
}
switch c.Direction {
case Both, Send, Receive:
default:
return fmt.Errorf("ungültige Richtung %q (both, send oder receive)", c.Direction)
}
if c.Keep < 1 || c.MaxChars < 1 || c.PollMs < 100 {
return errors.New("keep und max_chars müssen ≥ 1 sein, poll_ms ≥ 100")
}
_, err = c.Patterns()
return err
}
// Patterns compiles the exclude expressions.
func (c Config) Patterns() ([]*regexp.Regexp, error) {
patterns := make([]*regexp.Regexp, 0, len(c.Exclude))
for _, expr := range c.Exclude {
re, err := regexp.Compile(expr)
if err != nil {
return nil, fmt.Errorf("Ausschlussmuster %q: %w", expr, err)
}
patterns = append(patterns, re)
}
return patterns, nil
}
// Sends and Receives tell whether the direction includes that way.
func (d Direction) Sends() bool { return d == Both || d == Send }
func (d Direction) Receives() bool { return d == Both || d == Receive }
+87
View File
@@ -0,0 +1,87 @@
package config
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func valid() Config {
cfg := Defaults()
cfg.Server = "https://clip.example.org"
cfg.Token = "lc_abc"
return cfg
}
func TestSaveAndLoad(t *testing.T) {
path := filepath.Join(t.TempDir(), "sub", "agent.json")
cfg := valid()
cfg.Exclude = []string{`^\d{6}$`}
if err := Save(path, cfg); err != nil {
t.Fatal(err)
}
got, err := Load(path)
if err != nil {
t.Fatal(err)
}
if got.Server != cfg.Server || got.Token != cfg.Token || got.Exclude[0] != cfg.Exclude[0] {
t.Fatalf("roundtrip mismatch: %+v", got)
}
if runtime.GOOS != "windows" {
info, _ := os.Stat(path)
if info.Mode().Perm() != 0o600 {
t.Fatalf("config readable by others: %v", info.Mode())
}
}
}
func TestLoadFillsDefaults(t *testing.T) {
path := filepath.Join(t.TempDir(), "agent.json")
os.WriteFile(path, []byte(`{"server":"https://x.org","token":"lc_x"}`), 0o600)
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.Direction != Both || cfg.Keep != 50 || cfg.Project != "Zwischenablage" {
t.Fatalf("defaults missing: %+v", cfg)
}
}
func TestMissingFileExplainsSetup(t *testing.T) {
_, err := Load(filepath.Join(t.TempDir(), "nope.json"))
if err == nil || !strings.Contains(err.Error(), "lc-agent setup") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidate(t *testing.T) {
cases := map[string]func(*Config){
"http to remote": func(c *Config) { c.Server = "http://clip.example.org" },
"no host": func(c *Config) { c.Server = "https://" },
"bad token": func(c *Config) { c.Token = "secret" },
"bad direction": func(c *Config) { c.Direction = "sideways" },
"keep zero": func(c *Config) { c.Keep = 0 },
"poll too fast": func(c *Config) { c.PollMs = 10 },
"bad exclude": func(c *Config) { c.Exclude = []string{"("} },
}
for name, mutate := range cases {
cfg := valid()
mutate(&cfg)
if cfg.Validate() == nil {
t.Errorf("%s: expected an error", name)
}
}
local := valid()
local.Server = "http://localhost:8000"
if err := local.Validate(); err != nil {
t.Errorf("localhost over http should be allowed: %v", err)
}
}
func TestDirection(t *testing.T) {
if !Both.Sends() || !Both.Receives() || Send.Receives() || Receive.Sends() {
t.Fatal("direction helpers wrong")
}
}