1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 01:24:05 +08:00
This commit is contained in:
IT 老营长 @云轩领航-创始人 2022-07-25 15:07:05 +08:00
parent cbdabc9a74
commit 9fb2e967a9
6 changed files with 60 additions and 48 deletions

View File

@ -224,7 +224,7 @@ func HashCreate(m *ice.Message, arg ...ice.Any) *ice.Message {
args[i] = kit.Keys("extra", args[i])
}
}
return m.Cmd(INSERT, m.PrefixKey(), "", HASH, args)
return m.Cmdy(INSERT, m.PrefixKey(), "", HASH, args)
}
func HashRemove(m *ice.Message, arg ...ice.Any) *ice.Message {
m.OptionFields(m.Config(FIELD))
@ -278,3 +278,15 @@ func HashExport(m *ice.Message, arg ...ice.Any) *ice.Message {
func HashImport(m *ice.Message, arg ...ice.Any) *ice.Message {
return m.Cmd(IMPORT, m.PrefixKey(), "", HASH, kit.Simple(arg...))
}
func HashCache(m *ice.Message, h string, add func() ice.Any) ice.Any {
defer m.Lock()()
if add != nil && m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache")) == nil {
m.Debug("add cache %s:%s", m.PrefixKey(), kit.Keys(HASH, h, "_cache"))
m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache"), add()) // 添加
}
m.Debug("get cache %s:%s", m.PrefixKey(), kit.Keys(HASH, h, "_cache"))
return m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache")) // 读取
}

View File

@ -24,9 +24,10 @@ func init() {
aaa.USERNAME, m.Option(ice.MSG_USERNAME),
aaa.USERNICK, m.Option(ice.MSG_USERNICK),
)
m.Cmdy(GRANT, mdb.INSERT, web.SPACE, m.Option(web.SPACE),
aaa.USERNAME, m.Option(ice.MSG_USERNAME), aaa.USERNICK, m.Option(ice.MSG_USERNICK))
m.RenderRedirect(kit.MergeURL(m.Option(cli.BACK), ice.MSG_SESSID, sessid))
// m.Cmdy(GRANT, mdb.INSERT, web.SPACE, m.Option(web.SPACE),
// aaa.USERNAME, m.Option(ice.MSG_USERNAME), aaa.USERNICK, m.Option(ice.MSG_USERNICK))
// web.RenderCookie(m, sessid, web.CookieName(m.Option("back")))
// m.RenderRedirect(kit.MergeURL(m.Option("back")))
}},

View File

@ -6,7 +6,6 @@ import (
"os/exec"
"path"
"strings"
"sync"
"time"
pty "shylinux.com/x/creackpty"
@ -17,28 +16,28 @@ import (
kit "shylinux.com/x/toolkits"
)
const XTERM = "xterm"
func init() {
cache := sync.Map{}
add := func(m *ice.Message, key string) string {
func _xterm_socket(m *ice.Message, h, t string) {
defer m.RLock()()
m.Option(ice.MSG_DAEMON, m.Conf("", kit.Keys(mdb.HASH, h, mdb.META, mdb.TEXT)))
m.Option(mdb.TEXT, t)
}
func _xterm_get(m *ice.Message, h string, must bool) (f *os.File) {
f, _ = mdb.HashCache(m, h, func() ice.Any {
if !must {
return nil
}
cmd := exec.Command(cli.SystemFind(m, kit.Select("sh", m.Option(mdb.TYPE))))
cmd.Env = append(os.Environ(), "TERM=xterm")
m.Option(mdb.HASH, h)
tty, err := pty.Start(cmd)
m.Assert(err)
cache.Store(key, tty)
m.Go(func() {
defer m.Cmd(m.PrefixKey(), mdb.PRUNES)
defer cache.Delete(key)
buf := make([]byte, ice.MOD_BUFS)
for {
if n, e := tty.Read(buf); !m.Warn(e) {
m.Option(mdb.HASH, key)
m.Option(mdb.TEXT, base64.StdEncoding.EncodeToString(buf[:n]))
m.Option(ice.MSG_DAEMON, m.Conf(m.PrefixKey(), kit.Keys(mdb.HASH, key, mdb.META, mdb.TEXT)))
_xterm_socket(m, h, base64.StdEncoding.EncodeToString(buf[:n]))
m.PushNoticeGrow("data")
} else {
break
@ -46,23 +45,14 @@ func init() {
}
m.PushNoticeGrow("exit")
})
return key
}
get := func(m *ice.Message, key string) *os.File {
if w, ok := cache.Load(key); ok {
if w, ok := w.(*os.File); ok {
return w
}
}
add(m, key)
if w, ok := cache.Load(key); ok {
if w, ok := w.(*os.File); ok {
return w
}
}
return nil
}
return tty
}).(*os.File)
return
}
const XTERM = "xterm"
func init() {
Index.MergeCommands(ice.Commands{
XTERM: {Name: "xterm hash refresh", Help: "终端", Actions: ice.MergeAction(ice.Actions{
mdb.INPUTS: {Name: "inputs", Help: "补全", Hand: func(m *ice.Message, arg ...string) {
@ -77,16 +67,12 @@ func init() {
}
}},
mdb.CREATE: {Name: "create type name", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
if m.Option(mdb.TEXT, m.Option(ice.MSG_DAEMON)) != "" {
m.Echo(add(m, mdb.HashCreate(m, arg, m.OptionSimple(mdb.TEXT)).Result()))
}
mdb.HashCreate(m, arg, mdb.TEXT, m.Option(ice.MSG_DAEMON))
_xterm_get(m, m.Result(), true)
}},
mdb.REMOVE: {Name: "remove", Help: "删除", Hand: func(m *ice.Message, arg ...string) {
if w, ok := cache.Load(m.Option(mdb.HASH)); ok {
if w, ok := w.(*os.File); ok {
w.Close()
}
cache.Delete(m.Option(mdb.HASH))
if f := _xterm_get(m, m.Option(mdb.HASH), false); f != nil {
f.Close()
}
mdb.HashRemove(m, m.OptionSimple(mdb.HASH))
}},
@ -95,25 +81,29 @@ func init() {
}},
mdb.PRUNES: {Name: "prunes", Help: "清理", Hand: func(m *ice.Message, arg ...string) {
mdb.HashSelect(m).Tables(func(value ice.Maps) {
if _, ok := cache.Load(value[mdb.HASH]); !ok || kit.Time(m.Time())-kit.Time(value[mdb.TIME]) > int64(time.Hour) {
m.Cmd(m.PrefixKey(), mdb.REMOVE, kit.Dict(value))
if f := _xterm_get(m, value[mdb.HASH], false); f != nil {
if kit.Time(m.Time())-kit.Time(value[mdb.TIME]) < int64(time.Hour) {
return // 有效终端
}
f.Close()
}
m.Cmd("", mdb.REMOVE, kit.Dict(value))
})
}},
"resize": {Name: "resize", Help: "大小", Hand: func(m *ice.Message, arg ...string) {
pty.Setsize(get(m, m.Option(mdb.HASH)), &pty.Winsize{Rows: uint16(kit.Int(m.Option("rows"))), Cols: uint16(kit.Int(m.Option("cols")))})
pty.Setsize(_xterm_get(m, m.Option(mdb.HASH), true), &pty.Winsize{Rows: uint16(kit.Int(m.Option("rows"))), Cols: uint16(kit.Int(m.Option("cols")))})
}},
"rename": {Name: "rename", Help: "重命名", Hand: func(m *ice.Message, arg ...string) {
mdb.HashModify(m, m.OptionSimple(mdb.HASH), arg)
}},
"select": {Name: "select", Help: "连接", Hand: func(m *ice.Message, arg ...string) {
mdb.HashModify(m, m.OptionSimple(mdb.HASH), mdb.TEXT, m.Option(ice.MSG_DAEMON))
m.Cmd(m.PrefixKey(), "input", arg)
m.Cmd("", "input", arg)
}},
"input": {Name: "input", Help: "输入", Hand: func(m *ice.Message, arg ...string) {
mdb.HashModify(m, m.OptionSimple(mdb.HASH), mdb.TIME, m.Time())
if b, e := base64.StdEncoding.DecodeString(strings.Join(arg, "")); m.Assert(e) {
get(m, m.Option(mdb.HASH)).Write(b)
_xterm_get(m, m.Option(mdb.HASH), true).Write(b)
}
}},
}, mdb.HashAction(mdb.FIELD, "time,hash,type,name,text,extra"), ctx.CmdAction()), Hand: func(m *ice.Message, arg ...string) {

View File

@ -9,7 +9,10 @@ import (
var lock = map[string]*sync.RWMutex{}
var _lock = sync.Mutex{}
func _get_lock(key string) (*sync.RWMutex, string) {
func (m *Message) _lock(key string) (*sync.RWMutex, string) {
if key == "" {
key = m.PrefixKey()
}
_lock.Lock()
defer _lock.Unlock()
@ -21,7 +24,7 @@ func _get_lock(key string) (*sync.RWMutex, string) {
return l, key
}
func (m *Message) Lock(arg ...Any) func() {
l, key := _get_lock(kit.Keys(arg...))
l, key := m._lock(kit.Keys(arg...))
m.Debug("before lock %v", key)
l.Lock()
m.Debug("success lock %v", key)
@ -31,7 +34,7 @@ func (m *Message) Lock(arg ...Any) func() {
}
}
func (m *Message) RLock(arg ...Any) func() {
l, key := _get_lock(kit.Keys(arg...))
l, key := m._lock(kit.Keys(arg...))
m.Debug("before rlock %v", key)
l.RLock()
m.Debug("success rlock %v", key)

View File

@ -209,6 +209,9 @@ func (m *Message) _command(arg ...Any) *Message {
if len(list) == 0 {
return m
}
if list[0] == "" {
list[0] = m.PrefixKey()
}
ok := false
run := func(msg *Message, ctx *Context, cmd *Command, key string, arg ...string) {

View File

@ -528,6 +528,9 @@ func (m *Message) Confv(arg ...Any) (val Any) { // key sub val
}
key := kit.Format(arg[0])
if key == "" {
key = m.PrefixKey()
}
if conf, ok := m.target.Configs[key]; ok {
run(conf)
} else if conf, ok := m.source.Configs[key]; ok {