package client // // Storage // import ( "crypto/sha256" "encoding/json" "path/filepath" "forge.redroom.link/yves/meowlib" "github.com/dgraph-io/badger" ) type ServerStorage struct { DbFile string `json:"db_file,omitempty"` db *badger.DB } // Open a badger database from struct ServerStorage func (ss *ServerStorage) open() error { opts := badger.DefaultOptions(filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, ss.DbFile)) opts.Logger = nil var err error ss.db, err = badger.Open(opts) if err != nil { return err } return nil } // Store function StoreServer stores a server in a badger database with Server.GetUid() as key func (ss *ServerStorage) StoreServer(sc *Server) error { err := ss.open() if err != nil { return err } defer ss.close() // first marshal the Server to bytes with protobuf jsonsrv, err := json.Marshal(sc) if err != nil { return err } data, err := meowlib.SymEncrypt(GetConfig().memoryPassword, jsonsrv) if err != nil { return err } shakey := sha256.Sum256([]byte(sc.GetServerCard().GetUid())) key := shakey[:] // then store it in the database return ss.db.Update(func(txn *badger.Txn) error { return txn.Set(key, data) }) } // LoadServer function loads a Server from a badger database with Server.GetUid() as key func (ss *ServerStorage) LoadServer(uid string) (*Server, error) { var sc Server err := ss.open() if err != nil { return nil, err } defer ss.close() shakey := sha256.Sum256([]byte(uid)) key := shakey[:] err = ss.db.View(func(txn *badger.Txn) error { item, err := txn.Get(key) if err != nil { return err } return item.Value(func(val []byte) error { jsonsrv, err := meowlib.SymDecrypt(GetConfig().memoryPassword, val) if err != nil { return err } return json.Unmarshal(jsonsrv, &sc) }) }) return &sc, err } // DeleteServer function deletes a Server from a badger database with Server.GetUid() as key func (ss *ServerStorage) DeleteServer(uid string) error { err := ss.open() if err != nil { return err } defer ss.close() shakey := sha256.Sum256([]byte(uid)) key := shakey[:] return ss.db.Update(func(txn *badger.Txn) error { return txn.Delete(key) }) } // LoadAllServers function loads all Servers from a badger database func (ss *ServerStorage) LoadAllServers() ([]*Server, error) { var scs []*Server err := ss.open() if err != nil { return nil, err } defer ss.close() err = ss.db.View(func(txn *badger.Txn) error { opts := badger.DefaultIteratorOptions opts.PrefetchSize = 10 it := txn.NewIterator(opts) defer it.Close() for it.Rewind(); it.Valid(); it.Next() { item := it.Item() var sc Server err := item.Value(func(val []byte) error { jsonsrv, err := meowlib.SymDecrypt(GetConfig().memoryPassword, val) if err != nil { return err } return json.Unmarshal(jsonsrv, &sc) }) if err != nil { return err } scs = append(scs, &sc) } return nil }) return scs, err } // LoadAllServers function loads all ServersCards from a badger database func (ss *ServerStorage) LoadAllServerCards() ([]*meowlib.ServerCard, error) { var scs []*meowlib.ServerCard err := ss.open() if err != nil { return nil, err } defer ss.close() err = ss.db.View(func(txn *badger.Txn) error { opts := badger.DefaultIteratorOptions opts.PrefetchSize = 10 it := txn.NewIterator(opts) defer it.Close() for it.Rewind(); it.Valid(); it.Next() { item := it.Item() var sc Server err := item.Value(func(val []byte) error { jsonsrv, err := meowlib.SymDecrypt(GetConfig().memoryPassword, val) if err != nil { return err } return json.Unmarshal(jsonsrv, &sc) }) if err != nil { return err } scs = append(scs, sc.GetServerCard()) } return nil }) return scs, err } // LoadServersFromUids function loads Servers with id in []Uid parameter from a badger database func (ss *ServerStorage) LoadServersFromUids(uids []string) ([]*Server, error) { var scs []*Server err := ss.open() if err != nil { return nil, err } defer ss.close() err = ss.db.View(func(txn *badger.Txn) error { for _, uid := range uids { shakey := sha256.Sum256([]byte(uid)) key := shakey[:] item, err := txn.Get(key) if err != nil { return err } var sc Server err = item.Value(func(val []byte) error { jsonsrv, err := meowlib.SymDecrypt(GetConfig().memoryPassword, val) if err != nil { return err } return json.Unmarshal(jsonsrv, &sc) }) if err != nil { return err } scs = append(scs, &sc) } return nil }) return scs, err } // LoadServersFromUids function loads Servers with id in []Uid parameter from a badger database func (ss *ServerStorage) LoadServerCardsFromUids(uids []string) ([]*meowlib.ServerCard, error) { var scs []*meowlib.ServerCard err := ss.open() if err != nil { return nil, err } defer ss.close() err = ss.db.View(func(txn *badger.Txn) error { for _, uid := range uids { shakey := sha256.Sum256([]byte(uid)) key := shakey[:] item, err := txn.Get(key) if err != nil { return err } var sc Server err = item.Value(func(val []byte) error { jsonsrv, err := meowlib.SymDecrypt(GetConfig().memoryPassword, val) if err != nil { return err } return json.Unmarshal(jsonsrv, &sc) }) if err != nil { return err } scs = append(scs, sc.GetServerCard()) } return nil }) return scs, err } // close a badger database func (ss *ServerStorage) close() { ss.db.Close() }