-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
178 lines (149 loc) · 3.9 KB
/
client.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
package httptunnel
import (
"bufio"
"context"
"errors"
"io"
"net/http"
"github.com/google/uuid"
"github.com/mediabuyerbot/go-wirenet"
)
type contextKey int
const (
// ContextKeyStreamReader is populated in the context by stream reader.
ContextKeyStreamReader contextKey = iota + 1
// ContextKeyStreamWriter is populated in the context by stream writer.
ContextKeyStreamWriter
// ContextKeySessionID is populated in the context by current session id.
ContextKeySessionID
)
var (
// ErrStreamReaderNotDefined returned when no stream reader is defined in context.
ErrStreamReaderNotDefined = errors.New("httptunnel: stream reader not defined")
// ErrStreamWriterNotDefined returned when no stream reader is defined in context.
ErrStreamWriterNotDefined = errors.New("httptunnel: stream writer not defined")
)
type Client struct {
streamName string
wire wirenet.Wire
readResponse ReadResponse
}
func NewClient(w wirenet.Wire, streamName string, options ...ClientOption) *Client {
c := &Client{
wire: w,
streamName: streamName,
readResponse: http.ReadResponse,
}
for _, option := range options {
if option == nil {
continue
}
option(c)
}
return c
}
// ClientOption sets an optional parameter for client instance.
type ClientOption func(c *Client)
func SetReadResponse(r ReadResponse) ClientOption {
return func(c *Client) { c.readResponse = r }
}
func WithSession(ctx context.Context, sessionID uuid.UUID) context.Context {
return context.WithValue(ctx, ContextKeySessionID, sessionID)
}
func (c *Client) WithTx(ctx context.Context, sessionID uuid.UUID, fn func(streamCtx context.Context) error) error {
session, err := c.wire.Session(sessionID)
if err != nil {
return err
}
stream, err := session.OpenStream(c.streamName)
if err != nil {
return err
}
defer stream.Close()
reader := stream.Reader()
writer := stream.Writer()
ctx = context.WithValue(ctx, ContextKeyStreamReader, reader)
ctx = context.WithValue(ctx, ContextKeyStreamWriter, writer)
return fn(ctx)
}
func (c *Client) Do(req *http.Request) (resp *http.Response, err error) {
ctx := req.Context()
sid, ok := ctx.Value(ContextKeySessionID).(uuid.UUID)
if !ok {
resp, err = c.doTx(ctx, req)
} else {
resp, err = c.doSession(sid, req)
}
return resp, err
}
func (c *Client) doSession(sid uuid.UUID, req *http.Request) (*http.Response, error) {
session, err := c.wire.Session(sid)
if err != nil {
return nil, err
}
stream, err := session.OpenStream(c.streamName)
if err != nil {
return nil, err
}
reader := stream.Reader()
writer := stream.Writer()
if err := req.WriteProxy(writer); err != nil {
return nil, err
}
if req.Body != nil {
req.Body.Close()
}
writer.Close()
resp, err := c.readResponse(bufio.NewReader(reader), req)
if err != nil {
return nil, err
}
resp.Body = readCloser{
stream: stream,
body: resp.Body,
streamReader: reader,
}
return resp, nil
}
func (c *Client) doTx(ctx context.Context, req *http.Request) (*http.Response, error) {
streamReader, ok := ctx.Value(ContextKeyStreamReader).(io.ReadCloser)
if !ok {
return nil, ErrStreamReaderNotDefined
}
streamWriter, ok := ctx.Value(ContextKeyStreamWriter).(io.WriteCloser)
if !ok {
return nil, ErrStreamWriterNotDefined
}
if err := req.WriteProxy(streamWriter); err != nil {
return nil, err
}
if req.Body != nil {
req.Body.Close()
}
streamWriter.Close()
resp, err := c.readResponse(bufio.NewReader(streamReader), req)
if err != nil {
return nil, err
}
resp.Body = readCloser{
body: resp.Body,
streamReader: streamReader,
}
return resp, nil
}
type readCloser struct {
stream io.Closer
streamReader io.Closer
body io.ReadCloser
}
func (rc readCloser) Read(p []byte) (n int, err error) {
return rc.body.Read(p)
}
func (rc readCloser) Close() error {
rc.body.Close()
rc.streamReader.Close()
if rc.stream != nil {
rc.stream.Close()
}
return nil
}