-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwarpping.go
77 lines (63 loc) · 1.25 KB
/
warpping.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
package anyproxy
import (
"errors"
"net"
"net/http"
"sync"
"github.com/wzshiming/httpproxy"
)
var ErrNetClosing = errors.New("use of closed network connection")
type singleConnListener struct {
addr net.Addr
ch chan net.Conn
once sync.Once
}
func newSingleConnListener(conn net.Conn) net.Listener {
ch := make(chan net.Conn, 1)
ch <- conn
return &singleConnListener{
addr: conn.LocalAddr(),
ch: ch,
}
}
func (l *singleConnListener) Accept() (net.Conn, error) {
conn, ok := <-l.ch
if !ok || conn == nil {
return nil, ErrNetClosing
}
return &connCloser{
l: l,
Conn: conn,
}, nil
}
func (l *singleConnListener) shutdown() error {
l.once.Do(func() {
close(l.ch)
})
return nil
}
func (l *singleConnListener) Close() error {
return nil
}
func (l *singleConnListener) Addr() net.Addr {
return l.addr
}
type connCloser struct {
l *singleConnListener
net.Conn
}
func (c *connCloser) Close() error {
c.l.shutdown()
return c.Conn.Close()
}
type httpServeConn struct {
*http.Server
}
func NewHttpServeConn(s *http.Server) ServeConn {
return &httpServeConn{s}
}
func (w httpServeConn) ServeConn(conn net.Conn) {
conn = httpproxy.NewConnCompatibilityReadDeadline(conn)
listener := newSingleConnListener(conn)
w.Serve(listener)
}