diff --git a/libplugin/template.go b/libplugin/template.go new file mode 100644 index 00000000..e5477837 --- /dev/null +++ b/libplugin/template.go @@ -0,0 +1,53 @@ +package libplugin + +import ( + "fmt" + "os" + + "github.com/urfave/cli/v2" +) + +type PluginTemplate struct { + Name string + Usage string + Flags []cli.Flag + CreateConfig func(c *cli.Context) (*SshPiperPluginConfig, error) +} + +func CreateAndRunPluginTemplate(t *PluginTemplate) { + app := &cli.App{ + Name: t.Name, + Usage: t.Usage, + Flags: t.Flags, + HideHelpCommand: true, + HideHelp: true, + Writer: os.Stderr, + ErrWriter: os.Stderr, + Action: func(c *cli.Context) error { + if t == nil { + return fmt.Errorf("plugin template is nil") + } + + if t.CreateConfig == nil { + return fmt.Errorf("plugin template create config is nil") + } + + config, err := t.CreateConfig(c) + if err != nil { + return err + } + + p, err := NewFromStdio(*config) + if err != nil { + return err + } + + ConfigStdioLogrus(p, nil) + return p.Serve() + }, + } + + if err := app.Run(os.Args); err != nil { + fmt.Fprintf(os.Stderr, "cannot start plugin: %v\n", err) + } +} diff --git a/plugin/fixed/main.go b/plugin/fixed/main.go index c80ef34d..7067464f 100644 --- a/plugin/fixed/main.go +++ b/plugin/fixed/main.go @@ -1,9 +1,6 @@ package main import ( - "fmt" - "os" - log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/libplugin" "github.com/urfave/cli/v2" @@ -11,11 +8,9 @@ import ( func main() { - app := &cli.App{ - Name: "fixed", - Usage: "sshpiperd fixed plugin, only password auth is supported", - HideHelpCommand: true, - HideHelp: true, + libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ + Name: "fixed", + Usage: "sshpiperd fixed plugin, only password auth is supported", Flags: []cli.Flag{ &cli.StringFlag{ Name: "target", @@ -24,17 +19,15 @@ func main() { Required: true, }, }, - Writer: os.Stderr, - ErrWriter: os.Stderr, - Action: func(c *cli.Context) error { + CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) { target := c.String("target") host, port, err := libplugin.SplitHostPortForSSH(target) if err != nil { - return err + return nil, err } - config := libplugin.SshPiperPluginConfig{ + return &libplugin.SshPiperPluginConfig{ PasswordCallback: func(conn libplugin.ConnMetadata, password []byte) (*libplugin.Upstream, error) { log.Info("routing to ", target) return &libplugin.Upstream{ @@ -43,23 +36,8 @@ func main() { IgnoreHostKey: true, Auth: libplugin.CreatePasswordAuth(password), }, nil - }, - } - - p, err := libplugin.NewFromStdio(config) - if err != nil { - return err - } - - libplugin.ConfigStdioLogrus(p, nil) - - log.Printf("starting fix routing to ssh endpoint %v (password only)", target) - return p.Serve() + }, nil }, - } - - if err := app.Run(os.Args); err != nil { - fmt.Fprintf(os.Stderr, "cannot start plugin: %v\n", err) - } + }) } diff --git a/plugin/simplemath/main.go b/plugin/simplemath/main.go index 3e3f777c..07571e2e 100644 --- a/plugin/simplemath/main.go +++ b/plugin/simplemath/main.go @@ -7,45 +7,43 @@ import ( log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/libplugin" + "github.com/urfave/cli/v2" ) func main() { - config := libplugin.SshPiperPluginConfig{ - KeyboardInteractiveCallback: func(conn libplugin.ConnMetadata, client libplugin.KeyboardInteractiveChallenge) (*libplugin.Upstream, error) { - client("lets do math", "", false) - for { + libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ + Name: "simplemath", + Usage: "sshpiperd simplemath plugin, do math before ssh login", + CreateConfig: func(_ *cli.Context) (*libplugin.SshPiperPluginConfig, error) { + return &libplugin.SshPiperPluginConfig{ + KeyboardInteractiveCallback: func(conn libplugin.ConnMetadata, client libplugin.KeyboardInteractiveChallenge) (*libplugin.Upstream, error) { + client("lets do math", "", false) - a := rand.Intn(10) - b := rand.Intn(10) + for { - ans, err := client("", fmt.Sprintf("what is %v + %v = ", a, b), true) - if err != nil { - return nil, err - } + a := rand.Intn(10) + b := rand.Intn(10) - log.Printf("got ans = %v", ans) + ans, err := client("", fmt.Sprintf("what is %v + %v = ", a, b), true) + if err != nil { + return nil, err + } - if ans == fmt.Sprintf("%v", a+b) { - return &libplugin.Upstream{ - Auth: libplugin.CreateNextPluginAuth(map[string]string{ - "a": strconv.Itoa(a), - "b": strconv.Itoa(b), - "ans": ans, - }), - }, nil - } - } + log.Printf("got ans = %v", ans) + + if ans == fmt.Sprintf("%v", a+b) { + return &libplugin.Upstream{ + Auth: libplugin.CreateNextPluginAuth(map[string]string{ + "a": strconv.Itoa(a), + "b": strconv.Itoa(b), + "ans": ans, + }), + }, nil + } + } + }, + }, nil }, - } - - p, err := libplugin.NewFromStdio(config) - if err != nil { - panic(err) - } - - libplugin.ConfigStdioLogrus(p, nil) - - log.Printf("starting simple math additional auth") - panic(p.Serve()) + }) }