From eb4d78c48394159807c41c0bb9f9c339531166ff Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Sun, 19 Nov 2023 00:54:51 +0300 Subject: [PATCH] ssh: stderr support for the session printer --- go.mod | 1 + go.sum | 3 +++ internal/server/driver/ssh/driver.go | 4 +++- internal/server/driver/ssh/sshtun/printer.go | 21 +++++++++++++++++--- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 2eac9ba..c76879d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index dec80be..bbeb8ef 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/server/driver/ssh/driver.go b/internal/server/driver/ssh/driver.go index eb06d61..41754d2 100644 --- a/internal/server/driver/ssh/driver.go +++ b/internal/server/driver/ssh/driver.go @@ -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} } diff --git a/internal/server/driver/ssh/sshtun/printer.go b/internal/server/driver/ssh/sshtun/printer.go index c176a47..0805ed6 100644 --- a/internal/server/driver/ssh/sshtun/printer.go +++ b/internal/server/driver/ssh/sshtun/printer.go @@ -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) - for scan.Scan() { - log.Debug(scan.Text()) + 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() } }