-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
209 lines (174 loc) · 4.46 KB
/
conn.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
package esl
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log/slog"
"strconv"
"sync"
"time"
)
type conn struct {
r *bufio.Reader
w *bufio.Writer
mu sync.Mutex // write lock
log *slog.Logger
}
// newConn creates a new `conn` object.
func newConn(rw io.ReadWriter, log *slog.Logger) *conn {
if log == nil {
log = nopLogger
}
return &conn{
r: bufio.NewReader(rw),
w: bufio.NewWriter(rw),
mu: sync.Mutex{},
log: log,
}
}
// Write writes a command to the connection.
func (c *conn) Write(cmd command) error {
if cmd.IsZero() {
return nil
}
c.log.Info("esl: send", slog.Any("cmd", cmd))
c.mu.Lock()
defer c.mu.Unlock()
cmd.WriteTo(c.w) //nolint:errcheck // write to buffer
c.w.WriteString("\n\n") //nolint:errcheck // write to buffer
if err := c.w.Flush(); err != nil {
return fmt.Errorf("failed to send command: %w", err)
}
return nil
}
// Read reads the response from the connection.
//
// It reads the response line by line from the connection and
// parses the header values. It handles different header keys
// such as "Content-Type", "Reply-Text", "Job-UUID", and
// "Content-Length". If the "Content-Length" header is present,
// it reads the specified number of bytes as the response body.
// Finally, it logs the received response and returns it along
// with any error encountered during the process.
func (c *conn) Read() (response, error) {
var (
contentLength int
resp response
)
for {
line, err := c.readLine()
if err != nil {
return resp, err
}
if len(line) == 0 {
if resp.isZero() {
continue // skip empty response
}
break // the end of response header
}
idx := bytes.IndexByte(line, ':')
if idx <= 0 {
return resp, fmt.Errorf("malformed header line: %q", line)
}
key, value := string(line[:idx]), trimLeft(line[idx+1:])
switch key {
case "Content-Type":
resp.contentType = value
case "Reply-Text":
resp.text = value
case "Job-UUID":
resp.jobUUID = value
case "Content-Length":
contentLength, err = strconv.Atoi(value)
if err != nil {
return resp, fmt.Errorf("malformed content-length: %q", value)
}
default:
c.log.Warn(
"esl: unsupported response header",
slog.String("key", key),
slog.String("value", value),
)
}
}
if contentLength > 0 {
resp.body = make([]byte, contentLength)
if _, err := io.ReadFull(c.r, resp.body); err != nil {
return resp, fmt.Errorf("failed to read body: %w", err)
}
}
c.log.Info("esl: receive", slog.Any("response", resp))
return resp, nil
}
// readLine reads a line from the conn's reader.
func (c *conn) readLine() ([]byte, error) {
var fullLine []byte // to accumulate full line
for {
line, more, err := c.r.ReadLine()
if err != nil {
return nil, err //nolint:wrapcheck
}
if fullLine == nil && !more {
return line, nil // the whole line is read at once
}
fullLine = append(fullLine, line...) // accumulate
if !more {
return fullLine, nil // it's the end of line
}
}
}
// Authentication errors.
var (
ErrMissingAuthRequest = errors.New("missing auth request")
ErrAccessDenied = errors.New("access denied")
ErrInvalidPassword = errors.New("invalid password")
ErrTimeout = errors.New("timeout")
)
// Auth authenticates the connection using the provided password.
func (c *conn) Auth(password string) error {
resp, err := c.Read()
if err != nil {
return ErrMissingAuthRequest
}
switch contentType := resp.ContentType(); contentType {
case "auth/request": // OK
case "text/rude-rejection": // access denied
return ErrAccessDenied
case disconnectNotice: // not authorized
return io.EOF
default:
return fmt.Errorf("unexpected auth request content type: %s", contentType)
}
if err := c.Write(cmd("auth", password)); err != nil {
return fmt.Errorf("failed to send auth: %w", err)
}
resp, err = c.Read()
if err != nil {
return fmt.Errorf("failed to read auth response: %w", err)
}
if ct := resp.ContentType(); ct != commandReply {
return fmt.Errorf("unexpected auth response content type: %s", ct)
}
if resp.Text() != "+OK accepted" {
return ErrInvalidPassword
}
return nil
}
// AuthTimeout performs an authentication with a timeout.
func (c *conn) AuthTimeout(password string, timeout time.Duration) error {
chErr := make(chan error, 1)
go func() {
chErr <- c.Auth(password)
close(chErr)
}()
timer := time.NewTimer(timeout)
select {
case err := <-chErr:
timer.Stop()
return err
case <-timer.C:
return ErrTimeout
}
}