forked from yinqiwen/gsnova
/
http2.go
executable file
·223 lines (202 loc) · 5.07 KB
/
http2.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
package mux
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"sync"
"sync/atomic"
"time"
"github.com/yinqiwen/gsnova/common/helper"
"github.com/yinqiwen/gsnova/common/logger"
"github.com/yinqiwen/pmux"
"golang.org/x/net/http2"
)
// type cachedClientConnPool struct {
// conns sync.Map
// }
// func (c *cachedClientConnPool) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) {
// var conn *http2.ClientConn
// c.conns.Range(func(key, value interface{}) bool {
// conn = key.(*http2.ClientConn)
// return false
// })
// if nil != conn {
// return conn, nil
// }
// return nil, fmt.Errorf("Existing connection closed")
// }
// func (c *cachedClientConnPool) MarkDead(conn *http2.ClientConn) {
// c.conns.Delete(conn)
// }
// func (c *cachedClientConnPool) add(conn *http2.ClientConn) {
// c.conns.Store(conn, true)
// }
// var cacheConns cachedClientConnPool
var http2Transport = &http2.Transport{}
type http2ClientMuxStream struct {
w *io.PipeWriter
r io.ReadCloser
readReady chan struct{}
closed bool
}
func (s *http2ClientMuxStream) setReader(rr io.ReadCloser) {
s.r = rr
helper.AsyncNotify(s.readReady)
}
func (s *http2ClientMuxStream) Read(b []byte) (n int, err error) {
for s.r == nil && !s.closed {
select {
case <-s.readReady:
case <-time.After(30 * time.Second):
return 0, pmux.ErrTimeout
}
}
if s.closed {
return 0, io.EOF
}
n, err = s.r.Read(b)
return
}
func (s *http2ClientMuxStream) Write(b []byte) (n int, err error) {
if s.closed {
return 0, io.EOF
}
n, err = s.w.Write(b)
return
}
func (s *http2ClientMuxStream) Close() (err error) {
s.closed = true
s.w.Close()
if nil != s.r {
s.r.Close()
}
helper.AsyncNotify(s.readReady)
return nil
}
type HTTP2MuxSession struct {
streamCounter int64
net.Conn
h2Conn *http2.ClientConn
tr *http2.Transport
ServerHost string
//Client *http.Client
//Client *http2.Transport
AcceptCh chan MuxStream
closeCh chan struct{}
streams sync.Map
}
func (q *HTTP2MuxSession) CloseStream(stream MuxStream) error {
q.streams.Delete(stream)
atomic.AddInt64(&q.streamCounter, -1)
return nil
}
func (q *HTTP2MuxSession) OfferStream(stream TimeoutReadWriteCloser) error {
s := &ProxyMuxStream{TimeoutReadWriteCloser: stream, session: q}
select {
case q.AcceptCh <- s:
return nil
default:
stream.Close()
return fmt.Errorf("Can NOT accept new stream")
}
}
func (q *HTTP2MuxSession) Ping() (time.Duration, error) {
start := time.Now()
if nil != q.h2Conn {
q.h2Conn.Ping(context.Background())
return time.Now().Sub(start), nil
}
return 0, nil
}
func (q *HTTP2MuxSession) OpenStream() (MuxStream, error) {
if nil == q.Conn {
return nil, pmux.ErrSessionShutdown
}
pr, pw := io.Pipe()
req := &http.Request{
Method: http.MethodPost,
URL: &url.URL{Scheme: "https", Host: q.ServerHost},
Header: make(http.Header),
Proto: "HTTP/2.0",
ProtoMajor: 2,
ProtoMinor: 0,
Body: pr,
Host: q.ServerHost,
ContentLength: -1,
}
stream := &http2ClientMuxStream{
w: pw,
readReady: make(chan struct{}),
}
go func() {
//opt := http2.RoundTripOpt{OnlyCachedConn: true}
res, err := q.h2Conn.RoundTrip(req)
//res, err := q.Client.Do(req)
if nil != err {
logger.Error("Failed to post http/2 with error:%v", err)
stream.Close()
q.Close()
} else {
stream.setReader(res.Body)
}
}()
muxStream := &ProxyMuxStream{TimeoutReadWriteCloser: &helper.TimeoutReadWriteCloser{ReadWriteCloser: stream}, session: q}
atomic.AddInt64(&q.streamCounter, 1)
q.streams.Store(muxStream, true)
return muxStream, nil
}
func (q *HTTP2MuxSession) AcceptStream() (MuxStream, error) {
select {
case conn := <-q.AcceptCh:
q.streams.Store(conn, true)
return conn, nil
case <-q.closeCh:
return nil, pmux.ErrSessionShutdown
}
}
func (q *HTTP2MuxSession) NumStreams() int {
return int(q.streamCounter)
}
func (q *HTTP2MuxSession) Close() error {
helper.AsyncNotify(q.closeCh)
if nil != q.Conn {
q.Conn.Close()
q.Conn = nil
}
q.streams.Range(func(key, value interface{}) bool {
stream := key.(MuxStream)
stream.Close()
return true
})
http2Transport.CloseIdleConnections()
//q.streams = sync.Map{}
return nil
}
func NewHTTP2ServerMuxSession(conn net.Conn) *HTTP2MuxSession {
s := &HTTP2MuxSession{}
s.AcceptCh = make(chan MuxStream, 64)
s.closeCh = make(chan struct{})
s.Conn = conn
return s
}
func NewHTTP2ClientMuxSession(conn net.Conn, host string) (MuxSession, error) {
s := &HTTP2MuxSession{}
s.Conn = conn
s.closeCh = make(chan struct{})
cc, err := http2Transport.NewClientConn(conn)
if nil != err {
return nil, err
}
s.h2Conn = cc
//cacheConns.add(cc)
//tr.ConnPool = &singleClientConnPool{conn: cc}
//client := &http.Client{}
//client.Transport = tr
//s.Client = tr
//s.Client = client
s.ServerHost = host
return s, nil
}