mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 12:56:31 -04:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
726153b9be | ||
|
|
e8548dd907 | ||
|
|
768d4f6fbd | ||
|
|
83f59c3cd9 | ||
|
|
e9f49fb98a | ||
|
|
8da2648664 | ||
|
|
6d3e3cdb5b | ||
|
|
b6ebd22463 | ||
|
|
f4fdf65528 | ||
|
|
a091cd4952 | ||
|
|
20d8e141eb | ||
|
|
3fd2b13712 | ||
|
|
f9aeca5ba9 | ||
|
|
273e86d97e | ||
|
|
7ecd143b81 | ||
|
|
9c0ed69d7f | ||
|
|
50cf92df61 | ||
|
|
420333e412 | ||
|
|
25edd344c8 | ||
|
|
5b591e35d8 | ||
|
|
1351360b35 | ||
|
|
02a8196061 | ||
|
|
d7242fee96 | ||
|
|
ec9c6b5062 | ||
|
|
02fc5c1ffa | ||
|
|
e1c6469506 | ||
|
|
cb3a554676 | ||
|
|
b963ccf33e | ||
|
|
409db78d83 | ||
|
|
48e084ac0f | ||
|
|
f82b35bc43 | ||
|
|
2054f5b3cf | ||
|
|
07ad0a1bd9 | ||
|
|
3b4e75b592 | ||
|
|
4467a8c742 | ||
|
|
a54d15e331 | ||
|
|
d0e8640ce6 | ||
|
|
07571a698d | ||
|
|
d21de424cb | ||
|
|
d54b59030e | ||
|
|
ed19d0a416 | ||
|
|
5e5a7c57e9 | ||
|
|
a18c3ea1d6 | ||
|
|
28286788a0 | ||
|
|
571bc0a3bd | ||
|
|
422ca0295a | ||
|
|
b707d67113 | ||
|
|
d8d761b0ca | ||
|
|
bdbdce61aa | ||
|
|
cc626363c7 | ||
|
|
6004a94b77 | ||
|
|
fdce036be6 | ||
|
|
53d82984d9 | ||
|
|
2880ce2823 | ||
|
|
b591e2ef5a | ||
|
|
e2d1b8dc09 | ||
|
|
35db3bf20b | ||
|
|
941a2dc8b8 | ||
|
|
2ab777fd57 | ||
|
|
4e6c18db8a | ||
|
|
7e7fbd15b4 | ||
|
|
01be63c587 | ||
|
|
f16078fe01 | ||
|
|
e355c810da | ||
|
|
ffae2e3002 | ||
|
|
95a877d0b3 | ||
|
|
08bc065785 | ||
|
|
5dc3a5576d | ||
|
|
3ddce4bdf5 | ||
|
|
50d51f9d96 | ||
|
|
d2a6ed5eaa | ||
|
|
04e1b0dd37 | ||
|
|
336dc5c158 | ||
|
|
c63dff4092 | ||
|
|
8d1dd48035 | ||
|
|
80cf65775a | ||
|
|
ac4a86d3a4 | ||
|
|
8aabd01ed2 | ||
|
|
6d1513b469 | ||
|
|
bc793178e1 | ||
|
|
a0f21b31f7 | ||
|
|
a3b89fcb96 | ||
|
|
483af75613 | ||
|
|
b39a40407f | ||
|
|
14dda0b091 | ||
|
|
75ad2afcfb | ||
|
|
9703790336 | ||
|
|
8f4590cf34 | ||
|
|
bf1399ce8d | ||
|
|
d49632d221 | ||
|
|
3506504fb1 | ||
|
|
09501fda7d | ||
|
|
3905dc6e41 | ||
|
|
7c577b5778 | ||
|
|
aa10016f77 | ||
|
|
a39952489a | ||
|
|
b9175d7ff3 | ||
|
|
235f6b5ed4 | ||
|
|
1b02ccc056 | ||
|
|
3dc3a75967 | ||
|
|
cc58d3509f | ||
|
|
d023058630 | ||
|
|
fb2120c94e | ||
|
|
1a66a30bc1 | ||
|
|
591624eff6 | ||
|
|
3fdcc9e96d | ||
|
|
9fc9b208b6 | ||
|
|
41a12c6e4a | ||
|
|
bed23e987d | ||
|
|
dd622c59e2 | ||
|
|
362eb75dc4 | ||
|
|
d1b5c50d70 | ||
|
|
1c06713548 | ||
|
|
37a1a6049f | ||
|
|
cf5d5051fd | ||
|
|
0f03096ed2 | ||
|
|
d844fb1010 | ||
|
|
111324cc25 | ||
|
|
3daaf2d67a | ||
|
|
c0f55156d8 | ||
|
|
ca4913f054 | ||
|
|
4a0e0f8e9e | ||
|
|
87041a7bb3 | ||
|
|
d0f7b663d5 | ||
|
|
5f8789a0f0 | ||
|
|
2e3be57e3e | ||
|
|
4dab9f2c93 | ||
|
|
038cd3670d | ||
|
|
346fa35dc8 | ||
|
|
c4db7dfd59 | ||
|
|
2840ff3cf2 | ||
|
|
ca277ba773 | ||
|
|
5666974a7f | ||
|
|
c2fc911693 | ||
|
|
6e74e37e5b | ||
|
|
3dd07e7165 | ||
|
|
9ffd6d5cef | ||
|
|
a1c47b06a1 | ||
|
|
16aaa4f330 | ||
|
|
aa0e242748 | ||
|
|
40f0f35f45 | ||
|
|
74b8261bc8 | ||
|
|
50ab27c674 | ||
|
|
fa6f057b49 | ||
|
|
3c5db78da9 | ||
|
|
02c663447a | ||
|
|
c591601b6c | ||
|
|
e64da70edb | ||
|
|
35d053b46f | ||
|
|
cdf41c3d17 | ||
|
|
26db43eb29 | ||
|
|
162e277ad2 | ||
|
|
36bbf88690 | ||
|
|
84d227b803 | ||
|
|
8860ce4969 | ||
|
|
dfb5c10dcb | ||
|
|
491392562a | ||
|
|
fbfcdc5320 | ||
|
|
03c040b373 | ||
|
|
aec42cad96 | ||
|
|
0d66c26878 | ||
|
|
ac85577916 | ||
|
|
fb83d29a23 | ||
|
|
008088e2d6 | ||
|
|
e7293505bc | ||
|
|
f549b591c0 | ||
|
|
3404fc7482 | ||
|
|
21d60b8d0f | ||
|
|
826d94bc43 | ||
|
|
8807e8fe79 | ||
|
|
649b60e2fe | ||
|
|
a95bff8ef4 | ||
|
|
8f98e0b577 | ||
|
|
8f23589459 | ||
|
|
1dff1f463a | ||
|
|
4ed816cb11 | ||
|
|
451d695649 | ||
|
|
259d9a3309 | ||
|
|
51bb67f315 | ||
|
|
0f90e82514 | ||
|
|
44924737ab | ||
|
|
17223758b1 | ||
|
|
f40699076d | ||
|
|
a19a4aa1d5 | ||
|
|
456d20256c | ||
|
|
da95ede807 | ||
|
|
d35c9c2186 | ||
|
|
d70bd62372 | ||
|
|
e1222f94d9 | ||
|
|
e9d0c37043 | ||
|
|
173574eb85 | ||
|
|
4689cae3f4 | ||
|
|
eb2b349697 | ||
|
|
038e386df7 | ||
|
|
fc1cd349b8 | ||
|
|
fb5449cf1f | ||
|
|
55be8a3075 | ||
|
|
c71cacb507 | ||
|
|
81b78068a7 | ||
|
|
89b74cec1e | ||
|
|
0162b737c8 | ||
|
|
1c650d4751 | ||
|
|
5028fb21b0 | ||
|
|
4ca97834f4 | ||
|
|
b37dfdb912 | ||
|
|
cdb2318dc1 | ||
|
|
68e3108741 | ||
|
|
b5dd8ec502 | ||
|
|
d2034b458b | ||
|
|
fc2e77d8b8 | ||
|
|
c28d45c9da | ||
|
|
2feeeb74d7 | ||
|
|
1a1d4ed1e4 | ||
|
|
a4a39a3a99 | ||
|
|
1b9237af0b | ||
|
|
049af83390 | ||
|
|
f9aa2fc374 | ||
|
|
3b57d59ddc | ||
|
|
9ed7b8ad9b | ||
|
|
a4864b6544 | ||
|
|
619e4220a6 | ||
|
|
f2f9c523f4 | ||
|
|
1cebb39546 | ||
|
|
1db9467446 | ||
|
|
3e966ade11 | ||
|
|
dd6bde5e4d | ||
|
|
91fdeb53d5 | ||
|
|
d613ace494 | ||
|
|
003c373cc8 | ||
|
|
cdb58e9984 | ||
|
|
4f923e7272 | ||
|
|
fcde9689c8 | ||
|
|
ab3f6be41b | ||
|
|
1fb2834844 | ||
|
|
7b4241b2cd | ||
|
|
de508d17f2 |
@@ -4,3 +4,4 @@
|
||||
/libmuttonserver.exe
|
||||
/main
|
||||
/main.exe
|
||||
1output
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Randall Winkhart
|
||||
Copyright (c) 2024-2026 Randall Winkhart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -8,20 +8,12 @@ libmutton is a library for building simple, SSH-synchronized password managers i
|
||||
>I am not responsible for any data loss or breaches of your information resulting from the use of libmutton.
|
||||
>libmutton is a new project that is constantly being updated, and though safety and security are priorities, they cannot be guaranteed.
|
||||
|
||||
# Developing Thid-Party Clients
|
||||
# Developing Third-Party Clients
|
||||
See the [developer guide](https://github.com/rwinkhart/libmutton/blob/main/wiki/developers.md).
|
||||
|
||||
# Roadmap
|
||||
#### Release v0.3.0
|
||||
- [ ] Swap to native (cascade) encryption (custom)
|
||||
- [ ] Implement "netpin" (quick-unlock) with new encryption
|
||||
#### Release v0.4.0
|
||||
- [ ] Password aging support
|
||||
- [ ] Append UNIX timestamp to entry names
|
||||
- [ ] Add yellow/red dot indicators to entry list readout for when passwords should be changed
|
||||
#### Release v0.5.0
|
||||
- [ ] Add refresh/re-encrypt functionality
|
||||
#### Release v1.0.0
|
||||
- Stabilize API (reduce need for future breaking changes)
|
||||
- [ ] Create packaging scripts (libmuttonserver)
|
||||
- [ ] Stable source PKGBUILD
|
||||
- [ ] Stable source APKBUILD
|
||||
@@ -29,4 +21,4 @@ See the [developer guide](https://github.com/rwinkhart/libmutton/blob/main/wiki/
|
||||
- [ ] Fedora
|
||||
- [ ] FreeBSD
|
||||
- [ ] Windows installer
|
||||
- Hunt for polishing opportunities and bugs
|
||||
- Perform extensive testing
|
||||
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package age
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"math/big"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/crypt"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/libmutton/synccommon"
|
||||
)
|
||||
|
||||
// Entry creates/updates the age file for a vanity path.
|
||||
// It does not touch the actual entry, meaning it won't be synced;
|
||||
// this is because this function is meant to be used on entries that
|
||||
// are being created or modified, so they will sync. If using this
|
||||
// to age an entry without modifying the entry, the caller must also
|
||||
// update the mod time on the entry to trigger a sync.
|
||||
func Entry(vanityPath string, timestamp int64) error {
|
||||
ageFilePath := global.AgeDir + global.PathSeparator + strings.ReplaceAll(vanityPath, "/", global.FSPath)
|
||||
f, err := os.OpenFile(ageFilePath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return errors.New("unable to create age file for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
if err = os.Chtimes(ageFilePath, time.Now(), time.Unix(timestamp, 0)); err != nil {
|
||||
return errors.New("unable to set timestamp on age file for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AllPasswordEntries adds age data for all un-aged entries containing passwords.
|
||||
// Each entry is aged with a random timestamp from within the last year to prevent
|
||||
// all entries having their passwords expire at the same time.
|
||||
// It also updates the mod time on the actual entry to trigger a sync.
|
||||
// Leave rcwPassword nil to use RCW demonization.
|
||||
func AllPasswordEntries(forceReage bool, rcwPassword []byte) error {
|
||||
allVanityPaths, _, err := synccommon.WalkEntryDir()
|
||||
if err != nil {
|
||||
return errors.New("unable to walk entry directory: " + err.Error())
|
||||
}
|
||||
now := time.Now()
|
||||
|
||||
for _, vanityPath := range allVanityPaths {
|
||||
// ensure entry is not already aged (unless forcing re-age)
|
||||
if !forceReage {
|
||||
// ignore error; we only care if we can access the path or not
|
||||
isAccessible, _ := back.TargetIsFile(global.AgeDir+global.PathSeparator+strings.ReplaceAll(vanityPath, "/", global.FSPath), true)
|
||||
if isAccessible {
|
||||
// entry already aged, skip it
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
decSlice, err := crypt.DecryptFileToSlice(global.EntryRoot+vanityPath, rcwPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if decSlice != nil && decSlice[0] != "" {
|
||||
// calculate random UNIX timestamp from within the last 365 days
|
||||
offsetInt, _ := rand.Int(rand.Reader, big.NewInt(31557600))
|
||||
randomOffset := time.Duration(offsetInt.Int64()) * time.Second
|
||||
if err = Entry(vanityPath, now.Add(-randomOffset).Unix()); err != nil {
|
||||
return err
|
||||
}
|
||||
// update entry mod time to trigger sync
|
||||
if err = os.Chtimes(global.GetRealPath(vanityPath), now, now); err != nil {
|
||||
return errors.New("unable to update mod time on entry (" + vanityPath + "): " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TranslateAgeTimestamp returns a uint8 value indicating
|
||||
// the interpreted age status of an age timestamp associated
|
||||
// with an entry.
|
||||
// Magic number legend:
|
||||
// 0 -> no age, 1 -> fresh, 2 -> expiring soon (within a month), 3 -> expired
|
||||
func TranslateAgeTimestamp(timestamp *int64) uint8 {
|
||||
if timestamp == nil {
|
||||
return 0
|
||||
}
|
||||
daysOld := time.Since(time.Unix(*timestamp, 0)).Hours() / 24
|
||||
if daysOld >= 365 {
|
||||
return 3 // expired
|
||||
} else if daysOld >= 335 {
|
||||
return 2 // expiring soon
|
||||
}
|
||||
return 1 // fresh
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//go:build !android && !ios
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/crypt"
|
||||
)
|
||||
|
||||
// CopyShortcut (given a path) decrypts an
|
||||
// entry and copies a field to the clipboard.
|
||||
// Leave rcwPassword nil to use RCW demonization.
|
||||
func CopyShortcut(realPath string, field int, rcwPassword []byte) error {
|
||||
// ensure realPath exists and is a file
|
||||
_, err := back.TargetIsFile(realPath, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// decrypt entry
|
||||
decSlice, err := crypt.DecryptFileToSlice(realPath, rcwPassword)
|
||||
if err != nil {
|
||||
return errors.New("unable to decrypt entry: " + err.Error())
|
||||
}
|
||||
|
||||
// if field exists in entry...
|
||||
if len(decSlice) > field {
|
||||
if decSlice[field] == "" {
|
||||
return errors.New("field is empty")
|
||||
}
|
||||
|
||||
switch field {
|
||||
case 2: // TOTP
|
||||
fmt.Println(back.AnsiWarning + "[Starting]" + back.AnsiReset + " TOTP clipboard refresher")
|
||||
errorChan := make(chan error, 1)
|
||||
go TOTPCopier(decSlice[2], errorChan, nil) // "done" is not needed because the process runs until the program is killed
|
||||
if err = <-errorChan; err != nil { // handle error from first copy
|
||||
return errors.New("error encountered in TOTP refresh process: " + err.Error())
|
||||
}
|
||||
select {} // block indefinitely
|
||||
case 4:
|
||||
decSlice[field] = strings.TrimRight(decSlice[field], " ")
|
||||
fallthrough
|
||||
default:
|
||||
// copy field to clipboard; launch clipboard clearing process
|
||||
if err = CopyBytes(true, []byte(decSlice[field])); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
return errors.New("field does not exist in entry")
|
||||
}
|
||||
}
|
||||
|
||||
// ClearArgument reads the assigned clipboard contents from stdin and passes them to clipClearProcess.
|
||||
func ClearArgument() error {
|
||||
assignedContents := back.ReadFromStdin()
|
||||
if assignedContents == nil {
|
||||
os.Exit(0) // use os.Exit directly since this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
return ClearProcess(assignedContents)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//go:build !android && !ios
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
)
|
||||
|
||||
// ClearProcess clears the clipboard after 30 seconds if the clipboard contents have not changed.
|
||||
// assignedContents can be omitted to clear the clipboard immediately and unconditionally.
|
||||
func ClearProcess(assignedContents []byte) error {
|
||||
cmdPaste, cmdClear, err := getClipCommands()
|
||||
if err != nil {
|
||||
return errors.New("unable to determine clipboard platform: " + err.Error())
|
||||
}
|
||||
|
||||
clearClipboard := func() error {
|
||||
if err := cmdClear.Run(); err != nil {
|
||||
return errors.New("unable to clear clipboard")
|
||||
}
|
||||
back.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
// if assignedContents is empty, clear the clipboard immediately and unconditionally
|
||||
if assignedContents == nil {
|
||||
if err := clearClipboard(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// wait 30 seconds before checking clipboard contents
|
||||
time.Sleep(30 * time.Second)
|
||||
|
||||
newContents, err := cmdPaste.Output()
|
||||
if err != nil {
|
||||
return errors.New("unable to read clipboard contents")
|
||||
}
|
||||
if bytes.Equal(assignedContents, bytes.TrimRight(newContents, "\r\n")) {
|
||||
if err = clearClipboard(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
//go:build darwin && !ios
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/go-boilerplate/security"
|
||||
)
|
||||
|
||||
// CopyBytes copies a byte slice to the clipboard.
|
||||
func CopyBytes(clearClipboardAutomatically bool, copySubject []byte) error {
|
||||
cmd := exec.Command("pbcopy")
|
||||
_ = back.WriteToStdin(cmd, copySubject, false)
|
||||
if err := cmd.Run(); err != nil {
|
||||
security.ZeroizeBytes(copySubject)
|
||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||
}
|
||||
if clearClipboardAutomatically {
|
||||
LaunchClearProcess(copySubject)
|
||||
} else {
|
||||
security.ZeroizeBytes(copySubject)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getClipCommands returns the commands for pasting and clearing the clipboard contents.
|
||||
func getClipCommands() (*exec.Cmd, *exec.Cmd, error) {
|
||||
cmdClear := exec.Command("pbcopy")
|
||||
_ = back.WriteToStdin(cmdClear, nil, false)
|
||||
return exec.Command("pbpaste"), cmdClear, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//go:build !windows && !darwin && !android && !ios && !wsl
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/go-boilerplate/security"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
)
|
||||
|
||||
// CopyBytes copies a byte slice to the clipboard.
|
||||
func CopyBytes(clearClipboardAutomatically bool, copySubject []byte) error {
|
||||
// determine whether to use wl-copy (Wayland) or xclip (X11)
|
||||
sessionIsWayland, err := isWayland()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var cmdCopy *exec.Cmd
|
||||
if sessionIsWayland {
|
||||
cmdCopy = exec.Command("wl-copy", "-t", "text/plain")
|
||||
} else {
|
||||
cmdCopy = exec.Command("xclip", "-sel", "c", "-t", "text/plain")
|
||||
}
|
||||
cmdCopy.SysProcAttr = global.GetSysProcAttr()
|
||||
|
||||
_ = back.WriteToStdin(cmdCopy, copySubject, false)
|
||||
if err = cmdCopy.Run(); err != nil {
|
||||
security.ZeroizeBytes(copySubject)
|
||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||
}
|
||||
if clearClipboardAutomatically {
|
||||
LaunchClearProcess(copySubject)
|
||||
} else {
|
||||
security.ZeroizeBytes(copySubject)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getClipCommands returns the commands for pasting and clearing the clipboard contents.
|
||||
func getClipCommands() (*exec.Cmd, *exec.Cmd, error) {
|
||||
sessionIsWayland, err := isWayland()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if sessionIsWayland {
|
||||
return exec.Command("wl-paste"), exec.Command("wl-copy", "-c"), nil
|
||||
}
|
||||
return exec.Command("xclip", "-o", "-sel", "c"), exec.Command("xclip", "-i", "/dev/null", "-sel", "c"), nil
|
||||
}
|
||||
|
||||
// isWayland returns a boolean indicating whether the current session is Wayland.
|
||||
func isWayland() (bool, error) {
|
||||
var envSet bool // track whether environment variables are set
|
||||
if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet {
|
||||
return true, nil
|
||||
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.New("unable to detect Wayland or X11 session")
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//go:build windows || (linux && wsl)
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/go-boilerplate/security"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// CopyBytes copies a byte slice to the clipboard.
|
||||
func CopyBytes(clearClipboardAutomatically bool, copySubject []byte) error {
|
||||
cmd := exec.Command("clip.exe")
|
||||
_ = back.WriteToStdin(cmd, copySubject, false)
|
||||
if err := cmd.Run(); err != nil {
|
||||
security.ZeroizeBytes(copySubject)
|
||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||
}
|
||||
if clearClipboardAutomatically {
|
||||
LaunchClearProcess(copySubject)
|
||||
} else {
|
||||
security.ZeroizeBytes(copySubject)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getClipCommands returns the commands for pasting and clearing the clipboard contents.
|
||||
func getClipCommands() (*exec.Cmd, *exec.Cmd, error) {
|
||||
sysProcAttr := &syscall.SysProcAttr{CreationFlags: windows.CREATE_NO_WINDOW}
|
||||
pasteCMD := exec.Command("powershell.exe", "-c", "Get-Clipboard")
|
||||
pasteCMD.SysProcAttr = sysProcAttr
|
||||
clearCMD := exec.Command("clip.exe")
|
||||
clearCMD.SysProcAttr = sysProcAttr
|
||||
_ = back.WriteToStdin(clearCMD, nil, false)
|
||||
return pasteCMD, clearCMD, nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//go:build !ios && !android && !interactive
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
)
|
||||
|
||||
// LaunchClearProcess launches the timed clipboard clearing process.
|
||||
// For non-interactive CLI implementations, an entirely separate process is created for this purpose.
|
||||
func LaunchClearProcess(copySubject []byte) {
|
||||
cmd := exec.Command(os.Args[0], "clipclear")
|
||||
cmd.SysProcAttr = global.GetSysProcAttr()
|
||||
_ = back.WriteToStdin(cmd, copySubject, true)
|
||||
_ = cmd.Start()
|
||||
os.Exit(0) // use os.Exit directly since this version of this function is only meant for non-interactive CLI implementations
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
//go:build !ios && !android && interactive
|
||||
|
||||
package clip
|
||||
|
||||
// LaunchClearProcess launches the timed clipboard clearing process.
|
||||
// For interactive GUI/TUI implementations, the clipboard clearing process is launched as a goroutine.
|
||||
// copySubject can be omitted to clear the clipboard immediately and unconditionally.
|
||||
func LaunchClearProcess(copySubject []byte) {
|
||||
go ClearProcess(copySubject)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
//go:build !android && !ios
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
)
|
||||
|
||||
// TOTPCopier is meant to be run as a goroutine to keep
|
||||
// the clipboard up-to-date with the latest TOTP token.
|
||||
//
|
||||
// Note that errorChan should be buffered with capacity 1.
|
||||
// This is because TOTPCopier only returns errors on the first
|
||||
// iteration, as subsequent errors are highly unlikely to occur
|
||||
// and allowing the caller to move on from error-checking is beneficial.
|
||||
func TOTPCopier(secret string, errorChan chan<- error, done <-chan bool) {
|
||||
var firstRun = true
|
||||
var currentTime time.Time
|
||||
var token string
|
||||
var err error
|
||||
for {
|
||||
currentTime = time.Now()
|
||||
token, err = core.GenTOTP(secret, currentTime)
|
||||
if firstRun && err != nil {
|
||||
errorChan <- err
|
||||
}
|
||||
err = CopyBytes(false, []byte(token))
|
||||
if firstRun {
|
||||
if err != nil {
|
||||
errorChan <- err
|
||||
} else {
|
||||
errorChan <- nil // indicate that first copy was successful
|
||||
}
|
||||
firstRun = false
|
||||
}
|
||||
|
||||
// sleep till next 30-second interval
|
||||
time.Sleep(time.Duration(30-(currentTime.Second()%30)) * time.Second)
|
||||
|
||||
// exit after sleep if indicated (will not update clipboard again)
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
#!/bin/sh
|
||||
gofmt -l -w -s ./core/*.go ./sync/*.go ./libmuttonserver.go
|
||||
git commit -am "$1"
|
||||
git push
|
||||
@@ -0,0 +1,82 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
)
|
||||
|
||||
type CfgT struct {
|
||||
Libmutton struct {
|
||||
OfflineMode *bool `json:"offlineMode"`
|
||||
SSHUser *string `json:"sshUser"`
|
||||
SSHIP *string `json:"sshIP"`
|
||||
SSHPort *string `json:"sshPort"`
|
||||
SSHEntryRootPath *string `json:"sshEntryRootPath"`
|
||||
SSHAgeDirPath *string `json:"sshAgeDirPath"`
|
||||
SSHKeyPath *string `json:"sshKeyPath"`
|
||||
SSHKeyProtected *bool `json:"sshKeyProtected"`
|
||||
SSHIsWindows *bool `json:"sshIsWindows"`
|
||||
} `json:"libmutton"`
|
||||
ClientSpecific *map[string]any `json:"clientSpecific"`
|
||||
}
|
||||
|
||||
// Load loads libmuttoncfg.json and returns the configuration.
|
||||
func Load() (*CfgT, error) {
|
||||
cfgBytes, err := os.ReadFile(global.CfgPath)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to load libmuttoncfg.json: " + err.Error())
|
||||
}
|
||||
var cfg CfgT
|
||||
if err = json.Unmarshal(cfgBytes, &cfg); err != nil {
|
||||
return nil, errors.New("unable to unmarshal libmuttoncfg.json: " + err.Error())
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// Write writes cfg to libmuttoncfg.json.
|
||||
// If used in append mode, any nil values in the
|
||||
// input cfg will be substituted with the existing values.
|
||||
func Write(cfg *CfgT, appendMode bool) error {
|
||||
start:
|
||||
if appendMode {
|
||||
// check if any fields are nil
|
||||
var hasNilFields bool
|
||||
cfgValue := reflect.ValueOf(&cfg.Libmutton).Elem()
|
||||
for _, field := range cfgValue.Fields() {
|
||||
if field.IsNil() {
|
||||
hasNilFields = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// load old cfg and copy nil fields
|
||||
if hasNilFields {
|
||||
oldCfg, err := Load()
|
||||
if err != nil {
|
||||
// failed to load config, leave append mode
|
||||
appendMode = false
|
||||
goto start
|
||||
}
|
||||
oldValue := reflect.ValueOf(&oldCfg.Libmutton).Elem()
|
||||
for i := 0; i < cfgValue.NumField(); i++ {
|
||||
field := cfgValue.Field(i)
|
||||
if field.IsNil() {
|
||||
field.Set(oldValue.Field(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cfgBytes, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
return errors.New("unable to marshal new/updated cfg: " + err.Error())
|
||||
}
|
||||
if err = os.WriteFile(global.CfgPath, cfgBytes, 0600); err != nil {
|
||||
return errors.New("unable to write new/updated cfg to libmuttoncfg.json: " + err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
Home, _ = os.UserHomeDir()
|
||||
)
|
||||
|
||||
const (
|
||||
LibmuttonVersion = "0.2.1" // untagged releases feature a letter suffix corresponding to the eventual release version, e.g "0.2.A" -> "0.2.0", "0.2.B" -> "0.2.1"
|
||||
|
||||
FSSpace = "\u259d" // ▝ space/list separator
|
||||
FSPath = "\u259e" // ▞ path separator
|
||||
FSMisc = "\u259f" // ▟ misc. field separator (if \u259d is already used)
|
||||
|
||||
AnsiError = "\033[38;5;9m"
|
||||
AnsiReset = "\033[0m"
|
||||
|
||||
ErrorRead = 101
|
||||
ErrorWrite = 102
|
||||
ErrorSyncProcess = 103
|
||||
ErrorServerConnection = 104
|
||||
ErrorTargetNotFound = 105
|
||||
ErrorTargetExists = 106
|
||||
ErrorTargetWrongType = 107
|
||||
ErrorDecryption = 108
|
||||
ErrorEncryption = 109
|
||||
ErrorClipboard = 110
|
||||
ErrorOther = 111
|
||||
)
|
||||
@@ -1,18 +0,0 @@
|
||||
//go:build !windows
|
||||
|
||||
package core
|
||||
|
||||
var EntryRoot = Home + "/.local/share/libmutton" // path to libmutton entry directory
|
||||
var ConfigDir = Home + "/.config/libmutton" // path to libmutton configuration directory
|
||||
var ConfigPath = ConfigDir + "/libmutton.ini" // path to libmutton configuration file
|
||||
|
||||
const (
|
||||
PathSeparator = "/" // platform-specific path separator
|
||||
IsWindows = false // platform indicator
|
||||
)
|
||||
|
||||
// enableVirtualTerminalProcessing is a dummy function on UNIX-like systems (only needed on Windows).
|
||||
// TODO Remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation.
|
||||
func enableVirtualTerminalProcessing() {
|
||||
return
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var EntryRoot = Home + "\\AppData\\Local\\libmutton\\entries" // path to libmutton entry directory
|
||||
var ConfigDir = Home + "\\AppData\\Local\\libmutton\\config" // path to libmutton configuration directory
|
||||
var ConfigPath = ConfigDir + "\\libmutton.ini" // path to libmutton configuration file
|
||||
|
||||
const (
|
||||
PathSeparator = "\\" // platform-specific path separator
|
||||
IsWindows = true // platform indicator
|
||||
)
|
||||
|
||||
// enableVirtualTerminalProcessing ensures ANSI escape sequences are interpreted properly on Windows.
|
||||
// TODO Remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation.
|
||||
func enableVirtualTerminalProcessing() {
|
||||
stdout := syscall.Handle(os.Stdout.Fd())
|
||||
|
||||
var originalMode uint32
|
||||
syscall.GetConsoleMode(stdout, &originalMode)
|
||||
originalMode |= 0x0004
|
||||
|
||||
syscall.MustLoadDLL("kernel32").MustFindProc("SetConsoleMode").Call(uintptr(stdout), uintptr(originalMode))
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
// loadConfig loads the libmutton.ini file and returns the configuration.
|
||||
// It is a utility function for ParseConfig and WriteConfig; do not call directly.
|
||||
func loadConfig() *ini.File {
|
||||
cfg, err := ini.Load(ConfigPath)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to load libmutton.ini:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorRead)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// ParseConfig reads the libmutton.ini file and returns a slice of values for the specified keys.
|
||||
// Requires: requestedValues (a slice of length 2 arrays each containing a section and a key name),
|
||||
// missingValueError (an error message to display if a key is missing a value, set to "" for auto-generated or "0" to exit/return silently with code 0).
|
||||
// Returns: config (slice of values for the specified keys).
|
||||
func ParseConfig(valuesRequested [][2]string, missingValueError string) []string {
|
||||
cfg := loadConfig()
|
||||
|
||||
var config []string
|
||||
|
||||
for _, pair := range valuesRequested {
|
||||
value := cfg.Section(pair[0]).Key(pair[1]).String()
|
||||
|
||||
// ensure specified key has a value
|
||||
if value == "" {
|
||||
switch missingValueError {
|
||||
case "":
|
||||
fmt.Println(AnsiError + "Failed to find value for key \"" + pair[1] + "\" in section \"[" + pair[0] + "]\" in libmutton.ini" + AnsiReset)
|
||||
case "0":
|
||||
Exit(0)
|
||||
default:
|
||||
fmt.Println(AnsiError + missingValueError + AnsiReset)
|
||||
}
|
||||
os.Exit(ErrorRead)
|
||||
}
|
||||
|
||||
config = append(config, value)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// GenDeviceIDList returns a pointer to a slice of all registered device IDs.
|
||||
// Requires: errorOnFail (set to true to throw an error if the devices directory cannot be read/does not exist)
|
||||
func GenDeviceIDList(errorOnFail bool) *[]fs.DirEntry {
|
||||
// create a slice of all registered devices
|
||||
deviceIDList, err := os.ReadDir(ConfigDir + PathSeparator + "devices")
|
||||
if err != nil {
|
||||
if errorOnFail {
|
||||
fmt.Println(AnsiError+"Failed to read the devices directory:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorRead)
|
||||
} else {
|
||||
return nil // a nil return value indicates that the devices directory could not be read/does not exist
|
||||
}
|
||||
}
|
||||
return &deviceIDList
|
||||
}
|
||||
|
||||
// WriteConfig writes the provided key-value pairs under the specified section headers in the libmutton.ini file.
|
||||
// Requires: valuesToWrite (a slice of length 3 arrays each containing a section, a key name, and a value).
|
||||
func WriteConfig(valuesToWrite [][3]string, append bool) {
|
||||
var cfg *ini.File
|
||||
|
||||
if append {
|
||||
// load existing ini file
|
||||
cfg = loadConfig()
|
||||
} else {
|
||||
// create empty ini container
|
||||
cfg = ini.Empty()
|
||||
}
|
||||
|
||||
// set all specified key-value pairs in their respective sections
|
||||
var section *ini.Section
|
||||
for _, trio := range valuesToWrite {
|
||||
if cfg.Section(trio[0]) == nil {
|
||||
// create and aquire section if it doesn't exist
|
||||
section, _ = cfg.NewSection(trio[0])
|
||||
} else {
|
||||
// acquire existing section
|
||||
section = cfg.Section(trio[0])
|
||||
}
|
||||
|
||||
// set key-value pair
|
||||
section.Key(trio[1]).SetValue(trio[2])
|
||||
}
|
||||
|
||||
// save to libmutton.ini
|
||||
err := cfg.SaveTo(ConfigPath)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to save libmutton.ini:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
steamtotp "github.com/fortis/go-steam-totp"
|
||||
"github.com/pquerna/otp/totp"
|
||||
)
|
||||
|
||||
// CopyArgument copies a field from an entry to the clipboard.
|
||||
func CopyArgument(executableName, targetLocation string, field int) {
|
||||
if isFile, _ := TargetIsFile(targetLocation, true, 2); isFile {
|
||||
|
||||
decryptedEntry := DecryptGPG(targetLocation)
|
||||
var copySubject string // will store data to be copied
|
||||
|
||||
// ensure field exists in entry
|
||||
if len(decryptedEntry) > field {
|
||||
|
||||
// ensure field is not empty
|
||||
if decryptedEntry[field] == "" {
|
||||
fmt.Println(AnsiError + "Field is empty" + AnsiReset)
|
||||
os.Exit(ErrorTargetNotFound)
|
||||
}
|
||||
|
||||
if field != 2 {
|
||||
copySubject = decryptedEntry[field]
|
||||
} else { // TOTP mode
|
||||
var secret string // stores secret for TOTP generation
|
||||
var forSteam bool // indicates whether to generate TOTP in Steam format
|
||||
|
||||
if strings.HasPrefix(decryptedEntry[2], "steam@") {
|
||||
secret = decryptedEntry[2][6:]
|
||||
forSteam = true
|
||||
} else {
|
||||
secret = decryptedEntry[2]
|
||||
}
|
||||
|
||||
fmt.Println("Clipboard will be kept up to date with the current TOTP code until this process is closed")
|
||||
|
||||
for { // keep field copied to clipboard, refresh on 30-second intervals
|
||||
currentTime := time.Now()
|
||||
copyField("", GenTOTP(secret, currentTime, forSteam))
|
||||
// sleep until next 30-second interval
|
||||
time.Sleep(time.Duration(30-(currentTime.Second()%30)) * time.Second)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Println(AnsiError + "Field does not exist in entry" + AnsiReset)
|
||||
os.Exit(ErrorTargetNotFound)
|
||||
}
|
||||
|
||||
// copy field to clipboard, launch clipboard clearing process
|
||||
copyField(executableName, copySubject)
|
||||
}
|
||||
}
|
||||
|
||||
// ClipClearArgument is called to clear the clipboard after 30 seconds if the contents have not been modified.
|
||||
func ClipClearArgument() {
|
||||
// read previous clipboard contents from stdin
|
||||
clipScanner := bufio.NewScanner(os.Stdin)
|
||||
if clipScanner.Scan() {
|
||||
oldContents := clipScanner.Text()
|
||||
clipClear(oldContents)
|
||||
} else {
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
}
|
||||
|
||||
// GenTOTP generates a TOTP token from a secret (supports standard and Steam TOTP).
|
||||
func GenTOTP(secret string, time time.Time, forSteam bool) string {
|
||||
var totpToken string
|
||||
var err error
|
||||
|
||||
if forSteam {
|
||||
totpToken, err = steamtotp.GenerateAuthCode(secret, time)
|
||||
} else {
|
||||
totpToken, err = totp.GenerateCode(secret, time)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Error generating TOTP code" + AnsiReset)
|
||||
os.Exit(ErrorOther)
|
||||
}
|
||||
|
||||
return totpToken
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
//go:build darwin
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// copyField copies a field from an entry to the clipboard.
|
||||
func copyField(executableName, copySubject string) {
|
||||
cmd := exec.Command("pbcopy")
|
||||
writeToStdin(cmd, copySubject)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
if executableName != "" {
|
||||
cmd = exec.Command(executableName, "clipclear")
|
||||
writeToStdin(cmd, copySubject)
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - Does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
}
|
||||
|
||||
// clipClear is called in a separate process to clear the clipboard after 30 seconds.
|
||||
func clipClear(oldContents string) {
|
||||
time.Sleep(30 * time.Second)
|
||||
|
||||
cmd := exec.Command("pbpaste")
|
||||
newContents, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
cmd = exec.Command("pbcopy")
|
||||
writeToStdin(cmd, "")
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
//go:build linux && termux
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// copyField copies a field from an entry to the clipboard.
|
||||
func copyField(executableName, copySubject string) {
|
||||
cmd := exec.Command("termux-clipboard-set")
|
||||
writeToStdin(cmd, copySubject)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
if executableName != "" {
|
||||
cmd = exec.Command(executableName, "clipclear")
|
||||
writeToStdin(cmd, copySubject)
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - Does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
}
|
||||
|
||||
// clipClear is called in a separate process to clear the clipboard after 30 seconds.
|
||||
func clipClear(oldContents string) {
|
||||
time.Sleep(30 * time.Second)
|
||||
|
||||
cmd := exec.Command("termux-clipboard-get")
|
||||
newContents, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
cmd = exec.Command("termux-clipboard-set")
|
||||
writeToStdin(cmd, "")
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
//go:build !windows && !darwin && !termux && !wsl
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// copyField copies a field from an entry to the clipboard.
|
||||
func copyField(executableName, copySubject string) {
|
||||
var envSet bool // track whether environment variables are set
|
||||
var cmd *exec.Cmd
|
||||
// determine whether to use wl-copy (Wayland) or xclip (X11)
|
||||
if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet {
|
||||
cmd = exec.Command("wl-copy")
|
||||
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
||||
cmd = exec.Command("xclip", "-sel", "c")
|
||||
} else {
|
||||
fmt.Println(AnsiError + "Clipboard platform could not be determined - Note that the clipboard does not function in a raw TTY" + AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
writeToStdin(cmd, copySubject)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
if executableName != "" {
|
||||
cmd = exec.Command(executableName, "clipclear")
|
||||
writeToStdin(cmd, copySubject)
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - Does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
}
|
||||
|
||||
// clipClear is called in a separate process to clear the clipboard after 30 seconds.
|
||||
func clipClear(oldContents string) {
|
||||
time.Sleep(30 * time.Second)
|
||||
|
||||
// determine clipboard tool to use (wl-clipboard VS xclip)
|
||||
var envSet bool
|
||||
var cmdClear, cmdPaste *exec.Cmd
|
||||
if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet {
|
||||
cmdClear = exec.Command("wl-copy", "-c")
|
||||
cmdPaste = exec.Command("wl-paste")
|
||||
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
||||
cmdClear = exec.Command("xclip", "-i", "/dev/null", "-sel", "c")
|
||||
cmdPaste = exec.Command("xclip", "-o", "-sel", "c")
|
||||
} else {
|
||||
fmt.Println(AnsiError + "Clipboard platform could not be determined - Neither $WAYLAND_DISPLAY nor $DISPLAY are set" + AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// read current clipboard contents
|
||||
newContents, err := cmdPaste.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// clear clipboard if contents have not been modified
|
||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
err = cmdClear.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
//go:build windows || (linux && wsl)
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// copyField copies a field from an entry to the clipboard.
|
||||
func copyField(executableName, copySubject string) {
|
||||
cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''")))
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
if executableName != "" {
|
||||
cmd = exec.Command(executableName, "clipclear")
|
||||
writeToStdin(cmd, copySubject)
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - Does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
}
|
||||
|
||||
// clipClear is called in a separate process to clear the clipboard after 30 seconds.
|
||||
func clipClear(oldContents string) {
|
||||
time.Sleep(30 * time.Second)
|
||||
|
||||
cmd := exec.Command("powershell.exe", "-c", "Get-Clipboard")
|
||||
newContents, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
cmd = exec.Command("powershell.exe", "-c", "Set-Clipboard")
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
+22
-7
@@ -1,19 +1,34 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/crypt"
|
||||
)
|
||||
|
||||
// GetOldEntryData decrypts and returns old entry data (with all required lines present).
|
||||
func GetOldEntryData(targetLocation string, field int) []string {
|
||||
// ensure targetLocation exists
|
||||
TargetIsFile(targetLocation, true, 2)
|
||||
// This is a wrapper around the DecryptFileToSlice function that ensures all required lines are present in the returned slice.
|
||||
// This makes it ideal for editing entries, as it guarantees at least a baseline slice length.
|
||||
// Leave rcwPassword nil to use RCW demonization.
|
||||
func GetOldEntryData(realPath string, field int, rcwPassword []byte) ([]string, error) {
|
||||
// ensure realPath exists and is a file
|
||||
_, err := back.TargetIsFile(realPath, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// read old entry data
|
||||
unencryptedEntry := DecryptGPG(targetLocation)
|
||||
decryptedEntry, err := crypt.DecryptFileToSlice(realPath, rcwPassword)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to decrypt entry: " + err.Error())
|
||||
}
|
||||
|
||||
// return the old entry data with all required lines present
|
||||
if field > 0 {
|
||||
return ensureSliceLength(unencryptedEntry, field)
|
||||
} else {
|
||||
return unencryptedEntry
|
||||
return ensureSliceLength(decryptedEntry, field), nil
|
||||
}
|
||||
return decryptedEntry, nil
|
||||
}
|
||||
|
||||
// ensureSliceLength is a utility function that ensures a slice is long enough to contain the specified index.
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
//go:build !returnOnExit
|
||||
|
||||
package core
|
||||
|
||||
import "os"
|
||||
|
||||
// Exit (hard) is meant to be used in non-interactive CLI implementations to exit the program after an operation.
|
||||
func Exit(code int) {
|
||||
os.Exit(code)
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
//go:build returnOnExit
|
||||
|
||||
package core
|
||||
|
||||
// Exit (soft) is meant to be used in interactive implementations (GUIs/TUIs) to keep the program running after an operation.
|
||||
func Exit(code int) {
|
||||
return code
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TODO GPG support is a temporary feature - It will be replaced with a different encryption scheme in the future
|
||||
|
||||
// DecryptGPG decrypts a GPG-encrypted file and returns the contents as a slice of (trimmed) strings.
|
||||
func DecryptGPG(targetLocation string) []string {
|
||||
cmd := exec.Command("gpg", "--pinentry-mode", "loopback", "-q", "-d", targetLocation)
|
||||
output, err := cmd.Output()
|
||||
|
||||
// ensure ANSI escape sequences are interpreted properly on Windows
|
||||
enableVirtualTerminalProcessing()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to decrypt \"" + targetLocation + "\" - Ensure it is a valid GPG-encrypted file and that you entered your passphrase correctly" + AnsiReset)
|
||||
os.Exit(ErrorDecryption)
|
||||
}
|
||||
outputSlice := strings.Split(string(output), "\n")
|
||||
|
||||
return outputSlice
|
||||
}
|
||||
|
||||
// EncryptGPG encrypts a slice of strings using GPG and returns the encrypted data as a byte slice.
|
||||
func EncryptGPG(input []string) []byte {
|
||||
cmd := exec.Command("gpg", "-q", "-r", ParseConfig([][2]string{{"LIBMUTTON", "gpgID"}}, "")[0], "-e")
|
||||
writeToStdin(cmd, strings.Join(input, "\n"))
|
||||
encryptedBytes, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to encrypt data - Ensure that your GPG key is valid and that you have a valid GPG ID set in libmutton.ini" + AnsiReset)
|
||||
os.Exit(ErrorEncryption)
|
||||
}
|
||||
return encryptedBytes
|
||||
}
|
||||
+89
-78
@@ -1,94 +1,105 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"cmp"
|
||||
"errors"
|
||||
"maps"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/config"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/libmutton/syncclient"
|
||||
"github.com/rwinkhart/rcw/wrappers"
|
||||
)
|
||||
|
||||
// GpgUIDListGen generates a list of all GPG key IDs on the system and returns them as a slice of strings.
|
||||
func GpgUIDListGen() []string {
|
||||
cmd := exec.Command("gpg", "-k", "--with-colons")
|
||||
gpgOutputBytes, _ := cmd.Output()
|
||||
gpgOutputLines := strings.Split(string(gpgOutputBytes), "\n")
|
||||
var uidSlice []string
|
||||
for _, line := range gpgOutputLines {
|
||||
if strings.HasPrefix(line, "uid") {
|
||||
uid := strings.Split(line, ":")[9]
|
||||
uidSlice = append(uidSlice, uid)
|
||||
}
|
||||
}
|
||||
return uidSlice
|
||||
}
|
||||
|
||||
// GpgKeyGen generates a new GPG key and returns the key ID.
|
||||
func GpgKeyGen() string {
|
||||
gpgGenTempFile := CreateTempFile()
|
||||
defer func(name string) {
|
||||
_ = os.Remove(name) // error ignored; if the file could be created, it can probably be removed
|
||||
}(gpgGenTempFile.Name())
|
||||
|
||||
// create and write gpg-gen file
|
||||
unixTime := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
_, _ = gpgGenTempFile.WriteString(strings.Join([]string{"Key-Type: eddsa", "Key-Curve: ed25519", "Key-Usage: sign", "Subkey-Type: ecdh", "Subkey-Curve: cv25519", "Subkey-Usage: encrypt", "Name-Real: libmutton-" + unixTime, "Name-Comment: gpg-libmutton", "Name-Email: github.com/rwinkhart/libmutton", "Expire-Date: 0"}, "\n")) // error ignored; if the file could be created, it can probably be written to
|
||||
|
||||
// close gpg-gen file
|
||||
_ = gpgGenTempFile.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
|
||||
// generate GPG key based on gpg-gen file
|
||||
cmd := exec.Command("gpg", "-q", "--batch", "--generate-key", gpgGenTempFile.Name())
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to generate GPG key:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorOther)
|
||||
// LibmuttonInit creates the libmutton config structure based on user input.
|
||||
// deviceIDPrefix can be left blank to use the system hostname.
|
||||
// clientSpecificCfg can be left nil if not needed.
|
||||
func LibmuttonInit(inputCB func(prompt string) string, rcwPassword []byte, appendMode, forceOfflineMode bool, deviceIDPrefix string, clientSpecificCfg map[string]any) error {
|
||||
// handle clientSpecificCfg
|
||||
newCfg := &config.CfgT{}
|
||||
if clientSpecificCfg != nil {
|
||||
newClientSpecificMap := make(map[string]any)
|
||||
maps.Copy(newClientSpecificMap, clientSpecificCfg)
|
||||
newCfg.ClientSpecific = &newClientSpecificMap
|
||||
}
|
||||
|
||||
return "libmutton-" + unixTime + " (gpg-libmutton) <github.com/rwinkhart/libmutton>"
|
||||
}
|
||||
|
||||
// DirInit creates the libmutton directories.
|
||||
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID).
|
||||
func DirInit(preserveOldConfigDir bool) string {
|
||||
// create EntryRoot
|
||||
err := os.MkdirAll(EntryRoot, 0700)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to create \""+EntryRoot+"\":", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
|
||||
// get old device ID before its potential removal
|
||||
oldDeviceIDList := GenDeviceIDList(false) // errorOnFail is false so that nil is received when the devices directory does not exist
|
||||
var oldDeviceID string
|
||||
if oldDeviceIDList != nil && len(*oldDeviceIDList) > 0 { // ensure not derferencing nil, which occurs when the devices directory does not exist
|
||||
oldDeviceID = (*oldDeviceIDList)[0].Name()
|
||||
var r string
|
||||
if !forceOfflineMode {
|
||||
r = strings.ToLower(inputCB("Configure SSH settings (for synchronization)? (Y/n)"))
|
||||
} else {
|
||||
oldDeviceID = FSMisc // indicates to server that no device ID is being replaced
|
||||
r = "n"
|
||||
}
|
||||
if len(r) > 0 && r[0] == 'n' {
|
||||
// initialize libmutton directories
|
||||
_, err := global.DirInit(appendMode)
|
||||
if err != nil {
|
||||
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
||||
}
|
||||
|
||||
// remove existing config directory (if it exists and not in append mode)
|
||||
if !preserveOldConfigDir {
|
||||
_, isAccessible := TargetIsFile(ConfigDir, false, 1)
|
||||
if isAccessible {
|
||||
err = os.RemoveAll(ConfigDir)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to remove existing config directory:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
// write config file
|
||||
newCfg.Libmutton.OfflineMode = new(true)
|
||||
if err = config.Write(newCfg, false); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// ensure ssh key file exists (and is a file)
|
||||
fallbackSSHKey := global.SSHDir + global.PathSeparator + "id_ed25519"
|
||||
sshKeyPath := cmp.Or(back.ExpandPathWithHome(inputCB("SSH private identity file path (falls back to \""+fallbackSSHKey+"\"):")), fallbackSSHKey)
|
||||
_, err := back.TargetIsFile(sshKeyPath, true)
|
||||
if err != nil {
|
||||
return errors.New("unable to find SSH identity file: " + err.Error())
|
||||
}
|
||||
|
||||
// get other ssh info from user
|
||||
var sshKeyProtected bool
|
||||
r = strings.ToLower(inputCB("Is the identity file password-protected? (y/N)"))
|
||||
if len(r) > 0 && r[0] == 'y' {
|
||||
sshKeyProtected = true
|
||||
}
|
||||
|
||||
// perform operations based on collected user input
|
||||
//// initialize libmutton directories
|
||||
oldDeviceID, err := global.DirInit(appendMode)
|
||||
if err != nil {
|
||||
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
||||
}
|
||||
//// write config file
|
||||
//// temporarily leave sshEntryRootPath, sshAgeDirPath, and sshIsWindows as nil/default to pass initial device ID registration
|
||||
newCfg.Libmutton.OfflineMode = &forceOfflineMode // forceOfflineMode must be false to reach this point, so we can avoid the extra declaration
|
||||
newCfg.Libmutton.SSHUser = new(inputCB("Remote SSH username:"))
|
||||
newCfg.Libmutton.SSHIP = new(inputCB("Remote SSH IP/domain:"))
|
||||
newCfg.Libmutton.SSHPort = new(inputCB("Remote SSH port:"))
|
||||
newCfg.Libmutton.SSHKeyPath = &sshKeyPath
|
||||
newCfg.Libmutton.SSHKeyProtected = &sshKeyProtected
|
||||
if err = config.Write(newCfg, appendMode); err != nil { // pass appendMode to allow not completely destroying existing (client-specific) config
|
||||
return err
|
||||
}
|
||||
// generate and register device ID
|
||||
sshEntryRoot, sshAgeDir, sshIsWindows, err := syncclient.GenDeviceID(oldDeviceID, deviceIDPrefix)
|
||||
if err != nil {
|
||||
return errors.New("unable to generate device ID: " + err.Error())
|
||||
}
|
||||
// update config file
|
||||
newCfg.Libmutton.SSHEntryRootPath = &sshEntryRoot
|
||||
newCfg.Libmutton.SSHAgeDirPath = &sshAgeDir
|
||||
newCfg.Libmutton.SSHIsWindows = &sshIsWindows
|
||||
if err = config.Write(newCfg, true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// create config directory w/devices subdirectory
|
||||
err = os.MkdirAll(ConfigDir+PathSeparator+"devices", 0700)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to create \""+ConfigDir+"\":", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorWrite)
|
||||
// generate rcw sanity check file
|
||||
if err := RCWSanityCheckGen(rcwPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return oldDeviceID
|
||||
// RCWSanityCheckGen generates the RCW sanity check file for libmutton.
|
||||
func RCWSanityCheckGen(password []byte) error {
|
||||
if err := wrappers.GenSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password, true); err != nil {
|
||||
return errors.New("unable to generate sanity check file: " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
//go:build !windows
|
||||
|
||||
package core
|
||||
|
||||
// TargetLocationFormat returns the full location of an entry (given the name) formatted for the current platform.
|
||||
func TargetLocationFormat(targetLocationIncomplete string) string {
|
||||
return EntryRoot + targetLocationIncomplete
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package core
|
||||
|
||||
import "strings"
|
||||
|
||||
// TargetLocationFormat returns the full location of an entry (given the name) formatted for the current platform.
|
||||
func TargetLocationFormat(targetLocationIncomplete string) string {
|
||||
return EntryRoot + strings.ReplaceAll(targetLocationIncomplete, "/", PathSeparator)
|
||||
}
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp"
|
||||
"github.com/pquerna/otp/totp"
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/age"
|
||||
"github.com/rwinkhart/libmutton/crypt"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/libmutton/syncclient"
|
||||
"github.com/rwinkhart/libmutton/synccommon"
|
||||
"github.com/rwinkhart/rcw/wrappers"
|
||||
)
|
||||
|
||||
// WriteEntry writes decSlice to an encrypted file at realPath.
|
||||
// If the entry contains an updated password, an age file is also created.
|
||||
// Leave rcwPassword nil to use RCW demonization.
|
||||
func WriteEntry(realPath string, decSlice []string, passwordIsNew bool, rcwPassword []byte) error {
|
||||
err := os.WriteFile(realPath, crypt.EncryptBytes([]byte(strings.Join(clampTrailingWhitespace(decSlice), "\n")), rcwPassword), 0600)
|
||||
if err != nil {
|
||||
return errors.New("unable to write to file: " + err.Error())
|
||||
}
|
||||
|
||||
if decSlice != nil {
|
||||
if passwordIsNew { // update age data when password changes
|
||||
if decSlice[0] != "" { // if the password change was NOT a removal, update the age file
|
||||
if err = age.Entry(global.GetVanityPath(realPath), time.Now().Unix()); err != nil {
|
||||
return errors.New("unable to update age data: " + err.Error())
|
||||
}
|
||||
} else { // if the password change was a removal, remove the associated age file
|
||||
if err = syncclient.ShearRemote(global.GetVanityPath(realPath), true); err != nil {
|
||||
return errors.New("unable to remove age data: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EntryRefresh re-encrypts all libmutton entries with a new password
|
||||
// and optimizes each entry to ensure they are as slim as possible.
|
||||
func EntryRefresh(oldRCWPassword, newRCWPassword []byte, removeOldDir bool) error {
|
||||
// ensure global.EntryRoot+"-new" and global.EntryRoot-"old" do not exist
|
||||
dirEnds := []string{"-new", "-old"}
|
||||
for i, dirEnd := range dirEnds {
|
||||
if i == 1 && !removeOldDir {
|
||||
if _, err := os.Stat(global.EntryRoot + "-old"); !os.IsNotExist(err) {
|
||||
return errors.New("unable to refresh entries: \"" + global.EntryRoot + "-old\" already exists")
|
||||
}
|
||||
}
|
||||
if err := os.RemoveAll(global.EntryRoot + dirEnd); err != nil {
|
||||
return errors.New("unable to remove \"" + global.EntryRoot + dirEnd + "\": " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// create output directory structure (global.EntryRoot + "-new"/*)
|
||||
entries, folders, err := synccommon.WalkEntryDir()
|
||||
if err != nil {
|
||||
return errors.New("unable to walk entry directory: " + err.Error())
|
||||
}
|
||||
for _, folder := range folders {
|
||||
fullPath := global.EntryRoot + "-new" + strings.ReplaceAll(folder, "/", global.PathSeparator)
|
||||
if err := os.MkdirAll(fullPath, 0700); err != nil {
|
||||
return errors.New("unable to create temporary directory \"" + fullPath + "\": " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// decrypt, optimize, and re-encrypt each entry
|
||||
for _, vanityPath := range entries {
|
||||
realPath := global.GetRealPath(vanityPath)
|
||||
encBytes, err := os.ReadFile(realPath)
|
||||
if err != nil {
|
||||
return errors.New("unable to open \"" + realPath + "\" for decryption: " + err.Error())
|
||||
}
|
||||
decBytes, err := wrappers.Decrypt(encBytes, oldRCWPassword, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// split & optimize entry
|
||||
decSlice := clampTrailingWhitespace(strings.Split(string(decBytes), "\n"))
|
||||
|
||||
// re-encrypt the entry with the new password
|
||||
encBytes = wrappers.Encrypt([]byte(strings.Join(decSlice, "\n")), newRCWPassword, true, false)
|
||||
|
||||
// write the entry to the new directory
|
||||
if err = os.WriteFile(global.EntryRoot+"-new"+strings.ReplaceAll(vanityPath, "/", global.PathSeparator), encBytes, 0600); err != nil {
|
||||
return errors.New("unable to write to file: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// swap the new directory with the old one
|
||||
if err = os.Rename(global.EntryRoot, global.EntryRoot+"-old"); err != nil {
|
||||
return errors.New("unable to rename old directory: " + err.Error())
|
||||
}
|
||||
if err = os.Rename(global.EntryRoot+"-new", global.EntryRoot); err != nil {
|
||||
return errors.New("unable to rename new directory: " + err.Error())
|
||||
}
|
||||
|
||||
// generate new sanity check file
|
||||
if err = RCWSanityCheckGen(newRCWPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// VerifyEntries decrypts all entries to memory and returns an error if
|
||||
// any failures are encountered. Failures likely indicate corrupt entries.
|
||||
func VerifyEntries(rcwPassword []byte) error {
|
||||
entries, _, err := synccommon.WalkEntryDir()
|
||||
if err != nil {
|
||||
return errors.New("unable to walk entry directory: " + err.Error())
|
||||
}
|
||||
for _, vanityPath := range entries {
|
||||
realPath := global.GetRealPath(vanityPath)
|
||||
encBytes, err := os.ReadFile(realPath)
|
||||
if err != nil {
|
||||
return errors.New("unable to open \"" + realPath + "\" for decryption: " + err.Error())
|
||||
}
|
||||
decBytes, err := wrappers.Decrypt(encBytes, rcwPassword, false)
|
||||
if err != nil {
|
||||
return errors.New("unable to verify \"" + vanityPath + "\" (decryption failure): " + err.Error())
|
||||
}
|
||||
if len(decBytes) < 1 {
|
||||
return errors.New("unable to verify \"" + vanityPath + "\" (contains 0/nil bytes)")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// clampTrailingWhitespace ensures the provided decSlice contains no trailing blank lines.
|
||||
// If decSlice contains a note, it strips trailing newlines, carriage returns, and tabs from
|
||||
// each line in the note. Additionally, it removes single trailing spaces and truncates
|
||||
// multiple trailing spaces to two (for Markdown formatting).
|
||||
func clampTrailingWhitespace(decSlice []string) []string {
|
||||
decSlice = back.RemoveTrailingEmptyStrings(decSlice)
|
||||
if len(decSlice) >= 4 {
|
||||
for i, noteLine := range decSlice[4:] {
|
||||
// remove trailing tabs, carriage returns, and newlines
|
||||
noteLine = strings.TrimRight(noteLine, "\t\r\n")
|
||||
|
||||
// determine the number of trailing spaces in the trimmed line
|
||||
var endSpacesCount int
|
||||
for j := len(noteLine) - 1; j >= 0; j-- {
|
||||
if noteLine[j] != ' ' {
|
||||
break
|
||||
}
|
||||
endSpacesCount++
|
||||
}
|
||||
|
||||
// remove single spaces, truncate multiple spaces (leave two for Markdown formatting)
|
||||
switch endSpacesCount {
|
||||
case 0:
|
||||
// no trailing spaces
|
||||
decSlice[i+4] = noteLine
|
||||
case 1:
|
||||
// remove the single trailing space
|
||||
decSlice[i+4] = strings.TrimRight(noteLine, " ")
|
||||
default:
|
||||
// truncate the trailing spaces to two
|
||||
decSlice[i+4] = noteLine[:len(noteLine)-endSpacesCount+2]
|
||||
}
|
||||
}
|
||||
}
|
||||
return decSlice
|
||||
}
|
||||
|
||||
// EntryAddPrecheck ensures the directory meant to contain a new
|
||||
// entry exists and that realPath is not already used.
|
||||
// Returns: statusCode (0 = success, 1 = realPath already exists, 2 = containing directory is invalid).
|
||||
func EntryAddPrecheck(realPath string) (uint8, error) {
|
||||
// ensure realPath does not already exist
|
||||
isAccessible, _ := back.TargetIsFile(realPath, false) // error is ignored because dir/file status is irrelevant
|
||||
if isAccessible {
|
||||
return 1, errors.New("target location already exists")
|
||||
}
|
||||
// ensure target containing directory exists and is not a file
|
||||
containingDir := realPath[:strings.LastIndex(realPath, global.PathSeparator)]
|
||||
_, err := back.TargetIsFile(containingDir, false)
|
||||
if err != nil {
|
||||
return 2, errors.New("\"" + containingDir + "\" is not a valid containing directory: " + err.Error())
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// EntryIsNotEmpty iterates through entryData and returns true if any line is not empty.
|
||||
func EntryIsNotEmpty(entryData []string) bool {
|
||||
for _, line := range entryData {
|
||||
if line != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GenTOTP generates a TOTP token from a secret.
|
||||
// Prefix `secret` with "steam@" for Steam TOTP format.
|
||||
func GenTOTP(secret string, time time.Time) (string, error) {
|
||||
var totpToken string
|
||||
var err error
|
||||
|
||||
if strings.HasPrefix(secret, "steam@") {
|
||||
totpToken, err = totp.GenerateCodeCustom(secret[6:], time, totp.ValidateOpts{Period: 30, Digits: 5, Encoder: otp.EncoderSteam})
|
||||
} else {
|
||||
totpToken, err = totp.GenerateCode(secret, time)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return "", errors.New("unable to generate TOTP token: " + err.Error())
|
||||
}
|
||||
|
||||
return totpToken, nil
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"math/big"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TargetIsFile checks if the targetLocation is a file, directory, or is inaccessible.
|
||||
// Requires: failCondition (0 = fail on inaccessible, 1 = fail on inaccessible&file, 2 = fail on inaccessible&directory).
|
||||
// Returns: isFile, isAccessible.
|
||||
func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8) (bool, bool) {
|
||||
targetInfo, err := os.Stat(targetLocation)
|
||||
if err != nil {
|
||||
if errorOnFail {
|
||||
fmt.Println(AnsiError + "Failed to access \"" + targetLocation + "\" - Ensure it exists and has the correct permissions" + AnsiReset)
|
||||
os.Exit(ErrorTargetNotFound)
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
if targetInfo.IsDir() {
|
||||
if errorOnFail && failCondition == 2 {
|
||||
fmt.Println(AnsiError + "\"" + targetLocation + "\" is a directory" + AnsiReset)
|
||||
os.Exit(ErrorTargetWrongType)
|
||||
}
|
||||
return false, true
|
||||
} else {
|
||||
if errorOnFail && failCondition == 1 {
|
||||
fmt.Println(AnsiError + "\"" + targetLocation + "\" is a file" + AnsiReset)
|
||||
os.Exit(ErrorTargetWrongType)
|
||||
}
|
||||
return true, true
|
||||
}
|
||||
}
|
||||
|
||||
// WriteEntry writes entryData to an encrypted file at targetLocation.
|
||||
func WriteEntry(targetLocation string, entryData []string, verifyEntryDoesNotExist bool) {
|
||||
if verifyEntryDoesNotExist {
|
||||
_, isAccessible := TargetIsFile(targetLocation, false, 0)
|
||||
if isAccessible {
|
||||
fmt.Println(AnsiError + "Target location already exists" + AnsiReset)
|
||||
os.Exit(ErrorTargetExists)
|
||||
}
|
||||
}
|
||||
|
||||
encryptedBytes := EncryptGPG(entryData)
|
||||
err := os.WriteFile(targetLocation, encryptedBytes, 0600)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to write to file:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
}
|
||||
|
||||
// writeToStdin is a utility function that writes a string to a command's stdin.
|
||||
func writeToStdin(cmd *exec.Cmd, input string) {
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to access stdin for system command:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorOther)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func(stdin io.WriteCloser) {
|
||||
_ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed
|
||||
}(stdin)
|
||||
_, _ = io.WriteString(stdin, input)
|
||||
}()
|
||||
}
|
||||
|
||||
// CreateTempFile creates a temporary file and returns a pointer to it.
|
||||
func CreateTempFile() *os.File {
|
||||
tempFile, err := os.CreateTemp("", "*.markdown")
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to create temporary file:", err.Error()+AnsiReset)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
return tempFile
|
||||
}
|
||||
|
||||
// RemoveTrailingEmptyStrings removes empty strings from the end of a slice.
|
||||
func RemoveTrailingEmptyStrings(slice []string) []string {
|
||||
for i := len(slice) - 1; i >= 0; i-- {
|
||||
if slice[i] != "" {
|
||||
return slice[:i+1]
|
||||
}
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// StringGen generates a random string of a specified length and complexity.
|
||||
// Requires: complexity (minimum percentage of special characters to be returned in the generated string; only impacts complex strings),
|
||||
// safeForFileName: (if true, the generated string will only contain special characters that are safe for file names; only impacts complex strings).
|
||||
func StringGen(length int, complex bool, complexity float64, safeForFileName bool) string {
|
||||
var actualSpecialChars int // track the number of special characters in the generated string
|
||||
var minSpecialChars int // track the minimum number of special characters to accept
|
||||
var extendedCharset string // additions to character set used for complex strings
|
||||
|
||||
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // default character set used for all strings
|
||||
const extendedCharsetFiles = "!#$%&'()+,-.;=@[]^_`{}~" // additional special characters for complex strings (safe in file names)
|
||||
const extendedCharsetPassword = "\"*:><?/\\|" // additional special characters for complex strings (NOT safe in file names)
|
||||
if complex {
|
||||
minSpecialChars = int(math.Round(float64(length) * complexity)) // determine minimum number of special characters to accept
|
||||
if !safeForFileName {
|
||||
extendedCharset = extendedCharsetFiles + extendedCharsetPassword
|
||||
} else {
|
||||
extendedCharset = extendedCharsetFiles
|
||||
}
|
||||
charset += extendedCharset
|
||||
}
|
||||
|
||||
// loop until a string of the desired complexity is generated
|
||||
for {
|
||||
// generate a random string
|
||||
result := make([]byte, length)
|
||||
for i := range result {
|
||||
val, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
result[i] = charset[val.Int64()]
|
||||
}
|
||||
|
||||
// return early if the string is not complex
|
||||
if !complex {
|
||||
return string(result)
|
||||
}
|
||||
|
||||
// count the number of special characters in the generated string
|
||||
for _, char := range string(result) {
|
||||
if strings.ContainsRune(extendedCharset, char) {
|
||||
actualSpecialChars++
|
||||
}
|
||||
}
|
||||
|
||||
// return the generated string if it contains enough special characters
|
||||
if actualSpecialChars >= minSpecialChars {
|
||||
return string(result)
|
||||
}
|
||||
|
||||
// reset special character counter
|
||||
fmt.Println("Regenerating string until desired complexity is achieved...")
|
||||
actualSpecialChars = 0
|
||||
}
|
||||
}
|
||||
|
||||
// EntryIsNotEmpty iterates through entryData and returns true if any line is not empty.
|
||||
func EntryIsNotEmpty(entryData []string) bool {
|
||||
for _, line := range entryData {
|
||||
if line != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package crypt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/rcw/daemon"
|
||||
"github.com/rwinkhart/rcw/wrappers"
|
||||
)
|
||||
|
||||
var RetryPassword = true
|
||||
|
||||
// RCWDArgument reads the password from stdin and caches it via an RCW daemon.
|
||||
func RCWDArgument() {
|
||||
password := back.ReadFromStdin()
|
||||
if password == nil {
|
||||
os.Exit(0) // use os.Exit directly since this function is only intended for non-interactive CLI clients
|
||||
}
|
||||
daemon.Start(password)
|
||||
}
|
||||
|
||||
// DecryptFileToSlice decrypts an RCW wrapped file
|
||||
// and returns the contents as a slice of (trimmed) strings.
|
||||
// Leave rcwPassword nil to use RCW demonization.
|
||||
func DecryptFileToSlice(realPath string, rcwPassword []byte) ([]string, error) {
|
||||
// read encrypted file
|
||||
encBytes, err := os.ReadFile(realPath)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to open \"" + realPath + "\" for decryption: " + err.Error())
|
||||
}
|
||||
|
||||
// if no password was provided, assume daemon mode
|
||||
if rcwPassword == nil {
|
||||
rcwPassword = launchRCWDProcess()
|
||||
// if rcwPassword is still nil, the daemon is already running;
|
||||
// use it to encrypt the data
|
||||
if rcwPassword == nil {
|
||||
return strings.Split(string(daemon.GetDec(encBytes)), "\n"), nil
|
||||
}
|
||||
}
|
||||
|
||||
// if the daemon is not being used/was not already running,
|
||||
// use wrappers.Decrypt directly to avoid waiting for socket file creation
|
||||
decBytes, err := wrappers.Decrypt(encBytes, rcwPassword, true)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to decrypt \"" + realPath + "\": " + err.Error())
|
||||
}
|
||||
return strings.Split(string(decBytes), "\n"), nil
|
||||
}
|
||||
|
||||
// EncryptBytes encrypts a byte slice using RCW and returns the encrypted data.
|
||||
// Leave rcwPassword nil to use RCW demonization.
|
||||
// If providing rcwPassword, it is up to the client to perform the sanity check!
|
||||
func EncryptBytes(decBytes, rcwPassword []byte) []byte {
|
||||
// if no rcwPassword was provided, assume daemon mode
|
||||
if rcwPassword == nil {
|
||||
rcwPassword = launchRCWDProcess()
|
||||
// if rcwPassword is still nil, the daemon is already running;
|
||||
// use it to encrypt the data
|
||||
if rcwPassword == nil {
|
||||
return daemon.GetEnc(decBytes, true)
|
||||
}
|
||||
}
|
||||
|
||||
// if the daemon is not being used/was not already running,
|
||||
// use wrappers.Encrypt directly to avoid waiting for socket file creation
|
||||
return wrappers.Encrypt(decBytes, rcwPassword, true, true)
|
||||
}
|
||||
|
||||
// launchRCWDProcess launches an RCW daemon to cache a password.
|
||||
// If the daemon is not already running OR if not running in daemonize mode,
|
||||
// it collects and returns the password (otherwise returns nil).
|
||||
func launchRCWDProcess() []byte {
|
||||
if daemon.IsOpen() {
|
||||
return nil
|
||||
}
|
||||
var password []byte
|
||||
if RetryPassword {
|
||||
for {
|
||||
password = global.GetPassword("RCW Password:")
|
||||
if err := wrappers.RunSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password); err == nil {
|
||||
break
|
||||
}
|
||||
fmt.Println(back.AnsiError + "Incorrect password" + back.AnsiReset)
|
||||
}
|
||||
} else {
|
||||
// in this mode, it is up to the client to perform the sanity check
|
||||
password = global.GetPassword("RCW Password:")
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], "startrcwd")
|
||||
cmd.SysProcAttr = global.GetSysProcAttr()
|
||||
_ = back.WriteToStdin(cmd, password, false)
|
||||
_ = cmd.Start()
|
||||
|
||||
return password
|
||||
}
|
||||
@@ -1,3 +1,46 @@
|
||||
**libmutton v0.2.4**
|
||||
December 13, 2024
|
||||
|
||||
## IMPORTANT
|
||||
- This release includes a dependency bump to address a high-severity CVE (CVE-2024-45337) - Please update as soon as possible!
|
||||
|
||||
## Changes
|
||||
- (91fdeb53d5b58fe0a554ef29c1384a50b1a9ec33) The MIME type of data copied to the clipboard on UNIX-like systems (X11 and Wayland) is now statically set to "text/plain" to avoid potential issues with type inference
|
||||
|
||||
## Dependencies
|
||||
- Bumps (direct and indirect)
|
||||
- Go: v1.23.2 => v1.23.4
|
||||
- github.com/pkg/sftp: v1.13.6 => v1.13.7
|
||||
- golang.org/x/crypto: v0.28.0 => v0.31.0
|
||||
- Addresses CVE-2024-45337
|
||||
- golang.org/x/sys: v0.26.0 => v0.28.0
|
||||
- golang.org/x/term: v0.25.0 => v0.27.0
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.2.3**
|
||||
October 19, 2024
|
||||
|
||||
## Fixes
|
||||
- (1fb283484469192fc7c954f14d6f2be5234d7c6b) Fixed renaming/moving an entry/folder to an invalid path still having clients request the operation on the server
|
||||
|
||||
## Dependencies
|
||||
- Bumps (direct and indirect)
|
||||
- Go: v1.22.6 => v1.23.2
|
||||
- golang.org/x/crypto: v0.26.0 => v0.28.0
|
||||
- golang.org/x/sys: v0.24.0 => v0.26.0
|
||||
- golang.org/x/term: v0.23.0 => v0.25.0
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.2.2**
|
||||
August 20, 2024
|
||||
|
||||
## Fixes
|
||||
- (de508d17f2b8bebfe895df8c81a87aa058bb5313) Fixed a regression from v0.2.1 where edited entries are improperly synced if their line count is reduced
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.2.1**
|
||||
August 13, 2024
|
||||
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
**libmutton v0.4.4**
|
||||
November 23, 2025
|
||||
|
||||
This release fixes a minor bug and dependency-derived security issues.
|
||||
|
||||
## Fixes
|
||||
- (711515f3a1e368c28abb63c25b6a79a309844645) Fix subprocesses being killed prematurely when the main process is force quit
|
||||
- Fixes issues with master password caching and clipboard clearing
|
||||
|
||||
## Dependencies
|
||||
- Bumped (direct/replaced)
|
||||
- golang.org/x/crypto: v0.43.0 => v0.45.0
|
||||
- Addresses CVE-2025-58181 and CVE-2025-47914
|
||||
- github.com/rwinkhart/rcw: v0.2.3 => v0.2.4
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.4.2**
|
||||
November 09, 2025
|
||||
|
||||
This is purely a dependency bump release.
|
||||
|
||||
## Dependencies
|
||||
- Bumped (direct/replaced)
|
||||
- Go: v1.25.3 => v1.25.4
|
||||
- github.com/rwinkhart/rcw: v0.2.2 => v0.2.3
|
||||
- github.com/rwinkhart/peercred-mini: v0.1.1 => v0.1.2
|
||||
- github.com/rwinkhart/sys: v0.37.0 => v0.38.0
|
||||
- github.com/rwinkhart/go-winio-easy-pipe-handles: switched to tagged v0.1.0 of `github.com/rwinkhart/go-winio`
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.4.1**
|
||||
October 26, 2025
|
||||
|
||||
This is a dependency bump release with only minor changes for developers.
|
||||
|
||||
## Breaking (for developers)
|
||||
- (b9175d7ff3bea3bc703c20468defebcb1ac7e412) Standardized on "password" rather than a mix of "passphrase"/"password"
|
||||
- Some function names changed as a result
|
||||
- (aa10016f77b0be66ab75279df967f5d080d2b826) Developers using `LibmuttonInit` can now force clients into offline mode programmatically
|
||||
- This means `LibmuttonInit` now requires an extra parameter
|
||||
|
||||
## Dependencies
|
||||
- Bumped (direct/replaced)
|
||||
- Go: v1.24.6 => v1.25.3
|
||||
- github.com/pkg/sftp: v1.13.9 => v1.13.10
|
||||
- golang.org/x/crypto: v0.41.0 => v0.43.0
|
||||
- github.com/rwinkhart/sys: v0.35.0 => v0.37.0
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.4.0**
|
||||
August 17, 2025
|
||||
|
||||
This is the largest update to libmutton yet, and as such, these patch notes are non-exhaustive.
|
||||
Many minor changes and fixes to features mentioned in these patch notes have been made.
|
||||
Please see the commit history for a more complete list of changes.
|
||||
|
||||
## Breaking (for users)
|
||||
- GPG has been entirely replaced with [RCW](https://github.com/rwinkhart/rcw)
|
||||
- All entries created in previous versions must be converted using [this conversion program](https://github.com/rwinkhart/sshyp-labs/releases/tag/v2.0.0)
|
||||
- Steam TOTP keys now must be in base32 format
|
||||
- (3c5db78da9861e09112ac308c086c2d19f2abfeb) Release binaries of `libmuttonserver` now target x86_64_v2 and arm64v8.7
|
||||
- (3dd07e7165874e323586c21a14853935bb3dc61c) Offline mode is now manually specified in libmutton.ini
|
||||
|
||||
## Breaking (for developers)
|
||||
- libmutton has been further modularized
|
||||
- The `core` package has been split into `core`, `global`, `crypt`, and `cfg`
|
||||
- The `sync` package has been split into `syncclient`, `syncserver`, `synccommon`, and `synccycles`
|
||||
- This decreases the size of the server binary and allows for more modular clients
|
||||
- (40f0f35f452008997bb5ecfb72f496646c42ba06) Errors are now returned, rather than printed
|
||||
|
||||
## Features
|
||||
- (111324cc25e84a5bd8b6b8206b90b00394311590) Deletions (server-side) now follow the client through device ID changes
|
||||
- (ca277ba7735dd75cc94b26c1c2357f7d3192e26b) Added the `EntryRefresh()` function for re-encrypting entries with a new passphrase/optimizing bloated entries
|
||||
- (74b8261bc88fc122a25df5207b34edc3dee81e9a) Added the `LibmuttonInit()` function to ensure clients initialize libmutton as intended
|
||||
- (bed23e987d89b133204991c2c34b8fa1e8e335c9) (41a12c6e4a7c09b22fac0faf6bdef5993264f5de) Added iOS clipboard support
|
||||
- (3fdcc9e96d71bb4bef3baf9f43a627782237b142) (1a66a30bc184528ec971306497cca3597072202a) (d023058630958842f0f521ba2bef43b12955b9d8) Support one-time TOTP copy
|
||||
- (fb2120c94ebc69f7ac8e15163f1e27b2c8057d9a) Support custom device ID prefixes
|
||||
|
||||
## Fixes
|
||||
- (50ab27c6749d13fc6842d92259f8e0fd8fa5340c) libmutton.ini is now always created with 0600 permissions on *nix platforms
|
||||
- (4a0e0f8e9e96805b27232001bce81336ac2e6a4d) Old devices IDs are no longer removed from clients when registration of a new device ID fails
|
||||
|
||||
## Dependencies
|
||||
- Dropped
|
||||
- github.com/fortis/go-steam-totp (functionality now covered by github.com/pquerna/otp)
|
||||
- Bumped (direct)
|
||||
- Go: v1.24.2 => v1.24.6
|
||||
- github.com/pquerna/otp: v1.4.1-0.20231130234153-3357de7c0481 => v1.5.0
|
||||
- golang.design/x/clipboard: v0.7.0 => v0.7.1
|
||||
- golang.org/x/crypto: v0.37.0 => v0.41.0
|
||||
- Added (direct)
|
||||
- github.com/rwinkhart/go-boilerplate v0.1.0
|
||||
- github.com/rwinkhart/rcw v0.2.2
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.3.1**
|
||||
April 21, 2025
|
||||
|
||||
## Features
|
||||
- (da95ede80704e667f75372e2142de4cef1dd42da) Added `EntryAddPrecheck()` utility function for ensuring the target locations for new entries are valid
|
||||
- (17223758b11eae301106f398fe2a8815a04f2d62) (44924737ab0830ad49e90c1d1930b05bba307284) (0f90e8251413af2be73f56d071c14af6c00cdb79) Added initial native Android support
|
||||
- (51bb67f3150e70c2c8e8dc3cc72de5666d7c058a) Split device ID retrieval into dedicated exported function, `GetCurrentDeviceID`
|
||||
- (4ed816cb112686d995b1cb7ae8f5df990117ae4d) Allow more control over string generation to improve generated password compatibility
|
||||
|
||||
## Fixes
|
||||
- (51bb67f3150e70c2c8e8dc3cc72de5666d7c058a) Fixed clients having multiple device IDs after failed registrations
|
||||
- (451d695649c659a510a117254a9311fe621082b1) Fixed inability to add a folder on the server if the folder was already created on a client that failed to sync
|
||||
|
||||
## Dependencies
|
||||
- Bumps (direct and indirect)
|
||||
- Go: v1.24.0 => v1.24.2
|
||||
- github.com/pkg/sftp: v1.13.7 => v1.13.9
|
||||
- golang.org/x/crypto: v0.34.0 => v0.37.0
|
||||
- golang.org/x/sys: v0.30.0 => v0.32.0
|
||||
- New
|
||||
- Android builds only
|
||||
- golang.design/x/clipboard v0.7.0
|
||||
- golang.org/x/exp/shiny v0.0.0-20250408133849-7e4ce0ab07d0
|
||||
- golang.org/x/image v0.26.0
|
||||
- golang.org/x/mobile v0.0.0-20250408133729-978277e7eaf7
|
||||
|
||||
---
|
||||
|
||||
**libmutton v0.3.0**
|
||||
February 22, 2025
|
||||
|
||||
## Features
|
||||
- (f9aa2fc374b77fc8325d0c7644ffc9c96169c3c6) (d2034b458b702b8f1664bc188c1840040ec0f704) (81b78068a75a23c73be607cbe6d4dc8b1539f831) (e9d0c3704362c8963e63a6c3b70d036f3e6c2493) `core.LaunchClipClearProcess`, `core.WriteToStdin`, `core.ExpandPathWithHome`, and `core.PrintError` are now exported utility functions for direct use by clients
|
||||
- (c28d45c9da94ec45d89dbda6d645042a7fdbcd29) One-off sync functions can now be forced to run in offline mode
|
||||
- (5028fb21b019e8a867d031463f5de1402fc053f4) SSH connection attempts now have a 3-second timeout
|
||||
- (89b74cec1e014dba31e2b728e3544941d6edd2c2) Individual keys can now be removed from the config file
|
||||
- Many features for building interactive (GUI/TUI) clients
|
||||
- (619e4220a60fc0cb284f5640eaf9af86c0651ac7) Clipboard clearing can now be handled much more sensibly by interactive clients
|
||||
- (3b57d59ddcf0694b2bbae8c86ec06faff299e950) (55be8a30759ee5d12628c631af94a57d582281a4) The clipboard can now be cleared instantly (no timer); useful for clearing on client exit
|
||||
- (2feeeb74d7813dbbb75795d5ccdd817bbcea3602) `core.ParseConfig` now returns errors for proper handling in interactive clients
|
||||
- (68e3108741fae0aaf316dcf33a5074b0e19a7b4f) `sync.RunJob` can now return lists of synchronized entries for display in interactive clients
|
||||
|
||||
## Fixes
|
||||
- (eb2b349697ede136ea031cf7d82bfb72a5a0dcf9) Deletions are now synchronized before folders to avoid sync failures under unlikely conditions
|
||||
- (fc1cd349b8c5468afa3feaf7b5b9042eb4c467e7) Double-space line breaks with Markdown formatting are now preserved when saving an entry
|
||||
- (4689cae3f4a1e19486935f2da5b06bf0a90c731c) Sheared folder names now always end in a trailing slash for clarity and reduced duplicate entries in the deletions directory
|
||||
- Many fixes for building interactive (GUI/TUI) clients
|
||||
- (1cebb395460fb6068ebf4253ec8bd93e9d2b2a96) The soft exit function now specifies a return value
|
||||
- (a4a39a3a995d1560514dc3b782abb26329214c3f) Passphrase-protected SSH identity files are no longer broken for interactive clients
|
||||
- (1c650d475145c3db4e370576d89eb77abe6e5e52) (0162b737c84d42e168803e0c074103b5c0676384) Interactive clients will no longer crash on SSH dialing failures
|
||||
- (173574eb85b3172414d104a2bf391a3bce4ddc95) Error messages not making sense for interactive clients have been addressed
|
||||
|
||||
## Optimizations
|
||||
- (f2f9c523f441b49d682795c01454167d68aae573) An unnecessary variable declaration was removed in `core.DecryptGPG`
|
||||
- (fb5449cf1f75aa0f93c0484d590abca04f977d7a) A redundant (and late) check for the pre-existence of a new entry has been removed
|
||||
- (e9d0c3704362c8963e63a6c3b70d036f3e6c2493) `core.PrintError` can be used to decrease the overall size of client binaries through code re-use
|
||||
|
||||
## Dependencies
|
||||
- Bumps (direct and indirect)
|
||||
- Go: v1.23.4 => v1.24.0
|
||||
- golang.org/x/crypto: v0.31.0 => v0.34.0
|
||||
- golang.org/x/sys: v0.28.0 => v0.30.0
|
||||
@@ -0,0 +1,52 @@
|
||||
**libmutton v0.5.0**
|
||||
February 17, 2026
|
||||
|
||||
This release adds password age-tracking support and features large refactors that improve the robustness of core functionality. Many breaking changes are present. Highlights are listed below.
|
||||
|
||||
## Breaking (for users+developers)
|
||||
- **USERS**: These changes *require* updating your copy of `libmuttonserver`; additionally, you *must* re-generate your config file however your client allows (e.g. `mutn init`)
|
||||
- (50d51f9d96cca194bc89206fdd56de4db7a696a5) Password age tracking support
|
||||
- libmutton now tracks when passwords were last updated
|
||||
- Clients can use this information to warn users when passwords should be changed
|
||||
- Random (within the last year) age data can be added to existing password entries
|
||||
- This allows easing into this feature, rather than having all your passwords expire on the same day
|
||||
- Developers: see the new `age` package
|
||||
- (e355c810da06d794fd5a6ed10b8a886afa7f3492) (f16078fe01ec8a14fd7f2cd059340b33908ca1d6) (4e6c18db8a6716dcf1a9b73fd523f2f7161ce7f2) (2ab777fd571bb9b79e7601667e5c1f5b15400d03) (941a2dc8b80cb6e0327b33714c4a894962621bda) (35db3bf20b8ab3705bf349fdc7ed4b8e7899cddb) (5e5a7c57e9c5633edb74a968e46f21e141ab81ef) (07571a698d32eb414c24cdf84327d1868a0b455a) (d0e8640ce6e90583a7ea5ff5b3ed2cfdc2e00801) (3b4e75b592241b8405c4cb49dd40859773b7b027) Massive sync refactor
|
||||
- Sync info is now passed from the server to the client using rigidly defined JSON types
|
||||
- This greatly improves code maintainability and reduces potential for bugs
|
||||
- Sync now supports password age tracking data
|
||||
- (b591e2ef5a6da7f8fbee099c25349dd61433b297) (571bc0a3bd8773771c01191d9b7b8f03c7c5b436) New config file format
|
||||
- `libmutton.ini` is now `libmuttoncfg.json`
|
||||
- This format change was made since the JSON package was being adopted for the sync refactor (the `gopkg.in/ini.v1` dependency can now be dropped)
|
||||
- Developers: see the new `config` package, replacing the old `cfg` package
|
||||
- (6d3e3cdb5bcfccd103aadfcbb4789ce001bad946) Dropped built-in Termux support to simplify builds (clients can still implement this on their own)
|
||||
|
||||
## Breaking (for developers)
|
||||
- (bf1399ce8d6e39d04d3cdad2d99ae787c88effdf) (97037903360c70221e138b22630e7ef90ec378bc) (a0f21b31f743eab4cc9d0f879e3b5fc4d7627ad0) (80cf65775ada4ce7c0f1c6c3ab93d5bb561518ad) (8d1dd48035bd3abb6f4b8847c5a39a61be53f3a4) (c63dff409210e438c27a429b9fdb023a7bba66af) (336dc5c158af21214bd097c2a0eadbc44f7c2cf6) (04e1b0dd37d372fb93c168273761a7285f819dc8) (d2a6ed5eaa6600a44912edbebb3b2df0a78320b0) (fdce036be6d42cf8ab97a9fb66f88b243639252f) (6004a94b7718a97bdc847ea99a8a39761c7b0075) Massive clipboard refactor
|
||||
- Added new `clip` package
|
||||
- Non-continuous TOTP copy must use `core.GenTOTP` directly
|
||||
- The typical copy workflow now only supports continuous copy
|
||||
- Dropped mobile clipboard support
|
||||
- This can/should be handled by the GUI toolkit
|
||||
- (28286788a045a090be67b4f2919b09a8be570801) (a18c3ea1d6412dbb88fba5a5f69693450be9e97c) (5e5a7c57e9c5633edb74a968e46f21e141ab81ef) (4467a8c7421bb4b60e5088bca851c1fd7d4fec50) `core.LibmuttonInit` overhaul
|
||||
- Now supports non-destructive append
|
||||
- Now supports custom device ID prefixes
|
||||
|
||||
## New (non-breaking)
|
||||
- (cb3a554676ff8d43f4350f6ce74af786c003bf3b) Mass entry verification
|
||||
- (d7242fee96b32b6b1ba4cec6952b143019b59b78) (1351360b35b37763fdb418d6d600c86a1e03b360) Native iOS support
|
||||
|
||||
## Fixes
|
||||
- (2054f5b3cf2d3136b3704ddf01a6861eaa7e5ebf) Reduced clipboard latency on Windows
|
||||
- (f82b35bc4398030568a38cd806b5e017811f10a1) Fixed background processes dying w/terminal on Windows
|
||||
|
||||
## Dependencies (direct/replaced)
|
||||
- Dropped
|
||||
- golang.design/x/clipboard
|
||||
- gopkg.in/ini.v1
|
||||
- Bumped
|
||||
- Go: v1.25.4 => v1.26.0
|
||||
- github.com/rwinkhart/go-boilerplate: v0.1.0 => v0.3.1
|
||||
- github.com/rwinkhart/rcw: v0.2.4 => v0.3.0
|
||||
- golang.org/x/crypto: v0.45.0 => v0.48.0
|
||||
- github.com/rwinkhart/sys: v0.38.0 => v0.41.0
|
||||
@@ -0,0 +1,19 @@
|
||||
package global
|
||||
|
||||
var (
|
||||
GetPassword func(prompt string) []byte // Clients should set this to a function that fetches hidden input from the user
|
||||
RootLength = len(EntryRoot)
|
||||
)
|
||||
|
||||
const (
|
||||
LibmuttonVersion = "0.5.0" // Untagged releases feature a letter suffix corresponding to the eventual release version, e.g "0.B.0" -> "0.2.0", "0.2.A" -> "0.2.1"
|
||||
|
||||
FSSpace = "\u259d" // ▝ Space/list separator
|
||||
FSPath = "\u259e" // ▞ Path separator
|
||||
FSMisc = "\u259f" // ▟ Misc. field separator (if \u259d is already used)
|
||||
|
||||
ErrorSyncProcess = 104
|
||||
ErrorDecryption = 105
|
||||
ErrorEncryption = 106
|
||||
ErrorClipboard = 107
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
//go:build ios
|
||||
|
||||
package global
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
)
|
||||
|
||||
func init() {
|
||||
back.Home = fruityHome
|
||||
}
|
||||
|
||||
var (
|
||||
fruityHome = filepath.Join(back.Home, "Library", "Application Support", "libmutton-ios") // private, non-iCloud-backed dir
|
||||
EntryRoot = fruityHome + "/entries" // Path to libmutton entry directory
|
||||
CfgDir = fruityHome + "/config" // Path to libmutton configuration directory
|
||||
CfgPath = CfgDir + "/libmuttoncfg.json" // Path to libmutton configuration file
|
||||
AgeDir = CfgDir + "/age" // Path to libmutton password age directory
|
||||
SSHDir = fruityHome + "/ssh" // Path to SSH directory
|
||||
)
|
||||
|
||||
const (
|
||||
PathSeparator = "/" // Platform-specific path separator
|
||||
IsWindows = false // Platform indicator
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
//go:build !windows && !ios && !android
|
||||
|
||||
package global
|
||||
|
||||
import "github.com/rwinkhart/go-boilerplate/back"
|
||||
|
||||
var (
|
||||
EntryRoot = back.Home + "/.local/share/libmutton" // Path to libmutton entry directory
|
||||
CfgDir = back.Home + "/.config/libmutton" // Path to libmutton configuration directory
|
||||
CfgPath = CfgDir + "/libmuttoncfg.json" // Path to libmutton configuration file
|
||||
AgeDir = CfgDir + "/age" // Path to libmutton password age directory
|
||||
SSHDir = back.Home + "/.ssh" // Path to SSH directory
|
||||
)
|
||||
|
||||
const (
|
||||
PathSeparator = "/" // Platform-specific path separator
|
||||
IsWindows = false // Platform indicator
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
//go:build windows
|
||||
|
||||
package global
|
||||
|
||||
import "github.com/rwinkhart/go-boilerplate/back"
|
||||
|
||||
var (
|
||||
EntryRoot = back.Home + "\\AppData\\Local\\libmutton\\entries" // Path to libmutton entry directory
|
||||
CfgDir = back.Home + "\\AppData\\Local\\libmutton\\config" // Path to libmutton configuration directory
|
||||
CfgPath = CfgDir + "\\libmuttoncfg.json" // Path to libmutton configuration file
|
||||
AgeDir = CfgDir + "\\age" // Path to libmutton password age directory
|
||||
SSHDir = back.Home + "\\.ssh" // Path to SSH directory
|
||||
)
|
||||
|
||||
const (
|
||||
PathSeparator = "\\" // Platform-specific path separator
|
||||
IsWindows = true // Platform indicator
|
||||
)
|
||||
@@ -0,0 +1,34 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
// GetCurrentDeviceID returns the current device ID or
|
||||
// nil if there is no device ID (e.g. first run).
|
||||
func GetCurrentDeviceID() (*string, error) {
|
||||
deviceIDList, err := GenDeviceIDList()
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to generate device ID list: " + err.Error())
|
||||
}
|
||||
var deviceID string
|
||||
if len(deviceIDList) > 0 {
|
||||
deviceID = (deviceIDList)[0].Name()
|
||||
} else {
|
||||
return nil, nil // nil device ID indicates to server that no device ID is being replaced
|
||||
}
|
||||
return &deviceID, nil
|
||||
}
|
||||
|
||||
// GenDeviceIDList returns a slice of all registered device IDs.
|
||||
// Requires: errorOnFail (set to true to throw an error if the devices directory cannot be read/does not exist)
|
||||
func GenDeviceIDList() ([]fs.DirEntry, error) {
|
||||
// create a slice of all registered devices
|
||||
deviceIDList, err := os.ReadDir(CfgDir + PathSeparator + "devices")
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to read devices directory: " + err.Error())
|
||||
}
|
||||
return deviceIDList, nil
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package global
|
||||
|
||||
import "strings"
|
||||
|
||||
// GetRealAgePath returns the full path to an age file (given the vanity path)
|
||||
func GetRealAgePath(vanityPath string) string {
|
||||
return AgeDir + PathSeparator + strings.ReplaceAll(vanityPath, "/", FSPath)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !windows
|
||||
|
||||
package global
|
||||
|
||||
// GetRealPath returns the full path to an entry (given the vanity path).
|
||||
func GetRealPath(vanityPath string) string {
|
||||
return EntryRoot + vanityPath
|
||||
}
|
||||
|
||||
// GetVanityPath returns a vanityPath given a realPath
|
||||
func GetVanityPath(realPath string) string {
|
||||
return realPath[RootLength:]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//go:build windows
|
||||
|
||||
package global
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetRealPath returns the full path to an entry (given the vanity path).
|
||||
func GetRealPath(vanityPath string) string {
|
||||
return EntryRoot + strings.ReplaceAll(vanityPath, "/", PathSeparator)
|
||||
}
|
||||
|
||||
// GetVanityPath returns a vanityPath given a realPath
|
||||
func GetVanityPath(realPath string) string {
|
||||
return strings.ReplaceAll(realPath[RootLength:], "\\", "/")
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
)
|
||||
|
||||
// DirInit creates the libmutton directories.
|
||||
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID).
|
||||
func DirInit(preserveOldCfgDir bool) (*string, error) {
|
||||
var err error
|
||||
|
||||
// create EntryRoot
|
||||
if err = os.MkdirAll(EntryRoot, 0700); err != nil {
|
||||
return nil, errors.New("unable to create \"" + EntryRoot + "\": " + err.Error())
|
||||
}
|
||||
|
||||
// get old device ID before its potential removal
|
||||
oldDeviceID, _ := GetCurrentDeviceID() // error ignored; oldDeviceID is set to nil on error, which is the correct assumption
|
||||
|
||||
// remove existing config directory (if it exists and not in append mode)
|
||||
if !preserveOldCfgDir {
|
||||
isAccessible, _ := back.TargetIsFile(CfgDir, false) // error is ignored because dir/file status is irrelevant
|
||||
if isAccessible {
|
||||
if err = os.RemoveAll(CfgDir); err != nil {
|
||||
return nil, errors.New("unable to remove existing config directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create config directory w/devices subdirectory
|
||||
if err = os.MkdirAll(CfgDir+PathSeparator+"devices", 0700); err != nil {
|
||||
return nil, errors.New("unable to create \"" + CfgDir + "\": " + err.Error())
|
||||
}
|
||||
|
||||
// create password age directory
|
||||
if err = os.MkdirAll(AgeDir, 0700); err != nil {
|
||||
return nil, errors.New("unable to create \"" + AgeDir + "\": " + err.Error())
|
||||
}
|
||||
|
||||
return oldDeviceID, nil
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !windows
|
||||
|
||||
package global
|
||||
|
||||
import "syscall"
|
||||
|
||||
func GetSysProcAttr() *syscall.SysProcAttr {
|
||||
return &syscall.SysProcAttr{Setsid: true}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build windows
|
||||
|
||||
package global
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func GetSysProcAttr() *syscall.SysProcAttr {
|
||||
return &syscall.SysProcAttr{CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP | windows.DETACHED_PROCESS}
|
||||
}
|
||||
@@ -1,18 +1,23 @@
|
||||
module github.com/rwinkhart/libmutton
|
||||
|
||||
go 1.22.6
|
||||
go 1.26.3
|
||||
|
||||
require (
|
||||
github.com/fortis/go-steam-totp v0.0.0-20171114202746-18e928674727
|
||||
github.com/pkg/sftp v1.13.6
|
||||
github.com/pquerna/otp v1.4.1-0.20231130234153-3357de7c0481
|
||||
golang.org/x/crypto v0.26.0
|
||||
golang.org/x/term v0.23.0
|
||||
gopkg.in/ini.v1 v1.67.0
|
||||
github.com/pkg/sftp v1.13.10
|
||||
github.com/pquerna/otp v1.5.0
|
||||
github.com/rwinkhart/go-boilerplate v0.3.1
|
||||
github.com/rwinkhart/rcw v0.3.1
|
||||
golang.org/x/crypto v0.52.0
|
||||
golang.org/x/sys v0.45.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/boombuler/barcode v1.0.2 // indirect
|
||||
github.com/Microsoft/go-winio v0.6.2 // indirect
|
||||
github.com/boombuler/barcode v1.1.0 // indirect
|
||||
github.com/kr/fs v0.1.0 // indirect
|
||||
golang.org/x/sys v0.24.0 // indirect
|
||||
github.com/rwinkhart/peercred-mini v0.1.5 // indirect
|
||||
)
|
||||
|
||||
replace golang.org/x/sys => github.com/rwinkhart/sys v0.45.0
|
||||
|
||||
replace github.com/Microsoft/go-winio => github.com/rwinkhart/go-winio v0.1.1
|
||||
|
||||
@@ -1,62 +1,34 @@
|
||||
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
||||
github.com/boombuler/barcode v1.0.2 h1:79yrbttoZrLGkL/oOI8hBrUKucwOL0oOjUgEguGMcJ4=
|
||||
github.com/boombuler/barcode v1.0.2/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
||||
github.com/boombuler/barcode v1.1.0 h1:ChaYjBR63fr4LFyGn8E8nt7dBSt3MiU3zMOZqFvVkHo=
|
||||
github.com/boombuler/barcode v1.1.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fortis/go-steam-totp v0.0.0-20171114202746-18e928674727 h1:1RkPJqfzrncAuh9xgoslr9OplZskm+VRA9lkucHPQZ4=
|
||||
github.com/fortis/go-steam-totp v0.0.0-20171114202746-18e928674727/go.mod h1:wRAWHbTlpt0C4kwnKoa42L2Phrv6uIq+c50P2uKpb7I=
|
||||
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
|
||||
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
||||
github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
|
||||
github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk=
|
||||
github.com/pkg/sftp v1.13.10 h1:+5FbKNTe5Z9aspU88DPIKJ9z2KZoaGCu6Sr6kKR/5mU=
|
||||
github.com/pkg/sftp v1.13.10/go.mod h1:bJ1a7uDhrX/4OII+agvy28lzRvQrmIQuaHrcI1HbeGA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pquerna/otp v1.4.1-0.20231130234153-3357de7c0481 h1:FkxbO331O7mS5EJkP+MCi0o2gswh/Aezs+//NmefrR8=
|
||||
github.com/pquerna/otp v1.4.1-0.20231130234153-3357de7c0481/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
|
||||
github.com/pquerna/otp v1.5.0 h1:NMMR+WrmaqXU4EzdGJEE1aUUI0AMRzsp96fFFWNPwxs=
|
||||
github.com/pquerna/otp v1.5.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
|
||||
github.com/rwinkhart/go-boilerplate v0.3.1 h1:vkVRuptO2s1yPzzwpvtiiB0c/hPB6yr0p1mzZ2Myb9E=
|
||||
github.com/rwinkhart/go-boilerplate v0.3.1/go.mod h1:ES13A2r9fnCVfyezwMBgY/RgA4pOIudOUXz3Jk/ikes=
|
||||
github.com/rwinkhart/go-winio v0.1.1 h1:kAJKiqneR7cUR01Wn5/doAAV4kOGTEGPug4oinXc5N4=
|
||||
github.com/rwinkhart/go-winio v0.1.1/go.mod h1:ZWa7ssZJT30CCDGJ7fk/2SBTq9BIQrrVjrcss0UW2s0=
|
||||
github.com/rwinkhart/peercred-mini v0.1.5 h1:9sBR0ascteCJfzfhv7Gq/juqu7OOU+AphiEIG1+LcWA=
|
||||
github.com/rwinkhart/peercred-mini v0.1.5/go.mod h1:C73IeDteQwsKp1+PDdUBj4FC6dTziAJNbQsVDZSWE+M=
|
||||
github.com/rwinkhart/rcw v0.3.1 h1:qmozfcoEm71ttA8YSoH96y42Z2sFhCvY45GGtFPWVhg=
|
||||
github.com/rwinkhart/rcw v0.3.1/go.mod h1:TreA2OUl64v2UprL4xpXdDM7w8lJNzQ544rUIl8omtI=
|
||||
github.com/rwinkhart/sys v0.45.0 h1:HXZL0SuyToqKkvakaUuRAdH8sPUHelfO6q4PQRpL26Q=
|
||||
github.com/rwinkhart/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
|
||||
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
|
||||
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=
|
||||
golang.org/x/crypto v0.52.0/go.mod h1:1QgfPxDqh0T2M/elOJtp9RvuR95kVjir0e6/BvEmGbc=
|
||||
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
|
||||
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+104
-30
@@ -2,17 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
"github.com/rwinkhart/libmutton/sync"
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/go-boilerplate/other"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/libmutton/synccommon"
|
||||
"github.com/rwinkhart/libmutton/syncserver"
|
||||
)
|
||||
|
||||
const ansiBold = "\033[1m"
|
||||
|
||||
func main() {
|
||||
args := os.Args
|
||||
if len(args) < 2 {
|
||||
@@ -32,42 +34,116 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// allow reporting panic details to clients
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Printf(
|
||||
"{\"errMsg\":\"SERVER-SIDE PANIC OCCURRED: %v%sARGS: %v%sSTDIN: %v%sSTACK TRACE: %s\"}",
|
||||
r,
|
||||
global.FSSpace+global.FSSpace,
|
||||
global.FSSpace+strings.Join(os.Args, global.FSSpace),
|
||||
global.FSSpace+global.FSSpace,
|
||||
global.FSSpace+strings.Join(stdin, global.FSSpace),
|
||||
global.FSSpace+global.FSSpace,
|
||||
global.FSSpace+strings.ReplaceAll(strings.ReplaceAll(string(debug.Stack()), "\n", global.FSSpace), "\t", ""),
|
||||
)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
switch args[1] {
|
||||
case "fetch":
|
||||
// print all information needed for syncing to stdout for interpretation by the client
|
||||
// stdin[0] is expected to be the device ID
|
||||
sync.GetRemoteDataFromServer(stdin[0])
|
||||
syncserver.GetRemoteDataFromServer(stdin[0])
|
||||
case "rename":
|
||||
// move an entry to a new location before using fallthrough to add its previous iteration to the deletions directory
|
||||
// stdin[0] is evaluated after fallthrough
|
||||
// stdin[1] is expected to be the OLD incomplete target location with FSPath representing path separators - Always pass in UNIX format
|
||||
// stdin[2] is expected to be the NEW incomplete target location with FSPath representing path separators - Always pass in UNIX format
|
||||
sync.RenameLocal(strings.ReplaceAll(stdin[1], core.FSPath, "/"), strings.ReplaceAll(stdin[2], core.FSPath, "/"), true)
|
||||
// stdin[1] is expected to be the OLD vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
// stdin[2] is expected to be the NEW vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
if err := synccommon.RenameLocal(strings.ReplaceAll(stdin[1], global.FSPath, "/"), strings.ReplaceAll(stdin[2], global.FSPath, "/")); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
fallthrough // fallthrough to add the old entry to the deletions directory
|
||||
case "shear":
|
||||
// shear an entry from the server and add it to the deletions directory
|
||||
// stdin[0] is expected to be the device ID
|
||||
// stdin[1] is expected to be the incomplete target location with FSPath representing path separators - Always pass in UNIX format
|
||||
sync.ShearLocal(strings.ReplaceAll(stdin[1], core.FSPath, "/"), stdin[0])
|
||||
// stdin[1] is expected to be the vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
_, _, err := synccommon.ShearLocal(strings.ReplaceAll(stdin[1], global.FSPath, "/"), stdin[0], false)
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
case "shear-age":
|
||||
// shear ONLY the age file associated with an entry from the server and add it to the deletions directory
|
||||
// stdin[0] is expected to be the device ID
|
||||
// stdin[1] is expected to be the vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
_, _, err := synccommon.ShearLocal(strings.ReplaceAll(stdin[1], global.FSPath, "/"), stdin[0], true)
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
case "addfolder":
|
||||
// add a new folder to the server
|
||||
// stdin[0] is expected to be the incomplete target location with FSPath representing path separators - Always pass in UNIX format
|
||||
sync.AddFolderLocal(strings.ReplaceAll(stdin[0], core.FSPath, "/"))
|
||||
// stdin[0] is expected to be the vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
if err := synccommon.AddFolderLocal(strings.ReplaceAll(stdin[0], global.FSPath, "/")); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
case "register":
|
||||
// register a new device ID
|
||||
// stdin[0] is expected to be the device ID
|
||||
// stdin[1] is expected to be the old device ID (for removal)
|
||||
fileToClose, _ := os.OpenFile(core.ConfigDir+core.PathSeparator+"devices"+core.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600) // errors ignored; failure unlikely to occur if init was successful; "register" is not a user-facing argument and thus the error would not be visible
|
||||
_ = fileToClose.Close()
|
||||
if stdin[1] != core.FSMisc { // sync.FSMisc is used to indicate that no device ID is being replaced
|
||||
_ = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + stdin[1])
|
||||
// stdin[0] is expected to be JSON matching type synccommon.RegisterReqT
|
||||
var registerReq synccommon.RegisterReqT
|
||||
err := json.Unmarshal([]byte(stdin[0]), ®isterReq)
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
// print EntryRoot and bool indicating OS type to stdout for client to store in config
|
||||
fmt.Print(core.EntryRoot + core.FSSpace + strconv.FormatBool(core.IsWindows))
|
||||
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"devices"+global.PathSeparator+registerReq.NewDeviceID, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
_ = f.Close()
|
||||
if registerReq.OldDeviceID != nil { // nil is used to indicate that no device ID is being replaced
|
||||
// remove the old device ID file
|
||||
if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + *registerReq.OldDeviceID); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
// carry over deletions from the old device ID to the new one
|
||||
deletionsDirRoot := global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator
|
||||
deletionsList, err := os.ReadDir(deletionsDirRoot)
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
for i := range deletionsList {
|
||||
affectedIDVanityPath := strings.Split(deletionsList[i].Name(), global.FSSpace)
|
||||
if affectedIDVanityPath[0] == *registerReq.OldDeviceID {
|
||||
if err = os.Rename(deletionsDirRoot+deletionsList[i].Name(), deletionsDirRoot+registerReq.NewDeviceID+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2]); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print EntryRoot, AgeDir and bool indicating OS type to stdout for client to store in config
|
||||
registerRespBytes, err := json.Marshal(synccommon.RegisterRespT{EntryRoot: global.EntryRoot, AgeDir: global.AgeDir, IsWindows: global.IsWindows})
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Print(string(registerRespBytes))
|
||||
case "init":
|
||||
// create the necessary directories for libmuttonserver to function
|
||||
core.DirInit(false)
|
||||
_ = os.MkdirAll(core.ConfigDir+core.PathSeparator+"deletions", 0700) // error ignored; failure would have occurred by this point in core.DirInit
|
||||
_, err := global.DirInit(false)
|
||||
if err != nil {
|
||||
other.PrintError("Failed to initialize libmuttonserver directories: "+err.Error(), back.ErrorWrite)
|
||||
}
|
||||
_ = os.MkdirAll(global.CfgDir+global.PathSeparator+"deletions", 0700) // error ignored; failure would have occurred by this point in core.DirInit
|
||||
fmt.Println("libmuttonserver directories initialized")
|
||||
case "version":
|
||||
versionServer()
|
||||
@@ -77,13 +153,13 @@ func main() {
|
||||
}
|
||||
|
||||
func helpServer() {
|
||||
fmt.Print(ansiBold + "\nlibmuttonserver | Copyright (c) 2024 Randall Winkhart\n" + core.AnsiReset + `
|
||||
fmt.Print(back.AnsiBold + "\nlibmuttonserver | Copyright (c) 2024-2026 Randall Winkhart\n" + back.AnsiReset + `
|
||||
This software exists under the MIT license; you may redistribute it under certain conditions.
|
||||
This program comes with absolutely no warranty; type "libmuttonserver version" for details.
|
||||
|
||||
` + ansiBold + "Usage:" + core.AnsiReset + ` libmuttonserver <argument>
|
||||
` + back.AnsiBold + "Usage:" + back.AnsiReset + ` libmuttonserver <argument>
|
||||
|
||||
` + ansiBold + "Arguments (user):" + core.AnsiReset + `
|
||||
` + back.AnsiBold + "Arguments (user):" + back.AnsiReset + `
|
||||
help Bring up this menu
|
||||
version Display version and license information
|
||||
init Create the necessary directories for libmuttonserver to function` + "\n\n")
|
||||
@@ -91,7 +167,7 @@ This program comes with absolutely no warranty; type "libmuttonserver version" f
|
||||
}
|
||||
|
||||
func versionServer() {
|
||||
fmt.Print(ansiBold + "\n MIT License" + core.AnsiReset + `
|
||||
fmt.Print(back.AnsiBold + "\n MIT License" + back.AnsiReset + `
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
@@ -116,7 +192,5 @@ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
||||
OR OTHER DEALINGS IN THE SOFTWARE.` + "\n\n---------------------------------------------------------")
|
||||
fmt.Print(ansiBold + "\n\n libmuttonserver" + core.AnsiReset + " Version " + core.LibmuttonVersion + `
|
||||
|
||||
Copyright (c) 2024 Randall Winkhart` + "\n\n")
|
||||
fmt.Print(back.AnsiBold + "\n\n libmuttonserver" + back.AnsiReset + " Version " + global.LibmuttonVersion + "\n\n Copyright (c) 2024-2026: Randall Winkhart" + "\n\n")
|
||||
}
|
||||
|
||||
+10
-8
@@ -1,13 +1,15 @@
|
||||
#!/bin/sh
|
||||
# This script generates portable libmuttonserver release binaries for the following platforms:
|
||||
# - Linux (x86_64_v1)
|
||||
# - Linux (aarch64)
|
||||
# - Windows (x86_64_v1)
|
||||
# - Windows (aarch64)
|
||||
# - Linux (x86_64_v2)
|
||||
# - Linux (arm64v8.0)
|
||||
# - Linux (arm64v8.7)
|
||||
# - Windows (x86_64_v2)
|
||||
# - Windows (arm64v8.7)
|
||||
|
||||
mkdir -p ./1output
|
||||
cd ..
|
||||
GOOS=linux CGO_ENABLED=0 GOAMD64=v1 go build -o ./packaging/1output/libmuttonserver-linux-x86_64_v1 -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o ./packaging/1output/libmuttonserver-linux-aarch64 -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=windows CGO_ENABLED=0 GOAMD64=v1 go build -o ./packaging/1output/libmuttonserver-windows-x86_64_v1.exe -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build -o ./packaging/1output/libmuttonserver-windows-aarch64.exe -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=linux CGO_ENABLED=0 GOAMD64=v2 go build -o ./packaging/1output/libmuttonserver-linux-x86_64_v2 -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=linux CGO_ENABLED=0 GOARCH=arm64 GOARM64=v8.0 CGO_ENABLED=0 go build -o ./packaging/1output/libmuttonserver-linux-arm64v8.0 -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=linux CGO_ENABLED=0 GOARCH=arm64 GOARM64=v8.7 CGO_ENABLED=0 go build -o ./packaging/1output/libmuttonserver-linux-arm64v8.7 -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=windows CGO_ENABLED=0 GOAMD64=v2 go build -o ./packaging/1output/libmuttonserver-windows-x86_64_v2.exe -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
GOOS=windows CGO_ENABLED=0 GOARCH=arm64 GOARM64=v8.7 CGO_ENABLED=0 go build -o ./packaging/1output/libmuttonserver-windows-arm64v8.7.exe -ldflags="-s -w" -trimpath ./libmuttonserver.go
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package privkey
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
var keyBytesProvider = func(sshKeyPath *string) ([]byte, error) {
|
||||
if sshKeyPath != nil {
|
||||
key, err := os.ReadFile(*sshKeyPath)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to read private key: " + *sshKeyPath)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
return nil, errors.New("unable to identify private key location (nil config)")
|
||||
}
|
||||
|
||||
// GetBytes returns the SSH private key bytes.
|
||||
// On iOS, it calls the provider function that was set by the iOS app
|
||||
// using SetKeyBytesProvider or SetKeyBytes.
|
||||
func GetBytes(sshKeyPath *string) ([]byte, error) {
|
||||
keyBytes, err := keyBytesProvider(sshKeyPath)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to get key bytes from provider: " + err.Error())
|
||||
}
|
||||
if len(keyBytes) == 0 {
|
||||
return nil, errors.New("provider returned empty key bytes")
|
||||
}
|
||||
return keyBytes, nil
|
||||
}
|
||||
|
||||
// SetBytes allows the iOS app to directly set the SSH key bytes.
|
||||
func SetBytes(keyBytes []byte) {
|
||||
keyBytesProvider = func(*string) ([]byte, error) {
|
||||
return keyBytes, nil
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package sync
|
||||
|
||||
import "github.com/rwinkhart/libmutton/core"
|
||||
|
||||
var rootLength = len(core.EntryRoot) // length of core.EntryRoot string
|
||||
-476
@@ -1,476 +0,0 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/sftp"
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
// ANSI color constants used only in this file
|
||||
const (
|
||||
ansiDelete = "\033[38;5;1m"
|
||||
ansiDownload = "\033[38;5;2m"
|
||||
ansiUpload = "\033[38;5;4m"
|
||||
)
|
||||
|
||||
// GetSSHClient returns an SSH client connection to the server (also returns the remote EntryRoot and an indicator of the server's OS).
|
||||
// Only supports key-based authentication (passphrases are supported for CLI-based implementations).
|
||||
func GetSSHClient(manualSync bool) (*ssh.Client, string, bool) {
|
||||
// get SSH config info, exit if not configured (displaying an error if the sync job was called manually)
|
||||
var sshUserConfig []string
|
||||
var missingValueError string
|
||||
if manualSync {
|
||||
missingValueError = joinErrorWithEXE("SSH settings not configured - Run \"", " init\" to configure")
|
||||
} else {
|
||||
missingValueError = "0"
|
||||
}
|
||||
sshUserConfig = core.ParseConfig([][2]string{{"LIBMUTTON", "sshUser"}, {"LIBMUTTON", "sshIP"}, {"LIBMUTTON", "sshPort"}, {"LIBMUTTON", "sshKey"}, {"LIBMUTTON", "sshKeyProtected"}, {"LIBMUTTON", "sshEntryRoot"}, {"LIBMUTTON", "sshIsWindows"}}, missingValueError)
|
||||
|
||||
var user, ip, port, keyFile, keyFileProtected, entryRoot string
|
||||
var isWindows bool
|
||||
var err error
|
||||
for i, key := range sshUserConfig {
|
||||
switch i {
|
||||
case 0:
|
||||
user = key
|
||||
case 1:
|
||||
ip = key
|
||||
case 2:
|
||||
port = key
|
||||
case 3:
|
||||
keyFile = key
|
||||
case 4:
|
||||
keyFileProtected = key
|
||||
case 5:
|
||||
entryRoot = key
|
||||
case 6:
|
||||
isWindows, err = strconv.ParseBool(key)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to parse server OS type:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// read private key
|
||||
key, err := os.ReadFile(keyFile)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to read private key file:", keyFile+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
|
||||
// parse private key
|
||||
var parsedKey ssh.Signer
|
||||
if keyFileProtected != "true" {
|
||||
parsedKey, err = ssh.ParsePrivateKey(key)
|
||||
} else {
|
||||
parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, inputKeyFilePassphrase())
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to parse private key:", keyFile+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
|
||||
// read known hosts file
|
||||
var hostKeyCallback ssh.HostKeyCallback
|
||||
hostKeyCallback, err = knownhosts.New(core.Home + core.PathSeparator + ".ssh" + core.PathSeparator + "known_hosts")
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to read known hosts file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
|
||||
// configure SSH client
|
||||
sshConfig := &ssh.ClientConfig{
|
||||
User: user,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.PublicKeys(parsedKey),
|
||||
},
|
||||
HostKeyCallback: hostKeyCallback,
|
||||
}
|
||||
|
||||
// connect to SSH server
|
||||
sshClient, err := ssh.Dial("tcp", ip+":"+port, sshConfig)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to connect to remote server:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
|
||||
return sshClient, entryRoot, isWindows
|
||||
}
|
||||
|
||||
// GetSSHOutput runs a command over SSH and returns the output as a string.
|
||||
func GetSSHOutput(sshClient *ssh.Client, cmd, stdin string) string {
|
||||
// create a session
|
||||
sshSession, err := sshClient.NewSession()
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to establish SSH session:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
|
||||
// provide stdin data for session
|
||||
sshSession.Stdin = strings.NewReader(stdin)
|
||||
|
||||
// run the provided command
|
||||
var output []byte
|
||||
output, err = sshSession.CombinedOutput(cmd)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to run SSH command:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorSyncProcess)
|
||||
}
|
||||
|
||||
// convert the output to a string and remove leading/trailing whitespace
|
||||
outputString := string(output)
|
||||
outputString = strings.TrimSpace(outputString)
|
||||
|
||||
return outputString
|
||||
}
|
||||
|
||||
// getRemoteDataFromClient returns a map of remote entries to their modification times, a list of remote folders, a list of queued deletions, and the current server&client times as UNIX timestamps.
|
||||
func getRemoteDataFromClient(sshClient *ssh.Client, manualSync bool) (map[string]int64, []string, []string, int64, int64) {
|
||||
// get remote output over SSH
|
||||
deviceIDList := core.GenDeviceIDList(true)
|
||||
if len(*deviceIDList) == 0 {
|
||||
if manualSync {
|
||||
fmt.Println(joinErrorWithEXE("Sync failed - No device ID found; run \"", " init\" to generate a device ID"))
|
||||
os.Exit(core.ErrorTargetNotFound)
|
||||
} else {
|
||||
core.Exit(0) // exit silently if the sync job was called automatically, as the user may just be in offline mode
|
||||
}
|
||||
}
|
||||
clientTime := time.Now().Unix() // get client time now to avoid accuracy issues caused by unpredictable sync time
|
||||
output := GetSSHOutput(sshClient, "libmuttonserver fetch", (*deviceIDList)[0].Name())
|
||||
|
||||
// split output into slice based on occurrences of FSSpace
|
||||
outputSlice := strings.Split(output, core.FSSpace)
|
||||
|
||||
// parse output/re-form lists
|
||||
if len(outputSlice) != 5 { // ensure information from server is complete
|
||||
fmt.Println(core.AnsiError + "Sync failed - Unable to fetch remote data; server returned an unexpected response" + core.AnsiReset)
|
||||
os.Exit(core.ErrorSyncProcess)
|
||||
}
|
||||
serverTime, err := strconv.ParseInt(outputSlice[0], 10, 64)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to parse server time:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
entries := strings.Split(outputSlice[1], core.FSMisc)[1:]
|
||||
modsStrings := strings.Split(outputSlice[2], core.FSMisc)[1:]
|
||||
folders := strings.Split(outputSlice[3], core.FSMisc)[1:]
|
||||
deletions := strings.Split(outputSlice[4], core.FSMisc)[1:]
|
||||
|
||||
// convert the mod times to int64
|
||||
var mods []int64
|
||||
var mod int64
|
||||
for _, modString := range modsStrings {
|
||||
mod, err = strconv.ParseInt(modString, 10, 64)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to parse mod time:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
mods = append(mods, mod)
|
||||
}
|
||||
|
||||
// map remote entries to their modification times
|
||||
entryModMap := make(map[string]int64)
|
||||
for i, entry := range entries {
|
||||
entryModMap[entry] = mods[i]
|
||||
}
|
||||
|
||||
return entryModMap, folders, deletions, serverTime, clientTime
|
||||
}
|
||||
|
||||
// getLocalData returns a map of local entries to their modification times.
|
||||
func getLocalData() map[string]int64 {
|
||||
// get a list of all entries
|
||||
entries, _ := WalkEntryDir()
|
||||
|
||||
// get a list of all entry modification times
|
||||
modList := getModTimes(entries)
|
||||
|
||||
// map the entries to their modification times
|
||||
entryModMap := make(map[string]int64)
|
||||
for i, entry := range entries {
|
||||
entryModMap[entry] = modList[i]
|
||||
}
|
||||
|
||||
// return the lists
|
||||
return entryModMap
|
||||
}
|
||||
|
||||
// targetLocationFormatSFTP formats the target location to match the remote server's entry directory and path separator.
|
||||
func targetLocationFormatSFTP(targetName, serverEntryRoot string, serverIsWindows bool) string {
|
||||
if !serverIsWindows {
|
||||
return serverEntryRoot + targetName
|
||||
} else {
|
||||
return serverEntryRoot + strings.ReplaceAll(targetName, "/", "\\")
|
||||
}
|
||||
}
|
||||
|
||||
// sftpSync takes two slices of entries (one for downloads and one for uploads) and syncs them between the client and server using SFTP.
|
||||
func sftpSync(sshClient *ssh.Client, sshEntryRoot string, sshIsWindows bool, downloadList, uploadList []string) {
|
||||
// create an SFTP client from sshClient
|
||||
sftpClient, err := sftp.NewClient(sshClient)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to establish SFTP session:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
defer func(sftpClient *sftp.Client) {
|
||||
err = sftpClient.Close()
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to close SFTP client:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
}(sftpClient)
|
||||
|
||||
// iterate over the download list
|
||||
var filesTransferred bool
|
||||
for _, entryName := range downloadList {
|
||||
filesTransferred = true // set a flag to indicate that files have been downloaded (used to determine whether to print a gap between download and upload messages)
|
||||
|
||||
fmt.Println("Downloading " + ansiDownload + entryName + core.AnsiReset)
|
||||
|
||||
// store path to remote entry
|
||||
remoteEntryFullPath := targetLocationFormatSFTP(entryName, sshEntryRoot, sshIsWindows)
|
||||
|
||||
// save modification time of remote file
|
||||
var fileInfo os.FileInfo
|
||||
fileInfo, err = sftpClient.Stat(remoteEntryFullPath)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to get remote file info (mod time):", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
modTime := fileInfo.ModTime()
|
||||
|
||||
// open remote file
|
||||
var remoteFile *sftp.File
|
||||
remoteFile, err = sftpClient.Open(remoteEntryFullPath)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to open remote file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
|
||||
// store path to local entry
|
||||
localEntryFullPath := core.TargetLocationFormat(entryName)
|
||||
|
||||
// create local file
|
||||
var localFile *os.File
|
||||
localFile, err = os.OpenFile(localEntryFullPath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to create local file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
|
||||
// download the file
|
||||
_, err = remoteFile.WriteTo(localFile)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to download remote file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorSyncProcess)
|
||||
}
|
||||
|
||||
// close the files
|
||||
_ = remoteFile.Close() // errors ignored; if the files could be opened/created, it can probably be closed
|
||||
_ = localFile.Close()
|
||||
|
||||
// set the modification time of the local file to match the value saved from the remote file (from before the download)
|
||||
err = os.Chtimes(localEntryFullPath, time.Now(), modTime)
|
||||
}
|
||||
|
||||
if filesTransferred {
|
||||
fmt.Println() // add a gap between download and upload messages
|
||||
}
|
||||
|
||||
// iterate over the upload list
|
||||
filesTransferred = false
|
||||
for _, entryName := range uploadList {
|
||||
filesTransferred = true // set a flag to indicate that files have been uploaded (used to determine whether to print a gap between upload and sync complete messages)
|
||||
|
||||
fmt.Println("Uploading " + ansiUpload + entryName + core.AnsiReset)
|
||||
|
||||
// store path to local entry
|
||||
localEntryFullPath := core.TargetLocationFormat(entryName)
|
||||
|
||||
// save modification time of local file
|
||||
var fileInfo os.FileInfo
|
||||
fileInfo, err = os.Stat(localEntryFullPath)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to get local file info (mod time):", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
modTime := fileInfo.ModTime()
|
||||
|
||||
// open local file
|
||||
var localFile *os.File
|
||||
localFile, err = os.Open(localEntryFullPath)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to open local file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
|
||||
// store path to remote entry
|
||||
remoteEntryFullPath := targetLocationFormatSFTP(entryName, sshEntryRoot, sshIsWindows)
|
||||
|
||||
// create remote file
|
||||
var remoteFile *sftp.File
|
||||
remoteFile, err = sftpClient.OpenFile(remoteEntryFullPath, os.O_CREATE|os.O_WRONLY)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to create remote file ("+remoteEntryFullPath+"):", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
|
||||
// upload the file
|
||||
_, err = localFile.WriteTo(remoteFile)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to upload local file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorSyncProcess)
|
||||
}
|
||||
|
||||
// close the files
|
||||
_ = localFile.Close() // errors ignored; if the files could be opened/created, it can probably be closed
|
||||
_ = remoteFile.Close()
|
||||
|
||||
// set permissions on remote file
|
||||
err = sftpClient.Chmod(remoteEntryFullPath, 0600)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to set permissions on remote file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorSyncProcess)
|
||||
}
|
||||
|
||||
// set the modification time of the remote file to match the value saved from the local file (from before the upload)
|
||||
err = sftpClient.Chtimes(remoteEntryFullPath, time.Now(), modTime)
|
||||
}
|
||||
|
||||
if filesTransferred {
|
||||
fmt.Println() // add a gap between upload and sync complete messages
|
||||
}
|
||||
}
|
||||
|
||||
// syncLists determines which entries need to be downloaded and uploaded for synchronizations and calls sftpSync with this information.
|
||||
// Using maps means that syncing will be done in an arbitrary order, but it is a worthy tradeoff for speed and simplicity.
|
||||
func syncLists(sshClient *ssh.Client, sshEntryRoot string, sshIsWindows, timeSynced bool, localEntryModMap, remoteEntryModMap map[string]int64) {
|
||||
// initialize slices to store entries that need to be downloaded or uploaded
|
||||
var downloadList, uploadList []string
|
||||
|
||||
// iterate over client entries
|
||||
for entry, localModTime := range localEntryModMap {
|
||||
// check if the entry is present in the server map
|
||||
if remoteModTime, present := remoteEntryModMap[entry]; present {
|
||||
// entry exists on both client and server, compare mod times
|
||||
if remoteModTime > localModTime {
|
||||
fmt.Println(ansiDownload+entry+core.AnsiReset, "is newer on server, adding to download list")
|
||||
downloadList = append(downloadList, entry)
|
||||
} else if remoteModTime < localModTime {
|
||||
fmt.Println(ansiUpload+entry+core.AnsiReset, "is newer on client, adding to upload list")
|
||||
uploadList = append(uploadList, entry)
|
||||
}
|
||||
// remove entry from remoteEntryModMap (process of elimination)
|
||||
delete(remoteEntryModMap, entry)
|
||||
} else {
|
||||
fmt.Println(ansiUpload+entry+core.AnsiReset, "does not exist on server, adding to upload list")
|
||||
uploadList = append(uploadList, entry)
|
||||
}
|
||||
}
|
||||
|
||||
// iterate over remaining entries in remoteEntryModMap
|
||||
for entry := range remoteEntryModMap {
|
||||
fmt.Println(ansiDownload+entry+core.AnsiReset, "does not exist on client, adding to download list")
|
||||
downloadList = append(downloadList, entry)
|
||||
}
|
||||
|
||||
// call sftpSync with the download and upload lists
|
||||
if timeSynced && (max(len(downloadList), len(uploadList)) > 0) { // only call sftpSync if there are entries to download or upload
|
||||
fmt.Println() // add a gap between list-add messages and the actual sync messages from sftpSync
|
||||
sftpSync(sshClient, sshEntryRoot, sshIsWindows, downloadList, uploadList)
|
||||
} else if !timeSynced {
|
||||
// do not call sftpSync if the client and server times are out of sync
|
||||
core.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("Client is synchronized with server")
|
||||
}
|
||||
|
||||
// deletionSync removes entries from the client that have been deleted on the server (multi-client deletion).
|
||||
func deletionSync(deletions []string) {
|
||||
var filesDeleted bool
|
||||
for _, deletion := range deletions {
|
||||
filesDeleted = true // set a flag to indicate that files have been deleted (used to determine whether to print a gap between deletion and other messages)
|
||||
fmt.Println(ansiDelete+deletion+core.AnsiReset, "has been sheared, removing locally (if it exists)")
|
||||
err := os.RemoveAll(core.TargetLocationFormat(deletion))
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Failed to shear "+deletion+" locally:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
}
|
||||
|
||||
if filesDeleted {
|
||||
fmt.Println() // add a gap between deletion and other messages
|
||||
}
|
||||
}
|
||||
|
||||
// folderSync creates folders on the client (from the given list of folder names).
|
||||
func folderSync(folders []string) {
|
||||
for _, folder := range folders {
|
||||
// store the full local path of the folder
|
||||
folderFullPath := core.TargetLocationFormat(folder)
|
||||
|
||||
// check if folder already exists
|
||||
isFile, isAccessible := core.TargetIsFile(folderFullPath, false, 1)
|
||||
|
||||
if !isFile && !isAccessible {
|
||||
err := os.MkdirAll(folderFullPath, 0700)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Failed to create folder \""+folder+"\":", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
} else if isFile {
|
||||
fmt.Println(core.AnsiError + "Sync failed - Failed to create folder \"" + folder + "\" - A file with the same name already exists" + core.AnsiReset)
|
||||
os.Exit(core.ErrorTargetExists)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunJob runs the SSH sync job.
|
||||
// Setting manualSync to true will throw errors if sync is not configured (online mode is assumed).
|
||||
func RunJob(manualSync bool) {
|
||||
// get SSH client to re-use throughout the sync process
|
||||
sshClient, sshEntryRoot, sshIsWindows := GetSSHClient(manualSync)
|
||||
defer func(sshClient *ssh.Client) {
|
||||
err := sshClient.Close()
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to close SSH client:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
}(sshClient)
|
||||
|
||||
// fetch remote lists
|
||||
remoteEntryModMap, remoteFolders, deletions, serverTime, clientTime := getRemoteDataFromClient(sshClient, manualSync)
|
||||
|
||||
// sync folders
|
||||
folderSync(remoteFolders)
|
||||
|
||||
// sync deletions
|
||||
deletionSync(deletions)
|
||||
|
||||
// fetch local lists
|
||||
localEntryModMap := getLocalData()
|
||||
|
||||
// prior to syncing lists, ensure the client and server clocks are synced within 45 seconds
|
||||
var timeSynced = true
|
||||
timeDiff := serverTime - clientTime
|
||||
if timeDiff < -45 || timeDiff > 45 {
|
||||
timeSynced = false
|
||||
fmt.Print(core.AnsiError + "Client and server clocks are out of sync.\n\nPlease ensure both clocks are correct before attempting to sync again.\n\nA dry sync output will be printed below (if any operations would have been performed). It is strongly recommended to review it and manually update the modification times as applicable to ensure the correct version of each entry is kept.\n\nIf the client's clock is at fault, update the modification times of any entries pending upload, even if the correct (upload) operation is being performed on them. Failure to do so could result in entries being uploaded to the server with the incorrect modification times (could result in data loss).\n\n" + core.AnsiReset)
|
||||
}
|
||||
|
||||
// sync new and updated entries
|
||||
syncLists(sshClient, sshEntryRoot, sshIsWindows, timeSynced, localEntryModMap, remoteEntryModMap)
|
||||
|
||||
// exit program after successful sync
|
||||
core.Exit(0)
|
||||
}
|
||||
-113
@@ -1,113 +0,0 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
)
|
||||
|
||||
// getModTimes returns a list of all entry modification times.
|
||||
func getModTimes(entryList []string) []int64 {
|
||||
var modList []int64
|
||||
for _, file := range entryList {
|
||||
modTime, _ := os.Stat(core.TargetLocationFormat(file))
|
||||
modList = append(modList, modTime.ModTime().Unix())
|
||||
}
|
||||
|
||||
return modList
|
||||
}
|
||||
|
||||
// ShearLocal removes the target file or directory from the local system.
|
||||
// Returns: deviceID (only on client; for use in ShearRemoteFromClient).
|
||||
// If the local system is a server, it will also add the target to the deletions list for all clients (except the requesting client).
|
||||
// This function should only be used directly by the server binary.
|
||||
func ShearLocal(targetLocationIncomplete, clientDeviceID string) string {
|
||||
// determine if running on a server
|
||||
var onServer bool
|
||||
if clientDeviceID != "" {
|
||||
onServer = true
|
||||
}
|
||||
|
||||
deviceIDList := core.GenDeviceIDList(true)
|
||||
|
||||
// add the sheared target (incomplete, vanity) to the deletions list (if running on a server)
|
||||
if onServer {
|
||||
for _, device := range *deviceIDList {
|
||||
if device.Name() != clientDeviceID {
|
||||
fileToClose, err := os.OpenFile(core.ConfigDir+core.PathSeparator+"deletions"+core.PathSeparator+device.Name()+core.FSSpace+strings.ReplaceAll(targetLocationIncomplete, "/", core.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
// do not print error as there is currently no way of seeing server-side errors
|
||||
// failure to add the target to the deletions list will exit the program and result in a client re-uploading the target (non-critical)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
_ = fileToClose.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get the full targetLocation path and remove the target
|
||||
targetLocationComplete := core.TargetLocationFormat(targetLocationIncomplete)
|
||||
if !onServer { // error if target does not exist on client, needed because os.RemoveAll does not return an error if target does not exist
|
||||
core.TargetIsFile(targetLocationComplete, true, 0)
|
||||
}
|
||||
err := os.RemoveAll(targetLocationComplete)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Failed to remove local target:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
|
||||
if !onServer && len(*deviceIDList) > 0 { // return the device ID if running on the client and a device ID exists (online mode)
|
||||
return (*deviceIDList)[0].Name()
|
||||
}
|
||||
return ""
|
||||
|
||||
// do not exit program, as this function is used as part of ShearRemoteFromClient
|
||||
}
|
||||
|
||||
// RenameLocal renames oldLocationIncomplete to newLocationIncomplete on the local system.
|
||||
// This function should only be used directly by the server binary.
|
||||
func RenameLocal(oldLocationIncomplete, newLocationIncomplete string, verifyOldLocationExists bool) {
|
||||
// get full paths for both locations
|
||||
oldLocation := core.TargetLocationFormat(oldLocationIncomplete)
|
||||
newLocation := core.TargetLocationFormat(newLocationIncomplete)
|
||||
|
||||
if verifyOldLocationExists {
|
||||
core.TargetIsFile(oldLocation, true, 0)
|
||||
}
|
||||
|
||||
// ensure newLocation does not exist
|
||||
_, isAccessible := core.TargetIsFile(newLocation, false, 0)
|
||||
if isAccessible {
|
||||
fmt.Println(core.AnsiError + "\"" + newLocation + "\" already exists" + core.AnsiReset)
|
||||
os.Exit(core.ErrorTargetExists)
|
||||
}
|
||||
|
||||
// rename oldLocation to newLocation
|
||||
err := os.Rename(oldLocation, newLocation)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError + "Failed to rename - Does the target containing directory exist?" + core.AnsiReset)
|
||||
}
|
||||
|
||||
// do not exit program, as this function is used as part of RenameRemoteFromClient
|
||||
}
|
||||
|
||||
// AddFolderLocal creates a new entry-containing directory on the local system.
|
||||
// This function should only be used directly by the server binary.
|
||||
func AddFolderLocal(targetLocationIncomplete string) {
|
||||
// get the full targetLocation path and create the target
|
||||
targetLocationComplete := core.TargetLocationFormat(targetLocationIncomplete)
|
||||
err := os.Mkdir(targetLocationComplete, 0700)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
fmt.Println(core.AnsiError + "Directory already exists" + core.AnsiReset)
|
||||
os.Exit(core.ErrorTargetExists)
|
||||
} else {
|
||||
fmt.Println(core.AnsiError+"Failed to create directory:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
}
|
||||
|
||||
// do not exit program, as this function is used as part of AddFolderRemoteFromClient
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
//go:build !windows
|
||||
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
)
|
||||
|
||||
// WalkEntryDir walks the entry directory and returns lists of all files and directories found (two separate lists).
|
||||
// Regardless of platform, all paths are stored with forward slashes (UNIX-style).
|
||||
func WalkEntryDir() ([]string, []string) {
|
||||
// define file/directory containing slices so that they may be accessed by the anonymous WalkDir function
|
||||
var fileList []string
|
||||
var dirList []string
|
||||
|
||||
// walk entry directory
|
||||
_ = filepath.WalkDir(core.EntryRoot,
|
||||
func(fullPath string, entry fs.DirEntry, err error) error {
|
||||
|
||||
// check for errors encountered while walking directory
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println(core.AnsiError+"The entry directory does not exist - Run \""+os.Args[0], "init"+"\" to create it"+core.AnsiReset)
|
||||
} else {
|
||||
// otherwise, print the source of the error
|
||||
fmt.Println(core.AnsiError+"An unexpected error occurred while generating the entry list:", err.Error()+core.AnsiReset)
|
||||
}
|
||||
os.Exit(core.ErrorOther)
|
||||
}
|
||||
|
||||
// trim root path from each path before storing
|
||||
trimmedPath := fullPath[rootLength:]
|
||||
|
||||
// append the path to the appropriate slice
|
||||
if !entry.IsDir() {
|
||||
fileList = append(fileList, trimmedPath)
|
||||
} else {
|
||||
dirList = append(dirList, trimmedPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return fileList, dirList
|
||||
}
|
||||
|
||||
// joinErrorWithEXE is a utility function that joins and returns the two strings it is provided (in error format) with the executable name inserted between them.
|
||||
func joinErrorWithEXE(firstHalf, secondHalf string) string {
|
||||
return core.AnsiError + firstHalf + os.Args[0] + secondHalf + core.AnsiReset
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
)
|
||||
|
||||
// WalkEntryDir walks the entry directory and returns lists of all files and directories found (two separate lists).
|
||||
// Regardless of platform, all paths are stored with forward slashes (UNIX-style).
|
||||
func WalkEntryDir() ([]string, []string) {
|
||||
// define file/directory containing slices so that they may be accessed by the anonymous WalkDir function
|
||||
var fileList []string
|
||||
var dirList []string
|
||||
|
||||
// walk entry directory
|
||||
_ = filepath.WalkDir(core.EntryRoot,
|
||||
func(fullPath string, entry fs.DirEntry, err error) error {
|
||||
|
||||
// check for errors encountered while walking directory
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println(joinErrorWithEXE("The entry directory does not exist - Run \"", " init"+"\" to create it"))
|
||||
} else {
|
||||
// otherwise, print the source of the error
|
||||
fmt.Println(core.AnsiError+"An unexpected error occurred while generating the entry list:", err.Error()+core.AnsiReset)
|
||||
}
|
||||
os.Exit(core.ErrorOther)
|
||||
}
|
||||
|
||||
// trim root path from each path before storing and replace backslashes with forward slashes
|
||||
trimmedPath := strings.ReplaceAll(fullPath[rootLength:], "\\", "/")
|
||||
|
||||
// append the path to the appropriate slice
|
||||
if !entry.IsDir() {
|
||||
fileList = append(fileList, trimmedPath)
|
||||
} else {
|
||||
dirList = append(dirList, trimmedPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return fileList, dirList
|
||||
}
|
||||
|
||||
// joinErrorWithEXE is a utility function that joins and returns the two strings it is provided (in error format) with the executable name inserted between them.
|
||||
func joinErrorWithEXE(firstHalf, secondHalf string) string {
|
||||
return core.AnsiError + firstHalf + os.Args[0][strings.LastIndex(os.Args[0], "\\")+1:] + secondHalf + core.AnsiReset
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
)
|
||||
|
||||
// DeviceIDGen generates a new client device ID and registers it with the server (will replace existing one).
|
||||
// Device IDs are only needed for online synchronization.
|
||||
// Device IDs are guaranteed unique as the current UNIX time is appended to them.
|
||||
// Returns: the remote EntryRoot and OS type indicator.
|
||||
func DeviceIDGen(oldDeviceID string) (string, string) {
|
||||
// generate new device ID
|
||||
deviceIDPrefix, _ := os.Hostname()
|
||||
deviceIDSuffix := core.StringGen(rand.Intn(32)+48, true, 0.2, true) + "-" + strconv.FormatInt(time.Now().Unix(), 10)
|
||||
newDeviceID := deviceIDPrefix + "-" + deviceIDSuffix
|
||||
|
||||
// create new device ID file (locally)
|
||||
fileToClose, err := os.OpenFile(core.ConfigDir+core.PathSeparator+"devices"+core.PathSeparator+newDeviceID, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Failed to create local device ID file:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
_ = fileToClose.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
|
||||
// register new device ID with server and fetch remote EntryRoot and OS type
|
||||
// also removes the old device ID file (remotely)
|
||||
// manualSync is true so the user is alerted if device ID registration fails
|
||||
sshClient, _, _ := GetSSHClient(true)
|
||||
sshEntryRootSSHIsWindows := strings.Split(GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID), core.FSSpace)
|
||||
err = sshClient.Close()
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Init failed - Unable to close SSH client:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
|
||||
// remove old device ID file (locally; may not exist)
|
||||
err = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + oldDeviceID)
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Failed to remove old device ID file (locally):", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorWrite)
|
||||
}
|
||||
|
||||
return sshEntryRootSSHIsWindows[0], sshEntryRootSSHIsWindows[1]
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// inputKeyFilePassphrase prompts the user for a passphrase for an SSH key file.
|
||||
// TODO support non-CLI implementations
|
||||
func inputKeyFilePassphrase() []byte {
|
||||
fmt.Print("\nEnter passphrase for your SSH keyfile: ")
|
||||
passphrase, _ := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Println()
|
||||
return passphrase
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
)
|
||||
|
||||
// ShearRemoteFromClient removes the target file or directory from the local system and calls the server to remove it remotely and add it to the deletions list.
|
||||
// It can safely be called in offline mode, as well, so this is the intended interface for shearing (ShearLocal should only be used directly by the server binary).
|
||||
func ShearRemoteFromClient(targetLocationIncomplete string) {
|
||||
deviceID := ShearLocal(targetLocationIncomplete, "") // remove the target from the local system and get the device ID of the client
|
||||
|
||||
if deviceID != "" { // ensure a device ID exists (online mode)
|
||||
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
||||
sshClient, _, _ := GetSSHClient(false)
|
||||
|
||||
// call the server to remotely shear the target and add it to the deletions list
|
||||
GetSSHOutput(sshClient, "libmuttonserver shear", deviceID+"\n"+strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, core.FSPath))
|
||||
|
||||
// close the SSH client
|
||||
err := sshClient.Close()
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to close SSH client:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
}
|
||||
|
||||
core.Exit(0) // sync is not required after shearing since the target has already been removed from the local system
|
||||
}
|
||||
|
||||
// RenameRemoteFromClient renames oldLocationIncomplete to newLocationIncomplete on the local system and calls the server to perform the rename remotely and add the old target to the deletions list.
|
||||
// It can safely be called in offline mode, as well, so this is the intended interface for renaming (RenameLocal should only be used directly by the server binary).
|
||||
func RenameRemoteFromClient(oldLocationIncomplete, newLocationIncomplete string) {
|
||||
RenameLocal(oldLocationIncomplete, newLocationIncomplete, false) // move the target on the local system
|
||||
|
||||
deviceIDList := core.GenDeviceIDList(true)
|
||||
if len(*deviceIDList) > 0 { // ensure a device ID exists (online mode)
|
||||
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
||||
sshClient, _, _ := GetSSHClient(false)
|
||||
|
||||
// call the server to move the target on the remote system and add the old target to the deletions list
|
||||
GetSSHOutput(sshClient, "libmuttonserver rename",
|
||||
(*deviceIDList)[0].Name()+"\n"+
|
||||
strings.ReplaceAll(oldLocationIncomplete, core.PathSeparator, core.FSPath)+"\n"+
|
||||
strings.ReplaceAll(newLocationIncomplete, core.PathSeparator, core.FSPath))
|
||||
|
||||
// close the SSH client
|
||||
err := sshClient.Close()
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to close SSH client:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
}
|
||||
|
||||
core.Exit(0)
|
||||
}
|
||||
|
||||
// AddFolderRemoteFromClient creates a new entry-containing directory on the local system and calls the server to create the folder remotely.
|
||||
// It can safely be called in offline mode, as well, so this is the intended interface for adding folders (AddFolderLocal should only be used directly by the server binary).
|
||||
func AddFolderRemoteFromClient(targetLocationIncomplete string) {
|
||||
AddFolderLocal(targetLocationIncomplete) // add the folder on the local system
|
||||
|
||||
deviceIDList := core.GenDeviceIDList(true)
|
||||
if len(*deviceIDList) > 0 { // ensure a device ID exists (online mode)
|
||||
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
||||
sshClient, _, _ := GetSSHClient(false)
|
||||
|
||||
// call the server to create the folder remotely
|
||||
GetSSHOutput(sshClient, "libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, core.FSPath)) // call the server to create the folder remotely
|
||||
|
||||
// close the SSH client
|
||||
err := sshClient.Close()
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Sync failed - Unable to close SSH client:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorServerConnection)
|
||||
}
|
||||
}
|
||||
|
||||
core.Exit(0)
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/libmutton/core"
|
||||
)
|
||||
|
||||
// GetRemoteDataFromServer prints to stdout the remote entries, mod times, folders, and deletions.
|
||||
// Lists in output are separated by FSSpace.
|
||||
// Output is meant to be captured over SSH for interpretation by the client.
|
||||
func GetRemoteDataFromServer(clientDeviceID string) {
|
||||
entryList, dirList := WalkEntryDir()
|
||||
modList := getModTimes(entryList)
|
||||
deletionsList, err := os.ReadDir(core.ConfigDir + core.PathSeparator + "deletions")
|
||||
if err != nil {
|
||||
fmt.Println(core.AnsiError+"Failed to read the deletions directory:", err.Error()+core.AnsiReset)
|
||||
os.Exit(core.ErrorRead)
|
||||
}
|
||||
|
||||
// print the current UNIX timestamp to stdout
|
||||
fmt.Print(time.Now().Unix())
|
||||
|
||||
// print the lists to stdout
|
||||
// entry list
|
||||
fmt.Print(core.FSSpace)
|
||||
for _, entry := range entryList {
|
||||
fmt.Print(core.FSMisc + entry)
|
||||
}
|
||||
|
||||
// modification time list
|
||||
fmt.Print(core.FSSpace)
|
||||
for _, mod := range modList {
|
||||
fmt.Print(core.FSMisc)
|
||||
fmt.Print(mod)
|
||||
}
|
||||
|
||||
// directory/folder list
|
||||
fmt.Print(core.FSSpace)
|
||||
for _, dir := range dirList {
|
||||
fmt.Print(core.FSMisc + dir)
|
||||
}
|
||||
|
||||
// deletions list
|
||||
fmt.Print(core.FSSpace)
|
||||
for _, deletion := range deletionsList {
|
||||
// print deletion if it is relevant to the current client device
|
||||
affectedIDTargetLocationIncomplete := strings.Split(deletion.Name(), core.FSSpace)
|
||||
if affectedIDTargetLocationIncomplete[0] == clientDeviceID {
|
||||
fmt.Print(core.FSMisc + strings.ReplaceAll(affectedIDTargetLocationIncomplete[1], core.FSPath, "/"))
|
||||
|
||||
// assume successful client deletion and remove deletions file (if assumption is somehow false, worst case scenario is that the client will re-upload the deleted entry)
|
||||
_ = os.Remove(core.ConfigDir + core.PathSeparator + "deletions" + core.PathSeparator + deletion.Name()) // error ignored; function not run from a user-facing argument and thus the error would not be visible
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
package syncclient
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/sftp"
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/age"
|
||||
"github.com/rwinkhart/libmutton/config"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/libmutton/privkey"
|
||||
"github.com/rwinkhart/libmutton/synccommon"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
type syncListsT struct {
|
||||
Delete []string
|
||||
Download []string
|
||||
Upload []string
|
||||
}
|
||||
|
||||
// GetSSHClient
|
||||
// Returns:
|
||||
// sshClient,
|
||||
// offlineMode (whether the client is in offline mode).
|
||||
// sshIsWindows (whether the remote server is running Windows),
|
||||
// sshEntryRoot (the root directory for entries on the remote server),
|
||||
// Only supports key-based authentication (passwords are supported for CLI-based implementations).
|
||||
func GetSSHClient() (*ssh.Client, bool, *bool, *string, *string, error) {
|
||||
// get SSH config info
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to parse SSH config: " + err.Error())
|
||||
}
|
||||
if *cfg.Libmutton.OfflineMode {
|
||||
return nil, true, nil, nil, nil, nil
|
||||
}
|
||||
|
||||
// get private key
|
||||
key, err := privkey.GetBytes(cfg.Libmutton.SSHKeyPath)
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, err
|
||||
}
|
||||
|
||||
// parse private key
|
||||
var parsedKey ssh.Signer
|
||||
if !*cfg.Libmutton.SSHKeyProtected {
|
||||
parsedKey, err = ssh.ParsePrivateKey(key)
|
||||
} else {
|
||||
parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, global.GetPassword("Enter password for your SSH keyfile:"))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to parse private key: " + err.Error())
|
||||
}
|
||||
|
||||
// read known hosts file
|
||||
var hostKeyCallback ssh.HostKeyCallback
|
||||
hostKeyCallback, err = knownhosts.New(global.SSHDir + global.PathSeparator + "known_hosts")
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to read known hosts file: " + err.Error())
|
||||
}
|
||||
|
||||
// configure SSH client
|
||||
sshCfg := &ssh.ClientConfig{
|
||||
User: *cfg.Libmutton.SSHUser,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.PublicKeys(parsedKey),
|
||||
},
|
||||
HostKeyCallback: hostKeyCallback,
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
// connect to SSH server
|
||||
sshClient, err := ssh.Dial("tcp", *cfg.Libmutton.SSHIP+":"+*cfg.Libmutton.SSHPort, sshCfg)
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to connect to remote server: " + err.Error())
|
||||
}
|
||||
|
||||
return sshClient, false, cfg.Libmutton.SSHIsWindows, cfg.Libmutton.SSHEntryRootPath, cfg.Libmutton.SSHAgeDirPath, nil
|
||||
}
|
||||
|
||||
// GetSSHOutput runs a command over SSH and returns the output as a string.
|
||||
func GetSSHOutput(sshClient *ssh.Client, cmd, stdin string) ([]byte, error) {
|
||||
// create a session
|
||||
sshSession, err := sshClient.NewSession()
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to establish SSH session: " + err.Error())
|
||||
}
|
||||
|
||||
// provide stdin data for session
|
||||
sshSession.Stdin = strings.NewReader(stdin)
|
||||
|
||||
// run the provided command
|
||||
var output []byte
|
||||
output, err = sshSession.CombinedOutput(cmd)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to run SSH command: " + err.Error())
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// getRemoteDataFromClient returns:
|
||||
// a map of remote vanityPaths to their containing folders and mod+age timestamps,
|
||||
// a list of queued deletions,
|
||||
// and the current server&client times as UNIX timestamps.
|
||||
func getRemoteDataFromClient(sshClient *ssh.Client) (synccommon.EntryMapT, []synccommon.Deletion, int64, int64, error) {
|
||||
// get remote output over SSH
|
||||
deviceIDList, err := global.GenDeviceIDList()
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
if len(deviceIDList) == 0 {
|
||||
return nil, nil, 0, 0, errors.New("no device ID found")
|
||||
}
|
||||
clientTime := time.Now().Unix() // get client time now to avoid accuracy issues caused by unpredictable sync time
|
||||
output, err := GetSSHOutput(sshClient, "libmuttonserver fetch", (deviceIDList)[0].Name())
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, errors.New("unable to run remote command: " + err.Error())
|
||||
}
|
||||
|
||||
var fetchResp synccommon.FetchRespT
|
||||
if err = json.Unmarshal(output, &fetchResp); err != nil {
|
||||
fmt.Println(string(output))
|
||||
return nil, nil, 0, 0, errors.New("unable to unmarshal server fetch response: " + err.Error())
|
||||
}
|
||||
if fetchResp.ErrMsg != nil {
|
||||
return nil, nil, 0, 0, errors.New("unable to complete fetch; server-side error occurred: " + strings.ReplaceAll(*fetchResp.ErrMsg, global.FSSpace, "\n"))
|
||||
}
|
||||
|
||||
return fetchResp.Entries, fetchResp.Deletions, fetchResp.ServerTime, clientTime, nil
|
||||
}
|
||||
|
||||
// getRealPathSFTP formats the vanityPath to match the remote server's entry/age file directory and path separator.
|
||||
func getRealPathSFTP(vanityPath, serverEntryRoot string, serverIsWindows bool) string {
|
||||
if !serverIsWindows {
|
||||
return serverEntryRoot + vanityPath
|
||||
}
|
||||
return serverEntryRoot + strings.ReplaceAll(vanityPath, "/", "\\")
|
||||
}
|
||||
|
||||
// getRealPathSFTP formats the vanityPath to match the remote server's entry/age file directory and path separator.
|
||||
func getRealAgePathSFTP(vanityPath, serverAgeDir string, serverIsWindows bool) string {
|
||||
if !serverIsWindows {
|
||||
return serverAgeDir + "/" + strings.ReplaceAll(vanityPath, "/", global.FSPath)
|
||||
}
|
||||
return serverAgeDir + "\\" + strings.ReplaceAll(vanityPath, "/", global.FSPath)
|
||||
}
|
||||
|
||||
// sftpSync takes two slices of entries (one for downloads and one for uploads) and syncs them between the client and server using SFTP.
|
||||
func sftpSync(sshClient *ssh.Client, sshEntryRoot, sshAgeDir string, sshIsWindows bool, syncListsV *syncListsT) error {
|
||||
// create an SFTP client from sshClient
|
||||
sftpClient, err := sftp.NewClient(sshClient)
|
||||
if err != nil {
|
||||
return errors.New("unable to establish SFTP session: " + err.Error())
|
||||
}
|
||||
defer func(sftpClient *sftp.Client) {
|
||||
_ = sftpClient.Close()
|
||||
}(sftpClient)
|
||||
|
||||
// iterate over the download list
|
||||
var filesTransferred bool
|
||||
for _, vanityPath := range syncListsV.Download {
|
||||
filesTransferred = true // set a flag to indicate that files have been downloaded (used to determine whether to print a gap between download and upload messages)
|
||||
fmt.Println("Downloading " + back.AnsiGreen + vanityPath + back.AnsiReset)
|
||||
|
||||
// store path to remote entry
|
||||
remoteFileRealPath := getRealPathSFTP(vanityPath, sshEntryRoot, sshIsWindows)
|
||||
|
||||
// save modification time of remote file
|
||||
var fileInfo os.FileInfo
|
||||
fileInfo, err = sftpClient.Stat(remoteFileRealPath)
|
||||
if err != nil {
|
||||
return errors.New("unable to get remote file info (mod time): " + err.Error())
|
||||
}
|
||||
modTime := fileInfo.ModTime()
|
||||
|
||||
// open remote file
|
||||
var remoteFile *sftp.File
|
||||
remoteFile, err = sftpClient.Open(remoteFileRealPath)
|
||||
if err != nil {
|
||||
return errors.New("unable to open remote file: " + err.Error())
|
||||
}
|
||||
|
||||
// store path to local file
|
||||
localFileRealPath := global.GetRealPath(vanityPath)
|
||||
|
||||
// create local file
|
||||
var localFile *os.File
|
||||
localFile, err = os.OpenFile(localFileRealPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return errors.New("unable to create local file: " + err.Error())
|
||||
}
|
||||
|
||||
// download the file
|
||||
_, err = remoteFile.WriteTo(localFile)
|
||||
if err != nil {
|
||||
_ = localFile.Close()
|
||||
return errors.New("unable to download remote file: " + err.Error())
|
||||
}
|
||||
|
||||
// close the files
|
||||
_ = remoteFile.Close() // errors ignored; if the files could be opened/created, it can probably be closed
|
||||
_ = localFile.Close()
|
||||
|
||||
// set the modification time of the local file to match the value saved from the remote file (from before the download)
|
||||
if err = os.Chtimes(localFileRealPath, time.Now(), modTime); err != nil {
|
||||
return errors.New("unable to set local file modification time: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if filesTransferred {
|
||||
fmt.Println() // add a gap between download and upload messages
|
||||
}
|
||||
|
||||
// iterate over the upload list
|
||||
filesTransferred = false
|
||||
for _, vanityPath := range syncListsV.Upload {
|
||||
// determine if local file is an age file
|
||||
var isAgeFile bool
|
||||
if strings.HasPrefix(vanityPath, global.FSMisc) {
|
||||
vanityPath = strings.TrimLeft(vanityPath, global.FSMisc)
|
||||
isAgeFile = true
|
||||
} else {
|
||||
filesTransferred = true // set a flag to indicate that files have been uploaded (used to determine whether to print a gap between upload and sync complete messages)
|
||||
fmt.Println("Uploading " + back.AnsiBlue + vanityPath + back.AnsiReset)
|
||||
}
|
||||
|
||||
// store path to local file
|
||||
var localFileRealPath string
|
||||
if isAgeFile {
|
||||
localFileRealPath = global.GetRealAgePath(vanityPath)
|
||||
} else {
|
||||
localFileRealPath = global.GetRealPath(vanityPath)
|
||||
}
|
||||
|
||||
// save modification time of local file
|
||||
var fileInfo os.FileInfo
|
||||
fileInfo, err = os.Stat(localFileRealPath)
|
||||
if err != nil {
|
||||
return errors.New("unable to get local file info (mod time): " + err.Error())
|
||||
}
|
||||
modTime := fileInfo.ModTime()
|
||||
|
||||
// open local file
|
||||
var localFile *os.File
|
||||
localFile, err = os.Open(localFileRealPath)
|
||||
if err != nil {
|
||||
return errors.New("unable to open local file: " + err.Error())
|
||||
}
|
||||
|
||||
// store path to remote entry
|
||||
var remoteFileRealPath string
|
||||
if isAgeFile {
|
||||
remoteFileRealPath = getRealAgePathSFTP(vanityPath, sshAgeDir, sshIsWindows)
|
||||
} else {
|
||||
remoteFileRealPath = getRealPathSFTP(vanityPath, sshEntryRoot, sshIsWindows)
|
||||
}
|
||||
|
||||
// create remote file
|
||||
var remoteFile *sftp.File
|
||||
remoteFile, err = sftpClient.OpenFile(remoteFileRealPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
|
||||
if err != nil {
|
||||
_ = localFile.Close()
|
||||
return errors.New("unable to create remote file: " + err.Error())
|
||||
}
|
||||
|
||||
// upload the file
|
||||
_, err = localFile.WriteTo(remoteFile)
|
||||
if err != nil {
|
||||
_ = localFile.Close()
|
||||
_ = remoteFile.Close()
|
||||
return errors.New("unable to upload local file: " + err.Error())
|
||||
}
|
||||
|
||||
// close the files
|
||||
_ = localFile.Close() // errors ignored; if the files could be opened/created, it can probably be closed
|
||||
_ = remoteFile.Close()
|
||||
|
||||
// set permissions on remote file
|
||||
if err = sftpClient.Chmod(remoteFileRealPath, 0600); err != nil {
|
||||
return errors.New("unable to set permissions on remote file: " + err.Error())
|
||||
}
|
||||
|
||||
// set the modification time of the remote file to match the value saved from the local file (from before the upload)
|
||||
if err = sftpClient.Chtimes(remoteFileRealPath, time.Now(), modTime); err != nil {
|
||||
return errors.New("unable to set remote file modification time: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if filesTransferred {
|
||||
fmt.Println() // add a gap between upload and sync complete messages
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// syncLists determines which entries need to be downloaded and uploaded
|
||||
// for synchronization and calls sftpSync with this information.
|
||||
func syncLists(sshClient *ssh.Client, sshEntryRoot, sshAgeDir string, sshIsWindows bool, timeSyncedErr error, localEntryMap, remoteEntryMap synccommon.EntryMapT) (*syncListsT, error) {
|
||||
// initialize slices to store entries that need to be downloaded or uploaded
|
||||
var syncListsV syncListsT
|
||||
|
||||
// iterate over client entries in local map
|
||||
for vanityPath, localInfo := range localEntryMap {
|
||||
// check if the entry is present on the server
|
||||
if _, exists := remoteEntryMap[vanityPath]; exists {
|
||||
// entry exists on both client and server, compare mod times
|
||||
remoteInfo := remoteEntryMap[vanityPath]
|
||||
if remoteInfo.ModTime > localInfo.ModTime {
|
||||
fmt.Println(back.AnsiGreen+vanityPath+back.AnsiReset, "is newer on server, adding to download list")
|
||||
syncListsV.Download = append(syncListsV.Download, vanityPath)
|
||||
if remoteInfo.AgeTimestamp != nil {
|
||||
if err := age.Entry(vanityPath, *remoteInfo.AgeTimestamp); err != nil {
|
||||
return nil, errors.New("unable to update age timestamp for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
}
|
||||
} else if remoteInfo.ModTime < localInfo.ModTime {
|
||||
fmt.Println(back.AnsiBlue+vanityPath+back.AnsiReset, "is newer on client, adding to upload list")
|
||||
syncListsV.Upload = append(syncListsV.Upload, vanityPath)
|
||||
if localInfo.AgeTimestamp != nil && localInfo.AgeTimestamp != remoteInfo.AgeTimestamp {
|
||||
syncListsV.Upload = append(syncListsV.Upload, global.FSMisc+vanityPath)
|
||||
}
|
||||
}
|
||||
// remove entry from remote map (process of elimination)
|
||||
delete(remoteEntryMap, vanityPath)
|
||||
} else {
|
||||
fmt.Println(back.AnsiBlue+vanityPath+back.AnsiReset, "does not exist on server, adding to upload list")
|
||||
syncListsV.Upload = append(syncListsV.Upload, vanityPath)
|
||||
if localInfo.AgeTimestamp != nil {
|
||||
syncListsV.Upload = append(syncListsV.Upload, global.FSMisc+vanityPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// iterate over remaining entries in remote map
|
||||
for vanityPath, remoteInfo := range remoteEntryMap {
|
||||
fmt.Println(back.AnsiGreen+vanityPath+back.AnsiReset, "does not exist on client, adding to download list")
|
||||
syncListsV.Download = append(syncListsV.Download, vanityPath)
|
||||
if err := os.MkdirAll(global.GetRealPath(remoteInfo.ContainingFolder), 0700); err != nil {
|
||||
return nil, errors.New("unable to create containing folder for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
if remoteInfo.AgeTimestamp != nil {
|
||||
if err := age.Entry(vanityPath, *remoteInfo.AgeTimestamp); err != nil {
|
||||
return nil, errors.New("unable to update age timestamp for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// call sftpSync with the download and upload lists
|
||||
if timeSyncedErr == nil && (max(len(syncListsV.Download), len(syncListsV.Upload)) > 0) { // only call sftpSync if there are entries to download or upload
|
||||
fmt.Println() // add a gap between list-add messages and the actual sync messages from sftpSync
|
||||
if err := sftpSync(sshClient, sshEntryRoot, sshAgeDir, sshIsWindows, &syncListsV); err != nil {
|
||||
return nil, errors.New("unable to sync entries: " + err.Error())
|
||||
}
|
||||
fmt.Println("Client is synchronized with server")
|
||||
}
|
||||
|
||||
return &syncListsV, timeSyncedErr
|
||||
}
|
||||
|
||||
// deletionSync removes entries from the client that have been deleted on the server (multi-client deletion).
|
||||
func deletionSync(deletions []synccommon.Deletion) error {
|
||||
var entryDeleted bool
|
||||
for i := range deletions {
|
||||
if !deletions[i].IsAgeFile {
|
||||
entryDeleted = true // set a flag to indicate that at least one entry has been deleted (used to determine whether to print a gap between deletion and other messages)
|
||||
fmt.Println(synccommon.AnsiDelete+deletions[i].VanityPath+back.AnsiReset, "has been sheared, removing locally (if it exists)")
|
||||
}
|
||||
if err := os.RemoveAll(global.GetRealPath(deletions[i].VanityPath)); err != nil {
|
||||
if !deletions[i].IsAgeFile {
|
||||
return errors.New("unable to shear " + deletions[i].VanityPath + " locally: " + err.Error())
|
||||
}
|
||||
return errors.New("unable to shear age file for " + deletions[i].VanityPath + " locally: " + err.Error())
|
||||
}
|
||||
}
|
||||
if entryDeleted {
|
||||
fmt.Println() // add a gap between deletion and other messages
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunJob runs the SSH sync job and returns deletions, downloads,
|
||||
// and uploads lists for the client to report to the user.
|
||||
func RunJob() (*syncListsT, error) {
|
||||
// get SSH client to re-use throughout the sync process
|
||||
sshClient, offlineMode, sshIsWindows, sshEntryRoot, sshAgeDir, err := GetSSHClient()
|
||||
if offlineMode {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to connect to SSH client: " + err.Error())
|
||||
}
|
||||
defer func(sshClient *ssh.Client) {
|
||||
_ = sshClient.Close()
|
||||
}(sshClient)
|
||||
|
||||
// fetch remote lists
|
||||
remoteEntryMap, deletions, serverTime, clientTime, err := getRemoteDataFromClient(sshClient)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to fetch remote data: " + err.Error())
|
||||
}
|
||||
|
||||
// sync deletions
|
||||
if err = deletionSync(deletions); err != nil {
|
||||
return nil, errors.New("unable to sync deletions: " + err.Error())
|
||||
}
|
||||
|
||||
// fetch local lists
|
||||
localEntryMap, err := synccommon.GetAllEntryData()
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to fetch local entry data: " + err.Error())
|
||||
}
|
||||
|
||||
// before syncing lists, ensure the client and server clocks are synced within 45 seconds
|
||||
var timeSyncedErr error
|
||||
timeDiff := serverTime - clientTime
|
||||
if timeDiff < -45 || timeDiff > 45 {
|
||||
timeSyncedErr = errors.New("client and server clocks are out of sync\n\nplease ensure both clocks are correct before attempting to sync again\n\na dry sync has been performed; it is strongly recommended to review it and manually update the modification times as applicable to ensure the correct version of each entry is kept\n\nif the client's clock is at fault, update the modification times of any entries pending upload, even if the correct (upload) operation is being performed on them; failure to do so could result in entries being uploaded to the server with the incorrect modification times (could result in data loss)" + back.AnsiReset)
|
||||
}
|
||||
|
||||
// sync new and updated entries
|
||||
// if time is not synced, the time sync error and upload/download lists will be returned here
|
||||
syncListsV, err := syncLists(sshClient, *sshEntryRoot, *sshAgeDir, *sshIsWindows, timeSyncedErr, localEntryMap, remoteEntryMap)
|
||||
if err != nil {
|
||||
return syncListsV, errors.New("unable to sync entries: " + err.Error())
|
||||
}
|
||||
|
||||
// add deletions info to sync lists
|
||||
for i := range deletions {
|
||||
if !deletions[i].IsAgeFile {
|
||||
syncListsV.Delete = append(syncListsV.Delete, deletions[i].VanityPath)
|
||||
}
|
||||
}
|
||||
|
||||
return syncListsV, nil
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package syncclient
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/go-boilerplate/security"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/libmutton/synccommon"
|
||||
)
|
||||
|
||||
// ShearRemote removes the target file or directory from
|
||||
// the local system and calls the server to remove it remotely and
|
||||
// add it to the deletions list.
|
||||
// It can safely be called in offline mode, as well, so this is
|
||||
// the intended interface for shearing (ShearLocal should only
|
||||
// be used directly by the server binary).
|
||||
func ShearRemote(vanityPath string, onlyShearAgeFile bool) error {
|
||||
deviceID, isDir, err := synccommon.ShearLocal(vanityPath, "", onlyShearAgeFile) // remove the target from the local system and get the device ID of the client
|
||||
if err != nil {
|
||||
return errors.New("unable to shear target locally: " + err.Error())
|
||||
}
|
||||
|
||||
var modifier string
|
||||
var output []byte
|
||||
sshClient, offlineMode, _, _, _, err := GetSSHClient()
|
||||
if offlineMode {
|
||||
goto end
|
||||
}
|
||||
if err != nil {
|
||||
return errors.New("unable to connect to SSH client: " + err.Error())
|
||||
}
|
||||
if deviceID == "" {
|
||||
return errors.New("unable to shear target remotely: no device ID found")
|
||||
}
|
||||
|
||||
// ensure vanityPath ends with a slash if it is a directory (for clarity in shear message)
|
||||
if isDir && !strings.HasSuffix(vanityPath, "/") {
|
||||
vanityPath += "/"
|
||||
}
|
||||
|
||||
// call the server to remotely shear the target and add it to the deletions list
|
||||
if onlyShearAgeFile {
|
||||
modifier = "-age"
|
||||
}
|
||||
output, err = GetSSHOutput(sshClient, "libmuttonserver shear"+modifier, deviceID+"\n"+strings.ReplaceAll(vanityPath, global.PathSeparator, global.FSPath))
|
||||
if err != nil {
|
||||
return errors.New("unable to shear target remotely: " + err.Error())
|
||||
}
|
||||
if len(output) > 11 {
|
||||
return errors.New("unable to complete shear; server-side error occurred: " + strings.ReplaceAll(string(output)[11:len(output)-2], global.FSSpace, "\n"))
|
||||
}
|
||||
|
||||
// close the SSH client
|
||||
if err = sshClient.Close(); err != nil {
|
||||
return errors.New("unable to close SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
end:
|
||||
back.Exit(0) // sync is not required after shearing since the target has already been removed from the local system
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenameRemote renames oldVanityPath to newVanityPath on
|
||||
// the local system and calls the server to perform the rename remotely and add the
|
||||
// old target to the deletions list.
|
||||
// It can safely be called in offline mode, as well, so this is the intended
|
||||
// interface for renaming (RenameLocal should only be used directly by the server binary).
|
||||
func RenameRemote(oldVanityPath, newVanityPath string) error {
|
||||
// move the target on the local system
|
||||
if err := synccommon.RenameLocal(oldVanityPath, newVanityPath); err != nil {
|
||||
return errors.New("unable to rename target locally: " + err.Error())
|
||||
}
|
||||
|
||||
deviceIDList, err := global.GenDeviceIDList()
|
||||
if err != nil {
|
||||
return errors.New("unable to generate device ID list: " + err.Error())
|
||||
}
|
||||
if deviceIDList[0].Name() == "" {
|
||||
return errors.New("unable to rename target remotely: no device ID found")
|
||||
}
|
||||
|
||||
// create an SSH client
|
||||
var output []byte
|
||||
sshClient, offlineMode, _, _, _, err := GetSSHClient()
|
||||
if offlineMode {
|
||||
goto end
|
||||
}
|
||||
if err != nil {
|
||||
return errors.New("unable to connect to SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
// call the server to move the target on the remote system and add the old target to the deletions list
|
||||
output, err = GetSSHOutput(sshClient, "libmuttonserver rename",
|
||||
(deviceIDList)[0].Name()+"\n"+
|
||||
strings.ReplaceAll(oldVanityPath, global.PathSeparator, global.FSPath)+"\n"+
|
||||
strings.ReplaceAll(newVanityPath, global.PathSeparator, global.FSPath))
|
||||
if err != nil {
|
||||
return errors.New("unable to rename target remotely: " + err.Error())
|
||||
}
|
||||
if len(output) > 11 {
|
||||
return errors.New("unable to complete rename; server-side error occurred: " + strings.ReplaceAll(string(output)[11:len(output)-2], global.FSSpace, "\n"))
|
||||
}
|
||||
|
||||
// close the SSH client
|
||||
if err = sshClient.Close(); err != nil {
|
||||
return errors.New("unable to close SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
end:
|
||||
back.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddFolderRemote creates a new entry-containing directory
|
||||
// on the local system and calls the server to create the folder remotely.
|
||||
// It can safely be called in offline mode, as well, so this is the
|
||||
// intended interface for adding folders (AddFolderLocal should only be
|
||||
// used directly by the server binary).
|
||||
func AddFolderRemote(vanityPath string) error {
|
||||
// add the folder on the local system
|
||||
if err := synccommon.AddFolderLocal(vanityPath); err != nil {
|
||||
return errors.New("unable to add folder locally: " + err.Error())
|
||||
}
|
||||
|
||||
// create an SSH client
|
||||
var output []byte
|
||||
sshClient, offlineMode, _, _, _, err := GetSSHClient()
|
||||
if offlineMode {
|
||||
goto end
|
||||
}
|
||||
if err != nil {
|
||||
return errors.New("unable to connect to SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
// call the server to create the folder remotely
|
||||
output, err = GetSSHOutput(sshClient, "libmuttonserver addfolder", strings.ReplaceAll(vanityPath, global.PathSeparator, global.FSPath)) // call the server to create the folder remotely
|
||||
if err != nil {
|
||||
return errors.New("unable to add folder remotely: " + err.Error())
|
||||
}
|
||||
if len(output) > 11 {
|
||||
return errors.New("unable to complete addfolder; server-side error occurred: " + strings.ReplaceAll(string(output)[11:len(output)-2], global.FSSpace, "\n"))
|
||||
}
|
||||
|
||||
// close the SSH client
|
||||
if err = sshClient.Close(); err != nil {
|
||||
return errors.New("unable to close SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
end:
|
||||
back.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenDeviceID generates a new client device ID
|
||||
// and registers it with the server (will replace existing one).
|
||||
// Device IDs are only needed for online synchronization.
|
||||
// Device IDs are guaranteed unique as the current UNIX time is appended to them.
|
||||
// Leave prefix empty to use the current hostname as the prefix.
|
||||
// Returns: the remote EntryRoot, the remote AgeDir, and OS type indicator.
|
||||
func GenDeviceID(oldDeviceID *string, prefix string) (string, string, bool, error) {
|
||||
// generate new device ID
|
||||
if prefix == "" {
|
||||
prefix, _ = os.Hostname()
|
||||
}
|
||||
newDeviceID := prefix + "-" + string(security.BytesGen(rand.Intn(32)+48, 0.2, 1)) + "-" + strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
// create new device ID file (locally)
|
||||
newDeviceIDPath := global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID
|
||||
var oldDeviceIDPath string
|
||||
if oldDeviceID != nil {
|
||||
oldDeviceIDPath = global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + *oldDeviceID
|
||||
}
|
||||
f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return "", "", false, errors.New("unable to create local device ID file: " + err.Error())
|
||||
}
|
||||
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
|
||||
cleanupOnFail := func() {
|
||||
// remove new device ID file
|
||||
_ = os.RemoveAll(newDeviceIDPath)
|
||||
if oldDeviceID != nil {
|
||||
// restore old device ID file (if it existed and has already been removed due to DirInit)
|
||||
if isAccessible, _ := back.TargetIsFile(oldDeviceIDPath, true); !isAccessible {
|
||||
f, _ := os.OpenFile(oldDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// register new device ID with server and fetch remote EntryRoot and OS type
|
||||
// also removes the old device ID file (remotely)
|
||||
// if registration fails, remove the new device ID file locally and return before removing the old one
|
||||
sshClient, _, _, _, _, err := GetSSHClient()
|
||||
if err != nil {
|
||||
cleanupOnFail()
|
||||
return "", "", false, errors.New("unable to connect to SSH client: " + err.Error())
|
||||
}
|
||||
registerReqBytes, err := json.Marshal(synccommon.RegisterReqT{NewDeviceID: newDeviceID, OldDeviceID: oldDeviceID})
|
||||
if err != nil {
|
||||
return "", "", false, errors.New("unable to marshal client register request: " + err.Error())
|
||||
}
|
||||
output, err := GetSSHOutput(sshClient, "libmuttonserver register", string(registerReqBytes))
|
||||
if err != nil {
|
||||
cleanupOnFail()
|
||||
return "", "", false, errors.New("unable to register device ID with server: " + err.Error())
|
||||
}
|
||||
var registerResp synccommon.RegisterRespT
|
||||
if err = json.Unmarshal(output, ®isterResp); err != nil {
|
||||
cleanupOnFail()
|
||||
return "", "", false, errors.New("unable to unmarshal server register response: " + err.Error())
|
||||
}
|
||||
if registerResp.ErrMsg != nil {
|
||||
cleanupOnFail()
|
||||
return "", "", false, errors.New("unable to complete register; server-side error occurred: " + strings.ReplaceAll(*registerResp.ErrMsg, global.FSSpace, "\n"))
|
||||
}
|
||||
_ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it
|
||||
|
||||
// remove old device ID file (locally; may not exist)
|
||||
if oldDeviceID != nil {
|
||||
if err = os.RemoveAll(oldDeviceIDPath); err != nil {
|
||||
cleanupOnFail()
|
||||
return "", "", false, errors.New("unable to remove old device ID file (locally): " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return registerResp.EntryRoot, registerResp.AgeDir, registerResp.IsWindows, nil
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
package synccommon
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
)
|
||||
|
||||
// ANSI color constants used only in this file
|
||||
const (
|
||||
AnsiDelete = "\033[38;5;1m"
|
||||
)
|
||||
|
||||
// FetchRespT defines the structure of responses from `libmuttonserver fetch`.
|
||||
type FetchRespT struct {
|
||||
ErrMsg *string `json:"errMsg"` // nil if no error occurred
|
||||
ServerTime int64 `json:"serverTime"`
|
||||
Deletions []Deletion `json:"deletions"`
|
||||
Entries EntryMapT `json:"entries"`
|
||||
}
|
||||
type Deletion struct {
|
||||
VanityPath string `json:"vanityPath"`
|
||||
IsAgeFile bool `json:"isAgeFile"`
|
||||
}
|
||||
type EntryMapT map[string]EntryT // map vanity paths to containing folders and mod+age timestamps
|
||||
type EntryT struct {
|
||||
ContainingFolder string `json:"containingFolder"`
|
||||
ModTime int64 `json:"modTime"`
|
||||
AgeTimestamp *int64 `json:"ageTimestamp"` // nil if no age file is present (non-password entry)
|
||||
}
|
||||
|
||||
// RegisterRespT defines the structure of responses from `libmuttonserver register`
|
||||
type RegisterRespT struct {
|
||||
ErrMsg *string `json:"errMsg"` // nil if no error occurred
|
||||
EntryRoot string `json:"entryRoot"`
|
||||
AgeDir string `json:"ageDir"`
|
||||
IsWindows bool `json:"isWindows"`
|
||||
}
|
||||
|
||||
type RegisterReqT struct {
|
||||
NewDeviceID string `json:"newDeviceID"`
|
||||
OldDeviceID *string `json:"oldDeviceID"` // nil if not replacing an existing device ID
|
||||
}
|
||||
|
||||
// GetAllEntryData returns a map of all vanity paths to
|
||||
// their respective containing folders and mod+age timestamps.
|
||||
func GetAllEntryData() (EntryMapT, error) {
|
||||
var err error
|
||||
entryList, _, err := WalkEntryDir()
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to walk entry directory: " + err.Error())
|
||||
}
|
||||
// initialize vanityPath keys in map
|
||||
outputEntries := make(EntryMapT)
|
||||
var modInfo, ageInfo os.FileInfo
|
||||
for _, vanityPath := range entryList {
|
||||
containingFolder := vanityPath[:strings.LastIndex(vanityPath, "/")]
|
||||
modInfo, err = os.Stat(global.GetRealPath(vanityPath))
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to read mod time for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
ageInfo, err = os.Stat(global.GetRealAgePath(vanityPath))
|
||||
var ageTimestamp *int64
|
||||
if err == nil {
|
||||
ageTimestamp = new(ageInfo.ModTime().Unix())
|
||||
} else if !os.IsNotExist(err) {
|
||||
return nil, errors.New("unable to read age time for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
outputEntries[vanityPath] = EntryT{ContainingFolder: containingFolder, ModTime: modInfo.ModTime().Unix(), AgeTimestamp: ageTimestamp}
|
||||
}
|
||||
return outputEntries, nil
|
||||
}
|
||||
|
||||
// ShearLocal removes the target file or directory from the local system.
|
||||
// Returns: deviceID (only on client; for use in ShearRemoteFromClient),
|
||||
// isDir (only on client; for use in ShearRemoteFromClient).
|
||||
// If the local system is a server, it will also add the target to the deletions list for all clients (except the requesting client).
|
||||
// This function should only be used directly by the server binary.
|
||||
func ShearLocal(vanityPath, clientDeviceID string, onlyShearAgeFile bool) (string, bool, error) {
|
||||
// determine if running on a server
|
||||
var onServer bool
|
||||
if clientDeviceID != "" {
|
||||
onServer = true
|
||||
}
|
||||
|
||||
deviceIDList, err := global.GenDeviceIDList()
|
||||
if err != nil {
|
||||
return "", false, errors.New("unable to generate device ID list: " + err.Error())
|
||||
}
|
||||
|
||||
// add the sheared vanityPath to the deletions list (if running on a server)
|
||||
if onServer {
|
||||
for i := range deviceIDList {
|
||||
if deviceIDList[i].Name() != clientDeviceID {
|
||||
if !onlyShearAgeFile {
|
||||
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+deviceIDList[i].Name()+global.FSSpace+"entry"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
// failure to add the target to the deletions list will exit the program and result in a client re-uploading the target (non-critical)
|
||||
return "", false, err
|
||||
}
|
||||
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
}
|
||||
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+deviceIDList[i].Name()+global.FSSpace+"age"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
// failure to add the target to the deletions list will exit the program and result in a client re-uploading the target (non-critical)
|
||||
return "", false, err
|
||||
}
|
||||
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove the target locally
|
||||
realPath := global.GetRealPath(vanityPath)
|
||||
var isFile bool
|
||||
if !onServer { // error if target does not exist on client, needed because os.RemoveAll does not return an error if target does not exist
|
||||
isAccessible, err := back.TargetIsFile(realPath, true)
|
||||
if !isAccessible {
|
||||
return "", false, err
|
||||
}
|
||||
if err == nil { // fails if target is a directory, so no error indicates a file
|
||||
isFile = true
|
||||
}
|
||||
}
|
||||
if !onlyShearAgeFile {
|
||||
if err = os.RemoveAll(realPath); err != nil {
|
||||
return "", false, errors.New("unable to remove local entry (" + vanityPath + "): " + err.Error())
|
||||
}
|
||||
}
|
||||
if err = ShearAgeFileLocal(vanityPath); err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
if !onServer && len(deviceIDList) > 0 { // return the device ID if running on the client and a device ID exists (online mode)
|
||||
return (deviceIDList)[0].Name(), !isFile, nil
|
||||
}
|
||||
return "", true, nil
|
||||
|
||||
// do not exit program, as this function is used as part of ShearRemoteFromClient
|
||||
}
|
||||
|
||||
// ShearAgeFileLocal removes the age file for a vanity path.
|
||||
// This function should only be used directly by the server binary.
|
||||
func ShearAgeFileLocal(vanityPath string) error {
|
||||
if err := os.RemoveAll(global.AgeDir + global.PathSeparator + strings.ReplaceAll(vanityPath, "/", global.FSPath)); err != nil {
|
||||
return errors.New("unable to remove age file for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenameLocal renames oldLocationIncomplete to newLocationIncomplete on the local system.
|
||||
// This function should only be used directly by the server binary.
|
||||
func RenameLocal(oldVanityPath, newVanityPath string) error {
|
||||
// get full paths for both locations
|
||||
oldRealPath := global.GetRealPath(oldVanityPath)
|
||||
oldRealAgePath := global.GetRealAgePath(oldVanityPath)
|
||||
newRealPath := global.GetRealPath(newVanityPath)
|
||||
newRealAgePath := global.GetRealAgePath(newVanityPath)
|
||||
|
||||
// ensure newLocation does not exist
|
||||
isAccessible, _ := back.TargetIsFile(newRealPath, true) // error is ignored because dir/file status is irrelevant
|
||||
if isAccessible {
|
||||
return errors.New("new target (" + newRealPath + ") already exists")
|
||||
}
|
||||
|
||||
// rename oldLocation to newLocation
|
||||
if err := os.Rename(oldRealPath, newRealPath); err != nil {
|
||||
return errors.New("unable to rename: " + err.Error())
|
||||
}
|
||||
|
||||
// do the same for the age file (if one exists) - also back up timestamp first
|
||||
fileInfo, err := os.Stat(oldRealAgePath)
|
||||
if err == nil { // assume age file does not exist if os.Stat errors
|
||||
if err = os.Rename(oldRealAgePath, newRealAgePath); err != nil {
|
||||
return errors.New("unable to rename: " + err.Error())
|
||||
}
|
||||
if err = os.Chtimes(newRealAgePath, time.Now(), fileInfo.ModTime()); err != nil {
|
||||
return errors.New("unable to set timestamp on age file for " + newVanityPath + ": " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
// do not exit program, as this function is used as part of RenameRemoteFromClient
|
||||
}
|
||||
|
||||
// AddFolderLocal creates a new entry-containing directory on the local system.
|
||||
// This function should only be used directly by the server binary.
|
||||
func AddFolderLocal(vanityPath string) error {
|
||||
// create the target locally
|
||||
realPath := global.GetRealPath(vanityPath)
|
||||
if err := os.Mkdir(realPath, 0700); err != nil {
|
||||
if os.IsExist(err) {
|
||||
fmt.Println(back.AnsiBlue + "Directory already exists - libmutton will still ensure it exists on the server")
|
||||
} else {
|
||||
return errors.New("unable to create directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
// do not exit program, as this function is used as part of AddFolderRemoteFromClient
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package synccommon
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
)
|
||||
|
||||
// WalkEntryDir walks the entry directory and returns lists of all files and
|
||||
// directories found (two separate lists) relative to the libmutton entry root.
|
||||
// Regardless of platform, all paths are stored with forward slashes (UNIX-style).
|
||||
func WalkEntryDir() ([]string, []string, error) {
|
||||
// define file/directory containing slices so that they may be accessed by the anonymous WalkDir function
|
||||
var fileList, dirList []string
|
||||
|
||||
// walk entry directory
|
||||
err := filepath.WalkDir(global.EntryRoot,
|
||||
func(realPath string, entry fs.DirEntry, err error) error {
|
||||
|
||||
// check for errors encountered while walking directory
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return errors.New("entry directory does not exist; initialize libmutton to create it")
|
||||
}
|
||||
return errors.New("an unexpected error occurred while generating the entry list: " + err.Error())
|
||||
}
|
||||
|
||||
// trim root path from each path before storing and replace backslashes with forward slashes
|
||||
vanityPath := global.GetVanityPath(realPath)
|
||||
|
||||
// append the path to the appropriate slice
|
||||
if !entry.IsDir() {
|
||||
fileList = append(fileList, vanityPath)
|
||||
} else {
|
||||
dirList = append(dirList, vanityPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return fileList, dirList, nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package syncserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
"github.com/rwinkhart/libmutton/synccommon"
|
||||
)
|
||||
|
||||
// GetRemoteDataFromServer prints to stdout the remote entries, mod times, folders, and deletions.
|
||||
// Output is meant to be captured over SSH for interpretation by the client.
|
||||
func GetRemoteDataFromServer(clientDeviceID string) {
|
||||
// collect info
|
||||
entryMap, err := synccommon.GetAllEntryData()
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
deletionsList, err := os.ReadDir(global.CfgDir + global.PathSeparator + "deletions")
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// form response
|
||||
var fetchResp synccommon.FetchRespT
|
||||
//// server time
|
||||
fetchResp.ServerTime = time.Now().Unix()
|
||||
//// deletions
|
||||
for i := range deletionsList {
|
||||
// perform deletion if it is relevant to the current client device
|
||||
affectedIDVanityPath := strings.Split(deletionsList[i].Name(), global.FSSpace)
|
||||
if affectedIDVanityPath[0] == clientDeviceID {
|
||||
var isAgeFile bool
|
||||
if affectedIDVanityPath[1] == "age" {
|
||||
isAgeFile = true
|
||||
}
|
||||
fetchResp.Deletions = append(fetchResp.Deletions, synccommon.Deletion{VanityPath: strings.ReplaceAll(affectedIDVanityPath[2], global.FSPath, "/"), IsAgeFile: isAgeFile})
|
||||
|
||||
// assume successful client deletion and remove deletions file (if assumption is somehow false, worst case scenario is that the client will re-upload the deleted entry)
|
||||
if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator + deletionsList[i].Name()); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
//// entries
|
||||
fetchResp.Entries = entryMap
|
||||
|
||||
// marshal and print response to send to client
|
||||
fetchRespBytes, err := json.Marshal(fetchResp)
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Print(string(fetchRespBytes))
|
||||
}
|
||||
+1
-9
@@ -1,10 +1,2 @@
|
||||
## Planned Breaking Changes
|
||||
Leading up to the v1.0.0 release, breaking changes are both expected and planned. These changes are expected to require manual intervention from the end-user, and thus MUTN/libmutton should not be used prior to v1.0.0 if this is not acceptable.
|
||||
|
||||
These changes include, but may expand beyond the following:
|
||||
|
||||
- Migration to Go-native encryption (no reliance on GnuPG)
|
||||
- Will be based on symmetrical encryption
|
||||
- Will eventually allow combining multiple common encryption algorithms (cascading encryption)
|
||||
- Password aging data will be stored for each entry (to remind the user when it is time to change passwords)
|
||||
- Will be included in entry names or in external file (to prevent needing to decrypt entries to access this information)
|
||||
Leading up to the v1.0.0 release, breaking changes are both expected and planned. These changes are expected to require manual intervention from the end-user, and thus MUTN/libmutton should not be used prior to v1.0.0 if this is not acceptable.
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
## Known Bugs - libmutton
|
||||
- On Windows, GPG is sometimes (seems unpredictable) incredibly slow to start (often after a reboot), leading to many operations seemingly hanging
|
||||
- **This will be addressed** in the migration off of GPG that will take place before v1.0.0
|
||||
- Password-protected SSH identity files currently only prompt for password entry in the CLI, and thus they are not yet supported in GUI/TUI implementations
|
||||
@@ -6,6 +6,3 @@ The intended way for libmutton to interact with the system clipboard is for it t
|
||||
Clipboard managers save a history of what has been copied to the clipboard, which is already a big enough issue on its own for people who copy sensitive information to their clipboard. Some **clipboard managers simply will not allow the clipboard to be empty** and will replace its contents with the last copied item if you attempt to clear it. One such naughty clipboard manager is **KDE Klipper**, which comes **packaged into KDE Plasma** and is typically **enabled by default** on most distributions. Due to this behavior, **KDE Klipper breaks libmutton's clipboard clearing functionality** and should not be left enabled.
|
||||
|
||||
It is likely other popular clipboard managers exhibit this behavior. I noticed it with KDE Klipper, which is what prompted me to create this wiki page. **Clipboard managers should not be enabled by default in any environment** or distribution due to their **potential security implications**.
|
||||
### Termux cannot clear the clipboard from the background
|
||||
***
|
||||
If using libmutton on Termux (Android), the clipboard may not successfully be cleared after the 30 second timeout period if Termux is not actively in the foreground when the sleep timer expires. This is an unfortunate side-effect of running on Android and cannot be easily fixed. Due to Termux being at the bottom of the platform support priority list, I will not be investing time into working around this.
|
||||
+27
-30
@@ -3,47 +3,44 @@
|
||||
|
||||
libmutton was designed to be usable as a library for building other compatible password managers off of. [MUTN](https://github.com/rwinkhart/MUTN) is the official reference CLI password manager, however libmutton can be implemented in many other unique ways.
|
||||
|
||||
All functionality in the `core` and `sync` packages are designed to be used in other implementations.
|
||||
|
||||
If any functionality in these two packages proves to be difficult to implement in a third-party client, please open an issue so that it can be addressed.
|
||||
## Dependency Replacements (IMPORTANT)
|
||||
In libmutton's `go.mod`, you'll see dependency replacements for `golang.org/x/sys` and `github.com/Microsoft/go-winio`. Full support for Windows and FreeBSD requires these patched modules, so be sure to maintain these same replacements in your client's `go.mod`!
|
||||
|
||||
## Build Tags
|
||||
Custom build tags can (and sometimes must) be used to achieve desired results.
|
||||
|
||||
These are as follows:
|
||||
- `returnOnExit`: If making an interactive interface (GUI/TUI/interactive CLI), you probably need to use this build tag. Without it, your entire program will exit after any given operation is completed. This behavior is only desired for non-interactive CLI implementations, such as MUTN. Currently, errors will result in the program exiting **even with this build tag**. This may be changed in the future (under evaluation).
|
||||
- `interactive`: If making an interactive interface (GUI/TUI/interactive CLI), you probably need to use this build tag. Without it, your entire program will exit after any given operation is completed. This behavior is only desired for non-interactive CLI implementations, such as MUTN.
|
||||
- `wsl`: Allows creating a Linux binary that can interact with the Windows clipboard (for WSL)
|
||||
- `termux`: Allows creating a Linux binary that can interact with the Termux clipboard (for Android)
|
||||
|
||||
## Required Global Variable Manipulation
|
||||
- `global.GetPassword` must be set to allow for different types of clients (CLI, GUI, TUI) to prompt for the password in the most appropriate way.
|
||||
- `crypt.RetryPassword`, true by default, determines whether the crypt package should verify user-typed passwords and re-prompt if needed. Turn this off to handle this uniquely in the client.
|
||||
|
||||
## Required Arguments
|
||||
libmutton-based password manager clients should accept at least one specific required argument, as well as another recommended one:
|
||||
- `clipclear`: This argument is required for correct functionality. In order to clear the clipboard on a timer, libmutton-based password managers call another instance of their executable with the `clipclear` argument (e.g. `mutn clipclear`) with the intended clipboard contents provided via STDIN. If after 30 seconds the clipboard contents have not changed, they are cleared. Please accept a `clipclear` argument that is processed before the launch of any interactive interface. All this argument needs to do is call `core.ClipClearArgument()`.
|
||||
- `init`: This argument is optional, but recommended for CLI interfaces. Some error messages request the user to use the `init` argument to fix configuration issues. If the argument does not exist, this may confuse the user.
|
||||
- `clipclear`: Should be accepted by all non-interactive CLI libmutton implementations (not required for interactive GUI/TUI implementations). In order to clear the clipboard on a timer, non-interactive libmutton-based password managers call another instance of their executable with the `clipclear` argument (e.g. `mutn clipclear`) with the intended clipboard contents provided via STDIN. If after 30 seconds the clipboard contents have not changed, they are cleared. Please accept a `clipclear` argument that calls `clip.ClearArgument()`.
|
||||
- `startrcwd`: Should be accepted by all libmutton implementations making use of the RCW daemon to cache passwords. Please accept a `startrcwd` argument that calls `crypt.RCWDArgument()`.
|
||||
|
||||
## Mobile Clipboard Management
|
||||
In an effort to reduce dependencies not needed in most environments, libmutton no longer provides clipboard management for mobile platforms. This should be handled by your GUI toolkit/framework.
|
||||
|
||||
## Configuration
|
||||
libmutton-based password manager clients should all share the same INI configuration file.
|
||||
libmutton-based password manager clients should all share the same JSON configuration file.
|
||||
|
||||
On UNIX-like systems, this is located at `~/.config/libmutton/libmutton.ini`. On Windows, it is located at `~\AppData\Local\libmutton\config\libmutton.ini`.
|
||||
On UNIX-like systems, this is located at `~/.config/libmutton/libmuttoncfg.json`. On Windows, it is located at `~\AppData\Local\libmutton\config\libmuttoncfg.json`.
|
||||
|
||||
If creating a third-party client that requires extra configuration to be stored, please use the `ClientSpecific` map in the `config.CfgT` type (as used by `config.Write()`) to save your application-specific configuration.
|
||||
|
||||
### Base `libmutton.ini` Layout
|
||||
The current base layout of `libmutton.ini` will change leading up to release v1.0.0. As of right now, the specification is as follows:
|
||||
```
|
||||
[LIBMUTTON]
|
||||
gpgID = <gpg key id>
|
||||
sshUser = <remote user>
|
||||
sshIP = <remote ip>
|
||||
sshPort = <remote ssh port>
|
||||
sshKey = <ssh private key identity file path>
|
||||
sshKeyProtected = <true/false>
|
||||
sshEntryRoot = <remote entry root>
|
||||
sshIsWindows = <true/false>
|
||||
```
|
||||
If creating a third-party client that requires extra configuration to be stored, please use the same file and create a new INI section for your application-specific configuration, e.g.:
|
||||
```
|
||||
[THIRD-PARTY-CLIENT-NAME]
|
||||
configKey = <value>
|
||||
```
|
||||
This ensures that a user can use multiple client applications with the same configuration while avoiding conflicts.
|
||||
|
||||
# Relevant Bugs Affecting Third-Party Client Implementations
|
||||
- Password-protected SSH identity files currently only prompt for password entry in the CLI, and thus they are not yet supported in GUI/TUI implementations
|
||||
## Entry Format
|
||||
A decrypted libmutton entry is a plaintext file where each line indicates a new field in the entry.
|
||||
|
||||
These fields are as follows:
|
||||
```
|
||||
0/first line: password
|
||||
1/second line: username
|
||||
2/third line: TOTP secret
|
||||
3/fourth line: URL
|
||||
4+/fifth line+: notes
|
||||
```
|
||||
|
||||
+3
-4
@@ -1,8 +1,7 @@
|
||||
## Migrating From Other Password Managers
|
||||
**Important Notice**: The libmutton entry format is not final and has two [breaking changes planned prior to release v1.0.0](https://github.com/rwinkhart/libmutton/blob/main/wiki/breaking.md). This guide will be updated accordingly.
|
||||
### pass
|
||||
libmutton-based password managers *currently* use GnuPG encryption and an entry format similar to that of [pass](https://www.passwordstore.org/). Because of this, any entries in `pass` format can simply be dropped into `~/.local/share/libmutton`.
|
||||
Current libmutton-based password managers use a custom, embedded cryptography agent ([RCW](https://github.com/rwinkhart/rcw)). GPG-encrypted entries (from `pass` or libmutton releases prior to v0.4.0) can simply be decrypted and re-encrypted with RCW. A [conversion program](https://github.com/rwinkhart/sshyp-labs/releases/tag/v2.0.0) has been published for this purpose.
|
||||
### sshyp
|
||||
sshyp, though also `pass`-compatible, makes some changes to the entry format that take effect once the entry has been imported. The changes made by sshyp are not compatible with libmutton, and as such sshyp entries must be converted before they can be used. A script for doing that has been created and is available in the sshyp extension store. Simply run `sshyp tweak`, go to the "extension management" menu, and download the "export-to-libmutton" extension. After doing this, the `sshyp export` command can be used to export entries in libmutton format.
|
||||
`sshyp`, though also `pass`-compatible, makes some changes to the entry format that take effect once the entry has been imported. The changes made by `sshyp` are not compatible with libmutton, and as such `sshyp` entries must be converted before they can be used. A script for exporting to libmutton (prior to v0.4.0) has been published to the `sshyp` extension store. Simply run `sshyp tweak`, go to the "extension management" menu, and download the "export-to-libmutton" extension. After doing this, the `sshyp export` command can be used to export entries in libmutton v0.3.X format. If migrating into libmutton v0.4.0 or later, a second [conversion program](https://github.com/rwinkhart/sshyp-labs/releases/tag/v2.0.0) will be needed to re-encrypt the exported `sshyp` entries.
|
||||
### Other
|
||||
The formats for many other password managers can be converted to the `pass` format with community scripts. Some of these scripts are listed [here](https://www.passwordstore.org/#migration). Once converted, entries can be dropped into `~/.local/share/libmutton`.
|
||||
The formats for many other password managers can be converted to the `pass` format with community scripts. Some of these scripts are listed [here](https://www.passwordstore.org/#migration). Once converted, entries can be dropped into `~/.local/share/libmutton`. If migrating into libmutton v0.4.0 or later, a second [conversion program](https://github.com/rwinkhart/sshyp-labs/releases/tag/v2.0.0) will be needed to re-encrypt the converted entries.
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
## Miscellaneous Tips
|
||||
### TOTP Support
|
||||
#### Standard 6-digit TOTP
|
||||
For standard TOTP, all the user needs to do is add their TOTP secret (NOT the full URL) string as the value for the TOTP field in an entry.
|
||||
#### Steam TOTP
|
||||
For Steam TOTP, the user must first extract their TOTP secret from the Steam app for Android.
|
||||
|
||||
There are several methods for doing this, but the most consistent I have found is [detailed here](https://github.com/JustArchiNET/ArchiSteamFarm/discussions/2786). Note that a rooted phone is not required if using a ROM like LineageOS that offers rooted ADB (must be enabled in developer options and adb must be started with `adb -d root`).
|
||||
|
||||
This method will result in a Base64-encoded Steam TOTP secret. libmutton requires a base32-encoded secret, so this secret must be converted as follows (on Linux/FreeBSD/Mac): `printf '<shared_secret>' | base64 -d | base32`
|
||||
|
||||
To signal to libmutton that this TOTP secret is for Steam, prepend it with "steam@" when adding it to the TOTP field in an entry, e.g. "steam@bAsE32sEcReTkEy". This will tell libmutton to use the Steam-specific TOTP encoder.
|
||||
Reference in New Issue
Block a user