ssh: stderr support for the session printer

This commit is contained in:
Pavel 2023-11-19 00:54:51 +03:00
parent 408f88ebd2
commit eb4d78c483
4 changed files with 25 additions and 4 deletions

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/docker/go-connections v0.4.0
github.com/function61/gokit v0.0.0-20231117065306-355fe206d542
github.com/go-playground/validator/v10 v10.16.0
github.com/jonstacks/iomerge v0.0.0-20200607001240-c9a527e8abe8
github.com/kevinburke/ssh_config v1.2.0
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cast v1.5.1

3
go.sum
View File

@ -162,6 +162,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jonstacks/iomerge v0.0.0-20200607001240-c9a527e8abe8 h1:avdze4CXO+1TsCV84EH7ueX5WOc0GDjDYCyQWlC51Lo=
github.com/jonstacks/iomerge v0.0.0-20200607001240-c9a527e8abe8/go.mod h1:D+xdhbGYvTi/6hHTULOhUiYwEM89FvmRfPKEms6MJsc=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
@ -224,6 +226,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=

View File

@ -56,7 +56,9 @@ func (d *SSH) forward(val sshtun.Forward) conn {
d.Log())
ctx, cancel := context.WithCancel(d.Context())
tunDbgLog := d.Log().With("ssh-output", val.Remote.String())
go tun.Connect(ctx, sshtun.StdoutPrinterBannerCallback(tunDbgLog), sshtun.StdoutPrinterSessionCallback(tunDbgLog))
go tun.Connect(ctx,
sshtun.StdoutPrinterBannerCallback(tunDbgLog),
sshtun.StdoutPrinterSessionCallback(tunDbgLog))
return conn{ctx: ctx, cancel: cancel, tun: tun}
}

View File

@ -2,8 +2,10 @@ package sshtun
import (
"bufio"
"io"
"github.com/Neur0toxine/sshpoke/pkg/proto/ssh"
"github.com/jonstacks/iomerge"
"go.uber.org/zap"
)
@ -13,10 +15,23 @@ func StdoutPrinterSessionCallback(log *zap.SugaredLogger) SessionCallback {
if err != nil {
return
}
scan := bufio.NewScanner(stdout)
stderr, err := session.StderrPipe()
if err != nil {
return
}
combined := iomerge.NewStreamMerger(2)
combined.Write(func(readers chan<- io.Reader) {
readers <- stdout
readers <- stderr
})
combined.Read(func(r io.Reader) error {
scan := bufio.NewScanner(r)
for scan.Scan() {
log.Debug(scan.Text())
}
return nil
})
_ = combined.Wait()
}
}