Debian Codegraph @

remotessh.go

raw
1 package desync
2
3 import (
4 "fmt"
5 "net/url"
6 "os"
7 "os/exec"
8
9 "github.com/pkg/errors"
10 )
11
12 var _ Store = &RemoteSSH{}
13
14 // RemoteSSH is a remote casync store accessed via SSH. Supports running
15 // multiple sessions to improve throughput.
16 type RemoteSSH struct {
17 location *url.URL
18 pool chan *Protocol // use a buffered channel as session "pool"
19 n int
20 }
21
22 // NewRemoteSSHStore establishes up to n connections with a casync chunk server
23 func NewRemoteSSHStore(location *url.URL, opt StoreOptions) (*RemoteSSH, error) {
24 // The casync protocol always exchanges compressed chunks, storage
25 // options don't apply here. Reject encryption settings rather than
26 // silently ignoring them.
27 if opt.EncryptionConfigured() {
28 return nil, errors.New("encryption is not supported by casync protocol (ssh://) stores")
29 }
30 remote := RemoteSSH{location: location, pool: make(chan *Protocol, opt.N), n: opt.N}
31 // Start n sessions and put them into the pool (buffered channel)
32 for i := 0; i < remote.n; i++ {
33 s, err := StartProtocol(location)
34 if err != nil {
35 return &remote, errors.Wrap(err, "failed to start chunk server command")
36 }
37 remote.pool <- s
38 }
39 return &remote, nil
40 }
41
42 // GetChunk requests a chunk from the server and returns a (compressed) one.
43 // It uses any of the n sessions this store maintains in its pool. Blocks until
44 // one session becomes available
45 func (r *RemoteSSH) GetChunk(id ChunkID) (*Chunk, error) {
46 client := <-r.pool
47 chunk, err := client.RequestChunk(id)
48 r.pool <- client
49 return chunk, err
50 }
51
52 // Close terminates all client connections
53 func (r *RemoteSSH) Close() error {
54 var err error
55 for i := 0; i < r.n; i++ {
56 client := <-r.pool
57 err = client.SendGoodbye()
58 }
59 return err
60 }
61
62 // HasChunk returns true if the chunk is in the store. TODO: Implementing it
63 // this way, pulling the whole chunk just to see if it's present, is very
64 // inefficient. I'm not aware of a way to implement it with the casync protocol
65 // any other way.
66 func (r *RemoteSSH) HasChunk(id ChunkID) (bool, error) {
67 if _, err := r.GetChunk(id); err != nil {
68 return false, err
69 }
70 return true, nil
71 }
72
73 func (r *RemoteSSH) String() string {
74 return r.location.String()
75 }
76
77 // StartProtocol initiates a connection to the remote store server using
78 // the value in CASYNC_SSH_PATH (default "ssh"), and executes the command in
79 // CASYNC_REMOTE_PATH (default "casync"). It then performs the HELLO handshake
80 // to initialize the connection
81 func StartProtocol(u *url.URL) (*Protocol, error) {
82 sshCmd := os.Getenv("CASYNC_SSH_PATH")
83 if sshCmd == "" {
84 sshCmd = "ssh"
85 }
86 remoteCmd := os.Getenv("CASYNC_REMOTE_PATH")
87 if remoteCmd == "" {
88 remoteCmd = "casync"
89 }
90
91 host := u.Host
92 path := u.Path
93 // If a username was given in the URL, prefix the host
94 if u.User != nil {
95 host = u.User.Username() + "@" + u.Host
96 }
97 // Reject destinations that ssh would parse as command-line options.
98 if err := validateSSHHost(host); err != nil {
99 return nil, err
100 }
101
102 // "--" terminates ssh's option parsing so the destination can't be read as a
103 // flag, and the path is shell-quoted so it can't break out of the remote
104 // command string.
105 c := exec.Command(sshCmd, "--", host, fmt.Sprintf("%s pull - - - %s", remoteCmd, shellQuote(path)))
106 c.Stderr = os.Stderr
107 r, err := c.StdoutPipe()
108 if err != nil {
109 return nil, err
110 }
111 w, err := c.StdinPipe()
112 if err != nil {
113 return nil, err
114 }
115 if err = c.Start(); err != nil {
116 return nil, err
117 }
118
119 // Perform the handshake with the server
120 p := NewProtocol(r, w)
121 flags, err := p.Initialize(CaProtocolPullChunks)
122 if err != nil {
123 return nil, err
124 }
125 if flags&CaProtocolReadableStore == 0 {
126 return nil, errors.New("server not offering chunks")
127 }
128 return p, nil
129 }