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>
140 lines
4.0 KiB
Go
140 lines
4.0 KiB
Go
// 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 }
|