Switch config format from ini to json (drop ini dependency since json is now used for syncing)

This commit is contained in:
2025-12-17 00:36:20 -05:00
parent e2d1b8dc09
commit b591e2ef5a
11 changed files with 138 additions and 199 deletions
+16 -44
View File
@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
@@ -26,70 +25,43 @@ import (
// sshEntryRoot (the root directory for entries on the remote server),
// sshAgeDir (the directory housing age files 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) {
func GetSSHClient() (*ssh.Client, bool, *bool, *string, *string, error) {
// get SSH config info
sshUserConfig, err := cfg.ParseConfig([][2]string{{"LIBMUTTON", "offlineMode"}, {"LIBMUTTON", "sshUser"}, {"LIBMUTTON", "sshIP"}, {"LIBMUTTON", "sshPort"}, {"LIBMUTTON", "sshKey"}, {"LIBMUTTON", "sshKeyProtected"}, {"LIBMUTTON", "sshEntryRoot"}, {"LIBMUTTON", "sshAgeDir"}, {"LIBMUTTON", "sshIsWindows"}})
if len(sshUserConfig) == 1 {
// offline mode is enabled
return nil, true, false, "", "", nil
}
cfg, err := cfg.LoadConfig()
if err != nil {
return nil, false, false, "", "", errors.New("unable to parse SSH config: " + err.Error())
return nil, false, nil, nil, nil, errors.New("unable to parse SSH config: " + err.Error())
}
var user, ip, port, keyFile, keyFileProtected, entryRoot, ageDir string
var isWindows bool
for i, key := range sshUserConfig {
switch i {
case 1:
user = key
case 2:
ip = key
case 3:
port = key
case 4:
keyFile = key
case 5:
keyFileProtected = key
case 6:
entryRoot = key
case 7:
ageDir = key
case 8:
isWindows, err = strconv.ParseBool(key)
if err != nil {
return nil, false, false, "", "", errors.New("unable to parse server OS type: " + err.Error())
}
}
if *cfg.Libmutton.OfflineMode {
return nil, true, nil, nil, nil, nil
}
// read private key
key, err := os.ReadFile(keyFile)
key, err := os.ReadFile(*cfg.Libmutton.SSHKeyPath)
if err != nil {
return nil, false, false, "", "", errors.New("unable to read private key: " + keyFile)
return nil, false, nil, nil, nil, errors.New("unable to read private key: " + *cfg.Libmutton.SSHKeyPath)
}
// parse private key
var parsedKey ssh.Signer
if keyFileProtected != "true" {
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, false, "", "", errors.New("unable to parse private key: " + keyFile)
return nil, false, nil, nil, nil, errors.New("unable to parse private key: " + *cfg.Libmutton.SSHKeyPath)
}
// read known hosts file
var hostKeyCallback ssh.HostKeyCallback
hostKeyCallback, err = knownhosts.New(back.Home + global.PathSeparator + ".ssh" + global.PathSeparator + "known_hosts")
if err != nil {
return nil, false, false, "", "", errors.New("unable to read known hosts file: " + err.Error())
return nil, false, nil, nil, nil, errors.New("unable to read known hosts file: " + err.Error())
}
// configure SSH client
sshConfig := &ssh.ClientConfig{
User: user,
User: *cfg.Libmutton.SSHUser,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(parsedKey),
},
@@ -98,12 +70,12 @@ func GetSSHClient() (*ssh.Client, bool, bool, string, string, error) {
}
// connect to SSH server
sshClient, err := ssh.Dial("tcp", ip+":"+port, sshConfig)
sshClient, err := ssh.Dial("tcp", *cfg.Libmutton.SSHIP+":"+*cfg.Libmutton.SSHPort, sshConfig)
if err != nil {
return nil, false, false, "", "", errors.New("unable to connect to remote server: " + err.Error())
return nil, false, nil, nil, nil, errors.New("unable to connect to remote server: " + err.Error())
}
return sshClient, false, isWindows, entryRoot, ageDir, nil
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.
@@ -546,7 +518,7 @@ func RunJob(returnLists bool) ([3][]string, error) {
// sync new and updated entries
var lists [3][]string
if returnLists {
lists, err = syncLists(sshClient, sshEntryRoot, sshAgeDir, sshIsWindows, timeSynced, true, localEntryModMap, remoteEntryModMap, localAgeTimestampMap, remoteAgeTimestampMap)
lists, err = syncLists(sshClient, *sshEntryRoot, *sshAgeDir, *sshIsWindows, timeSynced, true, localEntryModMap, remoteEntryModMap, localAgeTimestampMap, remoteAgeTimestampMap)
if err != nil {
return [3][]string{nil, nil, nil}, errors.New("unable to sync entries: " + err.Error())
}
@@ -558,7 +530,7 @@ func RunJob(returnLists bool) ([3][]string, error) {
}
return lists, nil
}
_, err = syncLists(sshClient, sshEntryRoot, sshAgeDir, sshIsWindows, timeSynced, false, localEntryModMap, remoteEntryModMap, localAgeTimestampMap, remoteAgeTimestampMap)
_, err = syncLists(sshClient, *sshEntryRoot, *sshAgeDir, *sshIsWindows, timeSynced, false, localEntryModMap, remoteEntryModMap, localAgeTimestampMap, remoteAgeTimestampMap)
if err != nil {
return [3][]string{nil, nil, nil}, errors.New("unable to sync entries: " + err.Error())
}
+8 -8
View File
@@ -167,7 +167,7 @@ end:
// 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, prefix string) (string, string, string, error) {
func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) {
// generate new device ID
if prefix == "" {
prefix, _ = os.Hostname()
@@ -179,7 +179,7 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, string, error) {
oldDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID
f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return "", "", "", errors.New("unable to create local device ID file: " + err.Error())
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
@@ -201,22 +201,22 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, string, error) {
sshClient, _, _, _, _, err := GetSSHClient()
if err != nil {
cleanupOnFail()
return "", "", "", errors.New("unable to connect to SSH client: " + err.Error())
return "", "", false, errors.New("unable to connect to SSH client: " + err.Error())
}
output, err := GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID)
if err != nil {
cleanupOnFail()
return "", "", "", errors.New("unable to register device ID with server: " + err.Error())
return "", "", false, errors.New("unable to register device ID with server: " + err.Error())
}
var registerResp synccommon.RegisterResp
err = json.Unmarshal(output, &registerResp)
if err != nil {
cleanupOnFail()
return "", "", "", errors.New("unable to unmarshal server register response: " + err.Error())
return "", "", false, errors.New("unable to unmarshal server register response: " + err.Error())
}
if registerResp.ErrMsg != nil {
cleanupOnFail()
return "", "", "", errors.New("unable to complete register; server-side error occurred: " + strings.ReplaceAll(*registerResp.ErrMsg, global.FSSpace, "\n"))
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
@@ -224,8 +224,8 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, string, error) {
err = os.RemoveAll(oldDeviceIDPath)
if err != nil {
cleanupOnFail()
return "", "", "", errors.New("unable to remove old device ID file (locally): " + err.Error())
return "", "", false, errors.New("unable to remove old device ID file (locally): " + err.Error())
}
return registerResp.EntryRoot, registerResp.AgeDir, strconv.FormatBool(registerResp.IsWindows), nil
return registerResp.EntryRoot, registerResp.AgeDir, registerResp.IsWindows, nil
}