mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-09-04 08:07:18 -04:00
Implement serving passphrase from daemon
This commit is contained in:
+2
-5
@@ -2,12 +2,12 @@ package daemon
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var daemonHash []byte
|
var daemonHash []byte
|
||||||
|
var passphrase string
|
||||||
|
|
||||||
// RCWService provides an RPC method.
|
// RCWService provides an RPC method.
|
||||||
type RCWService struct{}
|
type RCWService struct{}
|
||||||
@@ -15,12 +15,9 @@ 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 {
|
||||||
if request == "hi" {
|
*reply = passphrase
|
||||||
*reply = "hello"
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("unexpected input, expected \"hi\"")
|
|
||||||
}
|
|
||||||
|
|
||||||
// getFileHash returns the SHA256 hash of the file at the given path.
|
// getFileHash returns the SHA256 hash of the file at the given path.
|
||||||
func getFileHash(path string) []byte {
|
func getFileHash(path string) []byte {
|
||||||
|
|||||||
@@ -15,7 +15,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Run should be called to start an RPC server.
|
// Run should be called to start an RPC server.
|
||||||
func Run() {
|
func Start(inputPassphrase string) {
|
||||||
|
// ensure daemon is not already running
|
||||||
|
if daemonIsOpen() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// store passphrase to be referenced by GetPass method
|
||||||
|
passphrase = inputPassphrase
|
||||||
|
|
||||||
// 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 {
|
||||||
log.Fatalf("Error registering RPC service: %v", err)
|
log.Fatalf("Error registering RPC service: %v", err)
|
||||||
|
|||||||
@@ -20,7 +20,15 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Run should be called to start an RPC server using Windows named pipes
|
// Run should be called to start an RPC server using Windows named pipes
|
||||||
func Run() {
|
func Start(inputPassphrase string) {
|
||||||
|
// ensure daemon is not already running
|
||||||
|
if daemonIsOpen() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// store passphrase to be referenced by GetPass method
|
||||||
|
passphrase = inputPassphrase
|
||||||
|
|
||||||
// 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 {
|
||||||
log.Fatalf("Error registering RPC service: %v", err)
|
log.Fatalf("Error registering RPC service: %v", err)
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package daemon
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
func daemonIsOpen() bool {
|
||||||
|
// check if socketPath exists and is a file
|
||||||
|
fileInfo, err := os.Stat(socketPath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return !fileInfo.IsDir()
|
||||||
|
}
|
||||||
+2
-7
@@ -8,13 +8,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
switch os.Args[1] {
|
daemon.Start(os.Args[1])
|
||||||
case "client":
|
|
||||||
fmt.Println("RPC Reply: " + daemon.Call())
|
|
||||||
default:
|
|
||||||
daemon.Run()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
daemon.Run()
|
fmt.Println(daemon.Call())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user