diff --git a/base/tcp/client.go b/base/tcp/client.go index 30cb7e0f..6fb2bf96 100644 --- a/base/tcp/client.go +++ b/base/tcp/client.go @@ -29,9 +29,7 @@ func (c *Conn) Write(b []byte) (int, error) { c.s.nw += n return n, e } -func (c *Conn) Close() error { - return c.Conn.Close() -} +func (c *Conn) Close() error { return c.Conn.Close() } func _client_dial(m *ice.Message, arg ...string) { c, e := net.Dial(TCP, m.Option(HOST)+ice.DF+m.Option(PORT)) @@ -39,7 +37,6 @@ func _client_dial(m *ice.Message, arg ...string) { if e == nil { defer c.Close() } - switch cb := m.OptionCB("").(type) { case func(net.Conn): if !m.Warn(e) { @@ -54,9 +51,9 @@ const ( PROTO = "proto" STATUS = "status" ERROR = "error" + START = "start" OPEN = "open" CLOSE = "close" - START = "start" STOP = "stop" ) const ( @@ -68,7 +65,6 @@ func init() { Index.MergeCommands(ice.Commands{ CLIENT: {Name: "client hash auto prunes", Help: "客户端", Actions: ice.MergeActions(ice.Actions{ ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { m.Conf("", mdb.HASH, "") }}, - ice.CTX_EXIT: {Hand: func(m *ice.Message, arg ...string) {}}, DIAL: {Name: "dial type name port=9010 host=", Help: "连接", Hand: func(m *ice.Message, arg ...string) { _client_dial(m, arg...) }}, diff --git a/base/tcp/host.go b/base/tcp/host.go index 085a300a..9c75598d 100644 --- a/base/tcp/host.go +++ b/base/tcp/host.go @@ -19,14 +19,12 @@ func _host_list(m *ice.Message, name string) { if len(v.HardwareAddr.String()) == 0 { continue } - if ips, e := v.Addrs(); m.Assert(e) { for _, x := range ips { ip := strings.Split(x.String(), ice.PS) if strings.Contains(ip[0], ice.DF) || len(ip) == 0 { continue } - m.Push(mdb.INDEX, v.Index) m.Push(mdb.NAME, v.Name) m.Push(aaa.IP, ip[0]) @@ -36,7 +34,6 @@ func _host_list(m *ice.Message, name string) { } } } - if len(m.Appendv(aaa.IP)) == 0 { m.Push(mdb.INDEX, -1) m.Push(mdb.NAME, LOCALHOST) @@ -44,28 +41,14 @@ func _host_list(m *ice.Message, name string) { m.Push("mask", "255.0.0.0") m.Push("hard", "") } -} - -func _islocalhost(m *ice.Message, ip string) (ok bool) { - if strings.HasPrefix(ip, "[") { - ip = strings.TrimPrefix(ip, "[") - ip = strings.Split(ip, "]")[0] - } - if ip == "::1" || strings.HasPrefix(ip, "127.") { - return true - } - if mdb.Richs(m, HOST, kit.Keym(aaa.BLACK), ip, nil) != nil { - return false - } - if mdb.Richs(m, HOST, kit.Keym(aaa.WHITE), ip, nil) != nil { - m.Auth(aaa.WHITE, ip) - return true - } - return false + m.StatusTimeCount() } const ( LOCALHOST = "localhost" + + ISLOCAL = "islocal" + PUBLISH = "publish" ) const HOST = "host" @@ -73,32 +56,32 @@ func init() { Index.MergeCommands(ice.Commands{ HOST: {Name: "host name auto", Help: "主机", Actions: ice.MergeActions(ice.Actions{ ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { - m.Cmd(HOST, func(value ice.Maps) { m.Cmd(HOST, aaa.WHITE, value[aaa.IP]) }) + m.Cmd("", func(value ice.Maps) { m.Cmd("", aaa.WHITE, LOCALHOST, value[aaa.IP]) }) }}, - aaa.BLACK: {Name: "black", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) { - m.Logs(mdb.CREATE, aaa.BLACK, arg[0]) - mdb.Rich(m, HOST, kit.Keym(aaa.BLACK), kit.Dict(mdb.TEXT, arg[0])) + aaa.WHITE: {Name: "white name text", Help: "白名单", Hand: func(m *ice.Message, arg ...string) { + mdb.HashCreate(m, mdb.TYPE, m.ActionKey(), m.OptionSimple(mdb.NAME, mdb.TEXT)) }}, - aaa.WHITE: {Name: "white", Help: "白名单", Hand: func(m *ice.Message, arg ...string) { - m.Logs(mdb.CREATE, aaa.WHITE, arg[0]) - mdb.Rich(m, HOST, kit.Keym(aaa.WHITE), kit.Dict(mdb.TEXT, arg[0])) + aaa.BLACK: {Name: "black name text", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) { + mdb.HashCreate(m, mdb.TYPE, m.ActionKey(), m.OptionSimple(mdb.NAME, mdb.TEXT)) }}, - "replace": {Name: "replace", Hand: func(m *ice.Message, arg ...string) { + ISLOCAL: {Hand: func(m *ice.Message, arg ...string) { + if arg[0] = strings.Split(strings.TrimPrefix(arg[0], "["), "]")[0]; arg[0] == "::1" || strings.HasPrefix(arg[0], "127.") { + m.Echo(ice.OK) + } else if mdb.HashSelectField(m, arg[0], mdb.TYPE) == aaa.WHITE { + m.Echo(ice.OK) + } + }}, + PUBLISH: {Hand: func(m *ice.Message, arg ...string) { if strings.Contains(arg[0], "://"+LOCALHOST) { arg[0] = strings.Replace(arg[0], "://"+LOCALHOST, "://"+m.Cmd("").Append(aaa.IP), 1) } m.Echo(arg[0]) }}, - }, mdb.HashAction(aaa.BLACK, kit.Data(mdb.SHORT, mdb.TEXT), aaa.WHITE, kit.Data(mdb.SHORT, mdb.TEXT))), Hand: func(m *ice.Message, arg ...string) { + }, mdb.HashAction(mdb.SHORT, mdb.TEXT)), Hand: func(m *ice.Message, arg ...string) { _host_list(m, kit.Select("", arg, 0)) }}, }) } -func IsLocalHost(m *ice.Message, ip string) bool { return _islocalhost(m, ip) } -func ReplaceLocalhost(m *ice.Message, url string) string { - if strings.Contains(url, "://"+LOCALHOST) { - url = strings.Replace(url, "://"+LOCALHOST, "://"+m.Cmd(HOST).Append(aaa.IP), 1) - } - return url -} +func IsLocalHost(m *ice.Message, ip string) bool { return m.Cmdx(HOST, ISLOCAL, ip) == ice.OK } +func PublishLocalhost(m *ice.Message, url string) string { return m.Cmdx(HOST, PUBLISH, url) } diff --git a/base/tcp/port.go b/base/tcp/port.go index c2074d3e..0ced6b53 100644 --- a/base/tcp/port.go +++ b/base/tcp/port.go @@ -13,34 +13,29 @@ import ( ) func _port_right(m *ice.Message, arg ...string) string { - current := kit.Int(kit.Select(m.Config(CURRENT), arg, 0)) - end := kit.Int(m.Config(END)) + current, end := kit.Int(kit.Select(m.Config(CURRENT), arg, 0)), kit.Int(m.Config(END)) if current >= end { current = kit.Int(m.Config(BEGIN)) } - for i := current; i < end; i++ { - if c, e := net.Dial(TCP, kit.Format(":%d", i)); e == nil { + if p := path.Join(ice.USR_LOCAL_DAEMON, kit.Format(i)); nfs.ExistsFile(m, p) { + + } else if c, e := net.Dial(TCP, kit.Format(":%d", i)); e == nil { m.Info("port exists %v", i) c.Close() - continue + } else { + nfs.MkdirAll(m, p) + m.Logs(mdb.SELECT, PORT, i) + return m.Config(CURRENT, i) } - p := path.Join(ice.USR_LOCAL_DAEMON, kit.Format(i)) - if nfs.ExistsFile(m, p) { - continue - } - - nfs.MkdirAll(m, p) - m.Logs(mdb.SELECT, PORT, i) - return m.Config(CURRENT, i) } return "" } const ( - RANDOM = "random" - CURRENT = "current" BEGIN = "begin" + CURRENT = "current" + RANDOM = "random" END = "end" ) const PORT = "port" @@ -48,44 +43,32 @@ const PORT = "port" func init() { Index.MergeCommands(ice.Commands{ PORT: {Name: "port port path auto", Help: "端口", Actions: ice.MergeActions(ice.Actions{ - CURRENT: {Name: "current", Help: "分配", Hand: func(m *ice.Message, arg ...string) { - m.Echo(m.Config(CURRENT)) - }}, - aaa.RIGHT: {Name: "right", Help: "分配", Hand: func(m *ice.Message, arg ...string) { - m.Echo(_port_right(m, arg...)) - }}, - nfs.TRASH: {Name: "trash", Help: "删除", Hand: func(m *ice.Message, arg ...string) { - if m.Option(PORT) != "" { - m.Cmd(nfs.TRASH, path.Join(ice.USR_LOCAL_DAEMON, m.Option(PORT))) - } + CURRENT: {Hand: func(m *ice.Message, arg ...string) { m.Echo(m.Config(CURRENT)) }}, + aaa.RIGHT: {Hand: func(m *ice.Message, arg ...string) { m.Echo(_port_right(m, arg...)) }}, + nfs.TRASH: {Hand: func(m *ice.Message, arg ...string) { + m.Assert(m.Option(PORT) != "") + m.Cmd(nfs.TRASH, path.Join(ice.USR_LOCAL_DAEMON, m.Option(PORT))) }}, }, mdb.HashAction(BEGIN, 10000, CURRENT, 10000, END, 20000)), Hand: func(m *ice.Message, arg ...string) { if len(arg) > 0 { m.Cmdy(nfs.DIR, arg[1:], kit.Dict(nfs.DIR_ROOT, path.Join(ice.USR_LOCAL_DAEMON, arg[0]))) return } - current := kit.Int(m.Config(BEGIN)) - m.Option(nfs.DIR_ROOT, ice.USR_LOCAL_DAEMON) - m.Cmd(nfs.DIR, nfs.PWD, nfs.DIR_CLI_FIELDS, func(value ice.Maps) { - bin := m.Cmd(nfs.DIR, path.Join(value[nfs.PATH], ice.BIN), nfs.DIR_CLI_FIELDS).Append(nfs.PATH) + m.Cmd(nfs.DIR, nfs.PWD, kit.Dict(nfs.DIR_ROOT, ice.USR_LOCAL_DAEMON), func(value ice.Maps) { + bin := m.CmdAppend(nfs.DIR, path.Join(value[nfs.PATH], ice.BIN), nfs.PATH) if bin == "" { - bin = m.Cmd(nfs.DIR, path.Join(value[nfs.PATH], "sbin"), nfs.DIR_CLI_FIELDS).Append(nfs.PATH) + bin = m.CmdAppend(nfs.DIR, path.Join(value[nfs.PATH], "sbin"), nfs.PATH) } port := kit.Int(path.Base(value[nfs.PATH])) - if port > current { - current = port - } - m.Push(mdb.TIME, value[mdb.TIME]) m.Push(PORT, port) m.Push(nfs.SIZE, value[nfs.SIZE]) m.Push(ice.BIN, strings.TrimPrefix(bin, value[nfs.PATH])) + current = kit.Max(current, port) }) - m.SortInt(PORT) - m.PushAction(nfs.TRASH) m.Config(CURRENT, current) - m.StatusTimeCount(m.ConfigSimple(BEGIN, CURRENT, END)) + m.PushAction(nfs.TRASH).StatusTimeCount(m.ConfigSimple(BEGIN, CURRENT, END)).SortInt(PORT) }}, }) } diff --git a/base/tcp/server.go b/base/tcp/server.go index c83ca877..8047b6ae 100644 --- a/base/tcp/server.go +++ b/base/tcp/server.go @@ -32,7 +32,6 @@ func _server_listen(m *ice.Message, arg ...string) { if e == nil { defer l.Close() } - switch cb := m.OptionCB("").(type) { case func(net.Listener): m.Assert(e) @@ -64,8 +63,7 @@ func init() { Index.MergeCommands(ice.Commands{ SERVER: {Name: "server hash auto prunes", Help: "服务器", Actions: ice.MergeActions(ice.Actions{ ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { m.Conf("", mdb.HASH, "") }}, - ice.CTX_EXIT: {Hand: func(m *ice.Message, arg ...string) { mdb.HashSelectClose(m) }}, - LISTEN: {Name: "listen type name port=9030 host=", Help: "监听", Hand: func(m *ice.Message, arg ...string) { + LISTEN: {Name: "listen type name port=9030 host=", Hand: func(m *ice.Message, arg ...string) { _server_listen(m, arg...) }}, }, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,type,name,host,port,error,nconn"))}, diff --git a/base/web/serve.go b/base/web/serve.go index 74d783ca..ff3f90d5 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -335,10 +335,8 @@ func init() { }}, SERVE_START: {Name: "source.stdio", Help: "终端", Hand: func(m *ice.Message, arg ...string) { m.Go(func() { - m.Sleep("2s") - url := m.Cmdx(SPACE, DOMAIN) - url2 := kit.MergePOD(m.Cmd(SPIDE, ice.DEV).Append(CLIENT_URL), ice.Info.NodeName) - m.Cmd(ssh.PRINTF, kit.Dict(nfs.CONTENT, ice.Render(m, ice.RENDER_QRCODE, url)+ice.NL+kit.Select("", url2+ice.NL, url != url2))) + defer m.Cmd(ssh.PROMPT) + m.Sleep("30ms", ssh.PRINTF, kit.Dict(nfs.CONTENT, "\r"+ice.Render(m, ice.RENDER_QRCODE, m.Cmdx(SPACE, DOMAIN))+ice.NL)) }) }}, DOMAIN: {Name: "domain", Help: "域名", Hand: func(m *ice.Message, arg ...string) { diff --git a/base/web/share.go b/base/web/share.go index f41e66f9..869518f4 100644 --- a/base/web/share.go +++ b/base/web/share.go @@ -36,7 +36,7 @@ func _share_render(m *ice.Message, arg ...string) { } func _share_link(m *ice.Message, p string, arg ...ice.Any) string { p = kit.Select("", SHARE_LOCAL, !strings.HasPrefix(p, ice.PS)) + p - return tcp.ReplaceLocalhost(m, MergeLink(m, p, arg...)) + return tcp.PublishLocalhost(m, MergeLink(m, p, arg...)) } func _share_cache(m *ice.Message, arg ...string) { if pod := m.Option(ice.POD); ctx.PodCmd(m, CACHE, arg[0]) { diff --git a/base/web/space.go b/base/web/space.go index 2d7a5e68..132b16f8 100644 --- a/base/web/space.go +++ b/base/web/space.go @@ -34,7 +34,7 @@ func _space_domain(m *ice.Message) (link string) { if link == "" { link = kit.Format("http://localhost:%s", kit.Select(m.Option(tcp.PORT), m.CmdAppend(SERVE, tcp.PORT))) } - return tcp.ReplaceLocalhost(m, link) + return tcp.PublishLocalhost(m, link) } func _space_dial(m *ice.Message, dev, name string, arg ...string) { if strings.HasPrefix(dev, ice.HTTP) { diff --git a/core/chat/favor.go b/core/chat/favor.go index b12248f9..454c371b 100644 --- a/core/chat/favor.go +++ b/core/chat/favor.go @@ -96,7 +96,7 @@ func init() { m.PushVideos(web.DISPLAY, text) } text = web.MergeLink(m, text) - text = tcp.ReplaceLocalhost(m, text) + text = tcp.PublishLocalhost(m, text) } m.PushScript(nfs.SCRIPT, text) m.PushQRCode(cli.QRCODE, text) diff --git a/core/chat/grant.go b/core/chat/grant.go index 4cf99c2b..48f9cbf1 100644 --- a/core/chat/grant.go +++ b/core/chat/grant.go @@ -22,7 +22,7 @@ func init() { } m.Go(func(msg *ice.Message) { link := web.MergePodCmd(m, "", "", web.SPACE, m.Option(mdb.NAME)) - link = tcp.ReplaceLocalhost(m, link) + link = tcp.PublishLocalhost(m, link) msg.Sleep300ms(web.SPACE, m.Option(mdb.NAME), cli.PWD, m.Option(mdb.NAME), link, msg.Cmdx(cli.QRCODE, link)) }) }}, diff --git a/core/chat/header.go b/core/chat/header.go index 97a0d883..9db473c4 100644 --- a/core/chat/header.go +++ b/core/chat/header.go @@ -35,7 +35,7 @@ func _header_share(m *ice.Message, arg ...string) { if m.Option(mdb.LINK) == "" { m.Cmdy(web.SHARE, mdb.CREATE, mdb.TYPE, web.LOGIN, arg) } else { - m.Option(mdb.LINK, tcp.ReplaceLocalhost(m, m.Option(mdb.LINK))) + m.Option(mdb.LINK, tcp.PublishLocalhost(m, m.Option(mdb.LINK))) } m.Push(mdb.NAME, m.Option(mdb.LINK)).PushQRCode(mdb.TEXT, m.Option(mdb.LINK)) } diff --git a/core/chat/keyboard.go b/core/chat/keyboard.go index 9787cbd2..6c22ba8d 100644 --- a/core/chat/keyboard.go +++ b/core/chat/keyboard.go @@ -29,7 +29,7 @@ func KeyboardAction() ice.Actions { return ice.Actions{ KEYBOARD: {Hand: func(m *ice.Message, arg ...string) { hash := m.Cmdx("web.chat.keyboard", mdb.CREATE, web.SPACE, m.Option(ice.MSG_DAEMON), ctx.INDEX, m.Option(ctx.INDEX), "input", "") - link := tcp.ReplaceLocalhost(m, web.MergePodCmd(m, "", "web.chat.keyboard", mdb.HASH, hash)) + link := tcp.PublishLocalhost(m, web.MergePodCmd(m, "", "web.chat.keyboard", mdb.HASH, hash)) m.Push(mdb.NAME, link).PushQRCode(mdb.TEXT, link) }}, } diff --git a/misc.go b/misc.go index 0d8efbec..a2621433 100644 --- a/misc.go +++ b/misc.go @@ -164,14 +164,20 @@ func (m *Message) Design(action Any, help string, input ...Any) { kit.Value(m._cmd.Meta, kit.Keys("_trans", k), help) } } +func (m *Message) _fileline() string { + switch m.target.Name { + case MDB, AAA: + return m._source + default: + return m._target + } +} func (m *Message) CmdHand(cmd *Command, key string, arg ...string) *Message { if m._key, m._cmd = key, cmd; cmd == nil { return m } - if m._target = logs.FileLine(cmd.Hand); cmd.RawHand != nil { - m._target = kit.Join(kit.Slice(kit.Split(kit.Format(cmd.RawHand), PS), -3), PS) - } - if fileline := kit.Select(m._target, m._source, m.target.Name == MDB); key == SELECT { + m._target = kit.Join(kit.Slice(kit.Split(cmd.GetFileLine(), PS), -3), PS) + if fileline := m._fileline(); key == SELECT { m.Log(LOG_CMDS, "%s.%s %d %v %v", m.Target().Name, key, len(arg), arg, m.Optionv(MSG_FIELDS), logs.FileLineMeta(fileline)) } else { m.Log(LOG_CMDS, "%s.%s %d %v", m.Target().Name, key, len(arg), arg, logs.FileLineMeta(fileline)) @@ -297,7 +303,7 @@ func (c *Context) _action(m *Message, cmd *Command, key string, sub string, h *A m._target = kit.Join(kit.Slice(kit.Split(kit.Format(cmd.RawHand), PS), -3), PS) } m.Log(LOG_CMDS, "%s.%s %s %d %v", c.Name, key, sub, len(arg), arg, - logs.FileLineMeta(kit.Select(m._target, m._source, m.target.Name == MDB))) + logs.FileLineMeta(m._fileline())) h.Hand(m, arg...) return m }