mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-08-28 04:46:42 -04:00
Add high-level function for calling the daemon if it is open
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
+2
-12
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+13
-3
@@ -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)
|
||||
}
|
||||
|
||||
+5
-1
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user