1
0
mirror of https://shylinux.com/x/icebergs synced 2025-05-03 03:57:01 +08:00

opt xterm

This commit is contained in:
harveyshao 2022-07-23 05:23:07 +08:00
parent fa7d3d6b4e
commit 44c610c203
3 changed files with 37 additions and 29 deletions

View File

@ -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)))
}},
})
}

View File

@ -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"

View File

@ -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"