-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftp.go
237 lines (203 loc) · 5.3 KB
/
ftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package scheme
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"path"
"time"
"github.com/jlaffaye/ftp"
api "github.com/macrat/ayd/lib-ayd"
)
var (
ErrMissingUsername = errors.New("username is required if set password")
ErrMissingPassword = errors.New("password is required if set username")
)
// ftpOptions makes ftp.DialOptions for the ftp library.
func ftpOptions(ctx context.Context, u *api.URL) []ftp.DialOption {
opts := []ftp.DialOption{
ftp.DialWithDialFunc(func(network, address string) (net.Conn, error) {
conn, err := (&net.Dialer{}).DialContext(ctx, network, address)
if err != nil {
return nil, err
}
go func() {
select {
case <-ctx.Done():
conn.SetDeadline(time.Now())
}
}()
return conn, nil
}),
}
if u.Scheme == "ftps" {
opts = append(opts, ftp.DialWithExplicitTLS(&tls.Config{}))
}
return opts
}
func ftpUserInfo(u *api.URL) (user, pass string) {
if u.User == nil {
return "anonymous", "anonymous"
}
user = u.User.Username()
pass, _ = u.User.Password()
return user, pass
}
// ftpConnectAndLogin makes FTP connection by URL.
//
// If the context timed out, it will terminate TCP connection without QUIT command to the server.
// This is not very graceful, but it can stop connection surely.
func ftpConnectAndLogin(ctx context.Context, u *api.URL) (conn *ftp.ServerConn, status api.Status, message string) {
host := u.Host
if u.ToURL().Port() == "" {
host += ":21"
}
conn, err := ftp.Dial(host, ftpOptions(ctx, u)...)
if err != nil {
status = api.StatusFailure
message = err.Error()
dnsErr := &net.DNSError{}
opErr := &net.OpError{}
if errors.As(err, &dnsErr) {
status = api.StatusUnknown
message = dnsErrorToMessage(dnsErr)
} else if errors.As(err, &opErr) && opErr.Op == "dial" && opErr.Addr != nil {
message = fmt.Sprintf("%s: connection refused", opErr.Addr)
}
return
}
if err := conn.Login(ftpUserInfo(u)); err != nil {
conn.Quit()
return nil, api.StatusFailure, err.Error()
}
return conn, api.StatusHealthy, ""
}
// FTPScheme is a probe/alert implementation for the FTP.
type FTPScheme struct {
target *api.URL
}
func NewFTPScheme(u *api.URL) (FTPScheme, error) {
s := FTPScheme{
target: &api.URL{
Scheme: u.Scheme,
User: u.User,
Host: u.Host,
Path: path.Clean(u.Path),
Fragment: u.Fragment,
},
}
if u.Host == "" {
return FTPScheme{}, ErrMissingHost
}
if u.User != nil {
if u.User.Username() == "" {
return FTPScheme{}, ErrMissingUsername
}
if _, ok := u.User.Password(); !ok {
return FTPScheme{}, ErrMissingPassword
}
}
if u.Path == "" {
s.target.Path = "/"
}
return s, nil
}
func (s FTPScheme) Target() *api.URL {
return s.target
}
func (s FTPScheme) list(conn *ftp.ServerConn) (files []*ftp.Entry, status api.Status, message string) {
ls, err := conn.List(s.target.Path)
if err != nil {
return nil, api.StatusFailure, err.Error()
}
if len(ls) == 0 {
return nil, api.StatusFailure, "no such file or directory"
}
return ls, api.StatusHealthy, ""
}
// Probe checks if the target FTP server is available.
func (s FTPScheme) Probe(ctx context.Context, r Reporter) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
stime := time.Now()
report := func(status api.Status, message string, extra map[string]interface{}) {
r.Report(s.target, timeoutOr(ctx, api.Record{
Time: stime,
Status: status,
Latency: time.Since(stime),
Target: s.target,
Message: message,
Extra: extra,
}))
}
conn, status, message := ftpConnectAndLogin(ctx, s.target)
if status != api.StatusHealthy {
report(status, message, nil)
return
}
defer conn.Quit()
ls, status, message := s.list(conn)
if status != api.StatusHealthy {
report(status, message, nil)
return
}
n := 0
for _, f := range ls {
if f.Name != "." && f.Name != ".." {
n++
}
}
if n == 1 && path.Base(ls[0].Name) == path.Base(s.target.Path) {
report(api.StatusHealthy, "file exists", map[string]interface{}{
"file_size": ls[0].Size,
"mtime": ls[0].Time.Format(time.RFC3339),
"type": "file",
})
} else {
extra := map[string]interface{}{
"file_count": n,
"type": "directory",
}
for _, f := range ls {
if f.Name == "." {
extra["mtime"] = f.Time.Format(time.RFC3339)
break
}
}
report(api.StatusHealthy, "directory exists", extra)
}
}
func (s FTPScheme) Alert(ctx context.Context, r Reporter, lastRecord api.Record) {
target := &api.URL{
Scheme: "alert",
Opaque: s.target.String(),
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
stime := time.Now()
report := func(status api.Status, message string, extra map[string]interface{}) {
r.Report(s.target, timeoutOr(ctx, api.Record{
Time: stime,
Status: status,
Latency: time.Since(stime),
Target: target,
Message: message,
Extra: extra,
}))
}
conn, status, message := ftpConnectAndLogin(ctx, s.target)
if status != api.StatusHealthy {
report(status, message, nil)
return
}
defer conn.Quit()
line := lastRecord.String() + "\n"
err := conn.Append(s.target.Path, bytes.NewBufferString(line))
if err != nil {
report(api.StatusFailure, fmt.Sprintf("failed to upload record: %s", err), nil)
} else {
report(api.StatusHealthy, fmt.Sprintf("uploaded %d bytes to the server", len(line)), nil)
}
}