mirror of
https://github.com/db9979/sshtermy.git
synced 2026-12-06 04:34:08 +01:00
Team-Ordner: Team-Paket automatisch aktuell halten und abonnieren
- Teilen mit „Aktuell halten“: sshTermy schreibt das Team-Paket neu, sobald sich die geteilten Verbindungen ändern (Prüfung jede Minute, ohne Änderung kein Schreiben). Aus einem Ordner geteilt = ganzer Ordner, neue Verbindungen gehen automatisch mit; Ordnerpfade relativ zum Quellordner. - Übernehmen mit „Abonnieren“: Datei alle 5 min lesen, Änderungen übernehmen, beim Absender gelöschte Verbindungen auch hier entfernen (mit Grabstein für den eigenen Sync). Eigene Zugangsdaten bleiben. - Ort: Dateipfad (Netzlaufwerk, OneDrive, Teams) oder https/WebDAV mit Anmeldung in der Adresse (Passwort wird in der Anzeige ausgeblendet). - Strg+Leertaste → „Team-Ordner“: Stand, jetzt abgleichen/veröffentlichen, beenden. domain.Data.TeamLinks ist rein lokal (nicht im Sync). - Tests: team (Ordner, Prüfsumme, Löschen, Skip, http mit Basic-Auth), ui (zwei Stores veröffentlichen/abonnieren, falsches Passwort). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUh25v4VzGH24nRbTQK
This commit is contained in:
13 files changed
+1029
-15
No files matched your search
@@ -493,6 +493,18 @@ Der Kollege nimmt Strg+Leertaste → „Team-Paket übernehmen“: Datei, Passwo
|
||||
Vorschau zeigt neue und aktualisierte Einträge. Wer dasselbe Paket später erneut übernimmt, bekommt die Änderungen;
|
||||
eigene Passwörter, Keys und Benutzernamen bleiben erhalten.
|
||||
|
||||
### Team-Ordner: automatisch aktuell
|
||||
|
||||
Leg die Datei beim Teilen in einen Ordner, den alle erreichen (Netzlaufwerk, OneDrive- oder Teams-Ordner) oder gib
|
||||
eine https-/WebDAV-Adresse an (`https://benutzer:passwort@nas:5006/team/acme.sshteam`), und schalte „Aktuell halten“
|
||||
ein. Dann schreibt sshTermy die Datei neu, sobald sich an den Verbindungen etwas ändert (Prüfung jede Minute). Wer aus
|
||||
einem Ordner im Manager geteilt hat, teilt den ganzen Ordner: Neue Verbindungen darin gehen automatisch mit.
|
||||
|
||||
Kollegen lassen beim Übernehmen „Abonnieren“ an. sshTermy liest die Datei dann alle 5 Minuten und übernimmt
|
||||
Änderungen: neue Verbindungen kommen dazu, geänderte werden aktualisiert, beim Absender gelöschte verschwinden auch
|
||||
hier. Eigene Passwörter, Keys und Benutzernamen bleiben. Strg+Leertaste → „Team-Ordner“ zeigt den Stand, gleicht
|
||||
sofort ab oder beendet ein Abo (die Verbindungen bleiben dann). Team-Ordner sind rein lokal und gehen nicht in den Sync.
|
||||
|
||||
## Lizenz
|
||||
|
||||
sshTermy steht unter der [MIT-Lizenz](LICENSE): Du darfst es benutzen, kopieren, ändern und weitergeben, auch
|
||||
|
||||
@@ -494,6 +494,18 @@ The colleague uses Ctrl+Space → „Import team package“: file, password, tar
|
||||
preview shows new and updated entries. Importing the same package again updates the entries but keeps the
|
||||
colleague's own passwords, keys and user names.
|
||||
|
||||
### Team folders: kept up to date
|
||||
|
||||
When sharing, put the file into a folder everyone can reach (network drive, OneDrive or Teams folder) or enter an
|
||||
https/WebDAV address (`https://user:password@nas:5006/team/acme.sshteam`) and turn on “Keep up to date”. sshTermy
|
||||
then rewrites the file whenever the connections change (checked every minute). If you shared from a folder in the
|
||||
manager, the whole folder is shared: new connections in it are included automatically.
|
||||
|
||||
Colleagues leave “Subscribe” on when importing. sshTermy then re-reads the file every 5 minutes and applies changes:
|
||||
new connections are added, changed ones updated, and ones the sender deleted disappear here too. Their own
|
||||
passwords, keys and user names stay. Ctrl+Space → “Team folders” shows the status, checks right away or ends a
|
||||
subscription (the connections stay). Team folders are local only and not part of the sync.
|
||||
|
||||
## License
|
||||
|
||||
sshTermy is released under the [MIT License](LICENSE): you may use, copy, change and pass it on, also commercially,
|
||||
|
||||
@@ -517,6 +517,46 @@ type Data struct {
|
||||
// vor Connection.KeyPath, wenn der Key hier woanders liegt als auf dem
|
||||
// Rechner, der die Verbindung angelegt hat.
|
||||
LocalKeys map[string]string `json:"local_keys,omitempty"`
|
||||
|
||||
// TeamLinks: Team-Ordner (veröffentlichen bzw. abonnieren). Rein lokal:
|
||||
// Pfade sind je Rechner anders, das Team-Passwort bleibt auf dem Gerät.
|
||||
TeamLinks []TeamLink `json:"team_links,omitempty"`
|
||||
}
|
||||
|
||||
// Rollen eines Team-Ordners.
|
||||
const (
|
||||
TeamPublish = "pub" // dieser Rechner schreibt das Team-Paket bei Änderungen neu
|
||||
TeamSubscribe = "sub" // dieser Rechner liest es regelmäßig und übernimmt Änderungen
|
||||
)
|
||||
|
||||
// TeamLink ist ein Team-Paket in einem gemeinsamen Ordner (Netzlaufwerk,
|
||||
// OneDrive, Teams) oder unter einer https-/WebDAV-Adresse.
|
||||
type TeamLink struct {
|
||||
ID string `json:"id"`
|
||||
Role string `json:"role"` // TeamPublish | TeamSubscribe
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"` // Datei oder https://[user:pw@]host/…/x.sshteam
|
||||
TeamPass Secret `json:"team_pass"`
|
||||
|
||||
// Veröffentlichen: was ins Paket geht. Folder = ganzer Ordner (auch
|
||||
// später dazugekommene Verbindungen), IDs = einzeln gewählte.
|
||||
Folder string `json:"folder,omitempty"`
|
||||
IDs []string `json:"ids,omitempty"`
|
||||
From string `json:"from,omitempty"`
|
||||
Users bool `json:"users,omitempty"`
|
||||
QuickCmds bool `json:"quick,omitempty"`
|
||||
Runbooks bool `json:"runbooks,omitempty"`
|
||||
Tunnels bool `json:"tunnels,omitempty"`
|
||||
|
||||
// Abonnieren: wohin und was schon da ist.
|
||||
Into string `json:"into,omitempty"` // Zielordner, z. B. „Team“
|
||||
DefaultUser string `json:"user,omitempty"` // für Verbindungen ohne Benutzernamen
|
||||
Have []string `json:"have,omitempty"` // IDs, die zuletzt aus dem Paket kamen
|
||||
Skip []string `json:"skip,omitempty"` // in der Vorschau abgewählt: nie übernehmen
|
||||
|
||||
LastSum string `json:"last_sum,omitempty"` // Stand des zuletzt geschriebenen/gelesenen Pakets
|
||||
LastRun time.Time `json:"last_run,omitempty"` // letzte Änderung (geschrieben/übernommen)
|
||||
LastErr string `json:"last_err,omitempty"`
|
||||
}
|
||||
|
||||
// Relay ist ein gekoppelter Vermittler.
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package i18n
|
||||
|
||||
// Texte zum Team-Ordner (veröffentlichen/abonnieren). Eigene Datei, damit
|
||||
// parallele Änderungen an en.go nicht kollidieren.
|
||||
func init() {
|
||||
for k, v := range enTeamAbo {
|
||||
en[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
var enTeamAbo = map[string]string{
|
||||
"\nSag den Kollegen den Ort der Datei und das Passwort (getrennt).\nSie übernehmen sie mit Strg+Leertaste → „Team-Paket übernehmen“ und „Abonnieren“.": "\nTell your colleagues where the file is and the password (separately).\nThey import it with Ctrl+Space → “Import team package” and “Subscribe”.",
|
||||
" · abonniert": " · subscribed",
|
||||
"%d Verbindungen · in „%s“": "%d connections · in “%s”",
|
||||
"Abo": "Subscription",
|
||||
"Abo beenden": "End subscription",
|
||||
"Abo beenden?": "End subscription?",
|
||||
"Abonnieren": "Subscribe",
|
||||
"Aktuell halten": "Keep up to date",
|
||||
"Automatisch aktuell: Änderungen an diesen Verbindungen schreibt sshTermy in die Datei (Strg+Leertaste → „Team-Ordner“).\n": "Kept up to date: sshTermy writes changes to these connections into the file (Ctrl+Space → “Team folders”).\n",
|
||||
"Datei alle 5 min neu lesen und Änderungen des Teams automatisch übernehmen": "re-read the file every 5 min and apply the team's changes automatically",
|
||||
"Datei bleibt liegen": "file stays in place",
|
||||
"Jetzt abgleichen": "Check now",
|
||||
"Jetzt veröffentlichen": "Publish now",
|
||||
"Nicht mehr veröffentlichen": "Stop publishing",
|
||||
"Pfad zur .sshteam-Datei oder https://…/x.sshteam (Einfügen mit Strg+V)": "path to the .sshteam file or https://…/x.sshteam (paste with Ctrl+V)",
|
||||
"Team-Ordner": "Team folders",
|
||||
"Team-Ordner (veröffentlicht / abonniert)": "Team folders (published / subscribed)",
|
||||
"Team-Ordner beenden?": "Stop this team folder?",
|
||||
"Team-Ordner „%s“ beendet": "Team folder “%s” stopped",
|
||||
"Team-Ordner „%s“ veröffentlicht: %s": "Team folder “%s” published: %s",
|
||||
"Team-Ordner „%s“: %d neu, %d aktualisiert, %d entfernt": "Team folder “%s”: %d new, %d updated, %d removed",
|
||||
"Team-Ordner „%s“: %s": "Team folder “%s”: %s",
|
||||
"Team-Ordner „%s“: keine Änderungen": "Team folder “%s”: no changes",
|
||||
"Team-Ordner: sshTermy schreibt die Datei bei jeder Änderung neu": "team folder: sshTermy rewrites the file on every change",
|
||||
"Verbindungen bleiben": "connections stay",
|
||||
"Veröffentlicht": "Published",
|
||||
"einzeln gewählt": "picked one by one",
|
||||
"kein Team-Paket an diesem Ort gefunden": "no team package found at this location",
|
||||
"z. B. in einen gemeinsamen Ordner (Teams, OneDrive, NAS) oder https://…/x.sshteam (WebDAV)": "e.g. a shared folder (Teams, OneDrive, NAS) or https://…/x.sshteam (WebDAV)",
|
||||
"Änderungen aus „%s“ kommen nicht mehr an. Die übernommenen Verbindungen bleiben und gehören ab jetzt dir.": "Changes from “%s” no longer arrive. The imported connections stay and are yours from now on.",
|
||||
"„%s“ wird nicht mehr aktualisiert. Die Datei %s bleibt liegen; lösche sie dort, wenn das Team sie nicht mehr bekommen soll.": "“%s” is no longer updated. The file %s stays in place; delete it there if the team should no longer get it.",
|
||||
"Noch keine Team-Ordner.\n\nVeröffentlichen: Strg+Leertaste → „Mit dem Team teilen“, als Datei einen gemeinsamen Ordner wählen und „Aktuell halten“ einschalten.\n\nAbonnieren: Strg+Leertaste → „Team-Paket übernehmen“ mit „Abonnieren“. sshTermy liest die Datei dann alle 5 Minuten und übernimmt Änderungen.": "No team folders yet.\n\nPublish: Ctrl+Space → “Share with the team”, choose a file in a shared folder and turn on “Keep up to date”.\n\nSubscribe: Ctrl+Space → “Import team package” with “Subscribe”. sshTermy then re-reads the file every 5 minutes and applies changes.",
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/db9979/sshtermy/internal/domain"
|
||||
"github.com/db9979/sshtermy/internal/vault"
|
||||
)
|
||||
|
||||
// Team-Ordner sind rein lokal wie Vermittler: kein Grabstein, nie in der
|
||||
// Sync-Datei. persist statt save, damit ein Abgleich keinen Sync auslöst.
|
||||
|
||||
func (s *Store) TeamLinks() []domain.TeamLink {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
if s.data == nil {
|
||||
return nil
|
||||
}
|
||||
return append([]domain.TeamLink(nil), s.data.TeamLinks...)
|
||||
}
|
||||
|
||||
// UpsertTeamLink legt an (ID leer) oder ersetzt.
|
||||
func (s *Store) UpsertTeamLink(l domain.TeamLink) (domain.TeamLink, error) {
|
||||
s.mu.Lock()
|
||||
if s.data == nil {
|
||||
s.mu.Unlock()
|
||||
return l, vault.ErrLocked
|
||||
}
|
||||
if l.ID == "" {
|
||||
l.ID = "t" + newID()
|
||||
s.data.TeamLinks = append(s.data.TeamLinks, l)
|
||||
} else {
|
||||
for i := range s.data.TeamLinks {
|
||||
if s.data.TeamLinks[i].ID == l.ID {
|
||||
s.data.TeamLinks[i] = l
|
||||
}
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return l, s.persist()
|
||||
}
|
||||
|
||||
func (s *Store) DeleteTeamLink(id string) error {
|
||||
s.mu.Lock()
|
||||
if s.data == nil {
|
||||
s.mu.Unlock()
|
||||
return vault.ErrLocked
|
||||
}
|
||||
ls := s.data.TeamLinks[:0]
|
||||
for _, l := range s.data.TeamLinks {
|
||||
if l.ID != id {
|
||||
ls = append(ls, l)
|
||||
}
|
||||
}
|
||||
s.data.TeamLinks = ls
|
||||
s.mu.Unlock()
|
||||
return s.persist()
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
package team
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/db9979/sshtermy/internal/domain"
|
||||
"github.com/db9979/sshtermy/internal/i18n"
|
||||
)
|
||||
|
||||
// Team-Ordner: Das Team-Paket liegt an einem festen Ort, den alle erreichen
|
||||
// (Netzlaufwerk, OneDrive/Teams-Ordner, NAS per https/WebDAV). Wer
|
||||
// veröffentlicht, schreibt es bei jeder Änderung neu; wer abonniert, liest es
|
||||
// regelmäßig und übernimmt Änderungen samt gelöschter Verbindungen.
|
||||
|
||||
// ErrNotFound: am Ort liegt (noch) kein Team-Paket.
|
||||
var ErrNotFound = i18n.NewError("kein Team-Paket an diesem Ort gefunden")
|
||||
|
||||
// IsURL: Ort ist eine http(s)-Adresse statt eines Dateipfads.
|
||||
func IsURL(loc string) bool {
|
||||
l := strings.ToLower(strings.TrimSpace(loc))
|
||||
return strings.HasPrefix(l, "https://") || strings.HasPrefix(l, "http://")
|
||||
}
|
||||
|
||||
// ShowLoc zeigt einen Ort ohne Passwort aus der Adresse.
|
||||
func ShowLoc(loc string) string {
|
||||
if !IsURL(loc) {
|
||||
return loc
|
||||
}
|
||||
u, err := url.Parse(strings.TrimSpace(loc))
|
||||
if err != nil {
|
||||
return loc
|
||||
}
|
||||
if u.User != nil {
|
||||
u.User = url.User(u.User.Username())
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func request(ctx context.Context, method, loc string, body []byte) (*http.Response, error) {
|
||||
u, err := url.Parse(strings.TrimSpace(loc))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var user, pass string
|
||||
if u.User != nil {
|
||||
user = u.User.Username()
|
||||
pass, _ = u.User.Password()
|
||||
u.User = nil
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, method, u.String(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user != "" {
|
||||
req.SetBasicAuth(user, pass)
|
||||
}
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
// ReadLoc liest das Team-Paket von einem Ort.
|
||||
func ReadLoc(ctx context.Context, loc string) ([]byte, error) {
|
||||
if !IsURL(loc) {
|
||||
b, err := os.ReadFile(loc)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return b, err
|
||||
}
|
||||
resp, err := request(ctx, http.MethodGet, loc, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if resp.StatusCode/100 != 2 {
|
||||
return nil, fmt.Errorf("GET: HTTP %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
}
|
||||
return io.ReadAll(io.LimitReader(resp.Body, 32<<20))
|
||||
}
|
||||
|
||||
// WriteLoc schreibt das Team-Paket (Datei: erst temporär, dann umbenennen,
|
||||
// damit Leser nie eine halbe Datei sehen).
|
||||
func WriteLoc(ctx context.Context, loc string, data []byte) error {
|
||||
if !IsURL(loc) {
|
||||
if err := os.MkdirAll(filepath.Dir(loc), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp := filepath.Join(filepath.Dir(loc), "."+filepath.Base(loc)+".tmp")
|
||||
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, loc)
|
||||
}
|
||||
resp, err := request(ctx, http.MethodPut, loc, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode/100 != 2 {
|
||||
return fmt.Errorf("PUT: HTTP %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sum: Prüfsumme für „hat sich etwas geändert?“.
|
||||
func Sum(b []byte) string { h := sha256.Sum256(b); return hex.EncodeToString(h[:16]) }
|
||||
|
||||
// LinkOptions: Paket-Optionen eines veröffentlichten Team-Ordners.
|
||||
func LinkOptions(l domain.TeamLink) Options {
|
||||
return Options{Users: l.Users, QuickCmds: l.QuickCmds, Runbooks: l.Runbooks, Tunnels: l.Tunnels}
|
||||
}
|
||||
|
||||
// LinkIDs: Verbindungen, die ein veröffentlichter Team-Ordner enthält
|
||||
// (ganzer Quellordner samt neuer Verbindungen plus einzeln gewählte).
|
||||
func LinkIDs(d *domain.Data, l domain.TeamLink) []string {
|
||||
want := map[string]bool{}
|
||||
for _, id := range l.IDs {
|
||||
want[id] = true
|
||||
}
|
||||
var out []string
|
||||
for _, c := range d.Connections {
|
||||
if want[c.ID] || (l.Folder != "" && domain.InFolder(c.Group, l.Folder)) {
|
||||
out = append(out, c.ID)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// BuildLink stellt das Paket eines veröffentlichten Team-Ordners zusammen.
|
||||
// sum ändert sich nur, wenn sich der Inhalt ändert (nicht mit der Uhrzeit).
|
||||
func BuildLink(d *domain.Data, l domain.TeamLink, now time.Time) (p *Package, sum string) {
|
||||
p, _ = Build(d, LinkIDs(d, l), l.From, LinkOptions(l), now)
|
||||
if l.Folder != "" {
|
||||
// Ordnerpfad relativ zum Quellordner, sonst stünde beim Kollegen
|
||||
// „Team / Kunden / ACME / …“ statt „Team / …“
|
||||
parent := domain.PathParent(l.Folder)
|
||||
for i := range p.Connections {
|
||||
if g := p.Connections[i].Group; parent != "" && domain.InFolder(g, parent) {
|
||||
p.Connections[i].Group = strings.TrimPrefix(strings.TrimPrefix(g, parent), domain.PathSep)
|
||||
}
|
||||
}
|
||||
}
|
||||
q := *p
|
||||
q.Created = time.Time{}
|
||||
b, _ := json.Marshal(q)
|
||||
return p, Sum(b)
|
||||
}
|
||||
|
||||
// Pull übernimmt ein gelesenes Paket in einen abonnierten Team-Ordner:
|
||||
// neue und geänderte Verbindungen wie Apply, dazu werden Verbindungen
|
||||
// entfernt, die früher aus dem Paket kamen und jetzt fehlen. Liefert die
|
||||
// neue Have-Liste zurück.
|
||||
func Pull(d *domain.Data, p *Package, l domain.TeamLink, now time.Time) (st Stats, have []string) {
|
||||
skip := map[string]bool{}
|
||||
for _, id := range l.Skip {
|
||||
skip[id] = true
|
||||
}
|
||||
ids := map[string]bool{}
|
||||
for _, c := range p.Connections {
|
||||
if !skip[c.ID] && c.ID != "" {
|
||||
ids[c.ID] = true
|
||||
}
|
||||
}
|
||||
st = Apply(d, p, ids, l.Into, l.DefaultUser, now)
|
||||
var gone []string
|
||||
for _, id := range l.Have {
|
||||
if !ids[id] && !skip[id] {
|
||||
gone = append(gone, id)
|
||||
}
|
||||
}
|
||||
st.Removed = Remove(d, gone, now)
|
||||
for id := range ids {
|
||||
have = append(have, id)
|
||||
}
|
||||
sort.Strings(have)
|
||||
return st, have
|
||||
}
|
||||
|
||||
// Remove löscht Verbindungen samt ihrer Tunnel und Runbooks und legt
|
||||
// Grabsteine an, damit der eigene Sync sie auch auf den anderen Rechnern
|
||||
// entfernt.
|
||||
func Remove(d *domain.Data, ids []string, now time.Time) int {
|
||||
if len(ids) == 0 {
|
||||
return 0
|
||||
}
|
||||
del := map[string]bool{}
|
||||
for _, id := range ids {
|
||||
del[id] = true
|
||||
}
|
||||
if d.Deleted == nil {
|
||||
d.Deleted = map[string]time.Time{}
|
||||
}
|
||||
now = now.UTC()
|
||||
n := 0
|
||||
cs := d.Connections[:0]
|
||||
for _, c := range d.Connections {
|
||||
if del[c.ID] {
|
||||
d.Deleted[c.ID] = now
|
||||
delete(d.LocalKeys, c.ID)
|
||||
n++
|
||||
continue
|
||||
}
|
||||
cs = append(cs, c)
|
||||
}
|
||||
d.Connections = cs
|
||||
ts := d.Tunnels[:0]
|
||||
for _, t := range d.Tunnels {
|
||||
if del[t.ConnectionID] {
|
||||
d.Deleted[t.ID] = now
|
||||
continue
|
||||
}
|
||||
ts = append(ts, t)
|
||||
}
|
||||
d.Tunnels = ts
|
||||
rs := d.Runbooks[:0]
|
||||
for _, r := range d.Runbooks {
|
||||
if r.ConnectionID != "" && del[r.ConnectionID] {
|
||||
d.Deleted[r.ID] = now
|
||||
continue
|
||||
}
|
||||
rs = append(rs, r)
|
||||
}
|
||||
d.Runbooks = rs
|
||||
return n
|
||||
}
|
||||
@@ -225,6 +225,7 @@ type Stats struct {
|
||||
Runbooks int
|
||||
QuickCmds int // Verbindungen mit eigenen Schnellbefehlen
|
||||
NeedUser, NeedPw int // ohne Benutzername / mit Passwort-Anmeldung (fragt beim Verbinden)
|
||||
Removed int // Team-Ordner: beim Absender entfernt, hier gelöscht
|
||||
}
|
||||
|
||||
// Known: welche Verbindungen des Pakets es hier schon gibt (gleiche ID, also
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package team
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -102,3 +107,123 @@ func TestApplyKeepsOwnCredentials(t *testing.T) {
|
||||
t.Fatalf("Auswahl: %+v", st)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkPublishPull(t *testing.T) {
|
||||
now := time.Date(2026, 11, 6, 12, 0, 0, 0, time.UTC)
|
||||
src := domain.NewData()
|
||||
src.Connections = []domain.Connection{
|
||||
{ID: "a", Alias: "web", Host: "10.0.0.1", Group: "Kunden / ACME", Password: "geheim", Auth: domain.AuthPassword},
|
||||
{ID: "b", Alias: "db", Host: "10.0.0.2", Group: "Kunden / ACME / DB"},
|
||||
{ID: "c", Alias: "privat", Host: "10.0.0.3", Group: "Privat"},
|
||||
}
|
||||
pub := domain.TeamLink{Role: domain.TeamPublish, Folder: "Kunden / ACME", TeamPass: "team-pass-123"}
|
||||
p, sum1 := BuildLink(src, pub, now)
|
||||
if len(p.Connections) != 2 {
|
||||
t.Fatalf("Ordner: %d Verbindungen", len(p.Connections))
|
||||
}
|
||||
if p.Connections[0].Group != "ACME" || p.Connections[1].Group != "ACME / DB" {
|
||||
t.Fatalf("Ordner relativ: %q %q", p.Connections[0].Group, p.Connections[1].Group)
|
||||
}
|
||||
if _, again := BuildLink(src, pub, now.Add(time.Hour)); again != sum1 {
|
||||
t.Fatal("Prüfsumme hängt an der Uhrzeit")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
loc := filepath.Join(dir, "acme.sshteam")
|
||||
raw, err := Seal(p, []byte(pub.TeamPass))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := WriteLoc(context.Background(), loc, raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dst := domain.NewData()
|
||||
sub := domain.TeamLink{Role: domain.TeamSubscribe, Path: loc, Into: "Team", DefaultUser: "kollege"}
|
||||
got, err := ReadLoc(context.Background(), loc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pk, err := Open(got, []byte("team-pass-123"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st, have := Pull(dst, pk, sub, now)
|
||||
if st.Added != 2 || len(have) != 2 || dst.Connections[0].Group != "Team / ACME" || dst.Connections[0].Username != "kollege" {
|
||||
t.Fatalf("erste Übernahme: %+v %v %+v", st, have, dst.Connections)
|
||||
}
|
||||
sub.Have = have
|
||||
|
||||
// Absender löscht db, benennt web um, legt neue an
|
||||
src.Connections = []domain.Connection{
|
||||
{ID: "a", Alias: "web-neu", Host: "10.0.0.1", Group: "Kunden / ACME"},
|
||||
{ID: "d", Alias: "mail", Host: "10.0.0.4", Group: "Kunden / ACME"},
|
||||
}
|
||||
p2, sum2 := BuildLink(src, pub, now)
|
||||
if sum2 == sum1 {
|
||||
t.Fatal("Änderung nicht erkannt")
|
||||
}
|
||||
st, have = Pull(dst, p2, sub, now)
|
||||
if st.Added != 1 || st.Updated != 1 || st.Removed != 1 || len(have) != 2 {
|
||||
t.Fatalf("zweite Übernahme: %+v %v", st, have)
|
||||
}
|
||||
if _, ok := dst.Deleted["b"]; !ok {
|
||||
t.Fatal("kein Grabstein für die entfernte Verbindung")
|
||||
}
|
||||
for _, c := range dst.Connections {
|
||||
if c.ID == "a" && (c.Alias != "web-neu" || c.Username != "kollege") {
|
||||
t.Fatalf("Aktualisierung: %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
// Abgewählte Verbindung bleibt draußen und wird nicht gelöscht
|
||||
sub.Have, sub.Skip = have, []string{"d"}
|
||||
dst.Connections = append(dst.Connections, domain.Connection{ID: "x", Alias: "eigene"})
|
||||
st, _ = Pull(dst, p2, sub, now)
|
||||
if st.Removed != 0 || st.Updated != 1 {
|
||||
t.Fatalf("mit Skip: %+v", st)
|
||||
}
|
||||
if len(dst.Connections) != 3 {
|
||||
t.Fatalf("eigene Verbindung angefasst: %+v", dst.Connections)
|
||||
}
|
||||
if _, err := ReadLoc(context.Background(), filepath.Join(dir, "fehlt.sshteam")); err != ErrNotFound {
|
||||
t.Fatalf("fehlende Datei: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocHTTP(t *testing.T) {
|
||||
var stored []byte
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if u, p, ok := r.BasicAuth(); !ok || u != "nas" || p != "pw" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodPut:
|
||||
stored, _ = io.ReadAll(r.Body)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
case http.MethodGet:
|
||||
if stored == nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Write(stored)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
loc := strings.Replace(srv.URL, "http://", "http://nas:pw@", 1) + "/team/acme.sshteam"
|
||||
if ShowLoc(loc) != strings.Replace(srv.URL, "http://", "http://nas@", 1)+"/team/acme.sshteam" {
|
||||
t.Fatalf("Passwort sichtbar: %s", ShowLoc(loc))
|
||||
}
|
||||
if _, err := ReadLoc(context.Background(), loc); err != ErrNotFound {
|
||||
t.Fatalf("leer: %v", err)
|
||||
}
|
||||
if err := WriteLoc(context.Background(), loc, []byte("x")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, err := ReadLoc(context.Background(), loc); err != nil || string(b) != "x" {
|
||||
t.Fatalf("lesen: %q %v", b, err)
|
||||
}
|
||||
if _, err := ReadLoc(context.Background(), srv.URL+"/team/acme.sshteam"); err == nil {
|
||||
t.Fatal("ohne Anmeldung gelesen")
|
||||
}
|
||||
}
|
||||
+13
-7
@@ -60,8 +60,8 @@ const (
|
||||
scrKeyForm // Schlüsseldatei auf diesem Rechner wählen (nach Sync)
|
||||
scrSSHKeyForm // SSH-Schlüssel erzeugen / übertragen (sshkey.go)
|
||||
scrRelayForm // Vermittler koppeln (relay.go)
|
||||
scrTeam // Team-Freigabe: Auswahl bzw. Vorschau (team.go)
|
||||
scrTeamForm // Team-Freigabe: Passwort, Datei, Optionen
|
||||
scrTeam // Team-Freigabe: Auswahl bzw. Vorschau (team.go)
|
||||
scrTeamForm // Team-Freigabe: Passwort, Datei, Optionen
|
||||
)
|
||||
|
||||
// App ist das Wurzel-Modell.
|
||||
@@ -109,11 +109,12 @@ type App struct {
|
||||
|
||||
sync syncState
|
||||
wiz *syncWizard
|
||||
imp importState // Import aus ssh-config, PuTTY, MobaXterm
|
||||
team teamState // Team-Freigabe (team.go)
|
||||
keyEd keyEditor // Tastenkürzel anpassen
|
||||
keys keymap // aktuelle Tastenbelegung
|
||||
lookSig string // Theme+Tasten, zuletzt übernommen
|
||||
imp importState // Import aus ssh-config, PuTTY, MobaXterm
|
||||
team teamState // Team-Freigabe (team.go)
|
||||
tl teamLinkState // Team-Ordner: veröffentlichen/abonnieren (teamlink.go)
|
||||
keyEd keyEditor // Tastenkürzel anpassen
|
||||
keys keymap // aktuelle Tastenbelegung
|
||||
lookSig string // Theme+Tasten, zuletzt übernommen
|
||||
|
||||
z zones // klickbare Bereiche des letzten Bildes
|
||||
click clickState // für Doppel-/Dreifachklick
|
||||
@@ -254,6 +255,8 @@ func (a *App) update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return a, a.sshKeyDone(m)
|
||||
case relayMsg:
|
||||
return a, m.apply(a)
|
||||
case teamLinkMsg:
|
||||
return a, m.apply(a)
|
||||
|
||||
case connectedMsg:
|
||||
if s := a.byID(m.sid); s != nil {
|
||||
@@ -487,6 +490,9 @@ func (a *App) onTick() tea.Cmd {
|
||||
if c := a.syncTick(); c != nil {
|
||||
cmds = append(cmds, c)
|
||||
}
|
||||
if c := a.teamLinkTick(); c != nil {
|
||||
cmds = append(cmds, c)
|
||||
}
|
||||
for _, s := range a.sessions {
|
||||
if s.term != nil && s.term.TakeBell() {
|
||||
cmds = append(cmds, func() tea.Msg { return nil })
|
||||
|
||||
@@ -216,6 +216,7 @@ func (a *App) paletteItems() []palItem {
|
||||
{icon: "⌕", label: i18n.T("Netzwerk-Scan: SSH-Geräte finden"), hint: a.keys.label(actScan), run: func() tea.Cmd { return a.openScan() }},
|
||||
{icon: "⇪", label: i18n.T("Mit dem Team teilen (verschlüsselt, ohne Passwörter)"), hint: "Team", run: func() tea.Cmd { a.scr = scrManager; return a.openTeamShare() }},
|
||||
{icon: "⇩", label: i18n.T("Team-Paket übernehmen"), hint: "Team", run: func() tea.Cmd { return a.openTeamTake() }},
|
||||
{icon: "⇄", label: i18n.T("Team-Ordner (veröffentlicht / abonniert)"), hint: "Team", run: func() tea.Cmd { return a.openTeamLinks() }},
|
||||
{icon: "☰", label: i18n.T("Sitzungsprotokolle durchsuchen"), hint: "l", run: func() tea.Cmd { return a.openLogs() }},
|
||||
}
|
||||
if a.restartPending != "" {
|
||||
|
||||
+86
-8
@@ -1,6 +1,7 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -48,6 +49,10 @@ type teamState struct {
|
||||
ids []string // Teilen: gewählte Verbindungen
|
||||
|
||||
folder, user string // Übernehmen: Zielordner und Benutzer für Einträge ohne
|
||||
|
||||
srcFolder string // Teilen: aus diesem Ordner geöffnet (Team-Ordner nimmt dann den ganzen Ordner)
|
||||
pass, sum string // Übernehmen: für das Abo
|
||||
abo bool // Übernehmen: abonnieren
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Teilen
|
||||
@@ -61,6 +66,7 @@ func (a *App) openTeamShare() tea.Cmd {
|
||||
switch {
|
||||
case cur.node != nil:
|
||||
on = domain.InFolder(c.Group, ctx) // Ordner gewählt: sein Inhalt
|
||||
a.team.srcFolder = ctx
|
||||
case cur.conn.ID != "":
|
||||
on = c.ID == cur.conn.ID
|
||||
}
|
||||
@@ -100,7 +106,8 @@ func (a *App) openTeamShareForm() tea.Cmd {
|
||||
f.toggle("quick", i18n.T("Schnellbefehle"), true, i18n.T("eigene Schnellbefehle (⇧F1 …) der Verbindungen"))
|
||||
f.toggle("runbooks", "Runbooks", true, i18n.T("Runbooks dieser Verbindungen und allgemeine"))
|
||||
f.toggle("tunnels", "Tunnel", true, i18n.T("Port-Weiterleitungen der Verbindungen"))
|
||||
f.text("path", i18n.T("Datei"), defaultTeamFile(), i18n.T("z. B. in einen gemeinsamen Ordner (Teams, OneDrive, NAS)"))
|
||||
f.text("path", i18n.T("Datei"), defaultTeamFile(), i18n.T("z. B. in einen gemeinsamen Ordner (Teams, OneDrive, NAS) oder https://…/x.sshteam (WebDAV)"))
|
||||
f.toggle("abo", i18n.T("Aktuell halten"), false, i18n.T("Team-Ordner: sshTermy schreibt die Datei bei jeder Änderung neu"))
|
||||
a.form, a.scr = f, scrTeamForm
|
||||
return f.setFocus(1)
|
||||
}
|
||||
@@ -128,22 +135,53 @@ func (a *App) saveTeamShare() error {
|
||||
if !strings.HasSuffix(strings.ToLower(path), team.Ext) {
|
||||
path += team.Ext
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
if !team.IsURL(path) {
|
||||
if abs, err := filepath.Abs(path); err == nil {
|
||||
path = abs
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if err := team.WriteLoc(ctx, path, raw); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(path, raw, 0o600); err != nil {
|
||||
return err
|
||||
abo := f.flag("abo")
|
||||
if abo {
|
||||
folder, ids := a.team.srcFolder, a.team.ids
|
||||
if folder != "" && sameIDs(team.LinkIDs(d, domain.TeamLink{Folder: folder}), ids) {
|
||||
ids = nil // ganzer Ordner: neue Verbindungen darin gehen automatisch mit
|
||||
} else {
|
||||
folder = ""
|
||||
}
|
||||
l := domain.TeamLink{Folder: folder, IDs: ids, From: f.str("from"), Users: o.Users, QuickCmds: o.QuickCmds, Runbooks: o.Runbooks, Tunnels: o.Tunnels}
|
||||
_, sum := team.BuildLink(d, l, time.Now())
|
||||
if folder != "" { // Paket so schreiben, wie es der Team-Ordner später auch tut (Ordner relativ)
|
||||
p2, _ := team.BuildLink(d, l, time.Now())
|
||||
if raw2, err := team.Seal(p2, []byte(pass)); err == nil {
|
||||
_ = team.WriteLoc(ctx, path, raw2)
|
||||
}
|
||||
}
|
||||
if err := a.addPublishLink(path, pass, l.From, folder, ids, o, sum); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString(fmt.Sprintf(i18n.T("%s · %d Runbooks · %d Tunnel\n"), plural(len(p.Connections), i18n.T("Verbindung"), i18n.T("Verbindungen")), len(p.Runbooks), len(p.Tunnels)))
|
||||
if len(jumps) > 0 {
|
||||
b.WriteString(fmt.Sprintf(i18n.T("Dazu %d Jump-Host(s), ohne die die Verbindungen nicht gingen.\n"), len(jumps)))
|
||||
}
|
||||
b.WriteString(i18n.T("Passwörter und Keys sind nicht enthalten.\n\nDatei: ") + path + "\n")
|
||||
b.WriteString(i18n.T("Passwörter und Keys sind nicht enthalten.\n\nDatei: ") + team.ShowLoc(path) + "\n")
|
||||
if abo {
|
||||
b.WriteString(i18n.T("Automatisch aktuell: Änderungen an diesen Verbindungen schreibt sshTermy in die Datei (Strg+Leertaste → „Team-Ordner“).\n"))
|
||||
}
|
||||
if generated {
|
||||
b.WriteString(i18n.T("\nTeam-Passwort (bitte notieren): ") + pass + "\n")
|
||||
}
|
||||
b.WriteString(i18n.T("\nSchick die Datei und sag das Passwort auf einem anderen Weg (Anruf, Chat).\nDer Kollege übernimmt sie mit Strg+Leertaste → „Team-Paket übernehmen“."))
|
||||
if abo {
|
||||
b.WriteString(i18n.T("\nSag den Kollegen den Ort der Datei und das Passwort (getrennt).\nSie übernehmen sie mit Strg+Leertaste → „Team-Paket übernehmen“ und „Abonnieren“."))
|
||||
} else {
|
||||
b.WriteString(i18n.T("\nSchick die Datei und sag das Passwort auf einem anderen Weg (Anruf, Chat).\nDer Kollege übernimmt sie mit Strg+Leertaste → „Team-Paket übernehmen“."))
|
||||
}
|
||||
a.form = nil
|
||||
a.scr = scrManager
|
||||
a.modal = &modal{kind: mInfo, title: i18n.T("✓ Team-Paket erstellt"), body: b.String()}
|
||||
@@ -179,10 +217,11 @@ func newestTeamFile() string {
|
||||
func (a *App) openTeamTake() tea.Cmd {
|
||||
a.team = teamState{mode: teamTake}
|
||||
f := &form{title: i18n.T("Team-Paket übernehmen")}
|
||||
f.text("path", i18n.T("Datei"), newestTeamFile(), i18n.T("Pfad zur .sshteam-Datei (Einfügen mit Strg+V)"))
|
||||
f.text("path", i18n.T("Datei"), newestTeamFile(), i18n.T("Pfad zur .sshteam-Datei oder https://…/x.sshteam (Einfügen mit Strg+V)"))
|
||||
f.secret("pass", i18n.T("Team-Passwort"), "", i18n.T("bekommst du vom Absender getrennt von der Datei"))
|
||||
f.text("folder", i18n.T("In Ordner"), "Team", i18n.T("wird den Ordnern des Absenders vorangestellt; leer = wie beim Absender"))
|
||||
f.text("user", i18n.T("Benutzer"), localUser(), i18n.T("für Verbindungen ohne Benutzernamen"))
|
||||
f.toggle("abo", i18n.T("Abonnieren"), true, i18n.T("Datei alle 5 min neu lesen und Änderungen des Teams automatisch übernehmen"))
|
||||
a.form, a.scr = f, scrTeamForm
|
||||
if f.str("path") != "" {
|
||||
return f.setFocus(1)
|
||||
@@ -196,7 +235,14 @@ func (a *App) openTeamFile() error {
|
||||
if path == "" {
|
||||
return errors.New(i18n.T("Datei fehlt"))
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if !team.IsURL(path) {
|
||||
if abs, err := filepath.Abs(path); err == nil {
|
||||
path = abs
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
raw, err := team.ReadLoc(ctx, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -215,6 +261,7 @@ func (a *App) openTeamFile() error {
|
||||
t := &a.team
|
||||
t.pkg, t.path, t.items, t.sel, t.err = p, path, nil, 0, ""
|
||||
t.folder, t.user = domain.NormPath(f.str("folder")), strings.TrimSpace(f.str("user"))
|
||||
t.pass, t.sum, t.abo = f.raw("pass"), team.Sum(raw), f.flag("abo")
|
||||
for _, c := range p.Connections {
|
||||
t.items = append(t.items, teamItem{c: c, on: true, known: known[c.ID]})
|
||||
}
|
||||
@@ -247,6 +294,21 @@ func (a *App) commitTeamTake() {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf(i18n.T("Team-Paket übernommen: %d neu, %d aktualisiert"), st.Added, st.Updated)
|
||||
if t.abo {
|
||||
var have, skip []string
|
||||
for _, it := range t.items {
|
||||
if it.on {
|
||||
have = append(have, it.c.ID)
|
||||
} else {
|
||||
skip = append(skip, it.c.ID)
|
||||
}
|
||||
}
|
||||
if err := a.addSubscribeLink(t.path, t.pass, t.folder, t.user, have, skip, t.sum); err != nil {
|
||||
t.err = err.Error()
|
||||
return
|
||||
}
|
||||
msg += i18n.T(" · abonniert")
|
||||
}
|
||||
if st.Runbooks > 0 {
|
||||
msg += fmt.Sprintf(i18n.T(" · %d Runbooks"), st.Runbooks)
|
||||
}
|
||||
@@ -449,3 +511,19 @@ func (a *App) viewTeam() string {
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func sameIDs(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
m := map[string]bool{}
|
||||
for _, x := range a {
|
||||
m[x] = true
|
||||
}
|
||||
for _, x := range b {
|
||||
if !m[x] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
|
||||
"github.com/db9979/sshtermy/internal/domain"
|
||||
"github.com/db9979/sshtermy/internal/i18n"
|
||||
"github.com/db9979/sshtermy/internal/team"
|
||||
)
|
||||
|
||||
// Team-Ordner (Abo): Ein Team-Paket an einem festen Ort, den alle erreichen.
|
||||
// Wer es veröffentlicht, schreibt es bei jeder Änderung neu (Prüfung jede
|
||||
// Minute, ohne Änderung kein Schreiben). Wer es abonniert, liest es alle
|
||||
// fünf Minuten und übernimmt Änderungen, auch Löschungen. Logik in
|
||||
// internal/team/link.go, gespeichert in domain.Data.TeamLinks (nur lokal).
|
||||
|
||||
const (
|
||||
teamPubEvery = time.Minute
|
||||
teamSubEvery = 5 * time.Minute
|
||||
)
|
||||
|
||||
type teamLinkState struct {
|
||||
next time.Time
|
||||
running map[string]bool
|
||||
checked map[string]time.Time // Abo: letzte Prüfung in dieser Sitzung
|
||||
}
|
||||
|
||||
// teamLinkMsg: Ergebnis im UI-Thread anwenden.
|
||||
type teamLinkMsg struct{ apply func(a *App) tea.Cmd }
|
||||
|
||||
func (a *App) teamLinkTick() tea.Cmd {
|
||||
if a.scr == scrUnlock || time.Now().Before(a.tl.next) {
|
||||
return nil
|
||||
}
|
||||
a.tl.next = time.Now().Add(teamPubEvery)
|
||||
var cmds []tea.Cmd
|
||||
for _, l := range a.st.TeamLinks() {
|
||||
switch l.Role {
|
||||
case domain.TeamPublish:
|
||||
cmds = append(cmds, a.publishLink(l, false))
|
||||
case domain.TeamSubscribe:
|
||||
if time.Since(a.tl.checked[l.ID]) >= teamSubEvery {
|
||||
cmds = append(cmds, a.pullLink(l, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
return tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func (a *App) tlBusy(id string, on bool) bool {
|
||||
if a.tl.running == nil {
|
||||
a.tl.running, a.tl.checked = map[string]bool{}, map[string]time.Time{}
|
||||
}
|
||||
if on && a.tl.running[id] {
|
||||
return true
|
||||
}
|
||||
a.tl.running[id] = on
|
||||
return false
|
||||
}
|
||||
|
||||
// saveLink: Stand speichern, ohne die Liste neu zu laden (Link kann
|
||||
// inzwischen entfernt worden sein).
|
||||
func (a *App) saveLink(l domain.TeamLink) {
|
||||
for _, cur := range a.st.TeamLinks() {
|
||||
if cur.ID == l.ID {
|
||||
_, _ = a.st.UpsertTeamLink(l)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// publishLink schreibt das Paket neu, wenn sich der Inhalt geändert hat
|
||||
// (force: auch ohne Änderung, z. B. „Jetzt veröffentlichen“).
|
||||
func (a *App) publishLink(l domain.TeamLink, force bool) tea.Cmd {
|
||||
d, _, err := a.st.Snapshot()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
p, sum := team.BuildLink(d, l, time.Now())
|
||||
if sum == l.LastSum && l.LastErr == "" && !force {
|
||||
return nil
|
||||
}
|
||||
if a.tlBusy(l.ID, true) {
|
||||
return nil
|
||||
}
|
||||
pass := []byte(l.TeamPass.Reveal())
|
||||
return func() tea.Msg {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
raw, err := team.Seal(p, pass)
|
||||
if err == nil {
|
||||
err = team.WriteLoc(ctx, l.Path, raw)
|
||||
}
|
||||
return teamLinkMsg{func(a *App) tea.Cmd {
|
||||
a.tlBusy(l.ID, false)
|
||||
if err != nil {
|
||||
if e := err.Error(); e != l.LastErr {
|
||||
a.flashErr(fmt.Sprintf(i18n.T("Team-Ordner „%s“: %s"), l.Name, e))
|
||||
}
|
||||
l.LastErr = err.Error()
|
||||
} else {
|
||||
if force {
|
||||
a.flashOK(fmt.Sprintf(i18n.T("Team-Ordner „%s“ veröffentlicht: %s"), l.Name, plural(len(p.Connections), i18n.T("Verbindung"), i18n.T("Verbindungen"))))
|
||||
}
|
||||
l.LastSum, l.LastErr, l.LastRun = sum, "", time.Now().UTC()
|
||||
}
|
||||
a.saveLink(l)
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
// pullLink liest das Paket und übernimmt Änderungen.
|
||||
func (a *App) pullLink(l domain.TeamLink, force bool) tea.Cmd {
|
||||
if a.tlBusy(l.ID, true) {
|
||||
return nil
|
||||
}
|
||||
a.tl.checked[l.ID] = time.Now()
|
||||
pass := []byte(l.TeamPass.Reveal())
|
||||
return func() tea.Msg {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
raw, err := team.ReadLoc(ctx, l.Path)
|
||||
sum := ""
|
||||
var p *team.Package
|
||||
if err == nil {
|
||||
sum = team.Sum(raw)
|
||||
if sum != l.LastSum || force {
|
||||
p, err = team.Open(raw, pass)
|
||||
}
|
||||
}
|
||||
return teamLinkMsg{func(a *App) tea.Cmd {
|
||||
a.tlBusy(l.ID, false)
|
||||
if err != nil {
|
||||
if e := err.Error(); e != l.LastErr || force {
|
||||
a.flashErr(fmt.Sprintf(i18n.T("Team-Ordner „%s“: %s"), l.Name, e))
|
||||
}
|
||||
l.LastErr = err.Error()
|
||||
a.saveLink(l)
|
||||
return nil
|
||||
}
|
||||
if p == nil { // unverändert
|
||||
if l.LastErr != "" {
|
||||
l.LastErr = ""
|
||||
a.saveLink(l)
|
||||
}
|
||||
if force {
|
||||
a.flashOK(fmt.Sprintf(i18n.T("Team-Ordner „%s“: keine Änderungen"), l.Name))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var st team.Stats
|
||||
var have []string
|
||||
if err := a.st.Update(func(d *domain.Data) { st, have = team.Pull(d, p, l, time.Now()) }); err != nil {
|
||||
return nil
|
||||
}
|
||||
l.Have, l.LastSum, l.LastErr, l.LastRun = have, sum, "", time.Now().UTC()
|
||||
a.saveLink(l)
|
||||
a.flash(fmt.Sprintf(i18n.T("Team-Ordner „%s“: %d neu, %d aktualisiert, %d entfernt"), l.Name, st.Added, st.Updated, st.Removed))
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Anlegen
|
||||
|
||||
// addPublishLink wird nach „Mit dem Team teilen“ mit „automatisch aktuell
|
||||
// halten“ aufgerufen.
|
||||
func (a *App) addPublishLink(path, pass, from, folder string, ids []string, o team.Options, sum string) error {
|
||||
l := domain.TeamLink{Role: domain.TeamPublish, Name: linkName(path), Path: path, TeamPass: domain.Secret(pass),
|
||||
Folder: folder, IDs: ids, From: from, Users: o.Users, QuickCmds: o.QuickCmds, Runbooks: o.Runbooks, Tunnels: o.Tunnels,
|
||||
LastSum: sum, LastRun: time.Now().UTC()}
|
||||
for _, cur := range a.st.TeamLinks() {
|
||||
if cur.Role == domain.TeamPublish && cur.Path == path {
|
||||
l.ID = cur.ID
|
||||
}
|
||||
}
|
||||
_, err := a.st.UpsertTeamLink(l)
|
||||
return err
|
||||
}
|
||||
|
||||
// addSubscribeLink wird nach „Team-Paket übernehmen“ mit „abonnieren“
|
||||
// aufgerufen.
|
||||
func (a *App) addSubscribeLink(path, pass, into, user string, have, skip []string, sum string) error {
|
||||
l := domain.TeamLink{Role: domain.TeamSubscribe, Name: linkName(path), Path: path, TeamPass: domain.Secret(pass),
|
||||
Into: into, DefaultUser: user, Have: have, Skip: skip, LastSum: sum, LastRun: time.Now().UTC()}
|
||||
for _, cur := range a.st.TeamLinks() {
|
||||
if cur.Role == domain.TeamSubscribe && cur.Path == path {
|
||||
l.ID = cur.ID
|
||||
}
|
||||
}
|
||||
l, err := a.st.UpsertTeamLink(l)
|
||||
if err == nil && a.tl.checked != nil {
|
||||
a.tl.checked[l.ID] = time.Now()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func linkName(path string) string {
|
||||
p := team.ShowLoc(path)
|
||||
if i := strings.LastIndexAny(p, `/\`); i >= 0 {
|
||||
p = p[i+1:]
|
||||
}
|
||||
return strings.TrimSuffix(p, filepath.Ext(p))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Verwalten
|
||||
|
||||
func linkStatus(l domain.TeamLink) string {
|
||||
switch {
|
||||
case l.LastErr != "":
|
||||
return "✗ " + trunc(l.LastErr, 40)
|
||||
case !l.LastRun.IsZero():
|
||||
return "✓ " + ago(l.LastRun)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *App) openTeamLinks() tea.Cmd {
|
||||
links := a.st.TeamLinks()
|
||||
if len(links) == 0 {
|
||||
a.modal = &modal{kind: mInfo, title: i18n.T("Team-Ordner"), body: i18n.T("Noch keine Team-Ordner.\n\n" +
|
||||
"Veröffentlichen: Strg+Leertaste → „Mit dem Team teilen“, als Datei einen gemeinsamen Ordner wählen und „Aktuell halten“ einschalten.\n\n" +
|
||||
"Abonnieren: Strg+Leertaste → „Team-Paket übernehmen“ mit „Abonnieren“. sshTermy liest die Datei dann alle 5 Minuten und übernimmt Änderungen.")}
|
||||
return nil
|
||||
}
|
||||
var items []menuItem
|
||||
for _, l := range links {
|
||||
l := l
|
||||
icon, what := "⇩", i18n.T("Abo")
|
||||
if l.Role == domain.TeamPublish {
|
||||
icon, what = "⇪", i18n.T("Veröffentlicht")
|
||||
}
|
||||
items = append(items, menuItem{label: icon + " " + what + ": " + l.Name, hint: linkStatus(l), run: func() tea.Cmd { return a.openTeamLink(l) }})
|
||||
}
|
||||
a.modal = &modal{kind: mMenu, title: i18n.T("Team-Ordner"), items: items}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) openTeamLink(l domain.TeamLink) tea.Cmd {
|
||||
var items []menuItem
|
||||
if l.Role == domain.TeamPublish {
|
||||
n := 0
|
||||
if d, _, err := a.st.Snapshot(); err == nil {
|
||||
n = len(team.LinkIDs(d, l))
|
||||
}
|
||||
src := l.Folder
|
||||
if src == "" {
|
||||
src = i18n.T("einzeln gewählt")
|
||||
}
|
||||
items = append(items,
|
||||
menuItem{label: i18n.T("Jetzt veröffentlichen"), hint: plural(n, i18n.T("Verbindung"), i18n.T("Verbindungen")) + " · " + src,
|
||||
run: func() tea.Cmd { return a.publishLink(l, true) }},
|
||||
menuItem{label: i18n.T("Nicht mehr veröffentlichen"), hint: i18n.T("Datei bleibt liegen"), run: func() tea.Cmd {
|
||||
a.modal = confirmModal(i18n.T("Team-Ordner beenden?"),
|
||||
fmt.Sprintf(i18n.T("„%s“ wird nicht mehr aktualisiert. Die Datei %s bleibt liegen; lösche sie dort, wenn das Team sie nicht mehr bekommen soll."), l.Name, team.ShowLoc(l.Path)),
|
||||
func() tea.Cmd { return a.dropLink(l) })
|
||||
return nil
|
||||
}})
|
||||
} else {
|
||||
items = append(items,
|
||||
menuItem{label: i18n.T("Jetzt abgleichen"), hint: fmt.Sprintf(i18n.T("%d Verbindungen · in „%s“"), len(l.Have), l.Into),
|
||||
run: func() tea.Cmd { return a.pullLink(l, true) }},
|
||||
menuItem{label: i18n.T("Abo beenden"), hint: i18n.T("Verbindungen bleiben"), run: func() tea.Cmd {
|
||||
a.modal = confirmModal(i18n.T("Abo beenden?"),
|
||||
fmt.Sprintf(i18n.T("Änderungen aus „%s“ kommen nicht mehr an. Die übernommenen Verbindungen bleiben und gehören ab jetzt dir."), l.Name),
|
||||
func() tea.Cmd { return a.dropLink(l) })
|
||||
return nil
|
||||
}})
|
||||
}
|
||||
a.modal = &modal{kind: mMenu, title: l.Name + " · " + team.ShowLoc(l.Path), items: items}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) dropLink(l domain.TeamLink) tea.Cmd {
|
||||
if err := a.st.DeleteTeamLink(l.ID); err != nil {
|
||||
a.flashErr(err.Error())
|
||||
} else {
|
||||
a.flash(fmt.Sprintf(i18n.T("Team-Ordner „%s“ beendet"), l.Name))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
|
||||
"github.com/db9979/sshtermy/internal/domain"
|
||||
"github.com/db9979/sshtermy/internal/store"
|
||||
"github.com/db9979/sshtermy/internal/team"
|
||||
)
|
||||
|
||||
func teamApp(t *testing.T) *App {
|
||||
t.Helper()
|
||||
t.Setenv("SSHTERMY_HOME", t.TempDir())
|
||||
st, err := store.Open()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.Init([]byte("correct horse"), false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a, err := New(st)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// run führt einen Befehl aus und wendet das Ergebnis an wie Update.
|
||||
func runTL(t *testing.T, a *App, c tea.Cmd) {
|
||||
t.Helper()
|
||||
if c == nil {
|
||||
t.Fatal("kein Befehl")
|
||||
}
|
||||
m, ok := c().(teamLinkMsg)
|
||||
if !ok {
|
||||
t.Fatal("keine teamLinkMsg")
|
||||
}
|
||||
m.apply(a)
|
||||
}
|
||||
|
||||
func TestTeamFolderPublishSubscribe(t *testing.T) {
|
||||
loc := filepath.Join(t.TempDir(), "acme.sshteam")
|
||||
|
||||
pub := teamApp(t)
|
||||
cs, err := pub.st.AddConnections([]domain.Connection{
|
||||
{Alias: "web", Host: "10.0.0.1", Port: 22, Group: "Kunden / ACME", Username: "db9", Auth: domain.AuthPassword, Password: "geheim"},
|
||||
{Alias: "db", Host: "10.0.0.2", Port: 22, Group: "Kunden / ACME / DB", Username: "root", Auth: domain.AuthAuto},
|
||||
{Alias: "privat", Host: "10.0.0.3", Port: 22, Group: "Privat", Username: "x", Auth: domain.AuthAuto},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pub.addPublishLink(loc, "team-pass-123", "Dominik", "Kunden / ACME", nil, team.Options{}, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runTL(t, pub, pub.publishLink(pub.st.TeamLinks()[0], false))
|
||||
if l := pub.st.TeamLinks()[0]; l.LastErr != "" || l.LastSum == "" {
|
||||
t.Fatalf("veröffentlichen: %+v", l)
|
||||
}
|
||||
if pub.publishLink(pub.st.TeamLinks()[0], false) != nil {
|
||||
t.Fatal("ohne Änderung darf nicht neu geschrieben werden")
|
||||
}
|
||||
|
||||
sub := teamApp(t)
|
||||
if err := sub.addSubscribeLink(loc, "team-pass-123", "Team", "kollege", nil, nil, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runTL(t, sub, sub.pullLink(sub.st.TeamLinks()[0], false))
|
||||
got := map[string]domain.Connection{}
|
||||
for _, c := range sub.st.Connections() {
|
||||
got[c.Alias] = c
|
||||
}
|
||||
if len(got) != 2 || got["web"].Group != "Team / ACME" || got["db"].Group != "Team / ACME / DB" || got["web"].Username != "kollege" {
|
||||
t.Fatalf("Abo erste Übernahme: %+v", got)
|
||||
}
|
||||
if got["web"].Password.IsSet() {
|
||||
t.Fatal("Passwort ist mitgekommen")
|
||||
}
|
||||
|
||||
// Absender: db löschen, neue Verbindung im Ordner
|
||||
if err := pub.st.DeleteConnection(cs[1].ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pub.st.AddConnections([]domain.Connection{{Alias: "mail", Host: "10.0.0.4", Port: 22, Group: "Kunden / ACME", Username: "y", Auth: domain.AuthAuto}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runTL(t, pub, pub.publishLink(pub.st.TeamLinks()[0], false))
|
||||
runTL(t, sub, sub.pullLink(sub.st.TeamLinks()[0], false))
|
||||
got = map[string]domain.Connection{}
|
||||
for _, c := range sub.st.Connections() {
|
||||
got[c.Alias] = c
|
||||
}
|
||||
if len(got) != 2 || got["mail"].ID == "" || got["db"].ID != "" {
|
||||
t.Fatalf("Abo nach Änderung: %+v", got)
|
||||
}
|
||||
|
||||
// falsches Passwort → Fehler am Link, nichts kaputt
|
||||
l := sub.st.TeamLinks()[0]
|
||||
l.TeamPass, l.LastSum = "falsch-falsch", ""
|
||||
if _, err := sub.st.UpsertTeamLink(l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runTL(t, sub, sub.pullLink(sub.st.TeamLinks()[0], false))
|
||||
if sub.st.TeamLinks()[0].LastErr == "" || len(sub.st.Connections()) != 2 {
|
||||
t.Fatal("falsches Passwort nicht gemeldet")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user