move typescript logger

This commit is contained in:
Boshi Lian 2022-07-04 13:29:12 +00:00
parent 4bb9470d7d
commit 5dda30365e
5 changed files with 20 additions and 115 deletions

View file

@ -17,6 +17,8 @@ type daemon struct {
config *ssh.PiperConfig
lis net.Listener
loginGraceTime time.Duration
uphook, downhook func(msg []byte) ([]byte, error)
}
func newDaemon(ctx *cli.Context) (*daemon, error) {
@ -126,8 +128,7 @@ func (d *daemon) run() error {
log.Infof("ssh connection pipe created %v -> %v", p.DownstreamConnMeta().RemoteAddr(), p.UpstreamConnMeta().RemoteAddr().String())
// TODO add screen recording
err = p.Wait()
err = p.WaitWithHook(d.uphook, d.downhook)
log.Infof("connection from %v closed reason: %v", c.RemoteAddr(), err)
}(conn)

View file

@ -113,6 +113,12 @@ func main() {
Usage: "log level, one of: trace, debug, info, warn, error, fatal, panic",
EnvVars: []string{"SSHPIPERD_LOG_LEVEL"},
},
&cli.StringFlag{
Name: "typescript-log-dir",
Value: "",
Usage: "create typescript format screen recording and save into the directory see https://linux.die.net/man/1/script",
EnvVars: []string{"SSHPIPERD_TYPESCRIPT_LOG_DIR"},
},
},
Action: func(ctx *cli.Context) error {
level, err := log.ParseLevel(ctx.String("log-level"))
@ -158,6 +164,16 @@ func main() {
return err
}
recorddir := ctx.String("typescript-log-dir")
if recorddir != "" {
recorder, err := newFilePtyLogger(recorddir)
if err != nil {
return err
}
d.uphook = recorder.loggingTty
}
return d.run()
},
}

View file

@ -1,4 +1,4 @@
package typescriptlogger
package main
import (
"fmt"

View file

@ -1,59 +0,0 @@
package auditor
import (
"golang.org/x/crypto/ssh"
"github.com/tg123/sshpiper/sshpiperd/registry"
)
// Hook is called after ssh connection pipe is established and all msg will be
// put into the hook and msg will be converted to the return value of this func
type Hook func(msg []byte) ([]byte, error)
// Auditor holds Hooks for upstream and downstream
type Auditor interface {
// All msg between piper and upstream will be put into the hook
// nil for ignore
GetUpstreamHook() Hook
// All msg between piper and downstream will be put into the hook
// nil for ignore
GetDownstreamHook() Hook
// Will be called when connection closed
Close() error
}
// Provider is a factory for Auditor
type Provider interface {
registry.Plugin
// Will be called when piped connection established
// nil for no Auditor needed for this connection
Create(ssh.ConnMetadata) (Auditor, error)
}
var (
drivers = registry.NewRegistry()
)
// Register adds an auditor with given name to registry
func Register(name string, driver Provider) {
drivers.Register(name, driver)
}
// All return all registered auditors
func All() []string {
return drivers.Drivers()
}
// Get returns an auditor by name, return nil if not found
func Get(name string) Provider {
if d, ok := drivers.Get(name).(Provider); ok {
return d
}
return nil
}

View file

@ -1,53 +0,0 @@
package typescriptlogger
import (
"os"
"path"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"github.com/tg123/sshpiper/sshpiperd/auditor"
)
type plugin struct {
Config struct {
OutputDir string `long:"auditor-typescriptlogger-outputdir" default:"/var/sshpiper" description:"Place where logged typescript files were saved" env:"SSHPIPERD_AUDITOR_TYPESCRIPTLOGGER_OUTPUTDIR" ini-name:"auditor-typescriptlogger-outputdir"`
}
}
func (p *plugin) GetName() string {
return "typescript-logger"
}
func (p *plugin) GetOpts() interface{} {
return &p.Config
}
func (p *plugin) Create(conn ssh.ConnMetadata) (auditor.Auditor, error) {
dir := path.Join(p.Config.OutputDir, conn.User())
err := os.MkdirAll(dir, 0700)
if err != nil {
return nil, err
}
return newFilePtyLogger(dir)
}
func (p *plugin) Init(logger *log.Logger) error {
return nil
}
func (l *filePtyLogger) GetUpstreamHook() auditor.Hook {
return l.loggingTty
}
func (l *filePtyLogger) GetDownstreamHook() auditor.Hook {
return nil
}
func init() {
auditor.Register("typescript-logger", new(plugin))
}