-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathhelper.go
38 lines (31 loc) · 947 Bytes
/
helper.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
package gomitmproxy
import (
"errors"
"io"
"net"
)
var errShutdown = errors.New("proxy is shutting down")
var errClose = errors.New("closing connection")
// isCloseable checks if the error signals about connection being closed
// or the proxy shutting down.
func isCloseable(err error) (ok bool) {
// TODO(ameshkov): use errors.Is.
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return true
}
switch err {
case io.EOF, io.ErrClosedPipe, errClose, errShutdown:
return true
}
return false
}
// A peekedConn subverts the net.Conn.Read implementation, primarily so that
// sniffed bytes can be transparently prepended.
type peekedConn struct {
net.Conn
r io.Reader
}
// Read allows control over the embedded net.Conn's read data. By using an
// io.MultiReader one can read from a conn, and then replace what they read, to
// be read again.
func (c *peekedConn) Read(buf []byte) (int, error) { return c.r.Read(buf) }