2022-06-29 00:04:08 +02:00
|
|
|
// Copyright 2022 Dashborg Inc
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-07-07 03:59:46 +02:00
|
|
|
"context"
|
2022-06-29 00:04:08 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-08-09 23:23:59 +02:00
|
|
|
"os/exec"
|
2022-08-11 19:21:11 +02:00
|
|
|
"sort"
|
2022-08-09 23:23:59 +02:00
|
|
|
"strings"
|
2022-06-29 00:04:08 +02:00
|
|
|
"sync"
|
|
|
|
|
2022-08-09 23:23:59 +02:00
|
|
|
"github.com/alessio/shellescape"
|
2022-06-29 00:04:08 +02:00
|
|
|
"github.com/scripthaus-dev/mshell/pkg/base"
|
|
|
|
"github.com/scripthaus-dev/mshell/pkg/packet"
|
2022-06-29 02:20:01 +02:00
|
|
|
"github.com/scripthaus-dev/mshell/pkg/shexec"
|
2022-06-29 00:04:08 +02:00
|
|
|
)
|
|
|
|
|
2022-07-07 02:16:45 +02:00
|
|
|
// TODO create unblockable packet-sender (backed by an array) for clientproc
|
2022-06-29 00:04:08 +02:00
|
|
|
type MServer struct {
|
2022-07-07 02:16:45 +02:00
|
|
|
Lock *sync.Mutex
|
|
|
|
MainInput *packet.PacketParser
|
|
|
|
Sender *packet.PacketSender
|
|
|
|
ClientMap map[base.CommandKey]*shexec.ClientProc
|
|
|
|
Debug bool
|
2022-06-29 00:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MServer) Close() {
|
|
|
|
m.Sender.Close()
|
|
|
|
m.Sender.WaitForDone()
|
|
|
|
}
|
|
|
|
|
2022-06-29 04:01:33 +02:00
|
|
|
func (m *MServer) ProcessCommandPacket(pk packet.CommandPacketType) {
|
|
|
|
ck := pk.GetCK()
|
|
|
|
if ck == "" {
|
2022-07-06 02:45:46 +02:00
|
|
|
m.Sender.SendMessage(fmt.Sprintf("received '%s' packet without ck", pk.GetType()))
|
2022-06-29 04:01:33 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Lock.Lock()
|
2022-07-07 02:16:45 +02:00
|
|
|
cproc := m.ClientMap[ck]
|
2022-06-29 04:01:33 +02:00
|
|
|
m.Lock.Unlock()
|
2022-07-07 02:16:45 +02:00
|
|
|
if cproc == nil {
|
2022-07-07 22:25:42 +02:00
|
|
|
m.Sender.SendCmdError(ck, fmt.Errorf("no client proc for ck '%s', pk=%s", ck, packet.AsString(pk)))
|
2022-06-29 04:01:33 +02:00
|
|
|
return
|
|
|
|
}
|
2022-07-07 02:16:45 +02:00
|
|
|
cproc.Input.SendPacket(pk)
|
|
|
|
return
|
2022-06-29 23:29:38 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 19:21:11 +02:00
|
|
|
func runSingleCompGen(cwd string, compType string, prefix string) ([]string, bool, error) {
|
|
|
|
if !packet.IsValidCompGenType(compType) {
|
|
|
|
return nil, false, fmt.Errorf("invalid compgen type '%s'", compType)
|
2022-08-09 23:23:59 +02:00
|
|
|
}
|
2022-08-11 19:21:11 +02:00
|
|
|
compGenCmdStr := fmt.Sprintf("cd %s; compgen -A %s -- %s | sort | uniq | head -n %d", shellescape.Quote(cwd), shellescape.Quote(compType), shellescape.Quote(prefix), packet.MaxCompGenValues+1)
|
2022-08-09 23:23:59 +02:00
|
|
|
ecmd := exec.Command("bash", "-c", compGenCmdStr)
|
|
|
|
outputBytes, err := ecmd.Output()
|
|
|
|
if err != nil {
|
2022-08-11 19:21:11 +02:00
|
|
|
return nil, false, fmt.Errorf("compgen error: %w", err)
|
2022-08-09 23:23:59 +02:00
|
|
|
}
|
|
|
|
outputStr := string(outputBytes)
|
|
|
|
parts := strings.Split(outputStr, "\n")
|
|
|
|
if len(parts) > 0 && parts[len(parts)-1] == "" {
|
|
|
|
parts = parts[0 : len(parts)-1]
|
|
|
|
}
|
2022-08-11 01:07:41 +02:00
|
|
|
hasMore := false
|
|
|
|
if len(parts) > packet.MaxCompGenValues {
|
|
|
|
hasMore = true
|
|
|
|
parts = parts[0:packet.MaxCompGenValues]
|
|
|
|
}
|
2022-08-11 19:21:11 +02:00
|
|
|
return parts, hasMore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendSlashes(comps []string) {
|
|
|
|
for idx, comp := range comps {
|
|
|
|
comps[idx] = comp + "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func strArrToMap(strs []string) map[string]bool {
|
|
|
|
rtn := make(map[string]bool)
|
|
|
|
for _, s := range strs {
|
|
|
|
rtn[s] = true
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MServer) runFileCompGen(compPk *packet.CompGenPacketType) {
|
|
|
|
// get directories and files, unique them and put slashes on directories for completion
|
|
|
|
reqId := compPk.GetReqId()
|
|
|
|
compDirs, hasMoreDirs, err := runSingleCompGen(compPk.Cwd, "directory", compPk.Prefix)
|
|
|
|
if err != nil {
|
|
|
|
m.Sender.SendErrorResponse(reqId, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
compFiles, hasMoreFiles, err := runSingleCompGen(compPk.Cwd, "file", compPk.Prefix)
|
|
|
|
if err != nil {
|
|
|
|
m.Sender.SendErrorResponse(reqId, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dirMap := strArrToMap(compDirs)
|
|
|
|
// seed comps with dirs (but append slashes)
|
|
|
|
comps := compDirs
|
|
|
|
appendSlashes(comps)
|
|
|
|
// add files that are not directories (look up in dirMap)
|
|
|
|
for _, file := range compFiles {
|
|
|
|
if dirMap[file] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
comps = append(comps, file)
|
|
|
|
}
|
|
|
|
sort.Strings(comps) // resort
|
|
|
|
m.Sender.SendResponse(reqId, map[string]interface{}{"comps": comps, "hasmore": (hasMoreFiles || hasMoreDirs)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MServer) runCompGen(compPk *packet.CompGenPacketType) {
|
|
|
|
reqId := compPk.GetReqId()
|
|
|
|
if compPk.CompType == "file" {
|
|
|
|
m.runFileCompGen(compPk)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
comps, hasMore, err := runSingleCompGen(compPk.Cwd, compPk.CompType, compPk.Prefix)
|
|
|
|
if err != nil {
|
|
|
|
m.Sender.SendErrorResponse(reqId, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if compPk.CompType == "directory" {
|
|
|
|
appendSlashes(comps)
|
|
|
|
}
|
|
|
|
m.Sender.SendResponse(reqId, map[string]interface{}{"comps": comps, "hasmore": hasMore})
|
2022-08-09 23:23:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 06:59:17 +02:00
|
|
|
func (m *MServer) reinit(reqId string) {
|
|
|
|
initPk, err := shexec.MakeServerInitPacket()
|
|
|
|
if err != nil {
|
|
|
|
m.Sender.SendErrorResponse(reqId, fmt.Errorf("error creating init packet: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
initPk.RespId = reqId
|
|
|
|
m.Sender.SendPacket(initPk)
|
|
|
|
}
|
|
|
|
|
2022-08-09 23:23:59 +02:00
|
|
|
func (m *MServer) ProcessRpcPacket(pk packet.RpcPacketType) {
|
|
|
|
reqId := pk.GetReqId()
|
|
|
|
if cdPk, ok := pk.(*packet.CdPacketType); ok {
|
|
|
|
err := os.Chdir(cdPk.Dir)
|
|
|
|
if err != nil {
|
|
|
|
m.Sender.SendErrorResponse(reqId, fmt.Errorf("cannot change directory: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Sender.SendResponse(reqId, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if compPk, ok := pk.(*packet.CompGenPacketType); ok {
|
|
|
|
go m.runCompGen(compPk)
|
|
|
|
return
|
|
|
|
}
|
2022-10-28 06:59:17 +02:00
|
|
|
if _, ok := pk.(*packet.ReInitPacketType); ok {
|
|
|
|
go m.reinit(reqId)
|
|
|
|
return
|
|
|
|
}
|
2022-08-09 23:23:59 +02:00
|
|
|
m.Sender.SendErrorResponse(reqId, fmt.Errorf("invalid rpc type '%s'", pk.GetType()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-29 02:20:01 +02:00
|
|
|
func (m *MServer) runCommand(runPacket *packet.RunPacketType) {
|
2022-06-29 04:01:33 +02:00
|
|
|
if err := runPacket.CK.Validate("packet"); err != nil {
|
2022-07-06 09:21:44 +02:00
|
|
|
m.Sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("server run packets require valid ck: %s", err))
|
2022-06-29 04:01:33 +02:00
|
|
|
return
|
|
|
|
}
|
2022-09-06 21:57:54 +02:00
|
|
|
ecmd, err := shexec.SSHOpts{}.MakeMShellSingleCmd(true)
|
2022-07-07 03:59:46 +02:00
|
|
|
if err != nil {
|
|
|
|
m.Sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("server run packets require valid ck: %s", err))
|
|
|
|
return
|
|
|
|
}
|
2022-09-16 21:27:14 +02:00
|
|
|
cproc, _, err := shexec.MakeClientProc(context.Background(), ecmd)
|
2022-07-07 02:16:45 +02:00
|
|
|
if err != nil {
|
|
|
|
m.Sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("starting mshell client: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Lock.Lock()
|
|
|
|
m.ClientMap[runPacket.CK] = cproc
|
|
|
|
m.Lock.Unlock()
|
2022-06-29 02:20:01 +02:00
|
|
|
go func() {
|
2022-07-07 02:16:45 +02:00
|
|
|
defer func() {
|
|
|
|
m.Lock.Lock()
|
|
|
|
delete(m.ClientMap, runPacket.CK)
|
|
|
|
m.Lock.Unlock()
|
|
|
|
cproc.Close()
|
|
|
|
}()
|
2022-07-07 03:59:46 +02:00
|
|
|
shexec.SendRunPacketAndRunData(context.Background(), cproc.Input, runPacket)
|
|
|
|
cproc.ProxySingleOutput(runPacket.CK, m.Sender)
|
2022-06-29 02:20:01 +02:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-06-29 00:04:08 +02:00
|
|
|
func RunServer() (int, error) {
|
2022-06-29 04:01:33 +02:00
|
|
|
debug := false
|
|
|
|
if len(os.Args) >= 3 && os.Args[2] == "--debug" {
|
|
|
|
debug = true
|
|
|
|
}
|
2022-06-29 00:04:08 +02:00
|
|
|
server := &MServer{
|
2022-07-07 02:16:45 +02:00
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
ClientMap: make(map[base.CommandKey]*shexec.ClientProc),
|
|
|
|
Debug: debug,
|
2022-06-29 04:01:33 +02:00
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
packet.GlobalDebug = true
|
2022-06-29 00:04:08 +02:00
|
|
|
}
|
|
|
|
server.MainInput = packet.MakePacketParser(os.Stdin)
|
|
|
|
server.Sender = packet.MakePacketSender(os.Stdout)
|
|
|
|
defer server.Close()
|
2022-07-02 02:37:37 +02:00
|
|
|
var err error
|
|
|
|
initPacket, err := shexec.MakeServerInitPacket()
|
|
|
|
if err != nil {
|
|
|
|
return 1, err
|
|
|
|
}
|
2022-06-29 00:04:08 +02:00
|
|
|
server.Sender.SendPacket(initPacket)
|
2022-06-29 07:05:47 +02:00
|
|
|
builder := packet.MakeRunPacketBuilder()
|
2022-06-29 00:04:08 +02:00
|
|
|
for pk := range server.MainInput.MainCh {
|
2022-06-29 04:01:33 +02:00
|
|
|
if server.Debug {
|
|
|
|
fmt.Printf("PK> %s\n", packet.AsString(pk))
|
|
|
|
}
|
2022-06-29 07:05:47 +02:00
|
|
|
ok, runPacket := builder.ProcessPacket(pk)
|
|
|
|
if ok {
|
|
|
|
if runPacket != nil {
|
|
|
|
server.runCommand(runPacket)
|
|
|
|
continue
|
|
|
|
}
|
2022-06-29 02:20:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-06-29 04:01:33 +02:00
|
|
|
if cmdPk, ok := pk.(packet.CommandPacketType); ok {
|
|
|
|
server.ProcessCommandPacket(cmdPk)
|
2022-06-29 00:04:08 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-08-09 23:23:59 +02:00
|
|
|
if rpcPk, ok := pk.(packet.RpcPacketType); ok {
|
|
|
|
server.ProcessRpcPacket(rpcPk)
|
|
|
|
continue
|
|
|
|
}
|
2022-07-07 02:16:45 +02:00
|
|
|
server.Sender.SendMessage(fmt.Sprintf("invalid packet '%s' sent to mshell server", packet.AsString(pk)))
|
2022-06-29 00:04:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|