ussd/cmd/ssh/main.go

145 lines
3.4 KiB
Go
Raw Permalink Normal View History

2024-09-26 16:15:06 +02:00
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path"
2024-09-26 16:15:06 +02:00
"sync"
"syscall"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging"
2025-01-06 09:11:37 +01:00
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers"
2024-09-26 16:15:06 +02:00
"git.grassecon.net/urdt/ussd/internal/ssh"
2025-01-06 09:11:37 +01:00
"git.grassecon.net/urdt/ussd/internal/storage"
2024-09-26 16:15:06 +02:00
)
var (
wg sync.WaitGroup
keyStore db.Db
2024-09-26 16:15:06 +02:00
logg = logging.NewVanilla()
scriptDir = path.Join("services", "registration")
build = "dev"
2024-09-26 16:15:06 +02:00
)
2025-01-06 09:11:37 +01:00
func init() {
initializers.LoadEnvVariables()
}
2024-09-26 16:15:06 +02:00
func main() {
2025-01-06 09:11:37 +01:00
config.LoadConfig()
var connStr string
var authConnStr string
2024-09-26 16:15:06 +02:00
var resourceDir string
var size uint
var engineDebug bool
var stateDebug bool
var host string
var port uint
2025-01-06 09:11:37 +01:00
flag.StringVar(&connStr, "c", "", "connection string")
flag.StringVar(&authConnStr, "authdb", "", "auth connection string")
2024-09-26 16:15:06 +02:00
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
2025-01-06 09:11:37 +01:00
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
2024-09-26 16:15:06 +02:00
flag.UintVar(&size, "s", 160, "max size of output")
2025-01-06 08:41:24 +01:00
flag.StringVar(&host, "h", "127.0.0.1", "socket host")
flag.UintVar(&port, "p", 7122, "socket port")
2024-09-26 16:15:06 +02:00
flag.Parse()
if connStr == "" {
2025-01-06 09:11:37 +01:00
connStr = config.DbConn
}
if authConnStr == "" {
authConnStr = connStr
}
connData, err := storage.ToConnData(connStr)
2025-01-06 09:11:37 +01:00
if err != nil {
fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
}
authConnData, err := storage.ToConnData(authConnStr)
if err != nil {
fmt.Fprintf(os.Stderr, "auth connstr err: %v", err)
os.Exit(1)
}
2025-01-06 09:11:37 +01:00
2024-09-26 16:15:06 +02:00
sshKeyFile := flag.Arg(0)
2025-01-06 09:11:37 +01:00
_, err = os.Stat(sshKeyFile)
2024-09-26 16:15:06 +02:00
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 <...>)")
logg.Infof("start command", "conn", connData, "authconn", authConnData, "resourcedir", resourceDir, "outputsize", size, "keyfile", sshKeyFile, "host", host, "port", port)
2024-09-26 16:15:06 +02:00
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
}
authKeyStore, err := ssh.NewSshKeyStore(ctx, authConnData.String())
2024-09-26 16:15:06 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "keystore file open error: %v", err)
os.Exit(1)
}
defer func() {
2024-09-26 16:15:06 +02:00
logg.TraceCtxf(ctx, "shutdown auth key store reached")
err = authKeyStore.Close()
if err != nil {
logg.ErrorCtxf(ctx, "keystore close error", "err", err)
}
}()
cint := make(chan os.Signal)
cterm := make(chan os.Signal)
signal.Notify(cint, os.Interrupt, syscall.SIGINT)
signal.Notify(cterm, os.Interrupt, syscall.SIGTERM)
runner := &ssh.SshRunner{
Cfg: cfg,
Debug: engineDebug,
FlagFile: pfp,
2025-01-06 09:11:37 +01:00
Conn: connData,
2024-09-26 16:15:06 +02:00
ResourceDir: resourceDir,
SrvKeyFile: sshKeyFile,
Host: host,
Port: port,
2024-09-26 16:15:06 +02:00
}
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-26 16:15:06 +02:00
}()
runner.Run(ctx, authKeyStore)
}