| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package scan |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"net/url" |
| 12 |
"os/exec" |
| 13 |
"regexp" |
| 14 |
"strconv" |
| 15 |
"strings" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/etix/mirrorbits/core" |
| 19 |
"github.com/etix/mirrorbits/utils" |
| 20 |
"github.com/gomodule/redigo/redis" |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
rsyncOutputLine = regexp.MustCompile(`^.+\s+([0-9,]+)\s+([0-9/]+)\s+([0-9:]+)\s+(.*)$`) |
| 25 |
) |
| 26 |
|
| 27 |
// RsyncScanner is the implementation of an rsync scanner |
| 28 |
type RsyncScanner struct { |
| 29 |
scan *scan |
| 30 |
} |
| 31 |
|
| 32 |
// Scan starts an rsync scan of the given mirror |
| 33 |
func (r *RsyncScanner) Scan(rsyncURL, identifier string, conn redis.Conn, stop <-chan struct{}) (core.Precision, error) { |
| 34 |
var env []string |
| 35 |
var cmdName string |
| 36 |
var actualURL string |
| 37 |
|
| 38 |
// We allow a custom rsyncs:// scheme which uses rsync-ssl instead |
| 39 |
if strings.HasPrefix(rsyncURL, "rsync://") { |
| 40 |
cmdName = "rsync" |
| 41 |
actualURL = rsyncURL |
| 42 |
} else if strings.HasPrefix(rsyncURL, "rsyncs://") { |
| 43 |
cmdName = "rsync-ssl" |
| 44 |
actualURL = "rsync://" + strings.TrimPrefix(rsyncURL, "rsyncs://") |
| 45 |
} else { |
| 46 |
return 0, fmt.Errorf("%s does not start with rsync:// or rsyncs://", rsyncURL) |
| 47 |
} |
| 48 |
|
| 49 |
u, err := url.Parse(actualURL) |
| 50 |
if err != nil { |
| 51 |
return 0, err |
| 52 |
} |
| 53 |
|
| 54 |
// Extract the credentials |
| 55 |
if u.User != nil { |
| 56 |
if u.User.Username() != "" { |
| 57 |
env = append(env, fmt.Sprintf("USER=%s", u.User.Username())) |
| 58 |
} |
| 59 |
if password, ok := u.User.Password(); ok { |
| 60 |
env = append(env, fmt.Sprintf("RSYNC_PASSWORD=%s", password)) |
| 61 |
} |
| 62 |
|
| 63 |
// Remove the credentials from the URL as we pass them through the environnement |
| 64 |
u.User = nil |
| 65 |
} |
| 66 |
|
| 67 |
// Don't use the local timezone, use UTC |
| 68 |
env = append(env, "TZ=UTC") |
| 69 |
|
| 70 |
args := []string{"-r", "--no-motd", "--exclude=.~tmp~/"} |
| 71 |
// rsync-ssl does not support --contimeout, but from what I see --timeout covers both and |
| 72 |
// triggers if openssl takes too long to connect. So use a longer combined timeout there. |
| 73 |
if cmdName == "rsync-ssl" { |
| 74 |
args = append(args, "--timeout=60") |
| 75 |
} else { |
| 76 |
args = append(args, "--timeout=30", "--contimeout=30") |
| 77 |
} |
| 78 |
args = append(args, u.String()) |
| 79 |
|
| 80 |
cmd := exec.Command(cmdName, args...) |
| 81 |
|
| 82 |
// Setup the environnement |
| 83 |
cmd.Env = env |
| 84 |
|
| 85 |
stdout, err := cmd.StdoutPipe() |
| 86 |
if err != nil { |
| 87 |
return 0, err |
| 88 |
} |
| 89 |
|
| 90 |
stderr, err := cmd.StderrPipe() |
| 91 |
if err != nil { |
| 92 |
return 0, err |
| 93 |
} |
| 94 |
|
| 95 |
// Pipe stdout |
| 96 |
reader := bufio.NewReader(stdout) |
| 97 |
readerErr := bufio.NewReader(stderr) |
| 98 |
|
| 99 |
if utils.IsStopped(stop) { |
| 100 |
return 0, ErrScanAborted |
| 101 |
} |
| 102 |
|
| 103 |
// Start the process |
| 104 |
if err := cmd.Start(); err != nil { |
| 105 |
return 0, err |
| 106 |
} |
| 107 |
|
| 108 |
log.Infof("[%s] Requesting file list via %s...", identifier, cmdName) |
| 109 |
|
| 110 |
scanfinished := make(chan bool) |
| 111 |
go func() { |
| 112 |
select { |
| 113 |
case <-stop: |
| 114 |
cmd.Process.Kill() |
| 115 |
return |
| 116 |
case <-scanfinished: |
| 117 |
return |
| 118 |
} |
| 119 |
}() |
| 120 |
defer close(scanfinished) |
| 121 |
|
| 122 |
line, err := readln(reader) |
| 123 |
for err == nil { |
| 124 |
var size int64 |
| 125 |
var f filedata |
| 126 |
var modTime time.Time |
| 127 |
var modString string |
| 128 |
|
| 129 |
if utils.IsStopped(stop) { |
| 130 |
return 0, ErrScanAborted |
| 131 |
} |
| 132 |
|
| 133 |
// Parse one line returned by rsync |
| 134 |
ret := rsyncOutputLine.FindStringSubmatch(line) |
| 135 |
if ret[0][0] == 'd' || ret[0][0] == 'l' { |
| 136 |
// Skip directories and links |
| 137 |
goto cont |
| 138 |
} |
| 139 |
|
| 140 |
// Add the leading slash |
| 141 |
if ret[4][0] != '/' { |
| 142 |
ret[4] = "/" + ret[4] |
| 143 |
} |
| 144 |
|
| 145 |
// Parse the mod time |
| 146 |
modString = ret[2] + " " + ret[3] |
| 147 |
modTime, err = time.Parse("2006/01/02 15:04:05", modString) |
| 148 |
if err != nil { |
| 149 |
log.Errorf("[%s] ScanRsync: Invalid mod time: %s", identifier, modString) |
| 150 |
goto cont |
| 151 |
} |
| 152 |
|
| 153 |
// Remove the commas in the file size |
| 154 |
ret[1] = strings.Replace(ret[1], ",", "", -1) |
| 155 |
// Convert the size to int |
| 156 |
size, err = strconv.ParseInt(ret[1], 10, 64) |
| 157 |
if err != nil { |
| 158 |
log.Errorf("[%s] ScanRsync: Invalid size: %s", identifier, ret[1]) |
| 159 |
goto cont |
| 160 |
} |
| 161 |
|
| 162 |
// Fill the struct |
| 163 |
f.size = size |
| 164 |
f.modTime = modTime |
| 165 |
f.path = ret[4] |
| 166 |
|
| 167 |
r.scan.ScannerAddFile(f) |
| 168 |
|
| 169 |
cont: |
| 170 |
line, err = readln(reader) |
| 171 |
} |
| 172 |
|
| 173 |
rsyncErrors := []string{} |
| 174 |
for line, err = readln(readerErr); err == nil; line, err = readln(readerErr) { |
| 175 |
if strings.Contains(line, ": opendir ") { |
| 176 |
rsyncErrors = append(rsyncErrors, line) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if err1 := cmd.Wait(); err1 != nil { |
| 181 |
switch err1.Error() { |
| 182 |
case "exit status 5": |
| 183 |
err1 = errors.New("rsync: Error starting client-server protocol") |
| 184 |
case "exit status 10": |
| 185 |
err1 = errors.New("rsync: Error in socket I/O") |
| 186 |
case "exit status 11": |
| 187 |
err1 = errors.New("rsync: Error in file I/O") |
| 188 |
case "exit status 23": |
| 189 |
for _, line := range rsyncErrors { |
| 190 |
log.Warningf("[%s] %s", identifier, line) |
| 191 |
} |
| 192 |
log.Warningf("[%s] rsync: Partial transfer due to error", identifier) |
| 193 |
err1 = nil |
| 194 |
case "exit status 30": |
| 195 |
err1 = errors.New("rsync: Timeout in data send/receive") |
| 196 |
case "exit status 35": |
| 197 |
err1 = errors.New("Timeout waiting for daemon connection") |
| 198 |
default: |
| 199 |
if utils.IsStopped(stop) { |
| 200 |
err1 = ErrScanAborted |
| 201 |
} else { |
| 202 |
err1 = errors.New("rsync: " + err1.Error()) |
| 203 |
} |
| 204 |
} |
| 205 |
return 0, err1 |
| 206 |
} |
| 207 |
|
| 208 |
if err != io.EOF { |
| 209 |
return 0, err |
| 210 |
} |
| 211 |
|
| 212 |
return core.Precision(time.Second), nil |
| 213 |
} |
| 214 |
|
| 215 |
func readln(r *bufio.Reader) (string, error) { |
| 216 |
var ( |
| 217 |
isPrefix = true |
| 218 |
err error |
| 219 |
line, ln []byte |
| 220 |
) |
| 221 |
for isPrefix && err == nil { |
| 222 |
line, isPrefix, err = r.ReadLine() |
| 223 |
ln = append(ln, line...) |
| 224 |
} |
| 225 |
return string(ln), err |
| 226 |
} |