mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-09-06 09:07:16 -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
|
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 daemonHash []byte
|
||||||
var passphrase string
|
var globalPassphrase string
|
||||||
|
|
||||||
// RCWService provides an RPC method.
|
// RCWService provides an RPC method.
|
||||||
type RCWService struct{}
|
type RCWService struct{}
|
||||||
@@ -15,7 +15,7 @@ type RCWService struct{}
|
|||||||
// GetPass is the RPC method.
|
// GetPass is the RPC method.
|
||||||
// For now (as a test/example), it returns "hello" if the input is "hi".
|
// For now (as a test/example), it returns "hello" if the input is "hi".
|
||||||
func (h *RCWService) GetPass(request string, reply *string) error {
|
func (h *RCWService) GetPass(request string, reply *string) error {
|
||||||
*reply = passphrase
|
*reply = globalPassphrase
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,13 +26,3 @@ func getFileHash(path string) []byte {
|
|||||||
io.Copy(hash, file)
|
io.Copy(hash, file)
|
||||||
return hash.Sum(nil)
|
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"
|
peercred "github.com/rwinkhart/peercred-mini"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run should be called to start an RPC server.
|
// Start should be called to serve the given passphrase through an RPC daemon.
|
||||||
func Start(inputPassphrase string) {
|
func Start(passphrase string) {
|
||||||
// ensure daemon is not already running
|
|
||||||
if daemonIsOpen() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// store passphrase to be referenced by GetPass method
|
// store passphrase to be referenced by GetPass method
|
||||||
passphrase = inputPassphrase
|
globalPassphrase = passphrase
|
||||||
|
|
||||||
// register RCWService with the RPC package
|
// register RCWService with the RPC package
|
||||||
if err := rpc.Register(&RCWService{}); err != nil {
|
if err := rpc.Register(&RCWService{}); err != nil {
|
||||||
@@ -32,13 +27,6 @@ func Start(inputPassphrase string) {
|
|||||||
// store the hash of the daemon binary
|
// store the hash of the daemon binary
|
||||||
daemonHash = getFileHash(binPath)
|
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
|
// listen on the Unix domain socket
|
||||||
listener, err := net.Listen("unix", socketPath)
|
listener, err := net.Listen("unix", socketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -19,15 +19,10 @@ const (
|
|||||||
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run should be called to start an RPC server using Windows named pipes
|
// Start should be called to serve the given passphrase through an RPC daemon.
|
||||||
func Start(inputPassphrase string) {
|
func Start(passphrase string) {
|
||||||
// ensure daemon is not already running
|
|
||||||
if daemonIsOpen() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// store passphrase to be referenced by GetPass method
|
// store passphrase to be referenced by GetPass method
|
||||||
passphrase = inputPassphrase
|
globalPassphrase = passphrase
|
||||||
|
|
||||||
// register RCWService with the RPC package
|
// register RCWService with the RPC package
|
||||||
if err := rpc.Register(&RCWService{}); err != nil {
|
if err := rpc.Register(&RCWService{}); err != nil {
|
||||||
|
|||||||
+13
-3
@@ -5,8 +5,18 @@ import (
|
|||||||
"net/rpc"
|
"net/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Call connects to the RPC server and requests the passphrase.
|
// CallDaemonIfOpen returns the passphrase served by the RCW daemon
|
||||||
func Call() string {
|
// (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
|
// connect to the UNIX domain socket/Windows named pipe
|
||||||
conn := getConn()
|
conn := getConn()
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
@@ -22,5 +32,5 @@ func Call() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// return the passphrase
|
// return the passphrase
|
||||||
return reply
|
return []byte(reply)
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -70,6 +70,10 @@ func main() {
|
|||||||
os.WriteFile(outputFile, encBytes, 0600)
|
os.WriteFile(outputFile, encBytes, 0600)
|
||||||
default:
|
default:
|
||||||
// request served data
|
// 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