diff --git a/daemon/1shared.go b/daemon/1shared.go index 1e73190..47a41e1 100644 --- a/daemon/1shared.go +++ b/daemon/1shared.go @@ -5,3 +5,13 @@ import ( ) var binPath, _ = os.Executable() // store binary path + +// daemonIsOpen checks if the socket/named pipe for the rcw +// daemon exists and returns a boolean indicator. +func daemonIsOpen() bool { + fileInfo, err := os.Stat(socketPath) + if err != nil { + return false + } + return !fileInfo.IsDir() +} diff --git a/daemon/2server.go b/daemon/2server.go index a29ca52..0790cbe 100644 --- a/daemon/2server.go +++ b/daemon/2server.go @@ -7,7 +7,7 @@ import ( ) var daemonHash []byte -var passphrase string +var globalPassphrase string // RCWService provides an RPC method. type RCWService struct{} @@ -15,7 +15,7 @@ type RCWService struct{} // GetPass is the RPC method. // For now (as a test/example), it returns "hello" if the input is "hi". func (h *RCWService) GetPass(request string, reply *string) error { - *reply = passphrase + *reply = globalPassphrase return nil } @@ -26,13 +26,3 @@ func getFileHash(path string) []byte { io.Copy(hash, file) return hash.Sum(nil) } - -// daemonIsOpen checks if the socket/named pipe for the rcw -// daemon exists and returns a boolean indicator. -func daemonIsOpen() bool { - fileInfo, err := os.Stat(socketPath) - if err != nil { - return false - } - return !fileInfo.IsDir() -} diff --git a/daemon/2serverUNIXGeneric.go b/daemon/2serverUNIXGeneric.go index bfcd814..552b15e 100644 --- a/daemon/2serverUNIXGeneric.go +++ b/daemon/2serverUNIXGeneric.go @@ -14,15 +14,10 @@ import ( peercred "github.com/rwinkhart/peercred-mini" ) -// Run should be called to start an RPC server. -func Start(inputPassphrase string) { - // ensure daemon is not already running - if daemonIsOpen() { - return - } - +// Start should be called to serve the given passphrase through an RPC daemon. +func Start(passphrase string) { // store passphrase to be referenced by GetPass method - passphrase = inputPassphrase + globalPassphrase = passphrase // register RCWService with the RPC package if err := rpc.Register(&RCWService{}); err != nil { @@ -32,13 +27,6 @@ func Start(inputPassphrase string) { // store the hash of the daemon binary daemonHash = getFileHash(binPath) - // remove the socket file if it already exists - if _, err := os.Stat(socketPath); err == nil { - if err := os.Remove(socketPath); err != nil { - log.Fatalf("Failed to remove existing socket: %v", err) - } - } - // listen on the Unix domain socket listener, err := net.Listen("unix", socketPath) if err != nil { diff --git a/daemon/2serverWindows.go b/daemon/2serverWindows.go index dcb1800..b69baec 100644 --- a/daemon/2serverWindows.go +++ b/daemon/2serverWindows.go @@ -19,15 +19,10 @@ const ( PROCESS_QUERY_LIMITED_INFORMATION = 0x1000 ) -// Run should be called to start an RPC server using Windows named pipes -func Start(inputPassphrase string) { - // ensure daemon is not already running - if daemonIsOpen() { - return - } - +// Start should be called to serve the given passphrase through an RPC daemon. +func Start(passphrase string) { // store passphrase to be referenced by GetPass method - passphrase = inputPassphrase + globalPassphrase = passphrase // register RCWService with the RPC package if err := rpc.Register(&RCWService{}); err != nil { diff --git a/daemon/3client.go b/daemon/3client.go index 830fc59..603c113 100644 --- a/daemon/3client.go +++ b/daemon/3client.go @@ -5,8 +5,18 @@ import ( "net/rpc" ) -// Call connects to the RPC server and requests the passphrase. -func Call() string { +// CallDaemonIfOpen returns the passphrase served by the RCW daemon +// (if one is available). If no RCW daemon is accessible, nil is returned. +func CallDaemonIfOpen() []byte { + if daemonIsOpen() { + call() + return call() + } + return nil +} + +// call connects to the RPC server and requests the passphrase. +func call() []byte { // connect to the UNIX domain socket/Windows named pipe conn := getConn() defer conn.Close() @@ -22,5 +32,5 @@ func Call() string { } // return the passphrase - return reply + return []byte(reply) } diff --git a/example.go b/example.go index d4afd0a..ea77117 100644 --- a/example.go +++ b/example.go @@ -70,6 +70,10 @@ func main() { os.WriteFile(outputFile, encBytes, 0600) default: // request served data - fmt.Println(daemon.Call()) + if servedData := daemon.CallDaemonIfOpen(); servedData != nil { + fmt.Println(string(servedData)) + } else { + fmt.Println("No RCW daemon available") + } } }