mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-03 07:37:23 -04:00
Implement device ID server registration and server-side init
This commit is contained in:
@@ -17,11 +17,20 @@ func main() {
|
|||||||
|
|
||||||
switch args[1] {
|
switch args[1] {
|
||||||
case "fetch":
|
case "fetch":
|
||||||
|
// print all information needed for syncing to stdout for interpretation by the client
|
||||||
sync.GetRemoteDataFromServer()
|
sync.GetRemoteDataFromServer()
|
||||||
case "shear":
|
case "shear":
|
||||||
|
// shear an entry from the server and add it to the deletions directory
|
||||||
deviceIDTargetLocation := strings.Split(args[2], "\x1d")
|
deviceIDTargetLocation := strings.Split(args[2], "\x1d")
|
||||||
targetLocationIncomplete := strings.ReplaceAll(strings.ReplaceAll(deviceIDTargetLocation[1], "\x1f", " "), "\x1e", backend.PathSeparator)
|
targetLocationIncomplete := strings.ReplaceAll(strings.ReplaceAll(deviceIDTargetLocation[1], "\x1f", " "), "\x1e", backend.PathSeparator)
|
||||||
sync.Shear(targetLocationIncomplete, deviceIDTargetLocation[0])
|
sync.Shear(targetLocationIncomplete, deviceIDTargetLocation[0])
|
||||||
|
case "register":
|
||||||
|
// register a new device ID
|
||||||
|
os.Create(backend.ConfigDir + backend.PathSeparator + "devices" + backend.PathSeparator + args[2])
|
||||||
|
case "init":
|
||||||
|
// create the necessary directories for libmuttonserver to function TODO consider clearing out old directories first
|
||||||
|
backend.DirInit()
|
||||||
|
os.MkdirAll(backend.ConfigDir+backend.PathSeparator+"deletions", 0700)
|
||||||
default:
|
default:
|
||||||
fmt.Println(backend.AnsiError + "Invalid argument" + backend.AnsiReset) // TODO add server help menu
|
fmt.Println(backend.AnsiError + "Invalid argument" + backend.AnsiReset) // TODO add server help menu
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -11,7 +11,7 @@ import (
|
|||||||
// TempInit ensures libmutton directories exist and writes the libmutton configuration file
|
// TempInit ensures libmutton directories exist and writes the libmutton configuration file
|
||||||
func TempInit(configFileMap map[string]string) {
|
func TempInit(configFileMap map[string]string) {
|
||||||
// create EntryRoot and ConfigDir
|
// create EntryRoot and ConfigDir
|
||||||
dirInit()
|
DirInit()
|
||||||
|
|
||||||
// remove existing config file
|
// remove existing config file
|
||||||
removeFile(ConfigPath)
|
removeFile(ConfigPath)
|
||||||
@@ -27,8 +27,6 @@ func TempInit(configFileMap map[string]string) {
|
|||||||
for key, value := range configFileMap {
|
for key, value := range configFileMap {
|
||||||
configFile.WriteString(key + " = " + value + "\n")
|
configFile.WriteString(key + " = " + value + "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GpgUIDListGen generates a list of all GPG key IDs on the system and returns them as a slice of strings
|
// GpgUIDListGen generates a list of all GPG key IDs on the system and returns them as a slice of strings
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
const FallbackEditor = "vi" // vi is pre-installed on most UNIX systems
|
const FallbackEditor = "vi" // vi is pre-installed on most UNIX systems
|
||||||
|
|
||||||
// dirInit creates the libmutton directories
|
// DirInit creates the libmutton directories
|
||||||
func dirInit() {
|
func DirInit() {
|
||||||
// create EntryRoot
|
// create EntryRoot
|
||||||
err := os.MkdirAll(EntryRoot, 0700)
|
err := os.MkdirAll(EntryRoot, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
const FallbackEditor = "nvim" // since there is no pre-installed CLI editor on Windows, default to the most popular one
|
const FallbackEditor = "nvim" // since there is no pre-installed CLI editor on Windows, default to the most popular one
|
||||||
|
|
||||||
// dirInit creates the libmutton directories
|
// DirInit creates the libmutton directories
|
||||||
func dirInit() {
|
func DirInit() {
|
||||||
// create EntryRoot (includes config directory on Windows)
|
// create EntryRoot (includes config directory on Windows)
|
||||||
err := os.MkdirAll(EntryRoot, 0700)
|
err := os.MkdirAll(EntryRoot, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+8
-6
@@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rwinkhart/MUTN/src/backend"
|
"github.com/rwinkhart/MUTN/src/backend"
|
||||||
|
"github.com/rwinkhart/MUTN/src/sync"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -30,12 +31,6 @@ func TempInitCli() {
|
|||||||
// SSH info
|
// SSH info
|
||||||
configSSH := inputBinary("Configure SSH settings (for synchronization)?")
|
configSSH := inputBinary("Configure SSH settings (for synchronization)?")
|
||||||
if configSSH {
|
if configSSH {
|
||||||
// client device ID
|
|
||||||
deviceIDPrefix, _ := os.Hostname()
|
|
||||||
deviceIDSuffix := backend.StringGen(rand.Intn(48)+48, true, 0.2)
|
|
||||||
deviceID := deviceIDPrefix + "-" + deviceIDSuffix
|
|
||||||
os.Create(backend.ConfigDir + backend.PathSeparator + "devices" + backend.PathSeparator + deviceID) // TODO remove existing device ID file if it exists, copy device ID to server
|
|
||||||
|
|
||||||
// necessary SSH info
|
// necessary SSH info
|
||||||
sshUser := input("Remote SSH username:")
|
sshUser := input("Remote SSH username:")
|
||||||
sshIP := input("Remote SSH IP address:")
|
sshIP := input("Remote SSH IP address:")
|
||||||
@@ -44,6 +39,13 @@ func TempInitCli() {
|
|||||||
|
|
||||||
// write config file
|
// write config file
|
||||||
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshIdentity": sshIdentity})
|
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshIdentity": sshIdentity})
|
||||||
|
|
||||||
|
// client device ID (perform after writing config files, as sync.GetSSHOutput must be able to read it) TODO move to backend
|
||||||
|
deviceIDPrefix, _ := os.Hostname()
|
||||||
|
deviceIDSuffix := backend.StringGen(rand.Intn(48)+48, false, 0) // TODO consider using complex string generator and removing unsafe characters manually
|
||||||
|
deviceID := deviceIDPrefix + "-" + deviceIDSuffix
|
||||||
|
os.Create(backend.ConfigDir + backend.PathSeparator + "devices" + backend.PathSeparator + deviceID) // TODO remove existing device ID file if it exists (from both client and server)
|
||||||
|
sync.GetSSHOutput("libmuttonserver register "+deviceID, false) // register device ID with server
|
||||||
} else {
|
} else {
|
||||||
// write config file
|
// write config file
|
||||||
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID})
|
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID})
|
||||||
|
|||||||
+4
-4
@@ -17,9 +17,9 @@ const (
|
|||||||
ansiUpload = "\033[38;5;4m"
|
ansiUpload = "\033[38;5;4m"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getSSHOutput runs a command over SSH and returns the output
|
// GetSSHOutput runs a command over SSH and returns the output
|
||||||
// currently only supports password-less key-based authentication TODO add password support, still require key
|
// currently only supports password-less key-based authentication TODO add password support, still require key
|
||||||
func getSSHOutput(cmd string, manualSync bool) string {
|
func GetSSHOutput(cmd string, manualSync bool) string {
|
||||||
// get SSH config info, exit if not configured (displaying an error if the sync job was called manually)
|
// get SSH config info, exit if not configured (displaying an error if the sync job was called manually)
|
||||||
var sshUserIPPortIdentity []string
|
var sshUserIPPortIdentity []string
|
||||||
if manualSync {
|
if manualSync {
|
||||||
@@ -102,7 +102,7 @@ func getSSHOutput(cmd string, manualSync bool) string {
|
|||||||
// getRemoteDataFromClient returns a map of remote entries to their modification times, a list of remote folders, and a list of queued deletions
|
// getRemoteDataFromClient returns a map of remote entries to their modification times, a list of remote folders, and a list of queued deletions
|
||||||
func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []string) {
|
func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []string) {
|
||||||
// get remote output over SSH
|
// get remote output over SSH
|
||||||
output := getSSHOutput("libmuttonserver fetch", manualSync)
|
output := GetSSHOutput("libmuttonserver fetch", manualSync)
|
||||||
|
|
||||||
// split output into slice based on occurrences of "\x1d"
|
// split output into slice based on occurrences of "\x1d"
|
||||||
outputSlice := strings.Split(output, "\x1d")
|
outputSlice := strings.Split(output, "\x1d")
|
||||||
@@ -172,7 +172,7 @@ func syncLists(localEntryModMap, remoteEntryModMap map[string]int64) {
|
|||||||
|
|
||||||
// iterate over remaining entries in remoteEntryModMap
|
// iterate over remaining entries in remoteEntryModMap
|
||||||
for entry := range remoteEntryModMap {
|
for entry := range remoteEntryModMap {
|
||||||
fmt.Println(ansiDownload+entry+backend.AnsiReset, "does not exist on server, downloading...")
|
fmt.Println(ansiDownload+entry+backend.AnsiReset, "does not exist on client, downloading...")
|
||||||
// TODO entry does not exist on client, download
|
// TODO entry does not exist on client, download
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -90,13 +90,11 @@ func Shear(targetLocationIncomplete string, deviceID string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else { // if running on the client... (online mode determined dynamically in GetSSHOutput, will simply exit if not in online mode)
|
||||||
backend.ReadConfig([]string{"sshUser"}, "0") // exits if value does not exist (indicates offline mode)
|
|
||||||
// if running on the client in online mode...
|
|
||||||
// determine client device ID (to send to server, avoids creating a deletion file for the client device)
|
// determine client device ID (to send to server, avoids creating a deletion file for the client device)
|
||||||
deviceID = deviceIDList[0].Name()
|
deviceID = deviceIDList[0].Name()
|
||||||
// below: deviceID and targetLocationIncomplete are separated by \x1d, path separators are replaced with \x1e, and spaces are replaced with \x1f
|
// below: deviceID and targetLocationIncomplete are separated by \x1d, path separators are replaced with \x1e, and spaces are replaced with \x1f
|
||||||
getSSHOutput("libmuttonserver shear "+deviceID+"\x1d"+strings.ReplaceAll(strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e"), " ", "\x1f"), true)
|
GetSSHOutput("libmuttonserver shear "+deviceID+"\x1d"+strings.ReplaceAll(strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e"), " ", "\x1f"), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|||||||
Reference in New Issue
Block a user