template for easier plugin
This commit is contained in:
parent
5dda30365e
commit
8d56673982
3 changed files with 91 additions and 62 deletions
53
libplugin/template.go
Normal file
53
libplugin/template.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue