2022-07-13 23:16:08 +02:00
|
|
|
package sstore
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
var MainBus *UpdateBus = MakeUpdateBus()
|
|
|
|
|
2022-07-16 02:37:32 +02:00
|
|
|
const PtyDataUpdateStr = "pty"
|
|
|
|
const SessionUpdateStr = "session"
|
|
|
|
const WindowUpdateStr = "window"
|
|
|
|
const CmdUpdateStr = "cmd"
|
|
|
|
const LineCmdUpdateStr = "line+cmd"
|
2022-08-09 23:24:57 +02:00
|
|
|
const InfoUpdateStr = "info"
|
2022-08-11 03:33:32 +02:00
|
|
|
const CompGenUpdateStr = "compgen"
|
2022-07-16 02:37:32 +02:00
|
|
|
|
|
|
|
type UpdatePacket interface {
|
|
|
|
UpdateType() string
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
type PtyDataUpdate struct {
|
|
|
|
SessionId string `json:"sessionid"`
|
|
|
|
CmdId string `json:"cmdid"`
|
|
|
|
PtyPos int64 `json:"ptypos"`
|
|
|
|
PtyData64 string `json:"ptydata64"`
|
|
|
|
PtyDataLen int64 `json:"ptydatalen"`
|
|
|
|
}
|
|
|
|
|
2022-07-16 02:37:32 +02:00
|
|
|
func (PtyDataUpdate) UpdateType() string {
|
|
|
|
return PtyDataUpdateStr
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
type WindowUpdate struct {
|
2022-08-11 03:33:32 +02:00
|
|
|
Window WindowType `json:"window"`
|
|
|
|
Info *InfoMsgType `json:"info,omitempty"`
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-09 23:24:57 +02:00
|
|
|
func (WindowUpdate) UpdateType() string {
|
2022-07-16 02:37:32 +02:00
|
|
|
return WindowUpdateStr
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
type SessionUpdate struct {
|
2022-08-09 01:21:46 +02:00
|
|
|
Sessions []*SessionType `json:"sessions"`
|
|
|
|
ActiveSessionId string `json:"activesessionid,omitempty"`
|
2022-08-11 03:33:32 +02:00
|
|
|
Info *InfoMsgType `json:"info,omitempty"`
|
2022-07-15 10:57:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 02:37:32 +02:00
|
|
|
func (SessionUpdate) UpdateType() string {
|
|
|
|
return SessionUpdateStr
|
|
|
|
}
|
|
|
|
|
2022-07-15 10:57:45 +02:00
|
|
|
func MakeSingleSessionUpdate(sessionId string) (*SessionUpdate, *SessionType) {
|
|
|
|
session := &SessionType{
|
|
|
|
SessionId: sessionId,
|
|
|
|
NotifyNum: -1,
|
|
|
|
}
|
|
|
|
update := &SessionUpdate{
|
|
|
|
Sessions: []*SessionType{session},
|
|
|
|
}
|
|
|
|
return update, session
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 02:37:32 +02:00
|
|
|
type LineUpdate struct {
|
2022-08-11 03:33:32 +02:00
|
|
|
Line *LineType `json:"line"`
|
|
|
|
Cmd *CmdType `json:"cmd,omitempty"`
|
|
|
|
Remove bool `json:"remove,omitempty"`
|
|
|
|
Info *InfoMsgType `json:"info,omitempty"`
|
2022-07-16 02:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (LineUpdate) UpdateType() string {
|
|
|
|
return LineCmdUpdateStr
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 21:08:26 +02:00
|
|
|
func ReadLineCmdIdFromUpdate(update UpdatePacket) (string, string) {
|
2022-08-12 08:45:15 +02:00
|
|
|
lineUpdate, ok := update.(LineUpdate)
|
|
|
|
if !ok {
|
2022-08-16 21:08:26 +02:00
|
|
|
return "", ""
|
2022-08-12 08:45:15 +02:00
|
|
|
}
|
|
|
|
if lineUpdate.Line == nil {
|
2022-08-16 21:08:26 +02:00
|
|
|
return "", ""
|
2022-08-12 08:45:15 +02:00
|
|
|
}
|
|
|
|
return lineUpdate.Line.LineId, lineUpdate.Line.CmdId
|
|
|
|
}
|
|
|
|
|
2022-08-11 03:33:32 +02:00
|
|
|
type InfoMsgType struct {
|
2022-08-11 19:21:45 +02:00
|
|
|
InfoTitle string `json:"infotitle"`
|
|
|
|
InfoError string `json:"infoerror,omitempty"`
|
|
|
|
InfoMsg string `json:"infomsg,omitempty"`
|
2022-08-23 03:53:38 +02:00
|
|
|
InfoComps []string `json:"infocomps,omitempty"`
|
|
|
|
InfoCompsMore bool `json:"infocompssmore,omitempty"`
|
|
|
|
InfoLines []string `json:"infolines,omitempty"`
|
2022-08-11 19:21:45 +02:00
|
|
|
TimeoutMs int64 `json:"timeoutms,omitempty"`
|
2022-08-11 03:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CmdLineType struct {
|
|
|
|
InsertChars string `json:"insertchars"`
|
|
|
|
InsertPos int64 `json:"insertpos"`
|
|
|
|
}
|
|
|
|
|
2022-08-09 23:24:57 +02:00
|
|
|
type InfoUpdate struct {
|
2022-08-11 03:33:32 +02:00
|
|
|
Info *InfoMsgType `json:"info,omitempty"`
|
|
|
|
CmdLine *CmdLineType `json:"cmdline,omitempty"`
|
2022-08-09 23:24:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (InfoUpdate) UpdateType() string {
|
|
|
|
return InfoUpdateStr
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
type UpdateChannel struct {
|
|
|
|
SessionId string
|
|
|
|
ClientId string
|
|
|
|
Ch chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uch UpdateChannel) Match(sessionId string) bool {
|
|
|
|
if sessionId == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return sessionId == uch.SessionId
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateBus struct {
|
|
|
|
Lock *sync.Mutex
|
|
|
|
Channels map[string]UpdateChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeUpdateBus() *UpdateBus {
|
|
|
|
return &UpdateBus{
|
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
Channels: make(map[string]UpdateChannel),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bus *UpdateBus) RegisterChannel(clientId string, sessionId string) chan interface{} {
|
|
|
|
bus.Lock.Lock()
|
|
|
|
defer bus.Lock.Unlock()
|
|
|
|
uch, found := bus.Channels[clientId]
|
|
|
|
if found {
|
|
|
|
close(uch.Ch)
|
|
|
|
uch.SessionId = sessionId
|
|
|
|
uch.Ch = make(chan interface{})
|
|
|
|
} else {
|
|
|
|
uch = UpdateChannel{
|
|
|
|
ClientId: clientId,
|
|
|
|
SessionId: sessionId,
|
|
|
|
Ch: make(chan interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bus.Channels[clientId] = uch
|
|
|
|
return uch.Ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bus *UpdateBus) UnregisterChannel(clientId string) {
|
|
|
|
bus.Lock.Lock()
|
|
|
|
defer bus.Lock.Unlock()
|
|
|
|
uch, found := bus.Channels[clientId]
|
|
|
|
if found {
|
|
|
|
close(uch.Ch)
|
|
|
|
delete(bus.Channels, clientId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bus *UpdateBus) SendUpdate(sessionId string, update interface{}) {
|
|
|
|
bus.Lock.Lock()
|
|
|
|
defer bus.Lock.Unlock()
|
|
|
|
for _, uch := range bus.Channels {
|
|
|
|
if uch.Match(sessionId) {
|
|
|
|
uch.Ch <- update
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|