Compare commits

..

No commits in common. "0813a619b4dddff14aeaf084cc4052a9e7bf369f" and "8dcba67fe922c4a4f90721dc010f398b7070e62c" have entirely different histories.

2 changed files with 65 additions and 140 deletions

View File

@ -6,6 +6,7 @@ import (
"errors"
"flag"
"fmt"
"log"
"net"
"path"
"os"
@ -78,8 +79,8 @@ func(a *auther) Get(k []byte) (string, error) {
return v, nil
}
//func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) error {
func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error {
// TODO: where should the session id be uniquely embedded
func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) error {
if ch == nil {
return errors.New("nil channel")
}
@ -101,6 +102,23 @@ func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.E
_ = requests
}(requests)
pe, err := mss.GetPersister(ctx)
if err != nil {
return fmt.Errorf("cannot get persister: %v", err)
}
lhs.SetPersister(pe)
lhs.Cfg.SessionId = sessionId
hl, err := lhs.GetHandler()
if err != nil {
return fmt.Errorf("cannot get handler: %v", err)
}
en := lhs.GetEngine()
en = en.WithFirst(hl.Init)
en = en.WithDebug(nil)
defer en.Finish()
cont, err := en.Exec(ctx, []byte{})
if err != nil {
return fmt.Errorf("initial engine exec err: %v", err)
@ -136,75 +154,7 @@ func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.E
return nil
}
type sshRunner struct {
Ctx context.Context
Cfg engine.Config
FlagFile string
DbDir string
ResourceDir string
Debug bool
}
func(s *sshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) {
ctx := s.Ctx
menuStorageService := storage.NewMenuStorageService(s.DbDir, s.ResourceDir)
err := menuStorageService.EnsureDbDir()
if err != nil {
return nil, nil, err
}
rs, err := menuStorageService.GetResource(ctx)
if err != nil {
return nil, nil, err
}
pe, err := menuStorageService.GetPersister(ctx)
if err != nil {
return nil, nil, err
}
userdatastore, err := menuStorageService.GetUserdataDb(ctx)
if err != nil {
return nil, nil, err
}
dbResource, ok := rs.(*resource.DbResource)
if !ok {
return nil, nil, err
}
lhs, err := handlers.NewLocalHandlerService(s.FlagFile, true, dbResource, s.Cfg, rs)
lhs.SetDataStore(&userdatastore)
lhs.SetPersister(pe)
lhs.Cfg.SessionId = sessionId
if err != nil {
return nil, nil, err
}
hl, err := lhs.GetHandler()
if err != nil {
return nil, nil, err
}
en := lhs.GetEngine()
en = en.WithFirst(hl.Init)
if s.Debug {
en = en.WithDebug(nil)
}
// TODO: this is getting very hacky!
closer := func() {
err := menuStorageService.Close()
if err != nil {
logg.ErrorCtxf(ctx, "menu storage service cleanup fail", "err", err)
}
}
return en, closer, nil
}
// adapted example from crypto/ssh package, NewServerConn doc
func(s *sshRunner) Run(ctx context.Context) {//, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) {
func sshRun(ctx context.Context, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) {
running := true
defer wg.Wait()
@ -217,11 +167,11 @@ func(s *sshRunner) Run(ctx context.Context) {//, mss *storage.MenuStorageService
privateBytes, err := os.ReadFile("/home/lash/.ssh/id_rsa_tmp")
if err != nil {
logg.ErrorCtxf(ctx, "Failed to load private key", "err", err)
log.Fatal("Failed to load private key: ", err)
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
logg.ErrorCtxf(ctx, "Failed to parse private key", "err", err)
log.Fatal("Failed to parse private key: ", err)
}
cfg.AddHostKey(private)
@ -258,20 +208,8 @@ func(s *sshRunner) Run(ctx context.Context) {//, mss *storage.MenuStorageService
logg.ErrorCtxf(ctx, "Cannot find authentication")
return
}
en, closer, err := s.GetEngine(sessionId)
if err != nil {
logg.ErrorCtxf(ctx, "engine won't start", "err", err)
return
}
defer func() {
err := en.Finish()
if err != nil {
logg.ErrorCtxf(ctx, "engine won't stop", "err", err)
}
closer()
}()
for ch := range nC {
err = serve(ctx, sessionId, ch, en)
err = serve(ctx, sessionId, ch, mss, lhs)
logg.ErrorCtxf(ctx, "ssh server finish", "err", err)
}
}
@ -279,7 +217,6 @@ func(s *sshRunner) Run(ctx context.Context) {//, mss *storage.MenuStorageService
}
}
func sshLoadKeys(ctx context.Context, dbDir string) error {
keyStoreFile := path.Join(dbDir, "ssh_authorized_keys.gdbm")
keyStore = gdbmdb.NewGdbmDb()
@ -326,19 +263,38 @@ func main() {
if engineDebug {
cfg.EngineDebug = true
}
err := sshLoadKeys(ctx, dbDir)
mss := storage.NewMenuStorageService(dbDir, resourceDir)
rs, err := mss.GetResource(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
runner := &sshRunner{
Cfg: cfg,
Debug: engineDebug,
FlagFile: pfp,
DbDir: dbDir,
ResourceDir: resourceDir,
err = mss.EnsureDbDir()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
runner.Run(ctx)
dbResource, ok := rs.(*resource.DbResource)
if !ok {
os.Exit(1)
}
userdataStore := mss.GetUserdataDb(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
lhs, err := handlers.NewLocalHandlerService(pfp, engineDebug, dbResource, cfg, rs)
lhs.SetDataStore(&userdataStore)
err = sshLoadKeys(ctx, dbDir)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
sshRun(ctx, mss, lhs)
}

View File

@ -11,13 +11,8 @@ import (
gdbmdb "git.defalsify.org/vise.git/db/gdbm"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/logging"
)
var (
logg = logging.NewVanilla().WithDomain("storage")
)
type StorageService interface {
GetPersister(ctx context.Context) (*persist.Persister, error)
GetUserdataDb(ctx context.Context) db.Db
@ -28,9 +23,6 @@ type StorageService interface {
type MenuStorageService struct{
dbDir string
resourceDir string
resourceStore db.Db
stateStore db.Db
userDataStore db.Db
}
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService {
@ -41,48 +33,35 @@ func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService
}
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
ms.stateStore = gdbmdb.NewGdbmDb()
store := gdbmdb.NewGdbmDb()
storeFile := path.Join(ms.dbDir, "state.gdbm")
err := ms.stateStore.Connect(ctx, storeFile)
if err != nil {
return nil, err
}
pr := persist.NewPersister(ms.stateStore)
logg.TraceCtxf(ctx, "menu storage service", "persist", pr, "store", ms.stateStore)
store.Connect(ctx, storeFile)
pr := persist.NewPersister(store)
return pr, nil
}
func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) {
ms.userDataStore = gdbmdb.NewGdbmDb()
func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) db.Db {
store := gdbmdb.NewGdbmDb()
storeFile := path.Join(ms.dbDir, "userdata.gdbm")
err := ms.userDataStore.Connect(ctx, storeFile)
if err != nil {
return nil, err
}
return ms.userDataStore, nil
store.Connect(ctx, storeFile)
return store
}
func (ms *MenuStorageService) GetResource(ctx context.Context) (resource.Resource, error) {
ms.resourceStore = fsdb.NewFsDb()
err := ms.resourceStore.Connect(ctx, ms.resourceDir)
store := fsdb.NewFsDb()
err := store.Connect(ctx, ms.resourceDir)
if err != nil {
return nil, err
}
rfs := resource.NewDbResource(ms.resourceStore)
rfs := resource.NewDbResource(store)
return rfs, nil
}
func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) {
if ms.stateStore != nil {
panic("set up store when already exists")
}
ms.stateStore = gdbmdb.NewGdbmDb()
store := gdbmdb.NewGdbmDb()
storeFile := path.Join(ms.dbDir, "state.gdbm")
err := ms.stateStore.Connect(ctx, storeFile)
if err != nil {
return nil, err
}
return ms.stateStore, nil
store.Connect(ctx, storeFile)
return store, nil
}
func (ms *MenuStorageService) EnsureDbDir() error {
@ -92,13 +71,3 @@ func (ms *MenuStorageService) EnsureDbDir() error {
}
return nil
}
func (ms *MenuStorageService) Close() error {
errA := ms.stateStore.Close()
errB := ms.userDataStore.Close()
errC := ms.resourceStore.Close()
if errA != nil || errB != nil || errC != nil {
return fmt.Errorf("%v %v %v", errA, errB, errC)
}
return nil
}