mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-27 20:36:29 -04:00
Rename "cfg" package to "config", always use "cfg" for variable names
This commit is contained in:
+1
-2
@@ -54,7 +54,6 @@ func isWayland() (bool, error) {
|
|||||||
return true, nil
|
return true, nil
|
||||||
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
||||||
return false, nil
|
return false, nil
|
||||||
} else {
|
|
||||||
return false, errors.New("unable to detect Wayland or X11 session")
|
|
||||||
}
|
}
|
||||||
|
return false, errors.New("unable to detect Wayland or X11 session")
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-10
@@ -1,4 +1,4 @@
|
|||||||
package cfg
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/rwinkhart/libmutton/global"
|
"github.com/rwinkhart/libmutton/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigT struct {
|
type CfgT struct {
|
||||||
Libmutton struct {
|
Libmutton struct {
|
||||||
OfflineMode *bool `json:"offlineMode"`
|
OfflineMode *bool `json:"offlineMode"`
|
||||||
SSHUser *string `json:"sshUser"`
|
SSHUser *string `json:"sshUser"`
|
||||||
@@ -24,13 +24,13 @@ type ConfigT struct {
|
|||||||
ThirdParty *map[string]any `json:"thirdParty"`
|
ThirdParty *map[string]any `json:"thirdParty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig loads libmuttoncfg.json and returns the configuration.
|
// Load loads libmuttoncfg.json and returns the configuration.
|
||||||
func LoadConfig() (*ConfigT, error) {
|
func Load() (*CfgT, error) {
|
||||||
cfgBytes, err := os.ReadFile(global.ConfigPath)
|
cfgBytes, err := os.ReadFile(global.CfgPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("unable to load libmuttoncfg.json: " + err.Error())
|
return nil, errors.New("unable to load libmuttoncfg.json: " + err.Error())
|
||||||
}
|
}
|
||||||
var cfg ConfigT
|
var cfg CfgT
|
||||||
err = json.Unmarshal(cfgBytes, &cfg)
|
err = json.Unmarshal(cfgBytes, &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("unable to unmarshal libmuttoncfg.json: " + err.Error())
|
return nil, errors.New("unable to unmarshal libmuttoncfg.json: " + err.Error())
|
||||||
@@ -38,10 +38,10 @@ func LoadConfig() (*ConfigT, error) {
|
|||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteConfig writes cfg to libmuttoncfg.json.
|
// Write writes cfg to libmuttoncfg.json.
|
||||||
// If used in append mode, any nil values in the
|
// If used in append mode, any nil values in the
|
||||||
// input cfg will be substituted with the existing values.
|
// input cfg will be substituted with the existing values.
|
||||||
func WriteConfig(cfg *ConfigT, appendMode bool) error {
|
func Write(cfg *CfgT, appendMode bool) error {
|
||||||
start:
|
start:
|
||||||
if appendMode {
|
if appendMode {
|
||||||
// check if any fields are nil
|
// check if any fields are nil
|
||||||
@@ -57,7 +57,7 @@ start:
|
|||||||
|
|
||||||
// load old cfg and copy nil fields
|
// load old cfg and copy nil fields
|
||||||
if hasNilFields {
|
if hasNilFields {
|
||||||
oldCfg, err := LoadConfig()
|
oldCfg, err := Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// failed to load config, leave append mode
|
// failed to load config, leave append mode
|
||||||
appendMode = false
|
appendMode = false
|
||||||
@@ -76,7 +76,7 @@ start:
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to marshal new/updated cfg: " + err.Error())
|
return errors.New("unable to marshal new/updated cfg: " + err.Error())
|
||||||
}
|
}
|
||||||
err = os.WriteFile(global.ConfigPath, cfgBytes, 0600)
|
err = os.WriteFile(global.CfgPath, cfgBytes, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to write new/updated cfg to libmuttoncfg.json: " + err.Error())
|
return errors.New("unable to write new/updated cfg to libmuttoncfg.json: " + err.Error())
|
||||||
}
|
}
|
||||||
+9
-9
@@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rwinkhart/go-boilerplate/back"
|
"github.com/rwinkhart/go-boilerplate/back"
|
||||||
"github.com/rwinkhart/libmutton/cfg"
|
"github.com/rwinkhart/libmutton/config"
|
||||||
"github.com/rwinkhart/libmutton/global"
|
"github.com/rwinkhart/libmutton/global"
|
||||||
"github.com/rwinkhart/libmutton/syncclient"
|
"github.com/rwinkhart/libmutton/syncclient"
|
||||||
"github.com/rwinkhart/rcw/wrappers"
|
"github.com/rwinkhart/rcw/wrappers"
|
||||||
@@ -15,9 +15,9 @@ import (
|
|||||||
|
|
||||||
// LibmuttonInit creates the libmutton config structure based on user input.
|
// LibmuttonInit creates the libmutton config structure based on user input.
|
||||||
// rcwPassword and clientSpecificIniData can be left blank if not needed.
|
// rcwPassword and clientSpecificIniData can be left blank if not needed.
|
||||||
func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map[string]any, rcwPassword []byte, preserveOldConfigDir bool, forceOfflineMode bool) error {
|
func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map[string]any, rcwPassword []byte, preserveOldCfgDir bool, forceOfflineMode bool) error {
|
||||||
// handle clientSpecificIniData
|
// handle clientSpecificIniData
|
||||||
newCfg := &cfg.ConfigT{}
|
newCfg := &config.CfgT{}
|
||||||
if clientSpecificIniData != nil {
|
if clientSpecificIniData != nil {
|
||||||
newThirdPartyMap := make(map[string]any)
|
newThirdPartyMap := make(map[string]any)
|
||||||
maps.Copy(newThirdPartyMap, clientSpecificIniData)
|
maps.Copy(newThirdPartyMap, clientSpecificIniData)
|
||||||
@@ -32,7 +32,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map
|
|||||||
}
|
}
|
||||||
if len(r) > 0 && r[0] == 'n' {
|
if len(r) > 0 && r[0] == 'n' {
|
||||||
// initialize libmutton directories
|
// initialize libmutton directories
|
||||||
_, err := global.DirInit(preserveOldConfigDir)
|
_, err := global.DirInit(preserveOldCfgDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map
|
|||||||
// write config file
|
// write config file
|
||||||
offlineMode := true
|
offlineMode := true
|
||||||
newCfg.Libmutton.OfflineMode = &offlineMode
|
newCfg.Libmutton.OfflineMode = &offlineMode
|
||||||
err = cfg.WriteConfig(newCfg, false)
|
err = config.Write(newCfg, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map
|
|||||||
|
|
||||||
// perform operations based on collected user input
|
// perform operations based on collected user input
|
||||||
//// initialize libmutton directories
|
//// initialize libmutton directories
|
||||||
oldDeviceID, err := global.DirInit(preserveOldConfigDir)
|
oldDeviceID, err := global.DirInit(preserveOldCfgDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map
|
|||||||
newCfg.Libmutton.SSHPort = &sshPort
|
newCfg.Libmutton.SSHPort = &sshPort
|
||||||
newCfg.Libmutton.SSHKeyPath = &sshKeyPath
|
newCfg.Libmutton.SSHKeyPath = &sshKeyPath
|
||||||
newCfg.Libmutton.SSHKeyProtected = &sshKeyProtected
|
newCfg.Libmutton.SSHKeyProtected = &sshKeyProtected
|
||||||
err = cfg.WriteConfig(newCfg, false)
|
err = config.Write(newCfg, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map
|
|||||||
newCfg.Libmutton.SSHEntryRootPath = &sshEntryRoot
|
newCfg.Libmutton.SSHEntryRootPath = &sshEntryRoot
|
||||||
newCfg.Libmutton.SSHAgeDirPath = &sshAgeDir
|
newCfg.Libmutton.SSHAgeDirPath = &sshAgeDir
|
||||||
newCfg.Libmutton.SSHIsWindows = &sshIsWindows
|
newCfg.Libmutton.SSHIsWindows = &sshIsWindows
|
||||||
err = cfg.WriteConfig(newCfg, true)
|
err = config.Write(newCfg, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map
|
|||||||
|
|
||||||
// RCWSanityCheckGen generates the RCW sanity check file for libmutton.
|
// RCWSanityCheckGen generates the RCW sanity check file for libmutton.
|
||||||
func RCWSanityCheckGen(password []byte) error {
|
func RCWSanityCheckGen(password []byte) error {
|
||||||
err := wrappers.GenSanityCheck(global.ConfigDir+global.PathSeparator+"sanity.rcw", password)
|
err := wrappers.GenSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to generate sanity check file: " + err.Error())
|
return errors.New("unable to generate sanity check file: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -71,7 +71,7 @@ func launchRCWDProcess() []byte {
|
|||||||
if RetryPassword {
|
if RetryPassword {
|
||||||
for {
|
for {
|
||||||
password = global.GetPassword("RCW Password:")
|
password = global.GetPassword("RCW Password:")
|
||||||
err := wrappers.RunSanityCheck(global.ConfigDir+global.PathSeparator+"sanity.rcw", password)
|
err := wrappers.RunSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ package global
|
|||||||
import "github.com/rwinkhart/go-boilerplate/back"
|
import "github.com/rwinkhart/go-boilerplate/back"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
EntryRoot = back.Home + "/.local/share/libmutton" // Path to libmutton entry directory
|
EntryRoot = back.Home + "/.local/share/libmutton" // Path to libmutton entry directory
|
||||||
ConfigDir = back.Home + "/.config/libmutton" // Path to libmutton configuration directory
|
CfgDir = back.Home + "/.config/libmutton" // Path to libmutton configuration directory
|
||||||
ConfigPath = ConfigDir + "/libmuttoncfg.json" // Path to libmutton configuration file
|
CfgPath = CfgDir + "/libmuttoncfg.json" // Path to libmutton configuration file
|
||||||
AgeDir = ConfigDir + "/age" // Path to libmutton password age directory
|
AgeDir = CfgDir + "/age" // Path to libmutton password age directory
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ package global
|
|||||||
import "github.com/rwinkhart/go-boilerplate/back"
|
import "github.com/rwinkhart/go-boilerplate/back"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
EntryRoot = back.Home + "\\AppData\\Local\\libmutton\\entries" // Path to libmutton entry directory
|
EntryRoot = back.Home + "\\AppData\\Local\\libmutton\\entries" // Path to libmutton entry directory
|
||||||
ConfigDir = back.Home + "\\AppData\\Local\\libmutton\\config" // Path to libmutton configuration directory
|
CfgDir = back.Home + "\\AppData\\Local\\libmutton\\config" // Path to libmutton configuration directory
|
||||||
ConfigPath = ConfigDir + "\\libmuttoncfg.json" // Path to libmutton configuration file
|
CfgPath = CfgDir + "\\libmuttoncfg.json" // Path to libmutton configuration file
|
||||||
AgeDir = ConfigDir + "\\age" // Path to libmutton password age directory
|
AgeDir = CfgDir + "\\age" // Path to libmutton password age directory
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ func GetCurrentDeviceID() (string, error) {
|
|||||||
// Requires: errorOnFail (set to true to throw an error if the devices directory cannot be read/does not exist)
|
// Requires: errorOnFail (set to true to throw an error if the devices directory cannot be read/does not exist)
|
||||||
func GenDeviceIDList() ([]fs.DirEntry, error) {
|
func GenDeviceIDList() ([]fs.DirEntry, error) {
|
||||||
// create a slice of all registered devices
|
// create a slice of all registered devices
|
||||||
deviceIDList, err := os.ReadDir(ConfigDir + PathSeparator + "devices")
|
deviceIDList, err := os.ReadDir(CfgDir + PathSeparator + "devices")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("unable to read devices directory: " + err.Error())
|
return nil, errors.New("unable to read devices directory: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
// DirInit creates the libmutton directories.
|
// DirInit creates the libmutton directories.
|
||||||
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID).
|
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID).
|
||||||
func DirInit(preserveOldConfigDir bool) (string, error) {
|
func DirInit(preserveOldCfgDir bool) (string, error) {
|
||||||
// create EntryRoot
|
// create EntryRoot
|
||||||
err := os.MkdirAll(EntryRoot, 0700)
|
err := os.MkdirAll(EntryRoot, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -23,10 +23,10 @@ func DirInit(preserveOldConfigDir bool) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// remove existing config directory (if it exists and not in append mode)
|
// remove existing config directory (if it exists and not in append mode)
|
||||||
if !preserveOldConfigDir {
|
if !preserveOldCfgDir {
|
||||||
isAccessible, _ := back.TargetIsFile(ConfigDir, false) // error is ignored because dir/file status is irrelevant
|
isAccessible, _ := back.TargetIsFile(CfgDir, false) // error is ignored because dir/file status is irrelevant
|
||||||
if isAccessible {
|
if isAccessible {
|
||||||
err = os.RemoveAll(ConfigDir)
|
err = os.RemoveAll(CfgDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("unable to remove existing config directory: " + err.Error())
|
return "", errors.New("unable to remove existing config directory: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -34,9 +34,9 @@ func DirInit(preserveOldConfigDir bool) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create config directory w/devices subdirectory
|
// create config directory w/devices subdirectory
|
||||||
err = os.MkdirAll(ConfigDir+PathSeparator+"devices", 0700)
|
err = os.MkdirAll(CfgDir+PathSeparator+"devices", 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("unable to create \"" + ConfigDir + "\": " + err.Error())
|
return "", errors.New("unable to create \"" + CfgDir + "\": " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// create password age directory
|
// create password age directory
|
||||||
|
|||||||
+4
-4
@@ -97,7 +97,7 @@ func main() {
|
|||||||
// register a new device ID
|
// register a new device ID
|
||||||
// stdin[0] is expected to be the device ID
|
// stdin[0] is expected to be the device ID
|
||||||
// stdin[1] is expected to be the old device ID (for removal)
|
// stdin[1] is expected to be the old device ID (for removal)
|
||||||
f, err := os.OpenFile(global.ConfigDir+global.PathSeparator+"devices"+global.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600)
|
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"devices"+global.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||||
return
|
return
|
||||||
@@ -105,13 +105,13 @@ func main() {
|
|||||||
_ = f.Close()
|
_ = f.Close()
|
||||||
if stdin[1] != global.FSMisc { // FSMisc is used to indicate that no device ID is being replaced
|
if stdin[1] != global.FSMisc { // FSMisc is used to indicate that no device ID is being replaced
|
||||||
// remove the old device ID file
|
// remove the old device ID file
|
||||||
err = os.RemoveAll(global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + stdin[1])
|
err = os.RemoveAll(global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + stdin[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// carry over deletions from the old device ID to the new one
|
// carry over deletions from the old device ID to the new one
|
||||||
deletionsDirRoot := global.ConfigDir + global.PathSeparator + "deletions" + global.PathSeparator
|
deletionsDirRoot := global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator
|
||||||
deletionsList, err := os.ReadDir(deletionsDirRoot)
|
deletionsList, err := os.ReadDir(deletionsDirRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||||
@@ -143,7 +143,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
other.PrintError("Failed to initialize libmuttonserver directories: "+err.Error(), back.ErrorWrite)
|
other.PrintError("Failed to initialize libmuttonserver directories: "+err.Error(), back.ErrorWrite)
|
||||||
}
|
}
|
||||||
_ = os.MkdirAll(global.ConfigDir+global.PathSeparator+"deletions", 0700) // error ignored; failure would have occurred by this point in core.DirInit
|
_ = 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")
|
fmt.Println("libmuttonserver directories initialized")
|
||||||
case "version":
|
case "version":
|
||||||
versionServer()
|
versionServer()
|
||||||
|
|||||||
+11
-11
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pkg/sftp"
|
"github.com/pkg/sftp"
|
||||||
"github.com/rwinkhart/go-boilerplate/back"
|
"github.com/rwinkhart/go-boilerplate/back"
|
||||||
"github.com/rwinkhart/libmutton/cfg"
|
"github.com/rwinkhart/libmutton/config"
|
||||||
"github.com/rwinkhart/libmutton/global"
|
"github.com/rwinkhart/libmutton/global"
|
||||||
"github.com/rwinkhart/libmutton/synccommon"
|
"github.com/rwinkhart/libmutton/synccommon"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
@@ -27,29 +27,29 @@ import (
|
|||||||
// Only supports key-based authentication (passwords are supported for CLI-based implementations).
|
// 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
|
// get SSH config info
|
||||||
config, err := cfg.LoadConfig()
|
cfg, err := config.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, nil, nil, nil, errors.New("unable to parse SSH config: " + err.Error())
|
return nil, false, nil, nil, nil, errors.New("unable to parse SSH config: " + err.Error())
|
||||||
}
|
}
|
||||||
if *config.Libmutton.OfflineMode {
|
if *cfg.Libmutton.OfflineMode {
|
||||||
return nil, true, nil, nil, nil, nil
|
return nil, true, nil, nil, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// read private key
|
// read private key
|
||||||
key, err := os.ReadFile(*config.Libmutton.SSHKeyPath)
|
key, err := os.ReadFile(*cfg.Libmutton.SSHKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, nil, nil, nil, errors.New("unable to read private key: " + *config.Libmutton.SSHKeyPath)
|
return nil, false, nil, nil, nil, errors.New("unable to read private key: " + *cfg.Libmutton.SSHKeyPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse private key
|
// parse private key
|
||||||
var parsedKey ssh.Signer
|
var parsedKey ssh.Signer
|
||||||
if !*config.Libmutton.SSHKeyProtected {
|
if !*cfg.Libmutton.SSHKeyProtected {
|
||||||
parsedKey, err = ssh.ParsePrivateKey(key)
|
parsedKey, err = ssh.ParsePrivateKey(key)
|
||||||
} else {
|
} else {
|
||||||
parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, global.GetPassword("Enter password for your SSH keyfile:"))
|
parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, global.GetPassword("Enter password for your SSH keyfile:"))
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, nil, nil, nil, errors.New("unable to parse private key: " + *config.Libmutton.SSHKeyPath)
|
return nil, false, nil, nil, nil, errors.New("unable to parse private key: " + *cfg.Libmutton.SSHKeyPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// read known hosts file
|
// read known hosts file
|
||||||
@@ -60,8 +60,8 @@ func GetSSHClient() (*ssh.Client, bool, *bool, *string, *string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// configure SSH client
|
// configure SSH client
|
||||||
sshConfig := &ssh.ClientConfig{
|
sshCfg := &ssh.ClientConfig{
|
||||||
User: *config.Libmutton.SSHUser,
|
User: *cfg.Libmutton.SSHUser,
|
||||||
Auth: []ssh.AuthMethod{
|
Auth: []ssh.AuthMethod{
|
||||||
ssh.PublicKeys(parsedKey),
|
ssh.PublicKeys(parsedKey),
|
||||||
},
|
},
|
||||||
@@ -70,12 +70,12 @@ func GetSSHClient() (*ssh.Client, bool, *bool, *string, *string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect to SSH server
|
// connect to SSH server
|
||||||
sshClient, err := ssh.Dial("tcp", *config.Libmutton.SSHIP+":"+*config.Libmutton.SSHPort, sshConfig)
|
sshClient, err := ssh.Dial("tcp", *cfg.Libmutton.SSHIP+":"+*cfg.Libmutton.SSHPort, sshCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, nil, nil, nil, 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, config.Libmutton.SSHIsWindows, config.Libmutton.SSHEntryRootPath, config.Libmutton.SSHAgeDirPath, 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.
|
// GetSSHOutput runs a command over SSH and returns the output as a string.
|
||||||
|
|||||||
@@ -175,8 +175,8 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) {
|
|||||||
newDeviceID := prefix + "-" + stringy.StringGen(rand.Intn(32)+48, 0.2, 1) + "-" + strconv.FormatInt(time.Now().Unix(), 10)
|
newDeviceID := prefix + "-" + stringy.StringGen(rand.Intn(32)+48, 0.2, 1) + "-" + strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
|
|
||||||
// create new device ID file (locally)
|
// create new device ID file (locally)
|
||||||
newDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID
|
newDeviceIDPath := global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID
|
||||||
oldDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID
|
oldDeviceIDPath := global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID
|
||||||
f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
|
f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", false, errors.New("unable to create local device ID file: " + err.Error())
|
return "", "", false, errors.New("unable to create local device ID file: " + err.Error())
|
||||||
|
|||||||
@@ -74,14 +74,14 @@ func ShearLocal(vanityPath, clientDeviceID string, onlyShearAgeFile bool) (strin
|
|||||||
for _, device := range deviceIDList {
|
for _, device := range deviceIDList {
|
||||||
if device.Name() != clientDeviceID {
|
if device.Name() != clientDeviceID {
|
||||||
if !onlyShearAgeFile {
|
if !onlyShearAgeFile {
|
||||||
f, err := os.OpenFile(global.ConfigDir+global.PathSeparator+"deletions"+global.PathSeparator+device.Name()+global.FSSpace+"entry"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
|
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+device.Name()+global.FSSpace+"entry"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
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)
|
// 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
|
return "", false, err
|
||||||
}
|
}
|
||||||
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||||
}
|
}
|
||||||
f, err := os.OpenFile(global.ConfigDir+global.PathSeparator+"deletions"+global.PathSeparator+device.Name()+global.FSSpace+"age"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
|
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+device.Name()+global.FSSpace+"age"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
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)
|
// 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
|
return "", false, err
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func GetRemoteDataFromServer(clientDeviceID string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
modList := synccommon.GetModTimes(entryList)
|
modList := synccommon.GetModTimes(entryList)
|
||||||
deletionsList, err := os.ReadDir(global.ConfigDir + global.PathSeparator + "deletions")
|
deletionsList, err := os.ReadDir(global.CfgDir + global.PathSeparator + "deletions")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||||
return
|
return
|
||||||
@@ -67,7 +67,7 @@ func GetRemoteDataFromServer(clientDeviceID string) {
|
|||||||
fetchResp.Deletions = append(fetchResp.Deletions, synccommon.Deletion{VanityPath: strings.ReplaceAll(affectedIDVanityPath[2], global.FSPath, "/"), IsAgeFile: isAgeFile})
|
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)
|
// 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)
|
||||||
err = os.RemoveAll(global.ConfigDir + global.PathSeparator + "deletions" + global.PathSeparator + deletion.Name()) // error ignored; function not run from a user-facing argument and thus the error would not be visible
|
err = os.RemoveAll(global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator + deletion.Name()) // error ignored; function not run from a user-facing argument and thus the error would not be visible
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user