implement new-host and new-node rpc node commands

This commit is contained in:
mykola2312 2025-01-24 03:46:33 +02:00
parent 8d9ccac993
commit 2b294c993d
4 changed files with 77 additions and 3 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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(),
}
}