diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-23 15:30:34 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-23 15:38:12 +0100 |
commit | 08e08d771ef03df80248c80d81475fe7c5ea6fe7 (patch) | |
tree | d12e9befa22409e8cf50c5bbc4895e69fd8a5f48 /plumbing/transport/ssh/common.go | |
parent | 844169a739fb8bf1f252d416f10d8c7034db9fe2 (diff) | |
download | go-git-08e08d771ef03df80248c80d81475fe7c5ea6fe7.tar.gz |
transport: create Client interface (#132)
* plumbing: move plumbing/client package to plumbing/transport.
* transport: create Client interface.
* A Client can instantiate any client transport service.
* InstallProtocol installs a Client for a given protocol,
instead of just a UploadPackService.
* A Client can open a session for fetch-pack or send-pack
for a specific Endpoint.
* Adapt ssh and http clients to the new client interface.
* updated doc
Diffstat (limited to 'plumbing/transport/ssh/common.go')
-rw-r--r-- | plumbing/transport/ssh/common.go | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/plumbing/transport/ssh/common.go b/plumbing/transport/ssh/common.go new file mode 100644 index 0000000..6f0f3d4 --- /dev/null +++ b/plumbing/transport/ssh/common.go @@ -0,0 +1,151 @@ +package ssh + +import ( + "errors" + "fmt" + "io" + "strings" + + "gopkg.in/src-d/go-git.v4/plumbing/transport" + + "golang.org/x/crypto/ssh" +) + +// New errors introduced by this package. +var ( + ErrAdvertistedReferencesAlreadyCalled = errors.New("cannot call AdvertisedReference twice") + ErrAlreadyConnected = errors.New("ssh session already created") + ErrAuthRequired = errors.New("cannot connect: auth required") + ErrNotConnected = errors.New("not connected") + ErrUploadPackAnswerFormat = errors.New("git-upload-pack bad answer format") + ErrUnsupportedVCS = errors.New("only git is supported") + ErrUnsupportedRepo = errors.New("only github.com is supported") +) + +type Client struct{} + +var DefaultClient = NewClient() + +func NewClient() transport.Client { + return &Client{} +} + +func (c *Client) NewFetchPackSession(ep transport.Endpoint) ( + transport.FetchPackSession, error) { + + return newFetchPackSession(ep) +} + +func (c *Client) NewSendPackSession(ep transport.Endpoint) ( + transport.SendPackSession, error) { + + return newSendPackSession(ep) +} + +type session struct { + connected bool + endpoint transport.Endpoint + client *ssh.Client + session *ssh.Session + stdin io.WriteCloser + stdout io.Reader + sessionDone chan error + auth AuthMethod +} + +func (s *session) SetAuth(auth transport.AuthMethod) error { + a, ok := auth.(AuthMethod) + if !ok { + return transport.ErrInvalidAuthMethod + } + + s.auth = a + return nil +} + +// Close closes the SSH session. +func (s *session) Close() error { + if !s.connected { + return nil + } + + s.connected = false + return s.client.Close() +} + +// ensureConnected connects to the SSH server, unless a AuthMethod was set with +// SetAuth method, by default uses an auth method based on PublicKeysCallback, +// it connects to a SSH agent, using the address stored in the SSH_AUTH_SOCK +// environment var. +func (s *session) connect() error { + if s.connected { + return ErrAlreadyConnected + } + + if err := s.setAuthFromEndpoint(); err != nil { + return err + } + + var err error + s.client, err = ssh.Dial("tcp", s.getHostWithPort(), s.auth.clientConfig()) + if err != nil { + return err + } + + if err := s.openSSHSession(); err != nil { + _ = s.client.Close() + return err + } + + s.connected = true + return nil +} + +func (s *session) getHostWithPort() string { + host := s.endpoint.Host + if strings.Index(s.endpoint.Host, ":") == -1 { + host += ":22" + } + + return host +} + +func (s *session) setAuthFromEndpoint() error { + var u string + if info := s.endpoint.User; info != nil { + u = info.Username() + } + + var err error + s.auth, err = NewSSHAgentAuth(u) + return err +} + +func (s *session) openSSHSession() error { + var err error + s.session, err = s.client.NewSession() + if err != nil { + return fmt.Errorf("cannot open SSH session: %s", err) + } + + s.stdin, err = s.session.StdinPipe() + if err != nil { + return fmt.Errorf("cannot pipe remote stdin: %s", err) + } + + s.stdout, err = s.session.StdoutPipe() + if err != nil { + return fmt.Errorf("cannot pipe remote stdout: %s", err) + } + + return nil +} + +func (s *session) runCommand(cmd string) chan error { + done := make(chan error) + go func() { + done <- s.session.Run(cmd) + }() + + return done +} |