waveterm/pkg/scpacket/scpacket.go

121 lines
3.5 KiB
Go
Raw Normal View History

2022-07-01 21:17:19 +02:00
package scpacket
import (
2022-10-19 03:03:02 +02:00
"fmt"
2022-07-01 21:17:19 +02:00
"reflect"
2022-10-19 03:03:02 +02:00
"strings"
2022-07-01 21:17:19 +02:00
2022-10-19 03:03:02 +02:00
"github.com/alessio/shellescape"
2022-08-24 22:21:54 +02:00
"github.com/scripthaus-dev/mshell/pkg/base"
2022-07-01 21:17:19 +02:00
"github.com/scripthaus-dev/mshell/pkg/packet"
2022-08-24 22:21:54 +02:00
"github.com/scripthaus-dev/sh2-server/pkg/sstore"
2022-07-01 21:17:19 +02:00
)
const FeCommandPacketStr = "fecmd"
const WatchScreenPacketStr = "watchscreen"
2022-08-24 22:21:54 +02:00
const FeInputPacketStr = "feinput"
const RemoteInputPacketStr = "remoteinput"
2022-07-01 21:17:19 +02:00
type FeCommandPacketType struct {
Type string `json:"type"`
MetaCmd string `json:"metacmd"`
MetaSubCmd string `json:"metasubcmd,omitempty"`
Args []string `json:"args,omitempty"`
Kwargs map[string]string `json:"kwargs,omitempty"`
2022-10-19 03:03:02 +02:00
RawStr string `json:"rawstr,omitempty"`
UIContext *UIContextType `json:"uicontext,omitempty"`
Interactive bool `json:"interactive"`
}
2022-10-19 03:03:02 +02:00
func (pk *FeCommandPacketType) GetRawStr() string {
if pk.RawStr != "" {
return pk.RawStr
}
cmd := "/" + pk.MetaCmd
if pk.MetaSubCmd != "" {
cmd = cmd + ":" + pk.MetaSubCmd
}
var args []string
for k, v := range pk.Kwargs {
argStr := fmt.Sprintf("%s=%s", shellescape.Quote(k), shellescape.Quote(v))
args = append(args, argStr)
}
for _, arg := range pk.Args {
args = append(args, shellescape.Quote(arg))
}
if len(args) == 0 {
return cmd
}
return cmd + " " + strings.Join(args, " ")
}
type UIContextType struct {
SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"`
WindowId string `json:"windowid"`
Remote *sstore.RemotePtrType `json:"remote,omitempty"`
2022-09-04 08:36:15 +02:00
TermOpts *packet.TermOpts `json:"termopts,omitempty"`
2022-11-23 23:34:49 +01:00
WinSize *WinSize `json:"winsize,omitempty"`
2022-07-01 21:17:19 +02:00
}
2022-08-24 22:21:54 +02:00
type FeInputPacketType struct {
Type string `json:"type"`
CK base.CommandKey `json:"ck"`
Remote sstore.RemotePtrType `json:"remote"`
InputData64 string `json:"inputdata64"`
SigName string `json:"signame,omitempty"`
WinSize *packet.WinSize `json:"winsize,omitempty"`
2022-08-24 22:21:54 +02:00
}
type RemoteInputPacketType struct {
Type string `json:"type"`
RemoteId string `json:"remoteid"`
InputData64 string `json:"inputdata64"`
}
type WatchScreenPacketType struct {
Type string `json:"type"`
SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"`
Connect bool `json:"connect"`
}
2022-07-01 21:17:19 +02:00
func init() {
packet.RegisterPacketType(FeCommandPacketStr, reflect.TypeOf(FeCommandPacketType{}))
packet.RegisterPacketType(WatchScreenPacketStr, reflect.TypeOf(WatchScreenPacketType{}))
2022-08-24 22:21:54 +02:00
packet.RegisterPacketType(FeInputPacketStr, reflect.TypeOf(FeInputPacketType{}))
packet.RegisterPacketType(RemoteInputPacketStr, reflect.TypeOf(RemoteInputPacketType{}))
2022-07-01 21:17:19 +02:00
}
func (*FeCommandPacketType) GetType() string {
return FeCommandPacketStr
}
func MakeFeCommandPacket() *FeCommandPacketType {
return &FeCommandPacketType{Type: FeCommandPacketStr}
}
2022-08-24 22:21:54 +02:00
func (*FeInputPacketType) GetType() string {
return FeInputPacketStr
}
func MakeFeInputPacket() *FeInputPacketType {
return &FeInputPacketType{Type: FeInputPacketStr}
}
func (*WatchScreenPacketType) GetType() string {
return WatchScreenPacketStr
}
func MakeWatchScreenPacket() *WatchScreenPacketType {
return &WatchScreenPacketType{Type: WatchScreenPacketStr}
}
func MakeRemoteInputPacket() *RemoteInputPacketType {
return &RemoteInputPacketType{Type: RemoteInputPacketStr}
}
func (*RemoteInputPacketType) GetType() string {
return RemoteInputPacketStr
}