2023-11-18 21:23:29 +03:00
|
|
|
package sshtun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-11-19 00:54:51 +03:00
|
|
|
"io"
|
2023-11-18 21:23:29 +03:00
|
|
|
|
2023-11-18 21:51:44 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/pkg/proto/ssh"
|
2023-11-19 00:54:51 +03:00
|
|
|
"github.com/jonstacks/iomerge"
|
2023-11-18 21:23:29 +03:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-11-19 13:06:38 +03:00
|
|
|
func OutputReaderCallback(callback func(string)) SessionCallback {
|
2023-11-18 21:23:29 +03:00
|
|
|
return func(session *ssh.Session) {
|
|
|
|
stdout, err := session.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-11-19 00:54:51 +03:00
|
|
|
stderr, err := session.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
2023-11-18 21:23:29 +03:00
|
|
|
}
|
2023-11-19 00:54:51 +03:00
|
|
|
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() {
|
2023-11-19 13:06:38 +03:00
|
|
|
callback(scan.Text())
|
2023-11-19 00:54:51 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
_ = combined.Wait()
|
2023-11-18 21:23:29 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-18 23:02:41 +03:00
|
|
|
|
2023-11-19 13:06:38 +03:00
|
|
|
func BannerDebugLogCallback(log *zap.SugaredLogger) ssh.BannerCallback {
|
2023-11-18 23:02:41 +03:00
|
|
|
return func(msg string) error {
|
2023-11-21 18:55:04 +03:00
|
|
|
log.Debug("banner: ", msg)
|
2023-11-18 23:02:41 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|