sshpoke/cmd/root.go

160 lines
4.2 KiB
Go
Raw Normal View History

2023-11-16 20:09:40 +03:00
package cmd
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/Neur0toxine/sshpoke/internal/api/plugin"
2023-11-22 23:31:35 +03:00
"github.com/Neur0toxine/sshpoke/internal/api/rest"
2023-11-16 20:09:40 +03:00
"github.com/Neur0toxine/sshpoke/internal/config"
"github.com/Neur0toxine/sshpoke/internal/docker"
"github.com/Neur0toxine/sshpoke/internal/logger"
2023-11-17 20:39:00 +03:00
"github.com/Neur0toxine/sshpoke/internal/server"
"github.com/Neur0toxine/sshpoke/pkg/dto"
plugin2 "github.com/Neur0toxine/sshpoke/pkg/plugin"
2023-11-17 20:39:00 +03:00
"github.com/go-playground/validator/v10"
2023-11-16 20:09:40 +03:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
2023-11-19 13:13:38 +03:00
var (
cfgFile string
Version string
)
2023-11-16 20:09:40 +03:00
var rootCmd = &cobra.Command{
2023-11-19 13:13:38 +03:00
Use: "sshpoke",
Version: Version,
Short: "Expose your Docker services to the Internet via SSH.",
Long: `sshpoke is a CLI application that listens to the docker socket and automatically exposes relevant services to the Internet.`,
2023-11-16 20:09:40 +03:00
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
2023-11-17 20:39:00 +03:00
server.DefaultManager = server.NewManager(ctx, config.Default.Servers, config.Default.DefaultServer)
runPluginServer()
2023-11-22 23:31:35 +03:00
runRestServer()
runDockerEventListener(ctx)
shutdown := makeShutdownFunc(cancel)
2023-11-16 20:09:40 +03:00
linuxSig := make(chan os.Signal, 1)
signal.Notify(linuxSig)
for sig := range linuxSig {
switch sig {
case os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM:
shutdown(sig)
2023-11-16 20:09:40 +03:00
default:
}
}
shutdown(syscall.SIGHUP)
2023-11-16 20:09:40 +03:00
},
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "config.yml", "Configuration file (default is config.yml)")
}
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
workingDir, err := os.Getwd()
cobra.CheckErr(err)
viper.AddConfigPath(workingDir)
viper.SetConfigType("yml")
viper.SetConfigName("config")
}
log := logger.New(os.Getenv("SSHPOKE_DEBUG") == "true").Sugar()
viper.SetEnvPrefix("SSHPOKE")
viper.AutomaticEnv()
2023-11-17 20:39:00 +03:00
if err := config.BindStructEnv(&config.Default); err != nil {
2023-11-16 20:09:40 +03:00
log.Fatalf("cannot bind configuration keys: %s", err)
}
if err := viper.ReadInConfig(); err == nil {
log.Debugf("using config file: %s", viper.ConfigFileUsed())
}
2023-11-17 20:39:00 +03:00
if err := viper.Unmarshal(&config.Default); err != nil {
2023-11-16 20:09:40 +03:00
log.Fatalf("cannot load configuration: %s", err)
}
2023-11-17 20:39:00 +03:00
if err := validator.New().Struct(config.Default); err != nil {
log.Fatalf("invalid configuration: %s", err)
}
2023-11-22 23:31:35 +03:00
if config.Default.API.Rest.Port == 0 {
config.Default.API.Rest.Port = rest.DefaultPort
}
if config.Default.API.Plugin.Port == 0 {
config.Default.API.Plugin.Port = plugin2.DefaultPort
}
2023-11-16 20:09:40 +03:00
logger.Initialize()
2023-11-17 20:39:00 +03:00
logger.Sugar.Debugw("configuration loaded", "config", config.Default)
2023-11-16 20:09:40 +03:00
}
func runPluginServer() {
port := config.Default.API.Plugin.Port
if port == 0 {
port = plugin2.DefaultPort
}
go plugin.StartServer(port, logger.Sugar.With("component", "pluginServer"))
}
2023-11-22 23:31:35 +03:00
func runRestServer() {
port := config.Default.API.Rest.Port
if port == 0 {
2023-11-22 23:31:35 +03:00
port = rest.DefaultPort
}
2023-11-22 23:31:35 +03:00
go rest.StartServer(
config.Default.API.Rest.Token, port, logger.Sugar.With("component", "webServer"), config.Default.Debug)
}
func runDockerEventListener(ctx context.Context) {
var err error
docker.Default, err = docker.New(ctx)
if err != nil {
logger.Sugar.Fatalf("cannot connect to docker daemon: %s", err)
}
for id, item := range docker.Default.Containers() {
err := server.DefaultManager.ProcessEvent(dto.Event{
Type: dto.EventStart,
Container: item,
})
if err != nil {
logger.Sugar.Errorw("cannot expose container", "id", id, "error", err)
}
}
events, err := docker.Default.Listen()
if err != nil {
logger.Sugar.Fatalf("cannot listen to docker events: %s", err)
}
go func() {
logger.Sugar.Debug("listening for docker events...")
for event := range events {
err := server.DefaultManager.ProcessEvent(event)
if err != nil {
logger.Sugar.Errorw("cannot expose container",
"id", event.Container.ID, "error", err)
}
}
}()
}
func makeShutdownFunc(cancel func()) func(os.Signal) {
return func(sig os.Signal) {
cancel()
server.DefaultManager.WaitForShutdown()
logger.Sugar.Infof("received %s, exiting...", sig)
os.Exit(0)
}
}