2022-07-13 23:16:08 +02:00
|
|
|
package sstore
|
|
|
|
|
2022-09-01 21:47:10 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-10-31 20:40:45 +01:00
|
|
|
"log"
|
2022-09-01 21:47:10 +02:00
|
|
|
"sync"
|
|
|
|
)
|
2022-07-13 23:16:08 +02:00
|
|
|
|
|
|
|
var MainBus *UpdateBus = MakeUpdateBus()
|
|
|
|
|
2022-07-16 02:37:32 +02:00
|
|
|
const PtyDataUpdateStr = "pty"
|
2022-08-24 11:14:16 +02:00
|
|
|
const ModelUpdateStr = "model"
|
2022-09-15 09:17:23 +02:00
|
|
|
const UpdateChSize = 100
|
2022-07-16 02:37:32 +02:00
|
|
|
|
|
|
|
type UpdatePacket interface {
|
|
|
|
UpdateType() string
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
type PtyDataUpdate struct {
|
2022-09-15 08:10:35 +02:00
|
|
|
SessionId string `json:"sessionid,omitempty"`
|
|
|
|
CmdId string `json:"cmdid,omitempty"`
|
|
|
|
RemoteId string `json:"remoteid,omitempty"`
|
2022-07-13 23:16:08 +02:00
|
|
|
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-08-24 11:14:16 +02:00
|
|
|
type ModelUpdate struct {
|
2022-10-11 10:11:04 +02:00
|
|
|
Sessions []*SessionType `json:"sessions,omitempty"`
|
|
|
|
ActiveSessionId string `json:"activesessionid,omitempty"`
|
2023-02-01 02:56:56 +01:00
|
|
|
Windows []*WindowType `json:"windows,omitempty"`
|
2022-10-11 10:11:04 +02:00
|
|
|
ScreenWindows []*ScreenWindowType `json:"screenwindows,omitempty"`
|
|
|
|
Line *LineType `json:"line,omitempty"`
|
|
|
|
Cmd *CmdType `json:"cmd,omitempty"`
|
|
|
|
CmdLine *CmdLineType `json:"cmdline,omitempty"`
|
|
|
|
Info *InfoMsgType `json:"info,omitempty"`
|
2022-10-30 20:52:40 +01:00
|
|
|
ClearInfo bool `json:"clearinfo,omitempty"`
|
2022-10-11 10:11:04 +02:00
|
|
|
Remotes []interface{} `json:"remotes,omitempty"` // []*remote.RemoteState
|
|
|
|
History *HistoryInfoType `json:"history,omitempty"`
|
|
|
|
Interactive bool `json:"interactive"`
|
|
|
|
Connect bool `json:"connect,omitempty"`
|
2023-02-22 03:03:13 +01:00
|
|
|
BookmarksView bool `json:"bookmarksview,omitempty"`
|
|
|
|
Bookmarks []*BookmarkType `json:"bookmarks,omitempty"`
|
2022-07-15 10:57:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 11:14:16 +02:00
|
|
|
func (ModelUpdate) UpdateType() string {
|
|
|
|
return ModelUpdateStr
|
2022-07-16 02:37:32 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 23:24:05 +02:00
|
|
|
func ReadHistoryDataFromUpdate(update UpdatePacket) (string, string, *RemotePtrType) {
|
2022-08-24 11:14:16 +02:00
|
|
|
modelUpdate, ok := update.(ModelUpdate)
|
2022-08-12 08:45:15 +02:00
|
|
|
if !ok {
|
2022-08-28 23:24:05 +02:00
|
|
|
return "", "", nil
|
2022-08-12 08:45:15 +02:00
|
|
|
}
|
2022-08-24 11:14:16 +02:00
|
|
|
if modelUpdate.Line == nil {
|
2022-08-28 23:24:05 +02:00
|
|
|
return "", "", nil
|
2022-08-12 08:45:15 +02:00
|
|
|
}
|
2022-08-28 23:24:05 +02:00
|
|
|
var rptr *RemotePtrType
|
|
|
|
if modelUpdate.Cmd != nil {
|
|
|
|
rptr = &modelUpdate.Cmd.Remote
|
|
|
|
}
|
|
|
|
return modelUpdate.Line.LineId, modelUpdate.Line.CmdId, rptr
|
2022-08-12 08:45:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 21:47:10 +02:00
|
|
|
func InfoMsgUpdate(infoMsgFmt string, args ...interface{}) *ModelUpdate {
|
|
|
|
msg := fmt.Sprintf(infoMsgFmt, args...)
|
|
|
|
return &ModelUpdate{
|
|
|
|
Info: &InfoMsgType{InfoMsg: msg},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 23:46:51 +02:00
|
|
|
type RemoteEditType struct {
|
2022-10-04 04:04:48 +02:00
|
|
|
RemoteEdit bool `json:"remoteedit"`
|
|
|
|
RemoteId string `json:"remoteid,omitempty"`
|
|
|
|
ErrorStr string `json:"errorstr,omitempty"`
|
|
|
|
InfoStr string `json:"infostr,omitempty"`
|
|
|
|
KeyStr string `json:"keystr,omitempty"`
|
|
|
|
HasPassword bool `json:"haspassword,omitempty"`
|
2022-09-30 23:46:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 03:33:32 +02:00
|
|
|
type InfoMsgType struct {
|
2022-09-30 23:46:51 +02:00
|
|
|
InfoTitle string `json:"infotitle"`
|
|
|
|
InfoError string `json:"infoerror,omitempty"`
|
|
|
|
InfoMsg string `json:"infomsg,omitempty"`
|
|
|
|
InfoComps []string `json:"infocomps,omitempty"`
|
|
|
|
InfoCompsMore bool `json:"infocompssmore,omitempty"`
|
|
|
|
InfoLines []string `json:"infolines,omitempty"`
|
|
|
|
TimeoutMs int64 `json:"timeoutms,omitempty"`
|
|
|
|
PtyRemoteId string `json:"ptyremoteid,omitempty"`
|
|
|
|
RemoteShowAll bool `json:"remoteshowall,omitempty"`
|
|
|
|
RemoteEdit *RemoteEditType `json:"remoteedit,omitempty"`
|
2022-08-11 03:33:32 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
type HistoryInfoType struct {
|
2022-09-01 08:12:26 +02:00
|
|
|
HistoryType string `json:"historytype"`
|
|
|
|
SessionId string `json:"sessionid,omitempty"`
|
|
|
|
WindowId string `json:"windowid,omitempty"`
|
|
|
|
Items []*HistoryItemType `json:"items"`
|
|
|
|
Show bool `json:"show"`
|
2022-08-30 01:31:06 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 03:33:32 +02:00
|
|
|
type CmdLineType struct {
|
2022-11-11 03:51:20 +01:00
|
|
|
CmdLine string `json:"cmdline"`
|
|
|
|
CursorPos int `json:"cursorpos"`
|
2022-08-11 03:33:32 +02:00
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 21:42:09 +02:00
|
|
|
// always returns a new channel
|
2022-07-13 23:16:08 +02:00
|
|
|
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
|
2022-09-15 09:17:23 +02:00
|
|
|
uch.Ch = make(chan interface{}, UpdateChSize)
|
2022-07-13 23:16:08 +02:00
|
|
|
} else {
|
|
|
|
uch = UpdateChannel{
|
|
|
|
ClientId: clientId,
|
|
|
|
SessionId: sessionId,
|
2022-09-15 09:17:23 +02:00
|
|
|
Ch: make(chan interface{}, UpdateChSize),
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2022-09-15 09:17:23 +02:00
|
|
|
select {
|
|
|
|
case uch.Ch <- update:
|
|
|
|
|
|
|
|
default:
|
2022-10-31 20:40:45 +01:00
|
|
|
log.Printf("[error] dropped update on updatebus uch clientid=%s\n", uch.ClientId)
|
2022-09-15 09:17:23 +02:00
|
|
|
}
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 11:14:16 +02:00
|
|
|
|
|
|
|
func MakeSessionsUpdateForRemote(sessionId string, ri *RemoteInstance) []*SessionType {
|
|
|
|
return []*SessionType{
|
|
|
|
&SessionType{
|
|
|
|
SessionId: sessionId,
|
|
|
|
Remotes: []*RemoteInstance{ri},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 07:08:23 +01:00
|
|
|
|
|
|
|
type BookmarksViewType struct {
|
|
|
|
Bookmarks []*BookmarkType `json:"bookmarks"`
|
|
|
|
}
|