forked from x/icebergs
opt broad
This commit is contained in:
parent
5ecd91626a
commit
9860966b10
54
base/tcp/broad.go
Normal file
54
base/tcp/broad.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package tcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
ice "shylinux.com/x/icebergs"
|
||||||
|
"shylinux.com/x/icebergs/base/mdb"
|
||||||
|
kit "shylinux.com/x/toolkits"
|
||||||
|
"shylinux.com/x/toolkits/logs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func _server_udp(m *ice.Message, arg ...string) {
|
||||||
|
l, e := net.ListenUDP(UDP4, UDPAddr(m, "0.0.0.0", m.Option(PORT)))
|
||||||
|
if e == nil {
|
||||||
|
defer l.Close()
|
||||||
|
}
|
||||||
|
mdb.HashCreate(m, arg, kit.Dict(mdb.TARGET, l), STATUS, kit.Select(ERROR, OPEN, e == nil), ERROR, kit.Format(e))
|
||||||
|
switch cb := m.OptionCB("").(type) {
|
||||||
|
case func(*net.UDPAddr, []byte):
|
||||||
|
m.Assert(e)
|
||||||
|
buf := make([]byte, 2*ice.MOD_BUFS)
|
||||||
|
for {
|
||||||
|
if n, from, e := l.ReadFromUDP(buf[:]); !m.Warn(e) {
|
||||||
|
cb(from, buf[:n])
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func _client_dial_udp4(m *ice.Message, arg ...string) {
|
||||||
|
c, e := net.DialUDP(UDP4, nil, UDPAddr(m, kit.Select("255.255.255.255", m.Option(HOST)), m.Option(PORT)))
|
||||||
|
if e == nil {
|
||||||
|
defer c.Close()
|
||||||
|
}
|
||||||
|
switch cb := m.OptionCB("").(type) {
|
||||||
|
case func(*net.UDPConn):
|
||||||
|
m.Assert(e)
|
||||||
|
cb(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
UDP4 = "udp4"
|
||||||
|
SEND = "send"
|
||||||
|
RECV = "recv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UDPAddr(m *ice.Message, host, port string) *net.UDPAddr {
|
||||||
|
if addr, e := net.ResolveUDPAddr(UDP4, host+ice.DF+port); !m.Warn(e, ice.ErrNotValid, host, port, logs.FileLineMeta(2)) {
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -7,16 +7,11 @@ import (
|
|||||||
"shylinux.com/x/icebergs/base/mdb"
|
"shylinux.com/x/icebergs/base/mdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Stat struct {
|
|
||||||
nc, nr, nw int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
|
net.Conn
|
||||||
m *ice.Message
|
m *ice.Message
|
||||||
h string
|
h string
|
||||||
s *Stat
|
s *Stat
|
||||||
|
|
||||||
net.Conn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Read(b []byte) (int, error) {
|
func (c *Conn) Read(b []byte) (int, error) {
|
||||||
@ -33,15 +28,11 @@ func (c *Conn) Close() error { return c.Conn.Close() }
|
|||||||
|
|
||||||
func _client_dial(m *ice.Message, arg ...string) {
|
func _client_dial(m *ice.Message, arg ...string) {
|
||||||
c, e := net.Dial(TCP, m.Option(HOST)+ice.DF+m.Option(PORT))
|
c, e := net.Dial(TCP, m.Option(HOST)+ice.DF+m.Option(PORT))
|
||||||
c = &Conn{m: m, s: &Stat{}, Conn: c}
|
c = &Conn{Conn: c, m: m, s: &Stat{}}
|
||||||
if e == nil {
|
if e == nil {
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
}
|
}
|
||||||
switch cb := m.OptionCB("").(type) {
|
switch cb := m.OptionCB("").(type) {
|
||||||
case func(*ice.Message, net.Conn):
|
|
||||||
if !m.Warn(e) {
|
|
||||||
cb(m, c)
|
|
||||||
}
|
|
||||||
case func(net.Conn):
|
case func(net.Conn):
|
||||||
if !m.Warn(e) {
|
if !m.Warn(e) {
|
||||||
cb(c)
|
cb(c)
|
||||||
@ -51,15 +42,6 @@ func _client_dial(m *ice.Message, arg ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
PROTO = "proto"
|
|
||||||
STATUS = "status"
|
|
||||||
ERROR = "error"
|
|
||||||
START = "start"
|
|
||||||
OPEN = "open"
|
|
||||||
CLOSE = "close"
|
|
||||||
STOP = "stop"
|
|
||||||
)
|
|
||||||
const (
|
const (
|
||||||
DIAL = "dial"
|
DIAL = "dial"
|
||||||
)
|
)
|
||||||
@ -69,8 +51,13 @@ func init() {
|
|||||||
Index.MergeCommands(ice.Commands{
|
Index.MergeCommands(ice.Commands{
|
||||||
CLIENT: {Name: "client hash auto prunes", Help: "客户端", Actions: ice.MergeActions(ice.Actions{
|
CLIENT: {Name: "client hash auto prunes", Help: "客户端", Actions: ice.MergeActions(ice.Actions{
|
||||||
DIAL: {Name: "dial type name port=9010 host=", Help: "连接", 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...)
|
switch m.Option(mdb.TYPE) {
|
||||||
|
case UDP4:
|
||||||
|
_client_dial_udp4(m, arg...)
|
||||||
|
default:
|
||||||
|
_client_dial(m, arg...)
|
||||||
|
}
|
||||||
}},
|
}},
|
||||||
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,type,name,host,port,error,nread,nwrite"), mdb.ClearOnExitHashAction())},
|
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,type,name,host,port,error"), mdb.ClearOnExitHashAction())},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
54
base/tcp/peek.go
Normal file
54
base/tcp/peek.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package tcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
kit "shylinux.com/x/toolkits"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Buf struct {
|
||||||
|
buf []byte
|
||||||
|
}
|
||||||
|
type PeekConn struct {
|
||||||
|
net.Conn
|
||||||
|
*Buf
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s PeekConn) Read(b []byte) (n int, err error) {
|
||||||
|
if len(s.buf) == 0 {
|
||||||
|
return s.Conn.Read(b)
|
||||||
|
}
|
||||||
|
if len(s.buf) < len(b) {
|
||||||
|
copy(b, s.buf)
|
||||||
|
s.buf = s.buf[:0]
|
||||||
|
return len(s.buf), nil
|
||||||
|
}
|
||||||
|
copy(b, s.buf)
|
||||||
|
s.buf = s.buf[len(b):]
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
func (s PeekConn) Peek(n int) (res []byte) {
|
||||||
|
b := make([]byte, n)
|
||||||
|
_n, _ := s.Conn.Read(b)
|
||||||
|
s.Buf.buf = b[:_n]
|
||||||
|
return b[:_n]
|
||||||
|
}
|
||||||
|
func (s PeekConn) IsHTTP() bool {
|
||||||
|
if bytes.Equal(s.Peek(4), []byte("GET ")) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
func (s PeekConn) Redirect(status int, location string) {
|
||||||
|
DF, NL := ": ", "\r\n"
|
||||||
|
s.Conn.Write([]byte(strings.Join([]string{
|
||||||
|
kit.Format("HTTP/1.1 %d %s", status, http.StatusText(status)), kit.JoinKV(DF, NL,
|
||||||
|
"Location", location, "Content-Length", "0",
|
||||||
|
)}, NL) + NL + NL))
|
||||||
|
}
|
||||||
|
func NewPeekConn(c net.Conn) PeekConn {
|
||||||
|
return PeekConn{Conn: c, Buf: &Buf{}}
|
||||||
|
}
|
@ -6,22 +6,15 @@ import (
|
|||||||
ice "shylinux.com/x/icebergs"
|
ice "shylinux.com/x/icebergs"
|
||||||
"shylinux.com/x/icebergs/base/mdb"
|
"shylinux.com/x/icebergs/base/mdb"
|
||||||
kit "shylinux.com/x/toolkits"
|
kit "shylinux.com/x/toolkits"
|
||||||
"shylinux.com/x/toolkits/logs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func _broad_addr(m *ice.Message, host, port string) *net.UDPAddr {
|
type Stat struct{ nc, nr, nw int }
|
||||||
if addr, e := net.ResolveUDPAddr("udp4", kit.Format("%s:%s", host, port)); !m.Warn(e, ice.ErrNotValid, host, port, logs.FileLineMeta(2)) {
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
|
net.Listener
|
||||||
m *ice.Message
|
m *ice.Message
|
||||||
h string
|
h string
|
||||||
s *Stat
|
s *Stat
|
||||||
|
|
||||||
net.Listener
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Listener) Accept() (net.Conn, error) {
|
func (l Listener) Accept() (net.Conn, error) {
|
||||||
@ -34,23 +27,9 @@ func (l Listener) Close() error {
|
|||||||
return l.Listener.Close()
|
return l.Listener.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func _server_udp(m *ice.Message, arg ...string) {
|
|
||||||
l, e := net.ListenUDP("udp4", _broad_addr(m, "0.0.0.0", m.Option(PORT)))
|
|
||||||
defer l.Close()
|
|
||||||
mdb.HashCreate(m, arg, kit.Dict(mdb.TARGET, l), STATUS, kit.Select(ERROR, OPEN, e == nil), ERROR, kit.Format(e))
|
|
||||||
switch cb := m.OptionCB("").(type) {
|
|
||||||
case func(*net.UDPConn):
|
|
||||||
m.Assert(e)
|
|
||||||
cb(l)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func _server_listen(m *ice.Message, arg ...string) {
|
func _server_listen(m *ice.Message, arg ...string) {
|
||||||
if m.Option("type") == "udp4" {
|
l, e := net.Listen(TCP, m.Option(HOST)+ice.DF+m.Option(PORT))
|
||||||
_server_udp(m, arg...)
|
l = &Listener{Listener: l, m: m, h: mdb.HashCreate(m, arg, kit.Dict(mdb.TARGET, l), STATUS, kit.Select(ERROR, OPEN, e == nil), ERROR, kit.Format(e)), s: &Stat{}}
|
||||||
return
|
|
||||||
}
|
|
||||||
l, e := net.Listen(TCP, m.Option(HOST)+":"+m.Option(PORT))
|
|
||||||
l = &Listener{m: m, h: mdb.HashCreate(m, arg, kit.Dict(mdb.TARGET, l), STATUS, kit.Select(ERROR, OPEN, e == nil), ERROR, kit.Format(e)), s: &Stat{}, Listener: l}
|
|
||||||
if e == nil {
|
if e == nil {
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
}
|
}
|
||||||
@ -60,7 +39,7 @@ func _server_listen(m *ice.Message, arg ...string) {
|
|||||||
cb(l)
|
cb(l)
|
||||||
case func(net.Conn):
|
case func(net.Conn):
|
||||||
for {
|
for {
|
||||||
if c, e := l.Accept(); e == nil {
|
if c, e := l.Accept(); !m.Warn(e) {
|
||||||
cb(c)
|
cb(c)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@ -77,6 +56,15 @@ const (
|
|||||||
HOSTNAME = "hostname"
|
HOSTNAME = "hostname"
|
||||||
NODENAME = "nodename"
|
NODENAME = "nodename"
|
||||||
)
|
)
|
||||||
|
const (
|
||||||
|
PROTO = "proto"
|
||||||
|
STATUS = "status"
|
||||||
|
ERROR = "error"
|
||||||
|
START = "start"
|
||||||
|
OPEN = "open"
|
||||||
|
CLOSE = "close"
|
||||||
|
STOP = "stop"
|
||||||
|
)
|
||||||
const (
|
const (
|
||||||
LISTEN = "listen"
|
LISTEN = "listen"
|
||||||
)
|
)
|
||||||
@ -85,7 +73,14 @@ const SERVER = "server"
|
|||||||
func init() {
|
func init() {
|
||||||
Index.MergeCommands(ice.Commands{
|
Index.MergeCommands(ice.Commands{
|
||||||
SERVER: {Name: "server hash auto prunes", Help: "服务器", Actions: ice.MergeActions(ice.Actions{
|
SERVER: {Name: "server hash auto prunes", Help: "服务器", Actions: ice.MergeActions(ice.Actions{
|
||||||
LISTEN: {Name: "listen type name port=9030 host=", Hand: func(m *ice.Message, arg ...string) { _server_listen(m, arg...) }},
|
LISTEN: {Name: "listen type name port=9030 host=", Hand: func(m *ice.Message, arg ...string) {
|
||||||
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,type,name,host,port,error,nconn"), mdb.ClearOnExitHashAction())},
|
switch m.Option(mdb.TYPE) {
|
||||||
|
case UDP4:
|
||||||
|
_server_udp(m, arg...)
|
||||||
|
default:
|
||||||
|
_server_listen(m, arg...)
|
||||||
|
}
|
||||||
|
}},
|
||||||
|
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,type,name,host,port,error"), mdb.ClearOnExitHashAction())},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,11 @@
|
|||||||
package tcp
|
package tcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
ice "shylinux.com/x/icebergs"
|
ice "shylinux.com/x/icebergs"
|
||||||
kit "shylinux.com/x/toolkits"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
SEND = "send"
|
|
||||||
RECV = "recv"
|
|
||||||
)
|
|
||||||
const TCP = "tcp"
|
const TCP = "tcp"
|
||||||
|
|
||||||
var Index = &ice.Context{Name: TCP, Help: "通信模块"}
|
var Index = &ice.Context{Name: TCP, Help: "通信模块"}
|
||||||
|
|
||||||
func init() { ice.Index.Register(Index, nil, HOST, PORT, CLIENT, SERVER) }
|
func init() { ice.Index.Register(Index, nil, HOST, PORT, CLIENT, SERVER) }
|
||||||
|
|
||||||
type Buf struct {
|
|
||||||
buf []byte
|
|
||||||
}
|
|
||||||
type PeekConn struct {
|
|
||||||
net.Conn
|
|
||||||
*Buf
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s PeekConn) Read(b []byte) (n int, err error) {
|
|
||||||
if len(s.buf) == 0 {
|
|
||||||
return s.Conn.Read(b)
|
|
||||||
}
|
|
||||||
if len(s.buf) < len(b) {
|
|
||||||
copy(b, s.buf)
|
|
||||||
s.buf = s.buf[:0]
|
|
||||||
return len(s.buf), nil
|
|
||||||
}
|
|
||||||
copy(b, s.buf)
|
|
||||||
s.buf = s.buf[len(b):]
|
|
||||||
return len(b), nil
|
|
||||||
}
|
|
||||||
func (s PeekConn) Peek(n int) (res []byte) {
|
|
||||||
b := make([]byte, n)
|
|
||||||
_n, _ := s.Conn.Read(b)
|
|
||||||
s.Buf.buf = b[:_n]
|
|
||||||
return b[:_n]
|
|
||||||
}
|
|
||||||
func (s PeekConn) IsHTTP() bool {
|
|
||||||
if bytes.Equal(s.Peek(4), []byte("GET ")) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
func (s PeekConn) Redirect(status int, location string) {
|
|
||||||
DF, NL := ": ", "\r\n"
|
|
||||||
s.Conn.Write([]byte(strings.Join([]string{
|
|
||||||
kit.Format("HTTP/1.1 %d %s", status, http.StatusText(status)), kit.JoinKV(DF, NL,
|
|
||||||
"Location", location, "Content-Length", "0",
|
|
||||||
)}, NL) + NL + NL))
|
|
||||||
}
|
|
||||||
func NewPeekConn(c net.Conn) PeekConn {
|
|
||||||
return PeekConn{Conn: c, Buf: &Buf{}}
|
|
||||||
}
|
|
||||||
|
@ -2,70 +2,36 @@ package web
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
|
||||||
|
|
||||||
ice "shylinux.com/x/icebergs"
|
ice "shylinux.com/x/icebergs"
|
||||||
"shylinux.com/x/icebergs/base/aaa"
|
"shylinux.com/x/icebergs/base/aaa"
|
||||||
"shylinux.com/x/icebergs/base/ctx"
|
"shylinux.com/x/icebergs/base/ctx"
|
||||||
|
"shylinux.com/x/icebergs/base/gdb"
|
||||||
"shylinux.com/x/icebergs/base/mdb"
|
"shylinux.com/x/icebergs/base/mdb"
|
||||||
"shylinux.com/x/icebergs/base/nfs"
|
"shylinux.com/x/icebergs/base/nfs"
|
||||||
"shylinux.com/x/icebergs/base/tcp"
|
"shylinux.com/x/icebergs/base/tcp"
|
||||||
kit "shylinux.com/x/toolkits"
|
kit "shylinux.com/x/toolkits"
|
||||||
"shylinux.com/x/toolkits/logs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func _broad_addr(m *ice.Message, host, port string) *net.UDPAddr {
|
func _broad_send(m *ice.Message, remote_host, remote_port string, host, port string, arg ...string) {
|
||||||
if addr, e := net.ResolveUDPAddr("udp4", kit.Format("%s:%s", host, port)); !m.Warn(e, ice.ErrNotValid, host, port, logs.FileLineMeta(2)) {
|
m.Cmd(tcp.CLIENT, tcp.DIAL, mdb.TYPE, tcp.UDP4, tcp.HOST, remote_host, tcp.PORT, kit.Select("9020", remote_port), func(s *net.UDPConn) {
|
||||||
return addr
|
msg := m.Spawn(kit.Dict(tcp.HOST, host, tcp.PORT, port, arg))
|
||||||
}
|
msg.Logs(tcp.SEND, BROAD, msg.FormatsMeta(nil), nfs.TO, remote_host+ice.DF+remote_port).FormatsMeta(s)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func _broad_send(m *ice.Message, host, port string, remote_host, remote_port string, arg ...string) {
|
|
||||||
if s, e := net.DialUDP("udp4", nil, _broad_addr(m, remote_host, remote_port)); !m.Warn(e, ice.ErrNotValid) {
|
|
||||||
defer s.Close()
|
|
||||||
msg := m.Spawn(kit.Dict(tcp.HOST, host, tcp.PORT, port), kit.Dict(arg))
|
|
||||||
m.Logs(tcp.SEND, BROAD, msg.FormatMeta(), nfs.TO, remote_host+ice.DF+remote_port)
|
|
||||||
s.Write([]byte(msg.FormatMeta()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func _broad_serve(m *ice.Message, port string) {
|
|
||||||
m.Cmd(tcp.HOST, func(value ice.Maps) {
|
|
||||||
_broad_send(m, value[aaa.IP], port, "255.255.255.255", "9020", mdb.TYPE, ice.Info.NodeType, mdb.NAME, ice.Info.NodeName)
|
|
||||||
})
|
})
|
||||||
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, "udp4", m.OptionSimple(mdb.NAME, tcp.HOST, tcp.PORT), func(l *net.UDPConn) {
|
}
|
||||||
buf := make([]byte, ice.MOD_BUFS)
|
func _broad_serve(m *ice.Message) {
|
||||||
for {
|
m.GoSleep("10ms", tcp.HOST, func(value ice.Maps) {
|
||||||
n, from, e := l.ReadFromUDP(buf[:])
|
_broad_send(m, "", "", value[aaa.IP], m.Option(tcp.PORT), gdb.EVENT, tcp.LISTEN, mdb.NAME, ice.Info.NodeName, mdb.TYPE, ice.Info.NodeType)
|
||||||
if e != nil {
|
})
|
||||||
break
|
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, tcp.UDP4, m.OptionSimple(mdb.NAME, tcp.HOST, tcp.PORT), func(from *net.UDPAddr, buf []byte) {
|
||||||
}
|
msg := m.Spawn(buf).Logs(tcp.RECV, BROAD, string(buf), nfs.FROM, from)
|
||||||
m.Logs(tcp.RECV, BROAD, string(buf[:n]), nfs.FROM, from)
|
if mdb.HashCreate(m, msg.OptionSimple(kit.Simple(msg.Optionv(ice.MSG_OPTION))...)); msg.Option(gdb.EVENT) == tcp.LISTEN {
|
||||||
msg := m.Spawn(buf[:n])
|
m.Cmds("", func(value ice.Maps) {
|
||||||
if msg.Option(mdb.ZONE) == "echo" {
|
_broad_send(m, msg.Option(tcp.HOST), msg.Option(tcp.PORT), value[tcp.HOST], value[tcp.PORT], mdb.TYPE, value[mdb.TYPE], mdb.NAME, value[mdb.NAME])
|
||||||
_broad_save(m, msg)
|
})
|
||||||
continue
|
|
||||||
}
|
|
||||||
if remote := _broad_addr(m, msg.Option(tcp.HOST), msg.Option(tcp.PORT)); remote != nil {
|
|
||||||
m.Cmd(BROAD, func(value ice.Maps) {
|
|
||||||
m.Logs(tcp.SEND, BROAD, kit.Format(value), nfs.TO, kit.Format(remote))
|
|
||||||
l.WriteToUDP([]byte(m.Spawn(value, kit.Dict(mdb.ZONE, "echo")).FormatMeta()), remote)
|
|
||||||
})
|
|
||||||
_broad_save(m, msg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func _broad_save(m, msg *ice.Message) {
|
|
||||||
save := false
|
|
||||||
m.Cmd(tcp.HOST, func(value ice.Maps) {
|
|
||||||
if strings.Split(msg.Option(tcp.HOST), ice.PT)[0] == strings.Split(value[aaa.IP], ice.PT)[0] {
|
|
||||||
save = true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if save {
|
|
||||||
mdb.HashCreate(m, msg.OptionSimple(kit.Simple(msg.Optionv(ice.MSG_OPTION))...))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const BROAD = "broad"
|
const BROAD = "broad"
|
||||||
|
|
||||||
@ -76,10 +42,7 @@ func init() {
|
|||||||
if arg[0] == mdb.FOREACH && arg[1] == "" {
|
if arg[0] == mdb.FOREACH && arg[1] == "" {
|
||||||
host, domain := m.Cmd(tcp.HOST).Append(aaa.IP), OptionUserWeb(m).Hostname()
|
host, domain := m.Cmd(tcp.HOST).Append(aaa.IP), OptionUserWeb(m).Hostname()
|
||||||
m.Cmds("", func(value ice.Maps) {
|
m.Cmds("", func(value ice.Maps) {
|
||||||
if value[tcp.HOST] == host {
|
switch kit.If(value[tcp.HOST] == host, func() { value[tcp.HOST] = domain }); value[mdb.TYPE] {
|
||||||
value[tcp.HOST] = domain
|
|
||||||
}
|
|
||||||
switch value[mdb.TYPE] {
|
|
||||||
case "sshd":
|
case "sshd":
|
||||||
m.PushSearch(mdb.NAME, ice.Render(m, ice.RENDER_SCRIPT, kit.Format("ssh -p %s %s@%s", value[tcp.PORT], m.Option(ice.MSG_USERNAME), value[tcp.HOST])),
|
m.PushSearch(mdb.NAME, ice.Render(m, ice.RENDER_SCRIPT, kit.Format("ssh -p %s %s@%s", value[tcp.PORT], m.Option(ice.MSG_USERNAME), value[tcp.HOST])),
|
||||||
mdb.TEXT, kit.Format("http://%s:%s", value[tcp.HOST], value[tcp.PORT]), value)
|
mdb.TEXT, kit.Format("http://%s:%s", value[tcp.HOST], value[tcp.PORT]), value)
|
||||||
@ -89,18 +52,12 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
SERVE: {Name: "serve port=9020", Hand: func(m *ice.Message, arg ...string) {
|
SERVE_START: {Hand: func(m *ice.Message, arg ...string) { m.Go(func() { m.Cmd("", SERVE, m.OptionSimple(tcp.PORT)) }) }},
|
||||||
_broad_serve(m, m.Option(tcp.PORT))
|
SERVE: {Name: "serve port=9020", Hand: func(m *ice.Message, arg ...string) { _broad_serve(m) }},
|
||||||
}},
|
|
||||||
OPEN: {Hand: func(m *ice.Message, arg ...string) {
|
OPEN: {Hand: func(m *ice.Message, arg ...string) {
|
||||||
ctx.ProcessOpen(m, kit.Format("http://%s:%s", m.Option(tcp.HOST), m.Option(tcp.PORT)))
|
ctx.ProcessOpen(m, kit.Format("http://%s:%s", m.Option(tcp.HOST), m.Option(tcp.PORT)))
|
||||||
}},
|
}},
|
||||||
tcp.SEND: {Hand: func(m *ice.Message, arg ...string) {
|
tcp.SEND: {Hand: func(m *ice.Message, arg ...string) { _broad_send(m, "", "", "", "", arg...) }},
|
||||||
_broad_send(m, "", "", "255.255.255.255", "9020", arg...)
|
|
||||||
}},
|
|
||||||
SERVE_START: {Hand: func(m *ice.Message, arg ...string) {
|
|
||||||
m.Go(func() { m.Cmd(BROAD, SERVE, m.OptionSimple(tcp.PORT)) })
|
|
||||||
}},
|
|
||||||
}, mdb.HashAction(mdb.SHORT, "host,port", mdb.FIELD, "time,hash,type,name,host,port", mdb.ACTION, OPEN), mdb.ClearOnExitHashAction())},
|
}, mdb.HashAction(mdb.SHORT, "host,port", mdb.FIELD, "time,hash,type,name,host,port", mdb.ACTION, OPEN), mdb.ClearOnExitHashAction())},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -171,6 +171,7 @@ func init() {
|
|||||||
SERVE_START: {Hand: func(m *ice.Message, arg ...string) {
|
SERVE_START: {Hand: func(m *ice.Message, arg ...string) {
|
||||||
m.Go(func() {
|
m.Go(func() {
|
||||||
ssh.PrintQRCode(m, tcp.PublishLocalhost(m, _serve_address(m)))
|
ssh.PrintQRCode(m, tcp.PublishLocalhost(m, _serve_address(m)))
|
||||||
|
return
|
||||||
opened := false
|
opened := false
|
||||||
for i := 0; i < 3 && !opened; i++ {
|
for i := 0; i < 3 && !opened; i++ {
|
||||||
m.Sleep("1s").Cmd(SPACE, func(value ice.Maps) { kit.If(value[mdb.TYPE] == CHROME, func() { opened = true }) })
|
m.Sleep("1s").Cmd(SPACE, func(value ice.Maps) { kit.If(value[mdb.TYPE] == CHROME, func() { opened = true }) })
|
||||||
|
@ -86,17 +86,17 @@ func _space_handle(m *ice.Message, safe bool, name string, c *websocket.Conn) {
|
|||||||
} else {
|
} else {
|
||||||
m.Warn(!mdb.HashSelectDetail(m, next, func(value ice.Map) {
|
m.Warn(!mdb.HashSelectDetail(m, next, func(value ice.Map) {
|
||||||
switch c := value[mdb.TARGET].(type) {
|
switch c := value[mdb.TARGET].(type) {
|
||||||
case (*websocket.Conn):
|
case (*websocket.Conn): // 转发报文
|
||||||
_space_echo(msg, source, target, c) // 转发报文
|
_space_echo(msg, source, target, c)
|
||||||
case ice.Handler:
|
case ice.Handler: // 接收响应
|
||||||
c(msg) // 接收响应
|
c(msg)
|
||||||
}
|
}
|
||||||
}), ice.ErrNotFound, next)
|
}), ice.ErrNotFound, next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func _space_domain(m *ice.Message) (link string) {
|
func _space_domain(m *ice.Message) (link string) {
|
||||||
m.Options(ice.MSG_OPTION, ice.MSG_USERNAME, ice.MSG_OPTS, ice.MSG_USERNAME)
|
// m.Options(ice.MSG_OPTION, ice.MSG_USERNAME, ice.MSG_OPTS, ice.MSG_USERNAME)
|
||||||
return kit.GetValid(
|
return kit.GetValid(
|
||||||
func() string { return ice.Info.Domain },
|
func() string { return ice.Info.Domain },
|
||||||
func() string {
|
func() string {
|
||||||
@ -197,7 +197,7 @@ func init() {
|
|||||||
ice.PS: {Hand: func(m *ice.Message, arg ...string) { _space_fork(m) }},
|
ice.PS: {Hand: func(m *ice.Message, arg ...string) { _space_fork(m) }},
|
||||||
}, mdb.HashAction(mdb.SHORT, mdb.NAME, mdb.FIELD, "time,type,name,text", ctx.ACTION, OPEN,
|
}, mdb.HashAction(mdb.SHORT, mdb.NAME, mdb.FIELD, "time,type,name,text", ctx.ACTION, OPEN,
|
||||||
REDIAL, kit.Dict("a", 3000, "b", 1000, "c", 1000),
|
REDIAL, kit.Dict("a", 3000, "b", 1000, "c", 1000),
|
||||||
), mdb.ClearOnExitHashAction(), aaa.WhiteAction()), Hand: func(m *ice.Message, arg ...string) {
|
), mdb.ClearOnExitHashAction()), Hand: func(m *ice.Message, arg ...string) {
|
||||||
if len(arg) < 2 {
|
if len(arg) < 2 {
|
||||||
mdb.HashSelect(m, arg...).Sort("").Table(func(value ice.Maps) {
|
mdb.HashSelect(m, arg...).Sort("").Table(func(value ice.Maps) {
|
||||||
m.PushButton(kit.Select(OPEN, LOGIN, value[mdb.TYPE] == LOGIN), mdb.REMOVE)
|
m.PushButton(kit.Select(OPEN, LOGIN, value[mdb.TYPE] == LOGIN), mdb.REMOVE)
|
||||||
|
@ -54,7 +54,7 @@ func (f *Frame) Start(m *ice.Message, arg ...string) {
|
|||||||
case func(http.Handler):
|
case func(http.Handler):
|
||||||
cb(f)
|
cb(f)
|
||||||
default:
|
default:
|
||||||
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, WEB, m.OptionSimple(mdb.NAME, tcp.HOST, tcp.PORT), func(l net.Listener) {
|
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, HTTP, m.OptionSimple(mdb.NAME, tcp.HOST, tcp.PORT), func(l net.Listener) {
|
||||||
defer mdb.HashCreateDeferRemove(m, m.OptionSimple(mdb.NAME, tcp.PROTO), arg, cli.STATUS, tcp.START)()
|
defer mdb.HashCreateDeferRemove(m, m.OptionSimple(mdb.NAME, tcp.PROTO), arg, cli.STATUS, tcp.START)()
|
||||||
gdb.EventDeferEvent(m, SERVE_START, arg)
|
gdb.EventDeferEvent(m, SERVE_START, arg)
|
||||||
m.Warn(f.Server.Serve(l))
|
m.Warn(f.Server.Serve(l))
|
||||||
|
1
exec.go
1
exec.go
@ -54,6 +54,7 @@ func (m *Message) Sleep(d Any, arg ...Any) *Message {
|
|||||||
func (m *Message) Sleep300ms(arg ...Any) *Message { return m.Sleep("300ms", arg...) }
|
func (m *Message) Sleep300ms(arg ...Any) *Message { return m.Sleep("300ms", arg...) }
|
||||||
func (m *Message) Sleep30ms(arg ...Any) *Message { return m.Sleep("30ms", arg...) }
|
func (m *Message) Sleep30ms(arg ...Any) *Message { return m.Sleep("30ms", arg...) }
|
||||||
func (m *Message) Sleep3s(arg ...Any) *Message { return m.Sleep("3s", arg...) }
|
func (m *Message) Sleep3s(arg ...Any) *Message { return m.Sleep("3s", arg...) }
|
||||||
|
func (m *Message) GoSleep(t string, arg ...Any) { m.Go(func() { m.Sleep(t).Cmd(arg...) }) }
|
||||||
func (m *Message) Go(cb func(), arg ...Any) {
|
func (m *Message) Go(cb func(), arg ...Any) {
|
||||||
kit.If(len(arg) == 0, func() { arg = append(arg, logs.FileLine(cb)) })
|
kit.If(len(arg) == 0, func() { arg = append(arg, logs.FileLine(cb)) })
|
||||||
task.Put(arg[0], func(task *task.Task) { m.TryCatch(m, true, func(m *Message) { cb() }) })
|
task.Put(arg[0], func(task *task.Task) { m.TryCatch(m, true, func(m *Message) { cb() }) })
|
||||||
|
2
logs.go
2
logs.go
@ -161,7 +161,7 @@ func (m *Message) FormatCost() string { return kit.FmtDuration(time.Since(m.time
|
|||||||
func (m *Message) FormatMeta() string { return kit.Format(m.meta) }
|
func (m *Message) FormatMeta() string { return kit.Format(m.meta) }
|
||||||
func (m *Message) FormatsMeta(w io.Writer, arg ...string) (res string) {
|
func (m *Message) FormatsMeta(w io.Writer, arg ...string) (res string) {
|
||||||
if w == nil {
|
if w == nil {
|
||||||
buf := bytes.NewBuffer(make([]byte, MOD_BUFS))
|
buf := bytes.NewBuffer(make([]byte, 0, MOD_BUFS))
|
||||||
defer func() { res = buf.String() }()
|
defer func() { res = buf.String() }()
|
||||||
w = buf
|
w = buf
|
||||||
}
|
}
|
||||||
|
2
type.go
2
type.go
@ -249,7 +249,7 @@ func (m *Message) Spawn(arg ...Any) *Message {
|
|||||||
for _, val := range arg {
|
for _, val := range arg {
|
||||||
switch val := val.(type) {
|
switch val := val.(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
json.Unmarshal(val, &msg.meta)
|
m.Warn(json.Unmarshal(val, &msg.meta))
|
||||||
case Option:
|
case Option:
|
||||||
msg.Option(val.Name, val.Value)
|
msg.Option(val.Name, val.Value)
|
||||||
case Maps:
|
case Maps:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user