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" "errors"
"flag" "flag"
"fmt" "fmt"
"log"
"net" "net"
"path" "path"
"os" "os"
@ -78,8 +79,8 @@ func(a *auther) Get(k []byte) (string, error) {
return v, nil return v, nil
} }
//func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) error { // TODO: where should the session id be uniquely embedded
func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error { func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) error {
if ch == nil { if ch == nil {
return errors.New("nil channel") return errors.New("nil channel")
} }
@ -101,6 +102,23 @@ func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.E
_ = requests _ = requests
}(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{}) cont, err := en.Exec(ctx, []byte{})
if err != nil { if err != nil {
return fmt.Errorf("initial engine exec err: %v", err) 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 return nil
} }
type sshRunner struct { func sshRun(ctx context.Context, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) {
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) {
running := true running := true
defer wg.Wait() 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") privateBytes, err := os.ReadFile("/home/lash/.ssh/id_rsa_tmp")
if err != nil { 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) private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "Failed to parse private key", "err", err) log.Fatal("Failed to parse private key: ", err)
} }
cfg.AddHostKey(private) cfg.AddHostKey(private)
@ -258,20 +208,8 @@ func(s *sshRunner) Run(ctx context.Context) {//, mss *storage.MenuStorageService
logg.ErrorCtxf(ctx, "Cannot find authentication") logg.ErrorCtxf(ctx, "Cannot find authentication")
return 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 { 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) 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 { func sshLoadKeys(ctx context.Context, dbDir string) error {
keyStoreFile := path.Join(dbDir, "ssh_authorized_keys.gdbm") keyStoreFile := path.Join(dbDir, "ssh_authorized_keys.gdbm")
keyStore = gdbmdb.NewGdbmDb() keyStore = gdbmdb.NewGdbmDb()
@ -327,18 +264,37 @@ func main() {
cfg.EngineDebug = true cfg.EngineDebug = true
} }
err := sshLoadKeys(ctx, dbDir) mss := storage.NewMenuStorageService(dbDir, resourceDir)
rs, err := mss.GetResource(ctx)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
runner := &sshRunner{ err = mss.EnsureDbDir()
Cfg: cfg, if err != nil {
Debug: engineDebug, fmt.Fprintf(os.Stderr, err.Error())
FlagFile: pfp, os.Exit(1)
DbDir: dbDir,
ResourceDir: resourceDir,
} }
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,11 +11,6 @@ import (
gdbmdb "git.defalsify.org/vise.git/db/gdbm" gdbmdb "git.defalsify.org/vise.git/db/gdbm"
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/logging"
)
var (
logg = logging.NewVanilla().WithDomain("storage")
) )
type StorageService interface { type StorageService interface {
@ -28,9 +23,6 @@ type StorageService interface {
type MenuStorageService struct{ type MenuStorageService struct{
dbDir string dbDir string
resourceDir string resourceDir string
resourceStore db.Db
stateStore db.Db
userDataStore db.Db
} }
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService { 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) { func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
ms.stateStore = gdbmdb.NewGdbmDb() store := gdbmdb.NewGdbmDb()
storeFile := path.Join(ms.dbDir, "state.gdbm") storeFile := path.Join(ms.dbDir, "state.gdbm")
err := ms.stateStore.Connect(ctx, storeFile) store.Connect(ctx, storeFile)
if err != nil { pr := persist.NewPersister(store)
return nil, err
}
pr := persist.NewPersister(ms.stateStore)
logg.TraceCtxf(ctx, "menu storage service", "persist", pr, "store", ms.stateStore)
return pr, nil return pr, nil
} }
func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) { func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) db.Db {
ms.userDataStore = gdbmdb.NewGdbmDb() store := gdbmdb.NewGdbmDb()
storeFile := path.Join(ms.dbDir, "userdata.gdbm") storeFile := path.Join(ms.dbDir, "userdata.gdbm")
err := ms.userDataStore.Connect(ctx, storeFile) store.Connect(ctx, storeFile)
if err != nil { return store
return nil, err
}
return ms.userDataStore, nil
} }
func (ms *MenuStorageService) GetResource(ctx context.Context) (resource.Resource, error) { func (ms *MenuStorageService) GetResource(ctx context.Context) (resource.Resource, error) {
ms.resourceStore = fsdb.NewFsDb() store := fsdb.NewFsDb()
err := ms.resourceStore.Connect(ctx, ms.resourceDir) err := store.Connect(ctx, ms.resourceDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
rfs := resource.NewDbResource(ms.resourceStore) rfs := resource.NewDbResource(store)
return rfs, nil return rfs, nil
} }
func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) { func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) {
if ms.stateStore != nil { store := gdbmdb.NewGdbmDb()
panic("set up store when already exists")
}
ms.stateStore = gdbmdb.NewGdbmDb()
storeFile := path.Join(ms.dbDir, "state.gdbm") storeFile := path.Join(ms.dbDir, "state.gdbm")
err := ms.stateStore.Connect(ctx, storeFile) store.Connect(ctx, storeFile)
if err != nil { return store, nil
return nil, err
}
return ms.stateStore, nil
} }
func (ms *MenuStorageService) EnsureDbDir() error { func (ms *MenuStorageService) EnsureDbDir() error {
@ -92,13 +71,3 @@ func (ms *MenuStorageService) EnsureDbDir() error {
} }
return nil 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
}