From 2b294c993dc719b451503630a7b517473d78f6cf Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Fri, 24 Jan 2025 03:46:33 +0200 Subject: [PATCH] implement new-host and new-node rpc node commands --- crypto/lux_key.go | 18 +++++++++++++++-- net/lux_router.go | 4 ++++ node/lux_node.go | 46 ++++++++++++++++++++++++++++++++++++++++++- rpc/lux_rpc_errors.go | 12 +++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/crypto/lux_key.go b/crypto/lux_key.go index 9c124f6..57a09de 100644 --- a/crypto/lux_key.go +++ b/crypto/lux_key.go @@ -87,7 +87,16 @@ func NewLuxKeyStore(filePath string) LuxKeyStore { } } +func NewLuxTempKeyStore() LuxKeyStore { + return NewLuxKeyStore("") +} + func (ks *LuxKeyStore) Load() error { + if ks.filePath == "" { + // skip for temp ks + return nil + } + bytes, err := os.ReadFile(ks.filePath) if err != nil { // probably no file exists, so we create it @@ -110,6 +119,11 @@ func (ks *LuxKeyStore) Load() error { } func (ks *LuxKeyStore) Save() error { + if ks.filePath == "" { + // skip for temp ks + return nil + } + wd := proto.AllocLuxBuffer(len(ks.keys)) for _, key := range ks.keys { key.Write(&wd) @@ -155,7 +169,7 @@ func (ks *LuxKeyStore) GetRpcName() string { func (ks *LuxKeyStore) Register(sv *rpc.LuxRpcServer) {} -func serializeKeyStore(ks *LuxKeyStore) rpc.LuxRpcKeyStore { +func LuxKeyStoreIntoRpc(ks *LuxKeyStore) rpc.LuxRpcKeyStore { keyStore := rpc.LuxRpcKeyStore{ Hosts: make([]rpc.LuxRpcKeyHost, 0), Nodes: make([]rpc.LuxRpcKeyNode, 0), @@ -191,7 +205,7 @@ func (ks *LuxKeyStore) Handle(request rpc.LuxRpcRequest, rpcType rpc.LuxRpcType) if request.Command == "get" { // get all keys, so we need to construct xml object for that return rpc.LuxRpcResponse{ - Keystore: serializeKeyStore(ks), + Keystore: LuxKeyStoreIntoRpc(ks), }, rpc.LuxRpcError{}, true } diff --git a/net/lux_router.go b/net/lux_router.go index dc4685a..30d386b 100644 --- a/net/lux_router.go +++ b/net/lux_router.go @@ -105,6 +105,10 @@ func (r *LuxRouter) GetThisKey() crypto.LuxKey { return r.thisKey } +func (r *LuxRouter) GetKeyStore() *crypto.LuxKeyStore { + return &r.keyStore +} + func (r *LuxRouter) GetRouterType() proto.LuxType { return r.thisKey.Type } diff --git a/node/lux_node.go b/node/lux_node.go index f8cd567..18500bb 100644 --- a/node/lux_node.go +++ b/node/lux_node.go @@ -258,5 +258,49 @@ func (node *LuxNode) Register(sv *rpc.LuxRpcServer) { } func (node *LuxNode) Handle(request rpc.LuxRpcRequest, rpcType rpc.LuxRpcType) (rpc.LuxRpcResponse, rpc.LuxRpcError, bool) { - return rpc.LuxRpcResponse{}, rpc.LUX_RPC_ERROR_UNKNOWN_COMMAND, false + var rpcRes rpc.LuxRpcResponse + + // only root can add hosts or neighbors + if rpcType != rpc.LuxRpcTypeRoot { + return rpcRes, rpc.LUX_RPC_ERROR_ACCESS_DENIED, false + } + + if request.Command == "new-host" { + ks := node.router.GetKeyStore() + // generate host key, add it to our keystore + host, err := crypto.NewLuxKey(proto.LuxTypeHost) + if err != nil { + return rpcRes, rpc.LuxRpcGenericError(err), false + } + if err := ks.Put(host); err != nil { + return rpcRes, rpc.LuxRpcGenericError(err), false + } + + // and create another keystore for host + ksHost := crypto.NewLuxTempKeyStore() + ksHost.Put(host) + + return rpc.LuxRpcResponse{ + Keystore: crypto.LuxKeyStoreIntoRpc(&ksHost), + }, rpc.LuxRpcError{}, true + } else if request.Command == "new-node" { + // to bootstrap neighbor node, first on this node we generate + // and add new node key, then copy all keys with new key to neighbor's keystore + ks := node.router.GetKeyStore() + + newNode, err := crypto.NewLuxKey(proto.LuxTypeNode) + if err != nil { + return rpcRes, rpc.LuxRpcGenericError(err), false + } + if err := ks.Put(newNode); err != nil { + return rpcRes, rpc.LuxRpcGenericError(err), false + } + + // yeah we just serialize our own keystore + return rpc.LuxRpcResponse{ + Keystore: crypto.LuxKeyStoreIntoRpc(ks), + }, rpc.LuxRpcError{}, true + } + + return rpcRes, rpc.LUX_RPC_ERROR_UNKNOWN_COMMAND, false } diff --git a/rpc/lux_rpc_errors.go b/rpc/lux_rpc_errors.go index 299dd66..d87290d 100644 --- a/rpc/lux_rpc_errors.go +++ b/rpc/lux_rpc_errors.go @@ -17,3 +17,15 @@ var LUX_RPC_ERROR_UNKNOWN_COMMAND = LuxRpcError{ ErrorCode: 3, Message: "unknown command", } + +var LUX_RPC_ERROR_GENERIC = LuxRpcError{ + ErrorCode: 4, + Message: "generic error", +} + +func LuxRpcGenericError(err error) LuxRpcError { + return LuxRpcError{ + ErrorCode: LUX_RPC_ERROR_GENERIC.ErrorCode, + Message: err.Error(), + } +}