ussd/cmd/ssh/main.go

116 lines
2.8 KiB
Go
Raw Normal View History

2024-09-22 04:06:24 +02:00
package main
import (
"context"
"flag"
"fmt"
"path"
"os"
"os/signal"
2024-09-22 04:06:24 +02:00
"sync"
"syscall"
2024-09-22 04:06:24 +02:00
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging"
2024-09-22 15:41:01 +02:00
"git.grassecon.net/urdt/ussd/internal/ssh"
2024-09-22 04:06:24 +02:00
)
var (
wg sync.WaitGroup
keyStore db.Db
logg = logging.NewVanilla()
scriptDir = path.Join("services", "registration")
)
func main() {
var dbDir string
var resourceDir string
var size uint
var engineDebug bool
var stateDebug bool
var host string
var port uint
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.BoolVar(&engineDebug, "engine-debug", 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.StringVar(&host, "h", "127.0.0.1", "http host")
flag.UintVar(&port, "p", 7122, "http port")
flag.Parse()
sshKeyFile := flag.Arg(0)
_, err := os.Stat(sshKeyFile)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot open ssh server private key file: %v\n", err)
os.Exit(1)
}
ctx := context.Background()
logg.WarnCtxf(ctx, "!!!!! WARNING WARNING WARNING")
logg.WarnCtxf(ctx, "!!!!! =======================")
logg.WarnCtxf(ctx, "!!!!! This is not a production ready server!")
logg.WarnCtxf(ctx, "!!!!! Do not expose to internet and only use with tunnel!")
logg.WarnCtxf(ctx, "!!!!! (See ssh -L <...>)")
2024-09-22 04:06:24 +02:00
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "keyfile", sshKeyFile, "host", host, "port", port)
pfp := path.Join(scriptDir, "pp.csv")
cfg := engine.Config{
Root: "root",
OutputSize: uint32(size),
FlagCount: uint32(16),
}
if stateDebug {
cfg.StateDebug = true
}
if engineDebug {
cfg.EngineDebug = true
}
2024-09-22 15:41:01 +02:00
authKeyStore, err := ssh.NewSshKeyStore(ctx, dbDir)
2024-09-22 04:06:24 +02:00
if err != nil {
2024-09-22 15:41:01 +02:00
fmt.Fprintf(os.Stderr, "keystore file open error: %v", err)
2024-09-22 04:06:24 +02:00
os.Exit(1)
}
defer func () {
logg.TraceCtxf(ctx, "shutdown auth key store reached")
err = authKeyStore.Close()
2024-09-22 15:41:01 +02:00
if err != nil {
logg.ErrorCtxf(ctx, "keystore close error", "err", err)
}
}()
2024-09-22 04:06:24 +02:00
cint := make(chan os.Signal)
cterm := make(chan os.Signal)
signal.Notify(cint, os.Interrupt, syscall.SIGINT)
signal.Notify(cterm, os.Interrupt, syscall.SIGTERM)
2024-09-22 15:41:01 +02:00
runner := &ssh.SshRunner{
2024-09-22 04:06:24 +02:00
Cfg: cfg,
Debug: engineDebug,
FlagFile: pfp,
DbDir: dbDir,
ResourceDir: resourceDir,
2024-09-22 15:41:01 +02:00
SrvKeyFile: sshKeyFile,
2024-09-22 04:06:24 +02:00
Host: host,
Port: port,
}
go func() {
select {
case _ = <-cint:
case _ = <-cterm:
}
logg.TraceCtxf(ctx, "shutdown runner reached")
err := runner.Stop()
if err != nil {
logg.ErrorCtxf(ctx, "runner stop error", "err", err)
}
}()
2024-09-22 15:41:01 +02:00
runner.Run(ctx, authKeyStore)
2024-09-22 04:06:24 +02:00
}