diff --git a/core/code/xterm.go b/core/code/xterm.go index 183102fb..b112cec0 100644 --- a/core/code/xterm.go +++ b/core/code/xterm.go @@ -1,49 +1,57 @@ package code import ( - "io" + "encoding/base64" "os" + "os/exec" + "sync" + "github.com/creack/pty" ice "shylinux.com/x/icebergs" - "shylinux.com/x/icebergs/base/cli" "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/web" + kit "shylinux.com/x/toolkits" ) const XTERM = "xterm" func init() { - cache := map[string]io.Writer{} - cache1 := map[string]io.Writer{} + cache := sync.Map{} + add := func(m *ice.Message, key string) string { + cmd := exec.Command("/bin/sh") + cmd.Env = append(os.Environ(), "TERM=xterm") + tty, err := pty.Start(cmd) + m.Assert(err) + m.Go(func() { + buf := make([]byte, 1024) + for { + if n, e := tty.Read(buf); m.Assert(e) { + m.PushNotice("grow", base64.StdEncoding.EncodeToString(buf[:n])) + } + } + }) + cache.Store(key, tty) + return key + } Index.MergeCommands(ice.Commands{ XTERM: {Name: "xterm auto", Help: "终端", Actions: ice.MergeAction(ice.Actions{ + ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { + m.Watch(web.SPACE_STOP, m.PrefixKey()) + }}, + web.SPACE_STOP: {Name: "space.stop", Help: "断开连接", Hand: func(m *ice.Message, arg ...string) { + + }}, "input": {Name: "input", Help: "输入", Hand: func(m *ice.Message, arg ...string) { - if w, ok := cache[m.Option("channel")]; ok { - m.Debug("write %v", []byte(arg[0])) - w.Write([]byte(arg[0])) + if w, ok := cache.Load(m.Option("channel")); ok { + if w, ok := w.(*os.File); ok { + w.Write([]byte(arg[0])) + return + } } }}, }, mdb.HashAction()), Hand: func(m *ice.Message, arg ...string) { - r0, w0, e0 := os.Pipe() - m.Assert(e0) - r1, w1, e1 := os.Pipe() - m.Assert(e1) - m.Option(cli.CMD_INPUT, r0) - m.Option(cli.CMD_OUTPUT, w1) - m.Cmd(cli.DAEMON, "bash") - m.Go(func() { - buf := make([]byte, 1024) - for { - n, e := r1.Read(buf) - m.Assert(e) - m.Debug("what %v", string(buf[:n])) - m.PushNotice("grow", string(buf[:n])) - } - }) - - cache["1"] = w0 - cache1["1"] = w1 - m.DisplayLocal("", "channel", "1") + m.DisplayLocal("", "channel", add(m, kit.Hashs(mdb.UNIQ))) }}, }) } diff --git a/misc/ssh/service_darwin.go b/misc/ssh/service_darwin.go index 0b3a6df1..7732db26 100644 --- a/misc/ssh/service_darwin.go +++ b/misc/ssh/service_darwin.go @@ -7,7 +7,7 @@ import ( "syscall" "unsafe" - "github.com/kr/pty" + "github.com/creack/pty" "golang.org/x/crypto/ssh" ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/cli" diff --git a/misc/ssh/service_linux.go b/misc/ssh/service_linux.go index 0b3a6df1..7732db26 100644 --- a/misc/ssh/service_linux.go +++ b/misc/ssh/service_linux.go @@ -7,7 +7,7 @@ import ( "syscall" "unsafe" - "github.com/kr/pty" + "github.com/creack/pty" "golang.org/x/crypto/ssh" ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/cli"