github.com/viant/toolbox@v0.34.5/ssh/service_replay.go (about)

     1  package ssh
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"golang.org/x/crypto/ssh"
     7  	"os"
     8  )
     9  
    10  //Service represents ssh service
    11  type replayService struct {
    12  	storage     map[string][]byte
    13  	shellPrompt string
    14  	system      string
    15  	commands    *ReplayCommands
    16  }
    17  
    18  //Service returns a service wrapper
    19  func (s *replayService) Client() *ssh.Client {
    20  	return &ssh.Client{}
    21  }
    22  
    23  //OpenMultiCommandSession opens multi command session
    24  func (s *replayService) OpenMultiCommandSession(config *SessionConfig) (MultiCommandSession, error) {
    25  	return NewReplayMultiCommandSession(s.shellPrompt, s.system, s.commands), nil
    26  }
    27  
    28  //Run runs supplied command
    29  func (s *replayService) Run(command string) error {
    30  	if commands, ok := s.commands.Commands[command]; ok {
    31  		if commands.Error != "" {
    32  			return errors.New(commands.Error)
    33  		}
    34  	}
    35  	s.commands.Next(command)
    36  	return nil
    37  }
    38  
    39  //Upload uploads provided content to specified destination
    40  func (s *replayService) Upload(destination string, mode os.FileMode, content []byte) error {
    41  	s.storage[destination] = content
    42  	return nil
    43  }
    44  
    45  //Download downloads content from specified source.
    46  func (s *replayService) Download(source string) ([]byte, error) {
    47  	if _, has := s.storage[source]; !has {
    48  		return nil, fmt.Errorf("no such file or directory")
    49  	}
    50  	return s.storage[source], nil
    51  }
    52  
    53  //OpenTunnel opens a tunnel between local to remote for network traffic.
    54  func (s *replayService) OpenTunnel(localAddress, remoteAddress string) error {
    55  	return nil
    56  }
    57  
    58  func (s *replayService) NewSession() (*ssh.Session, error) {
    59  	return &ssh.Session{}, nil
    60  }
    61  
    62  func (s *replayService) Reconnect() error {
    63  	return errors.New("unsupported")
    64  }
    65  
    66  func (s *replayService) Close() error {
    67  	return nil
    68  }
    69  
    70  func NewReplayService(shellPrompt, system string, commands *ReplayCommands, storage map[string][]byte) Service {
    71  	if len(storage) == 0 {
    72  		storage = make(map[string][]byte)
    73  	}
    74  	return &replayService{
    75  		storage:     storage,
    76  		shellPrompt: shellPrompt,
    77  		system:      system,
    78  		commands:    commands,
    79  	}
    80  }