Enable env, config, db in ssh

This commit is contained in:
lash 2025-01-06 08:11:37 +00:00
parent 5c7a535288
commit 52fd1eced2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 40 additions and 26 deletions

View File

@ -35,8 +35,10 @@ func main() {
var sessionId string var sessionId string
var database string var database string
var engineDebug bool var engineDebug bool
var resourceDir string
var err error var err error
flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir")
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&connStr, "c", "", "connection string") flag.StringVar(&connStr, "c", "", "connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
@ -67,7 +69,6 @@ func main() {
MenuSeparator: menuSeparator, MenuSeparator: menuSeparator,
} }
resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(connData, resourceDir) menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)

View File

@ -14,7 +14,10 @@ import (
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers"
"git.grassecon.net/urdt/ussd/internal/ssh" "git.grassecon.net/urdt/ussd/internal/ssh"
"git.grassecon.net/urdt/ussd/internal/storage"
) )
var ( var (
@ -24,7 +27,14 @@ var (
scriptDir = path.Join("services", "registration") scriptDir = path.Join("services", "registration")
) )
func init() {
initializers.LoadEnvVariables()
}
func main() { func main() {
config.LoadConfig()
var connStr string
var dbDir string var dbDir string
var resourceDir string var resourceDir string
var size uint var size uint
@ -32,17 +42,25 @@ func main() {
var stateDebug bool var stateDebug bool
var host string var host string
var port uint var port uint
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") flag.StringVar(&connStr, "c", "", "connection string")
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", "127.0.0.1", "socket host") flag.StringVar(&host, "h", "127.0.0.1", "socket host")
flag.UintVar(&port, "p", 7122, "socket port") flag.UintVar(&port, "p", 7122, "socket port")
flag.Parse() flag.Parse()
if connStr != "" {
connStr = config.DbConn
}
connData, err := storage.ToConnData(config.DbConn)
if err != nil {
fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
}
sshKeyFile := flag.Arg(0) sshKeyFile := flag.Arg(0)
_, err := os.Stat(sshKeyFile) _, err = os.Stat(sshKeyFile)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "cannot open ssh server private key file: %v\n", err) fmt.Fprintf(os.Stderr, "cannot open ssh server private key file: %v\n", err)
os.Exit(1) os.Exit(1)
@ -93,7 +111,7 @@ func main() {
Cfg: cfg, Cfg: cfg,
Debug: engineDebug, Debug: engineDebug,
FlagFile: pfp, FlagFile: pfp,
DbDir: dbDir, Conn: connData,
ResourceDir: resourceDir, ResourceDir: resourceDir,
SrvKeyFile: sshKeyFile, SrvKeyFile: sshKeyFile,
Host: host, Host: host,

View File

@ -71,6 +71,20 @@ func(a *auther) Get(k []byte) (string, error) {
return v, nil return v, nil
} }
type SshRunner struct {
Ctx context.Context
Cfg engine.Config
FlagFile string
Conn storage.ConnData
ResourceDir string
Debug bool
SrvKeyFile string
Host string
Port uint
wg sync.WaitGroup
lst net.Listener
}
func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error { func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error {
if ch == nil { if ch == nil {
return errors.New("nil channel") return errors.New("nil channel")
@ -128,32 +142,13 @@ func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChanne
return nil return nil
} }
type SshRunner struct {
Ctx context.Context
Cfg engine.Config
FlagFile string
DbDir string
ResourceDir string
Debug bool
SrvKeyFile string
Host string
Port uint
wg sync.WaitGroup
lst net.Listener
}
func(s *SshRunner) Stop() error { func(s *SshRunner) Stop() error {
return s.lst.Close() return s.lst.Close()
} }
func(s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) { func(s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) {
ctx := s.Ctx ctx := s.Ctx
menuStorageService := storage.NewMenuStorageService(s.DbDir, s.ResourceDir) menuStorageService := storage.NewMenuStorageService(s.Conn, s.ResourceDir)
err := menuStorageService.EnsureDbDir()
if err != nil {
return nil, nil, err
}
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)
if err != nil { if err != nil {