Reduce size of server binary by not compiling SSH

This commit is contained in:
2024-05-19 19:34:28 -04:00
parent 2525eca904
commit f320c7887f
3 changed files with 56 additions and 32 deletions
+1 -1
View File
@@ -13,5 +13,5 @@ var (
const ( const (
AnsiError = "\033[38;5;9m" AnsiError = "\033[38;5;9m"
AnsiReset = "\033[0m" AnsiReset = "\033[0m"
LibmuttonVersion = "0.2.X" LibmuttonVersion = "0.2.A" // 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"
) )
+20
View File
@@ -275,6 +275,18 @@ func syncLists(localEntryModMap, remoteEntryModMap map[string]int64, manualSync
} }
} }
// 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
// can safely be called in offline mode, as well, so this is the intended interface for shearing (ShearLocal should only be used directly in 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
// call the server to remotely shear the target and add it to the deletions list
// deviceID and targetLocationIncomplete are separated by \x1d, path separators are replaced with \x1e, and spaces are replaced with \x1f TODO is there a need to combine deviceID and targetLocationIncomplete into one argument?
GetSSHOutput("libmuttonserver shear "+deviceID+"\x1d"+strings.ReplaceAll(strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e"), " ", "\x1f"), false)
os.Exit(0) // sync is not required after shearing since the target has already been removed from the local system
}
// deletionSync removes entries from the client that have been deleted on the server (multi-client deletion) // deletionSync removes entries from the client that have been deleted on the server (multi-client deletion)
func deletionSync(deletions []string) { func deletionSync(deletions []string) {
for _, deletion := range deletions { for _, deletion := range deletions {
@@ -283,6 +295,14 @@ func deletionSync(deletions []string) {
} }
} }
// AddFolderRemoteFromClient creates a new entry-containing directory on the local system and calls the server to create the folder remotely
func AddFolderRemoteFromClient(targetLocationIncomplete string) {
AddFolderLocal(targetLocationIncomplete) // add the folder on the local system
GetSSHOutput("libmuttonserver addfolder "+strings.ReplaceAll(targetLocationIncomplete, " ", "\x1f"), false) // call the server to create the folder remotely
os.Exit(0)
}
// folderSync creates folders on the client (from the given list of folder names) // folderSync creates folders on the client (from the given list of folder names)
func folderSync(folders []string) { func folderSync(folders []string) {
for _, folder := range folders { for _, folder := range folders {
+35 -31
View File
@@ -58,51 +58,59 @@ func getModTimes(entryList []string) []int64 {
return modList return modList
} }
// Shear removes the target file or directory from the system // ShearLocal removes the target file or directory from the local system
// if running on the server, it will also add the target to the deletions list // returns: deviceID (on client), for use in ShearRemoteFromClient
// if running on the client, it will call the server to add the target to the deletions list // if the local system is a server, it will also add the target to the deletions list for all clients (except the requesting client)
func Shear(targetLocationIncomplete string, deviceID string) { // this function should only be used directly by the server binary
// get the full targetLocation path and remove the target TODO move so that the target is not removed if the server fails to add it to the deletions list, on server do not error if target does not exist func ShearLocal(targetLocationIncomplete, clientDeviceID string) string {
targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:]) // determine if running on a server
backend.TargetIsFile(targetLocationComplete, true, 0) // needed because os.RemoveAll does not return an error if target does not exist var onServer bool
err := os.RemoveAll(targetLocationComplete) if clientDeviceID != "" {
if err != nil { onServer = true
fmt.Println(backend.AnsiError + "Failed to remove local target: " + err.Error() + backend.AnsiReset)
os.Exit(1)
} }
// read the devices directory // create a slice of all registered devices
var deviceIDList []os.DirEntry deviceIDList, err := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices")
deviceIDList, err = os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices")
if err != nil { if err != nil {
fmt.Println(backend.AnsiError + "Failed to read the devices directory: " + err.Error() + backend.AnsiReset) fmt.Println(backend.AnsiError + "Failed to read the devices directory: " + err.Error() + backend.AnsiReset)
os.Exit(1) os.Exit(1)
} }
// add the sheared target (incomplete, vanity) to the deletions list // add the sheared target (incomplete, vanity) to the deletions list (if running on a server)
if deviceID != "" { // if running on the server... if onServer {
for _, device := range deviceIDList { for _, device := range deviceIDList {
if device.Name() != deviceID { if device.Name() != clientDeviceID {
_, err = os.Create(backend.ConfigDir + backend.PathSeparator + "deletions" + backend.PathSeparator + device.Name() + "\x1d" + strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e")) _, err = os.Create(backend.ConfigDir + backend.PathSeparator + "deletions" + backend.PathSeparator + device.Name() + "\x1d" + strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e"))
if err != nil { if err != nil {
// do not print error as there is currently no way of seeing server-side errors // 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(1) os.Exit(1)
} }
} }
} }
} else { // if running on the client... (online mode determined dynamically in GetSSHOutput, will silently exit if not in online mode) // TODO separate into own function to avoid server needing to build SSH
// determine client device ID (to send to server, avoids creating a deletion file for the client device)
deviceID = deviceIDList[0].Name()
// 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"), false)
} }
os.Exit(0) // get the full targetLocation path and remove the target
targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:])
if !onServer { // error if target does not exist on client, needed because os.RemoveAll does not return an error if target does not exist
backend.TargetIsFile(targetLocationComplete, true, 0)
}
err = os.RemoveAll(targetLocationComplete)
if err != nil {
fmt.Println(backend.AnsiError + "Failed to remove local target: " + err.Error() + backend.AnsiReset)
os.Exit(1)
}
if !onServer { // return the device ID if running on the client
return deviceIDList[0].Name()
}
return ""
// do not exit program, as this function is used as part of ShearRemoteFromClient
} }
// AddFolder creates a new directory at targetLocation // AddFolderLocal creates a new entry-containing directory on the local system
// if running on the client, it will also call the server to create the directory // this function should only be used directly by the server binary
func AddFolder(targetLocationIncomplete string, onServer bool) { func AddFolderLocal(targetLocationIncomplete string) {
// get the full targetLocation path and create the target // get the full targetLocation path and create the target
targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:]) targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:])
err := os.Mkdir(targetLocationComplete, 0700) err := os.Mkdir(targetLocationComplete, 0700)
@@ -116,9 +124,5 @@ func AddFolder(targetLocationIncomplete string, onServer bool) {
} }
} }
if !onServer { // if running on the client... (online mode determined dynamically in GetSSHOutput, will silently exit if not in online mode) // TODO separate into own function to avoid server needing to build SSH // do not exit program, as this function is used as part of AddFolderRemoteFromClient
GetSSHOutput("libmuttonserver addfolder "+strings.ReplaceAll(targetLocationIncomplete, " ", "\x1f"), false)
}
os.Exit(0)
} }