github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/ssh/sshapi/session.go (about) 1 package sshapi 2 3 import ( 4 "io" 5 "time" 6 7 "golang.org/x/crypto/ssh" 8 ) 9 10 type CryptoSSHSessionFactory struct { 11 Client *ssh.Client 12 } 13 14 func (c *CryptoSSHSessionFactory) New() (SSHSession, error) { 15 return c.Client.NewSession() 16 } 17 18 //go:generate counterfeiter -o mocks/fake_ssh_session.go . SSHSession 19 type SSHSession interface { 20 StdinPipe() (io.WriteCloser, error) 21 StdoutPipe() (io.Reader, error) 22 StderrPipe() (io.Reader, error) 23 SendRequest(name string, wantReply bool, payload []byte) (bool, error) 24 RequestPty(term string, h, w int, termmodes ssh.TerminalModes) error 25 exposedSession 26 } 27 28 type exposedSession interface { 29 Shell() error 30 Run(string) error 31 Wait() error 32 Close() error 33 } 34 35 type Session struct { 36 KeepAliveTicker *time.Ticker 37 sshSession SSHSession 38 exposedSession 39 } 40 41 func (s *Session) KeepAlive() (stopChan chan<- struct{}) { 42 receiveStopChan := make(chan struct{}) 43 44 go func() { 45 for { 46 select { 47 case <-s.KeepAliveTicker.C: 48 s.sshSession.SendRequest("keepalive@cloudfoundry.org", true, nil) 49 case <-receiveStopChan: 50 s.KeepAliveTicker.Stop() 51 return 52 } 53 } 54 }() 55 56 return receiveStopChan 57 } 58 59 func (s *Session) Resize(width, height int) error { 60 message := struct { 61 Width uint32 62 Height uint32 63 PixelWidth uint32 64 PixelHeight uint32 65 }{uint32(width), uint32(height), 0, 0} 66 67 _, err := s.sshSession.SendRequest("window-change", false, ssh.Marshal(message)) 68 return err 69 }