2024-07-16 03:00:10 +02:00
|
|
|
package shellexec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
|
2024-08-10 03:49:35 +02:00
|
|
|
"github.com/creack/pty"
|
2024-07-16 03:00:10 +02:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConnInterface interface {
|
|
|
|
Kill()
|
|
|
|
Wait() error
|
|
|
|
Start() error
|
|
|
|
StdinPipe() (io.WriteCloser, error)
|
|
|
|
StdoutPipe() (io.ReadCloser, error)
|
|
|
|
StderrPipe() (io.ReadCloser, error)
|
2024-07-19 01:56:00 +02:00
|
|
|
SetSize(w int, h int) error
|
2024-08-10 03:49:35 +02:00
|
|
|
pty.Pty
|
2024-07-16 03:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CmdWrap struct {
|
|
|
|
Cmd *exec.Cmd
|
2024-08-10 03:49:35 +02:00
|
|
|
pty.Pty
|
2024-07-16 03:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cw CmdWrap) Kill() {
|
|
|
|
cw.Cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw CmdWrap) Wait() error {
|
|
|
|
return cw.Cmd.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw CmdWrap) Start() error {
|
|
|
|
defer func() {
|
|
|
|
for _, extraFile := range cw.Cmd.ExtraFiles {
|
|
|
|
if extraFile != nil {
|
|
|
|
extraFile.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return cw.Cmd.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw CmdWrap) StdinPipe() (io.WriteCloser, error) {
|
|
|
|
return cw.Cmd.StdinPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw CmdWrap) StdoutPipe() (io.ReadCloser, error) {
|
|
|
|
return cw.Cmd.StdoutPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw CmdWrap) StderrPipe() (io.ReadCloser, error) {
|
|
|
|
return cw.Cmd.StderrPipe()
|
|
|
|
}
|
|
|
|
|
2024-07-19 01:56:00 +02:00
|
|
|
func (cw CmdWrap) SetSize(w int, h int) error {
|
2024-08-10 03:49:35 +02:00
|
|
|
err := pty.Setsize(cw.Pty, &pty.Winsize{Rows: uint16(w), Cols: uint16(h)})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-19 01:56:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-16 03:00:10 +02:00
|
|
|
type SessionWrap struct {
|
|
|
|
Session *ssh.Session
|
|
|
|
StartCmd string
|
2024-08-10 03:49:35 +02:00
|
|
|
Tty pty.Tty
|
|
|
|
pty.Pty
|
2024-07-16 03:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sw SessionWrap) Kill() {
|
|
|
|
sw.Tty.Close()
|
|
|
|
sw.Session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw SessionWrap) Wait() error {
|
|
|
|
return sw.Session.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw SessionWrap) Start() error {
|
|
|
|
return sw.Session.Start(sw.StartCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw SessionWrap) StdinPipe() (io.WriteCloser, error) {
|
|
|
|
return sw.Session.StdinPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw SessionWrap) StdoutPipe() (io.ReadCloser, error) {
|
|
|
|
stdoutReader, err := sw.Session.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return io.NopCloser(stdoutReader), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw SessionWrap) StderrPipe() (io.ReadCloser, error) {
|
|
|
|
stderrReader, err := sw.Session.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return io.NopCloser(stderrReader), nil
|
|
|
|
}
|
2024-07-19 01:56:00 +02:00
|
|
|
|
|
|
|
func (sw SessionWrap) SetSize(h int, w int) error {
|
|
|
|
return sw.Session.WindowChange(h, w)
|
|
|
|
}
|