Skip to content

Commit

Permalink
v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
VHSgunzo committed Apr 29, 2024
1 parent 691498d commit c6978f6
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 53 deletions.
15 changes: 14 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ require (
golang.org/x/term v0.19.0
)

require golang.org/x/sys v0.19.0 // indirect
require (
github.com/shirou/gopsutil/v3 v3.24.3
golang.org/x/sys v0.19.0
)

require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
)
46 changes: 46 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v3 v3.24.3 h1:eoUGJSmdfLzJ3mxIhmOAhgKEKgQkeOwKpz1NbhVnuPE=
github.com/shirou/gopsutil/v3 v3.24.3/go.mod h1:JpND7O217xa72ewWz9zN2eIIkPWsDN/3pl0H8Qt0uwg=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
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=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
174 changes: 149 additions & 25 deletions shellsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os/signal"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
Expand All @@ -22,6 +23,8 @@ import (

"github.com/creack/pty"
"github.com/hashicorp/yamux"
"github.com/shirou/gopsutil/v3/process"
"golang.org/x/sys/unix"
"golang.org/x/term"
)

Expand Down Expand Up @@ -254,6 +257,38 @@ func ssrv_env_vars_parse() {
}
}

func child_pids_walk(pid int, wg *sync.WaitGroup, child_pids *[]int) {
defer wg.Done()
proc, _ := process.NewProcess(int32(pid))
proc_child, _ := proc.Children()
for _, child := range proc_child {
*child_pids = append(*child_pids, int(child.Pid))
wg.Add(1)
go child_pids_walk(int(child.Pid), wg, child_pids)
}
}

func get_child_pids(pid int) []int {
var wg sync.WaitGroup
var child_pids []int
wg.Add(1)
child_pids_walk(int(pid), &wg, &child_pids)
wg.Wait()
sort.Slice(child_pids, func(i, j int) bool {
return child_pids[i] < child_pids[j]
})
return child_pids
}

func is_pid_exist(pid int) bool {
proc, err := os.FindProcess(pid)
if err != nil {
return false
}
err = proc.Signal(syscall.SIGCONT)
return err == nil
}

func srv_handle(conn net.Conn, self_cpids_dir string) {
var wg sync.WaitGroup
disconnect := func(session *yamux.Session, remote string) {
Expand Down Expand Up @@ -380,6 +415,7 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
exec_cmd.Stdin = stdin_channel
cmd_stdout, _ = exec_cmd.StdoutPipe()
cmd_stderr, _ = exec_cmd.StderrPipe()
exec_cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = exec_cmd.Start()
}
if err != nil {
Expand All @@ -391,8 +427,9 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
return
}

cmd_pid := strconv.Itoa(exec_cmd.Process.Pid)
log.Printf("[%s] pid: %s", remote, cmd_pid)
cmd_pid := exec_cmd.Process.Pid
cmd_pgid, _ := syscall.Getpgid(cmd_pid)
log.Printf("[%s] pid: %d", remote, cmd_pid)

cpid := fmt.Sprint(self_cpids_dir, "/", cmd_pid)
if is_dir_exists(self_cpids_dir) {
Expand All @@ -406,18 +443,19 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
touch_file(cpid)
}
}
defer os.Remove(cpid)

cp := func(dst io.Writer, src io.Reader) {
defer wg.Done()
io.Copy(dst, src)
}

control_channel, err := session.Accept()
if err != nil {
log.Printf("[%s] control channel accept error: %v", remote, err)
return
}
if is_alloc_pty {
control_channel, err := session.Accept()
if err != nil {
log.Printf("[%s] control channel accept error: %v", remote, err)
return
}
go func() {
decoder := gob.NewDecoder(control_channel)
for {
Expand All @@ -441,6 +479,52 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
go cp(data_channel, cmd_ptmx)
go cp(cmd_ptmx, data_channel)
} else {
go func() {
exec_cmd_kill := func(sig syscall.Signal) {
child_pids := []int{cmd_pid}
child_pids = append(child_pids, get_child_pids(cmd_pid)...)

syscall.Kill(-cmd_pgid, sig)
pgid_wait := time.Second
for {
if is_pid_exist(-cmd_pgid) && pgid_wait != 0 {
pgid_wait -= 10 * time.Millisecond
time.Sleep(10 * time.Millisecond)
} else {
break
}
}

for _, pid := range child_pids {
syscall.Kill(pid, sig)
if is_pid_exist(pid) &&
sig != syscall.SIGHUP &&
sig != syscall.SIGUSR1 &&
sig != syscall.SIGUSR2 {
syscall.Kill(pid, syscall.SIGKILL)
}
}
}
reader := bufio.NewReader(control_channel)
sig, err := reader.ReadString('\r')
if err != nil && err != io.EOF {
log.Printf("[%s] control channel reader error: %v", remote, err)
}
switch sig {
case "SIGINT\r":
exec_cmd_kill(syscall.SIGINT)
case "SIGTERM\r":
exec_cmd_kill(syscall.SIGTERM)
case "SIGQUIT\r":
exec_cmd_kill(syscall.SIGQUIT)
case "SIGHUP\r":
exec_cmd_kill(syscall.SIGHUP)
case "SIGUSR1\r":
exec_cmd_kill(syscall.SIGUSR1)
case "SIGUSR2\r":
exec_cmd_kill(syscall.SIGUSR2)
}
}()
wg.Add(2)
go cp(data_channel, cmd_stdout)
go cp(stderr_channel, cmd_stderr)
Expand All @@ -465,7 +549,6 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
}

wg.Wait()
os.Remove(cpid)
}

func server(proto, socket string) {
Expand Down Expand Up @@ -600,7 +683,9 @@ func client(proto, socket string, exec_args []string) int {
}
defer conn.Close()

session, err := yamux.Client(conn, nil)
yamux_config := yamux.DefaultConfig()
yamux_config.StreamOpenTimeout = 0
session, err := yamux.Client(conn, yamux_config)
if err != nil {
log.Fatalf("session error: %v", err)
}
Expand All @@ -616,22 +701,33 @@ func client(proto, socket string, exec_args []string) int {
if term.IsTerminal(stdin) {
is_stdin_term = true
}

stdout := int(os.Stdout.Fd())
is_stdout_term := false
if term.IsTerminal(stdout) {
is_stdout_term = true
}

stderr := int(os.Stderr.Fd())
is_stderr_term := false
if term.IsTerminal(stderr) {
is_stderr_term = true
}

pid := os.Getpid()
pgid, err := unix.Getpgid(pid)
if err != nil {
log.Fatalf("error getting process group ID: %v", err)
}
is_foreground := true
tpgid, err := unix.IoctlGetInt(unix.Stdin, unix.TIOCGPGRP)
if err == nil && pgid != tpgid {
is_foreground = false
}

var term_old_state *term.State
if (is_stdin_term && is_stderr_term && is_stdout_term) || (*is_pty && is_stdin_term) {
if (is_stdin_term && is_stderr_term && is_stdout_term && is_foreground) ||
(*is_pty && is_stdin_term) {
if is_alloc_pty && is_stdin_term {
is_foreground = true
term_old_state, err = term.MakeRaw(stdin)
if err != nil {
log.Fatalf("unable to make terminal raw: %v", err)
Expand Down Expand Up @@ -727,11 +823,11 @@ func client(proto, socket string, exec_args []string) int {
io.Copy(dst, src)
}

control_channel, err := session.Open()
if err != nil {
log.Fatalf("control channel open error: %v", err)
}
if is_alloc_pty {
control_channel, err := session.Open()
if err != nil {
log.Fatalf("control channel open error: %v", err)
}
go func() {
encoder := gob.NewEncoder(control_channel)
sig := make(chan os.Signal, 1)
Expand All @@ -751,14 +847,40 @@ func client(proto, socket string, exec_args []string) int {
<-sig
}
}()
} else {
sig_chan := make(chan os.Signal, 1)
signal.Notify(sig_chan, os.Interrupt,
syscall.SIGINT, syscall.SIGTERM,
syscall.SIGHUP, syscall.SIGQUIT,
syscall.SIGUSR1, syscall.SIGUSR2,
)
go func() {
sig := <-sig_chan
switch sig {
case syscall.SIGINT:
control_channel.Write([]byte("SIGINT\r"))
case syscall.SIGTERM:
control_channel.Write([]byte("SIGTERM\r"))
case syscall.SIGQUIT:
control_channel.Write([]byte("SIGQUIT\r"))
case syscall.SIGHUP:
control_channel.Write([]byte("SIGHUP\r"))
case syscall.SIGUSR1:
control_channel.Write([]byte("SIGUSR1\r"))
case syscall.SIGUSR2:
control_channel.Write([]byte("SIGUSR2\r"))
}
}()
}

if !is_stdin_term {
wg.Add(1)
go pipe_stdin(stdin_channel, os.Stdin)
} else {
wg.Add(1)
go cp(data_channel, os.Stdin)
if is_foreground {
if !is_stdin_term {
wg.Add(1)
go pipe_stdin(stdin_channel, os.Stdin)
} else {
wg.Add(1)
go cp(data_channel, os.Stdin)
}
}
if !is_alloc_pty {
wg.Add(1)
Expand All @@ -777,15 +899,17 @@ func client(proto, socket string, exec_args []string) int {
if err != nil {
log.Printf("failed to parse exit code: %v", err)
}
} else {
} else if err != io.EOF {
log.Printf("error reading from command channel: %v", err)
}

if term_old_state != nil {
term.Restore(stdin, term_old_state)
wg.Done()
if is_foreground {
wg.Done()
}
}
if is_stdin_term && ((!*is_pty && !*is_no_pty) ||
if is_foreground && is_stdin_term && ((!*is_pty && !*is_no_pty) ||
(*is_no_pty && (!is_stdout_term || !is_stderr_term)) || *is_no_pty) {
if !is_stderr_term || !is_alloc_pty {
wg.Done()
Expand Down
13 changes: 12 additions & 1 deletion tls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ go 1.21.6
require (
github.com/creack/pty v1.1.21
github.com/hashicorp/yamux v0.1.1
github.com/shirou/gopsutil/v3 v3.24.3
golang.org/x/term v0.19.0
)

require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
)

require (
golang.org/x/crypto v0.22.0
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.19.0
)
Loading

0 comments on commit c6978f6

Please sign in to comment.