-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconn.go
More file actions
76 lines (68 loc) · 1.36 KB
/
conn.go
File metadata and controls
76 lines (68 loc) · 1.36 KB
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
package proxy
import (
"bytes"
"crypto/tls"
"io"
"net"
)
type Conn struct {
net.Conn
PeekRd *PeekReader
}
func (c *Conn) Read(p []byte) (int, error) {
multiRd := io.MultiReader(c.PeekRd.buf, c.Conn)
return multiRd.Read(p)
}
func (c *Conn) Peek(n int) ([]byte, error) {
buf := make([]byte, n)
total := copy(buf, c.PeekRd.buf.Bytes())
for total < n {
readN, err := c.Conn.Read(buf[total:])
if readN > 0 {
c.PeekRd.buf.Write(buf[total : total+readN])
total += readN
}
if err != nil || readN == 0 {
if err == io.EOF && total > 0 {
break
}
return buf[:total], err
}
}
return buf[:total], nil
}
func (c *Conn) IsTLS() bool {
_, ok := c.Conn.(*tls.Conn)
return ok
}
func NewConn(inner net.Conn) *Conn {
return &Conn{
Conn: inner,
PeekRd: &PeekReader{
rd: inner,
buf: bytes.NewBuffer(nil),
//bufRd: bufio.NewReader(inner),
},
}
}
type PeekReader struct {
rd io.Reader
buf *bytes.Buffer
//bufRd *bufio.Reader
}
func (r *PeekReader) Read(p []byte) (int, error) {
//bytesRd := bytes.NewReader(r.buf.Bytes())
//teeRd := io.TeeReader(r.rd, r.buf)
//multiRd := io.MultiReader(bytesRd, teeRd)
//return multiRd.Read(p)
total := copy(p, r.buf.Bytes())
if total < len(p) {
readN, err := r.rd.Read(p[total:])
if readN > 0 {
r.buf.Write(p[total : total+readN])
total += readN
}
return total, err
}
return total, nil
}