|
| 1 | +package simulator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "strings" |
| 8 | + |
| 9 | + simssh "github.com/alphasoc/flightsim/simulator/ssh" |
| 10 | + "github.com/alphasoc/flightsim/utils" |
| 11 | + "golang.org/x/crypto/ssh" |
| 12 | + |
| 13 | + bytesize "github.com/inhies/go-bytesize" |
| 14 | +) |
| 15 | + |
| 16 | +// SSHTransfer defines this simulation. |
| 17 | +type SSHTransfer struct { |
| 18 | + src net.IP // Connect from this IP. |
| 19 | + sendSize bytesize.ByteSize |
| 20 | +} |
| 21 | + |
| 22 | +// Client connection results struct. |
| 23 | +type clientConnRes struct { |
| 24 | + c *simssh.Client |
| 25 | + err error |
| 26 | +} |
| 27 | + |
| 28 | +// NewSSHTransfer creates a new SSH/SFTP simulator. |
| 29 | +func NewSSHTransfer() *SSHTransfer { |
| 30 | + return &SSHTransfer{} |
| 31 | +} |
| 32 | + |
| 33 | +// defaultSendSize returns a 100 bytesize.MB default. |
| 34 | +func (s *SSHTransfer) defaultSendSize() bytesize.ByteSize { |
| 35 | + return 100 * bytesize.MB |
| 36 | +} |
| 37 | + |
| 38 | +// defualtTargetHosts returns a default string slice of targets in the {HOST:IP} form. |
| 39 | +func (s *SSHTransfer) defaultTargetHosts() []string { |
| 40 | + return []string{"ssh.sandbox-services.alphasoc.xyz:22"} |
| 41 | +} |
| 42 | + |
| 43 | +// HostMsg implements the HostMsgFormatter interface, returning a custom host message |
| 44 | +// string to be output by the run command. |
| 45 | +func (s *SSHTransfer) HostMsg(host string) string { |
| 46 | + return fmt.Sprintf( |
| 47 | + "Simulating an SSH/SFTP file transfer of %v (%v) to %v", |
| 48 | + s.sendSize.Format("%.0f", "B", false), |
| 49 | + s.sendSize.Format("%.2f", "", false), |
| 50 | + host) |
| 51 | +} |
| 52 | + |
| 53 | +// Init sets the source IP for this simulation. |
| 54 | +func (s *SSHTransfer) Init(src net.IP) error { |
| 55 | + s.src = src |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +// newClient initializes and returns SSH/SFTP Client along with an error. |
| 60 | +func newClient( |
| 61 | + ctx context.Context, |
| 62 | + clientName string, |
| 63 | + src net.IP, |
| 64 | + dst string, |
| 65 | + signer ssh.Signer) (*simssh.Client, error) { |
| 66 | + // Create a Client that's ready to use for SSH/SFTP transfers. |
| 67 | + c, err := simssh.NewClient(ctx, clientName, src, dst, signer) |
| 68 | + if err != nil { |
| 69 | + // No need to invoke client Teardown(), as underlying connections would have been |
| 70 | + // closed by NewClient(). |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + // Init/Version. |
| 74 | + initResp, err := c.SendInit() |
| 75 | + if err != nil { |
| 76 | + c.Teardown() |
| 77 | + return c, err |
| 78 | + } |
| 79 | + // TODO: Do we really care about version mismatches? From the sftp spec, a 3 can be |
| 80 | + // followed by some form of version negotiaion. |
| 81 | + if initResp.Version != simssh.ClientVer { |
| 82 | + c.Teardown() |
| 83 | + return c, fmt.Errorf("server version mismatch, expecting %v, received %v", |
| 84 | + simssh.ClientVer, initResp.Version) |
| 85 | + } |
| 86 | + return c, nil |
| 87 | +} |
| 88 | + |
| 89 | +type simulationContext struct { |
| 90 | + Ctx context.Context |
| 91 | + Dst string |
| 92 | + ClientName string |
| 93 | + Handle string |
| 94 | + SendSize bytesize.ByteSize |
| 95 | + Signer ssh.Signer |
| 96 | + Ch chan<- simssh.WriteResponse |
| 97 | +} |
| 98 | + |
| 99 | +// simulate performs the actual client connect and write on behalf of Simulate(). |
| 100 | +func (s *SSHTransfer) simulate(simCtx *simulationContext) { |
| 101 | + c, err := newClient(simCtx.Ctx, simCtx.ClientName, s.src, simCtx.Dst, simCtx.Signer) |
| 102 | + if err != nil { |
| 103 | + // Piggy back client connect errors on WriteResponse chan. |
| 104 | + res := simssh.WriteResponse{} |
| 105 | + res.ClientName = simCtx.ClientName |
| 106 | + res.Err = err |
| 107 | + simCtx.Ch <- res |
| 108 | + return |
| 109 | + } |
| 110 | + simCtx.Ch <- c.WriteRandom(simCtx.Handle, simCtx.SendSize) |
| 111 | + c.Teardown() |
| 112 | +} |
| 113 | + |
| 114 | +// Simulate an ssh/sftp file transfer. |
| 115 | +func (s *SSHTransfer) Simulate(ctx context.Context, dst string) error { |
| 116 | + // Auth. |
| 117 | + signer, err := simssh.NewSignerFromKey() |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + // Compute number of clients and a send size for each, such that we don't exceed |
| 122 | + // maxClients. |
| 123 | + const maxClients = 2 |
| 124 | + const minSendSize = 1 * bytesize.MB |
| 125 | + if s.sendSize <= 0 { |
| 126 | + return fmt.Errorf("invalid send size: %v", s.sendSize) |
| 127 | + } |
| 128 | + senderSizes := utils.ComputeSenderSizes(maxClients, s.sendSize, minSendSize) |
| 129 | + numClients := len(senderSizes) |
| 130 | + // Create a WriteResponse channel, used by clients to return connection errors and |
| 131 | + // write responses. |
| 132 | + writeCh := make(chan simssh.WriteResponse, numClients) |
| 133 | + for i := 0; i < numClients; i++ { |
| 134 | + go s.simulate(&simulationContext{ |
| 135 | + Ctx: ctx, |
| 136 | + Dst: dst, |
| 137 | + ClientName: fmt.Sprintf("alphasoc-%v", i), |
| 138 | + Handle: fmt.Sprintf("flightsim-ssh-transfer-%v", i), |
| 139 | + SendSize: senderSizes[i], |
| 140 | + Signer: signer, |
| 141 | + Ch: writeCh}) |
| 142 | + |
| 143 | + } |
| 144 | + var errsEncountered []string |
| 145 | + var totalBytesSent bytesize.ByteSize |
| 146 | + for i := 0; i < numClients; i++ { |
| 147 | + res := <-writeCh |
| 148 | + // Append all client connect and write errors, but continue. |
| 149 | + if res.Err != nil { |
| 150 | + errsEncountered = append(errsEncountered, fmt.Sprintf("client %v: %v", res.ClientName, res.Err.Error())) |
| 151 | + } |
| 152 | + totalBytesSent += bytesize.ByteSize(res.BytesSent) |
| 153 | + } |
| 154 | + if len(errsEncountered) != 0 { |
| 155 | + // Don't append ':" to leading '%v' as composed err already has trailing ':'. |
| 156 | + return fmt.Errorf( |
| 157 | + "[%v (%v) successfully transferred] Errors encountered:\n\t%v", |
| 158 | + totalBytesSent.Format("%.0f", "B", false), |
| 159 | + totalBytesSent.Format("%.2f", "", false), |
| 160 | + strings.Join(errsEncountered, "\n\t"), |
| 161 | + ) |
| 162 | + } |
| 163 | + // Success. |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +// Cleanup is a no-op. |
| 168 | +func (s *SSHTransfer) Cleanup() {} |
| 169 | + |
| 170 | +// Hosts sets the simulation send size, and extracts the destination hosts. A slice of |
| 171 | +// strings representing the destination hosts (IP:port) is returned along with an error. |
| 172 | +func (s *SSHTransfer) Hosts(scope string, size int) ([]string, error) { |
| 173 | + dstHosts, sendSize, err := simssh.ParseScope(scope, s.defaultTargetHosts(), s.defaultSendSize()) |
| 174 | + if err != nil { |
| 175 | + return dstHosts, err |
| 176 | + } |
| 177 | + s.sendSize = sendSize |
| 178 | + return dstHosts, nil |
| 179 | +} |
0 commit comments