|
|
@ -16,7 +16,6 @@ import (
|
|
|
|
"b612.me/starnet"
|
|
|
|
"b612.me/starnet"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var builder *starnet.StarQueue
|
|
|
|
|
|
|
|
var aesKey = []byte{0x19, 0x96, 0x11, 0x27, 228, 187, 187, 231, 142, 137, 230, 179, 189, 229, 184, 133}
|
|
|
|
var aesKey = []byte{0x19, 0x96, 0x11, 0x27, 228, 187, 187, 231, 142, 137, 230, 179, 189, 229, 184, 133}
|
|
|
|
|
|
|
|
|
|
|
|
func encodeFunc(data []byte) []byte {
|
|
|
|
func encodeFunc(data []byte) []byte {
|
|
|
@ -27,18 +26,12 @@ func decodeFunc(data []byte) []byte {
|
|
|
|
return starcrypto.AesDecryptCFB(data, aesKey)
|
|
|
|
return starcrypto.AesDecryptCFB(data, aesKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
builder = starnet.NewQueue()
|
|
|
|
|
|
|
|
builder.EncodeFunc = encodeFunc
|
|
|
|
|
|
|
|
builder.DecodeFunc = decodeFunc
|
|
|
|
|
|
|
|
builder.Encode = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// StarNotifyS 为Server端
|
|
|
|
// StarNotifyS 为Server端
|
|
|
|
type StarNotifyS struct {
|
|
|
|
type StarNotifyS struct {
|
|
|
|
// Queue 是用来处理收发信息的简单消息队列
|
|
|
|
// Queue 是用来处理收发信息的简单消息队列
|
|
|
|
Queue *starnet.StarQueue
|
|
|
|
Queue *starnet.StarQueue
|
|
|
|
// FuncLists 记录了被通知项所记录的函数
|
|
|
|
// FuncLists 记录了被通知项所记录的函数
|
|
|
|
|
|
|
|
aesKey []byte
|
|
|
|
FuncLists map[string]func(SMsg) string
|
|
|
|
FuncLists map[string]func(SMsg) string
|
|
|
|
funcMu sync.Mutex
|
|
|
|
funcMu sync.Mutex
|
|
|
|
defaultFunc func(SMsg) string
|
|
|
|
defaultFunc func(SMsg) string
|
|
|
@ -78,6 +71,24 @@ type SMsg struct {
|
|
|
|
wait chan int
|
|
|
|
wait chan int
|
|
|
|
nickName func(string, string) error
|
|
|
|
nickName func(string, string) error
|
|
|
|
getName func(string) string
|
|
|
|
getName func(string) string
|
|
|
|
|
|
|
|
queue *starnet.StarQueue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarNotifyS) SetAesKey(key []byte) {
|
|
|
|
|
|
|
|
star.aesKey = key
|
|
|
|
|
|
|
|
star.Queue.EncodeFunc = func(data []byte) []byte {
|
|
|
|
|
|
|
|
return starcrypto.AesEncryptCFB(data, key)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
star.Queue.DecodeFunc = func(data []byte) []byte {
|
|
|
|
|
|
|
|
return starcrypto.AesDecryptCFB(data, key)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarNotifyS) GetAesKey() []byte {
|
|
|
|
|
|
|
|
if len(star.aesKey) == 0 {
|
|
|
|
|
|
|
|
return aesKey
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return star.aesKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarNotifyS) getName(conn string) string {
|
|
|
|
func (star *StarNotifyS) getName(conn string) string {
|
|
|
@ -97,11 +108,11 @@ func (star *StarNotifyS) GetConnPool() []SMsg {
|
|
|
|
var result []SMsg
|
|
|
|
var result []SMsg
|
|
|
|
star.connPool.Range(func(k, val interface{}) bool {
|
|
|
|
star.connPool.Range(func(k, val interface{}) bool {
|
|
|
|
v := val.(net.Conn)
|
|
|
|
v := val.(net.Conn)
|
|
|
|
result = append(result, SMsg{Conn: v, mode: "pa", nickName: star.setNickName, getName: star.getName})
|
|
|
|
result = append(result, SMsg{Conn: v, mode: "pa", nickName: star.setNickName, getName: star.getName, queue: star.Queue})
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
for _, v := range star.udpPool {
|
|
|
|
for _, v := range star.udpPool {
|
|
|
|
result = append(result, SMsg{UDP: v, uconn: star.UDPConn, mode: "pa0", nickName: star.setNickName, getName: star.getName})
|
|
|
|
result = append(result, SMsg{UDP: v, uconn: star.UDPConn, mode: "pa0", nickName: star.setNickName, getName: star.getName, queue: star.Queue})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -111,10 +122,10 @@ func (star *StarNotifyS) GetClient(name string) (SMsg, error) {
|
|
|
|
if str, ok := star.nickName[name]; ok {
|
|
|
|
if str, ok := star.nickName[name]; ok {
|
|
|
|
if tmp, ok := star.connPool.Load(str); ok {
|
|
|
|
if tmp, ok := star.connPool.Load(str); ok {
|
|
|
|
conn := tmp.(net.Conn)
|
|
|
|
conn := tmp.(net.Conn)
|
|
|
|
return SMsg{Conn: conn, mode: "pa", nickName: star.setNickName, getName: star.getName}, nil
|
|
|
|
return SMsg{Conn: conn, mode: "pa", nickName: star.setNickName, getName: star.getName, queue: star.Queue}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conn, ok := star.udpPool[str]; ok {
|
|
|
|
if conn, ok := star.udpPool[str]; ok {
|
|
|
|
return SMsg{UDP: conn, uconn: star.UDPConn, mode: "pa0", nickName: star.setNickName, getName: star.getName}, nil
|
|
|
|
return SMsg{UDP: conn, uconn: star.UDPConn, mode: "pa0", nickName: star.setNickName, getName: star.getName, queue: star.Queue}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SMsg{}, errors.New("Not Found")
|
|
|
|
return SMsg{}, errors.New("Not Found")
|
|
|
@ -157,9 +168,9 @@ func (nmsg *SMsg) ReplyRaw(msg interface{}) error {
|
|
|
|
func (nmsg *SMsg) Reply(msg string) error {
|
|
|
|
func (nmsg *SMsg) Reply(msg string) error {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
if nmsg.uconn == nil {
|
|
|
|
if nmsg.uconn == nil {
|
|
|
|
_, err = nmsg.Conn.Write(builder.BuildMessage([]byte(nmsg.mode + "||" + nmsg.addSlash(nmsg.Key) + "||" + msg)))
|
|
|
|
_, err = nmsg.Conn.Write(nmsg.queue.BuildMessage([]byte(nmsg.mode + "||" + nmsg.addSlash(nmsg.Key) + "||" + msg)))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage([]byte(nmsg.mode+"||"+nmsg.addSlash(nmsg.Key)+"||"+msg)), nmsg.UDP)
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(nmsg.queue.BuildMessage([]byte(nmsg.mode+"||"+nmsg.addSlash(nmsg.Key)+"||"+msg)), nmsg.UDP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -168,9 +179,9 @@ func (nmsg *SMsg) Reply(msg string) error {
|
|
|
|
func (nmsg *SMsg) Send(key, value string) error {
|
|
|
|
func (nmsg *SMsg) Send(key, value string) error {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
if nmsg.uconn == nil {
|
|
|
|
if nmsg.uconn == nil {
|
|
|
|
_, err = nmsg.Conn.Write(builder.BuildMessage([]byte("pa||" + nmsg.addSlash(key) + "||" + value)))
|
|
|
|
_, err = nmsg.Conn.Write(nmsg.queue.BuildMessage([]byte("pa||" + nmsg.addSlash(key) + "||" + value)))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage([]byte("pa||"+nmsg.addSlash(key)+"||"+value)), nmsg.UDP)
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(nmsg.queue.BuildMessage([]byte("pa||"+nmsg.addSlash(key)+"||"+value)), nmsg.UDP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -198,9 +209,9 @@ func (star *StarNotifyS) SendWait(source SMsg, key, value string, tmout time.Dur
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
mode := "sr" + fmt.Sprintf("%d%06d", time.Now().UnixNano(), rand.Intn(999999))
|
|
|
|
mode := "sr" + fmt.Sprintf("%d%06d", time.Now().UnixNano(), rand.Intn(999999))
|
|
|
|
if source.uconn == nil {
|
|
|
|
if source.uconn == nil {
|
|
|
|
_, err = source.Conn.Write(builder.BuildMessage([]byte(mode + "||" + source.addSlash(key) + "||" + value)))
|
|
|
|
_, err = source.Conn.Write(star.Queue.BuildMessage([]byte(mode + "||" + source.addSlash(key) + "||" + value)))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
_, err = source.uconn.WriteToUDP(builder.BuildMessage([]byte(mode+"||"+source.addSlash(key)+"||"+value)), source.UDP)
|
|
|
|
_, err = source.uconn.WriteToUDP(star.Queue.BuildMessage([]byte(mode+"||"+source.addSlash(key)+"||"+value)), source.UDP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return SMsg{}, err
|
|
|
|
return SMsg{}, err
|
|
|
@ -225,11 +236,12 @@ func (star *StarNotifyS) SendWait(source SMsg, key, value string, tmout time.Dur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarNotifyS) starinits() {
|
|
|
|
func (star *StarNotifyS) starinits() {
|
|
|
|
|
|
|
|
builder := starnet.NewQueue()
|
|
|
|
|
|
|
|
builder.EncodeFunc = encodeFunc
|
|
|
|
|
|
|
|
builder.DecodeFunc = decodeFunc
|
|
|
|
|
|
|
|
builder.Encode = true
|
|
|
|
star.stopSign, star.cancel = context.WithCancel(context.Background())
|
|
|
|
star.stopSign, star.cancel = context.WithCancel(context.Background())
|
|
|
|
star.Queue = starnet.NewQueue()
|
|
|
|
star.Queue = builder
|
|
|
|
star.Queue.EncodeFunc = encodeFunc
|
|
|
|
|
|
|
|
star.Queue.DecodeFunc = decodeFunc
|
|
|
|
|
|
|
|
star.Queue.Encode = true
|
|
|
|
|
|
|
|
star.udpPool = make(map[string]*net.UDPAddr)
|
|
|
|
star.udpPool = make(map[string]*net.UDPAddr)
|
|
|
|
star.FuncLists = make(map[string]func(SMsg) string)
|
|
|
|
star.FuncLists = make(map[string]func(SMsg) string)
|
|
|
|
star.nickName = make(map[string]string)
|
|
|
|
star.nickName = make(map[string]string)
|
|
|
@ -284,7 +296,7 @@ func doudps(netype, value string) (*StarNotifyS, error) {
|
|
|
|
star.Queue.ParseMessage(buf[0:n], addr)
|
|
|
|
star.Queue.ParseMessage(buf[0:n], addr)
|
|
|
|
if _, ok := star.udpPool[addr.String()]; !ok {
|
|
|
|
if _, ok := star.udpPool[addr.String()]; !ok {
|
|
|
|
if star.Connected != nil {
|
|
|
|
if star.Connected != nil {
|
|
|
|
go star.Connected(SMsg{UDP: addr, uconn: star.UDPConn, nickName: star.setNickName, getName: star.getName})
|
|
|
|
go star.Connected(SMsg{UDP: addr, uconn: star.UDPConn, nickName: star.setNickName, getName: star.getName, queue: star.Queue})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
star.connMu.Lock()
|
|
|
|
star.connMu.Lock()
|
|
|
@ -372,7 +384,7 @@ func notudps(netype, value string) (*StarNotifyS, error) {
|
|
|
|
}(conn)
|
|
|
|
}(conn)
|
|
|
|
star.connPool.Store(fmt.Sprint(conn), conn)
|
|
|
|
star.connPool.Store(fmt.Sprint(conn), conn)
|
|
|
|
if star.Connected != nil {
|
|
|
|
if star.Connected != nil {
|
|
|
|
go star.Connected(SMsg{Conn: conn, nickName: star.setNickName, getName: star.getName})
|
|
|
|
go star.Connected(SMsg{Conn: conn, nickName: star.setNickName, getName: star.getName, queue: star.Queue})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
@ -452,9 +464,9 @@ func (star *StarNotifyS) notify() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var rmsg SMsg
|
|
|
|
var rmsg SMsg
|
|
|
|
if !star.isUDP {
|
|
|
|
if !star.isUDP {
|
|
|
|
rmsg = SMsg{data.Conn.(net.Conn), key, value, nil, nil, mode, nil, star.setNickName, star.getName}
|
|
|
|
rmsg = SMsg{data.Conn.(net.Conn), key, value, nil, nil, mode, nil, star.setNickName, star.getName, star.Queue}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
rmsg = SMsg{nil, key, value, data.Conn.(*net.UDPAddr), star.UDPConn, mode, nil, star.setNickName, star.getName}
|
|
|
|
rmsg = SMsg{nil, key, value, data.Conn.(*net.UDPAddr), star.UDPConn, mode, nil, star.setNickName, star.getName, star.Queue}
|
|
|
|
if key == "b612ryzstop" {
|
|
|
|
if key == "b612ryzstop" {
|
|
|
|
star.connMu.Lock()
|
|
|
|
star.connMu.Lock()
|
|
|
|
delete(star.udpPool, rmsg.UDP.String())
|
|
|
|
delete(star.udpPool, rmsg.UDP.String())
|
|
|
|