move typescript logger
This commit is contained in:
parent
4bb9470d7d
commit
5dda30365e
5 changed files with 20 additions and 115 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
},
|
||||
}
|
||||
|
|
|
|||
88
cmd/sshpiperd/typescript.go
Normal file
88
cmd/sshpiperd/typescript.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
msgChannelData = 94
|
||||
)
|
||||
|
||||
type filePtyLogger struct {
|
||||
typescript *os.File
|
||||
timing *os.File
|
||||
|
||||
oldtime time.Time
|
||||
}
|
||||
|
||||
func newFilePtyLogger(outputdir string) (*filePtyLogger, error) {
|
||||
|
||||
now := time.Now()
|
||||
|
||||
filename := fmt.Sprintf("%d", now.Unix())
|
||||
|
||||
typescript, err := os.OpenFile(path.Join(outputdir, fmt.Sprintf("%v.typescript", filename)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = typescript.Write([]byte(fmt.Sprintf("Script started on %v\n", now.Format(time.ANSIC))))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
timing, err := os.OpenFile(path.Join(outputdir, fmt.Sprintf("%v.timing", filename)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &filePtyLogger{
|
||||
typescript: typescript,
|
||||
timing: timing,
|
||||
oldtime: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *filePtyLogger) loggingTty(msg []byte) ([]byte, error) {
|
||||
|
||||
if msg[0] == msgChannelData {
|
||||
|
||||
buf := msg[9:]
|
||||
|
||||
now := time.Now()
|
||||
|
||||
delta := now.Sub(l.oldtime)
|
||||
|
||||
// see term-utils/script.c
|
||||
fmt.Fprintf(l.timing, "%v.%06v %v\n", int64(delta/time.Second), int64(delta/time.Microsecond), len(buf))
|
||||
|
||||
l.oldtime = now
|
||||
|
||||
_, err := l.typescript.Write(buf)
|
||||
|
||||
if err != nil {
|
||||
return msg, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (l *filePtyLogger) Close() (err error) {
|
||||
// if _, err = ; err != nil {
|
||||
// return err
|
||||
// }
|
||||
_, _ = l.typescript.Write([]byte(fmt.Sprintf("Script done on %v\n", time.Now().Format(time.ANSIC))))
|
||||
|
||||
l.typescript.Close()
|
||||
l.timing.Close()
|
||||
|
||||
return nil // TODO
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue