-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtransporter_builder.go
312 lines (275 loc) · 8.55 KB
/
transporter_builder.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package rsocket
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"os"
"github.com/gorilla/websocket"
"github.com/rsocket/rsocket-go/core/transport"
)
const (
// DefaultUnixSockPath is the default UDS sock file path.
DefaultUnixSockPath = "/var/run/rsocket.sock"
// DefaultPort is the default port RSocket used.
DefaultPort = 7878
)
// TCPClientBuilder provides builder which can be used to create a client-side TCP transport easily.
type TCPClientBuilder struct {
addr string
tlsCfg *tls.Config
}
// TCPServerBuilder provides builder which can be used to create a server-side TCP transport easily.
type TCPServerBuilder struct {
addr string
tlsCfg *tls.Config
}
// WebsocketClientBuilder provides builder which can be used to create a client-side Websocket transport easily.
type WebsocketClientBuilder struct {
url string
tlsCfg *tls.Config
header http.Header
proxy func(*http.Request) (*url.URL, error)
}
// WebsocketServerBuilder provides builder which can be used to create a server-side Websocket transport easily.
type WebsocketServerBuilder struct {
addr string
path string
tlsConfig *tls.Config
upgrader *websocket.Upgrader
}
// UnixClientBuilder provides builder which can be used to create a client-side UDS transport easily.
type UnixClientBuilder struct {
path string
}
// UnixServerBuilder provides builder which can be used to create a server-side UDS transport easily.
type UnixServerBuilder struct {
path string
}
// SetPath sets UDS sock file path.
func (us *UnixServerBuilder) SetPath(path string) *UnixServerBuilder {
us.path = path
return us
}
// Build builds and returns a new ServerTransporter.
func (us *UnixServerBuilder) Build() transport.ServerTransporter {
return func(ctx context.Context) (transport.ServerTransport, error) {
if _, err := os.Stat(us.path); !os.IsNotExist(err) {
return nil, err
}
return transport.NewTCPServerTransportWithAddr("unix", us.path, nil), nil
}
}
// SetPath sets UDS sock file path.
func (uc *UnixClientBuilder) SetPath(path string) *UnixClientBuilder {
uc.path = path
return uc
}
// Build builds and returns a new ClientTransporter.
func (uc UnixClientBuilder) Build() transport.ClientTransporter {
return func(ctx context.Context) (*transport.Transport, error) {
return transport.NewTCPClientTransportWithAddr(ctx, "unix", uc.path, nil)
}
}
// SetAddr sets the websocket listen addr. Default addr is "127.0.0.1:7878".
func (ws *WebsocketServerBuilder) SetAddr(addr string) *WebsocketServerBuilder {
ws.addr = addr
return ws
}
// SetPath sets the path of websocket.
func (ws *WebsocketServerBuilder) SetPath(path string) *WebsocketServerBuilder {
ws.path = path
return ws
}
// SetTLSConfig sets the tls config.
//
// You can generate cert.pem and key.pem for local testing:
//
// go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost
//
// Load X509
// cert, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
// if err != nil {
// panic(err)
// }
// // Init TLS configuration.
// tc := &tls.Config{
// MinVersion: tls.VersionTLS12,
// CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
// PreferServerCipherSuites: true,
// CipherSuites: []uint16{
// tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
// tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_RSA_WITH_AES_256_CBC_SHA,
// },
// Certificates: []tls.Certificate{cert},
// }
func (ws *WebsocketServerBuilder) SetTLSConfig(c *tls.Config) *WebsocketServerBuilder {
ws.tlsConfig = c
return ws
}
// SetUpgrader sets websocket upgrader.
// You can customize your own websocket upgrader instead of the default upgrader.
//
// Example(also the default value):
// upgrader := &websocket.Upgrader{
// ReadBufferSize: 1024,
// WriteBufferSize: 1024,
// CheckOrigin: func(r *http.Request) bool {
// return true
// },
// }
func (ws *WebsocketServerBuilder) SetUpgrader(upgrader *websocket.Upgrader) *WebsocketServerBuilder {
ws.upgrader = upgrader
return ws
}
// Build builds and returns a new websocket ServerTransporter.
func (ws *WebsocketServerBuilder) Build() transport.ServerTransporter {
return func(ctx context.Context) (transport.ServerTransport, error) {
return transport.NewWebsocketServerTransportWithAddr(ws.addr, ws.path, ws.upgrader, ws.tlsConfig), nil
}
}
// SetTLSConfig sets the tls config.
//
// Here's an example:
//
// tc := &tls.Config{
// InsecureSkipVerify: true,
// }
func (wc *WebsocketClientBuilder) SetTLSConfig(c *tls.Config) *WebsocketClientBuilder {
wc.tlsCfg = c
return wc
}
// SetURL sets the target url.
// Example: ws://127.0.0.1:7878/hello/world
func (wc *WebsocketClientBuilder) SetURL(url string) *WebsocketClientBuilder {
wc.url = url
return wc
}
// SetHeader sets header.
func (wc *WebsocketClientBuilder) SetHeader(header http.Header) *WebsocketClientBuilder {
wc.header = header
return wc
}
// SetProxy sets proxy.
func (wc *WebsocketClientBuilder) SetProxy(proxy func(*http.Request) (*url.URL, error)) *WebsocketClientBuilder {
wc.proxy = proxy
return wc
}
// Build builds and returns a new websocket ClientTransporter
func (wc *WebsocketClientBuilder) Build() transport.ClientTransporter {
return func(ctx context.Context) (*transport.Transport, error) {
return transport.NewWebsocketClientTransport(ctx, wc.url, wc.tlsCfg, wc.header, wc.proxy)
}
}
// SetHostAndPort sets the host and port.
func (ts *TCPServerBuilder) SetHostAndPort(host string, port int) *TCPServerBuilder {
ts.addr = fmt.Sprintf("%s:%d", host, port)
return ts
}
// SetAddr sets the addr.
func (ts *TCPServerBuilder) SetAddr(addr string) *TCPServerBuilder {
ts.addr = addr
return ts
}
// SetTLSConfig sets the tls config.
//
// You can generate cert.pem and key.pem for local testing:
//
// go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost
//
// Load X509
// cert, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
// if err != nil {
// panic(err)
// }
// // Init TLS configuration.
// tc := &tls.Config{
// MinVersion: tls.VersionTLS12,
// CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
// PreferServerCipherSuites: true,
// CipherSuites: []uint16{
// tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
// tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_RSA_WITH_AES_256_CBC_SHA,
// },
// Certificates: []tls.Certificate{cert},
// }
func (ts *TCPServerBuilder) SetTLSConfig(c *tls.Config) *TCPServerBuilder {
ts.tlsCfg = c
return ts
}
// Build builds and returns a new TCP ServerTransporter.
func (ts *TCPServerBuilder) Build() transport.ServerTransporter {
return func(ctx context.Context) (transport.ServerTransport, error) {
return transport.NewTCPServerTransportWithAddr("tcp", ts.addr, ts.tlsCfg), nil
}
}
// SetHostAndPort sets the host and port.
func (tc *TCPClientBuilder) SetHostAndPort(host string, port int) *TCPClientBuilder {
tc.addr = fmt.Sprintf("%s:%d", host, port)
return tc
}
// SetAddr sets the addr
func (tc *TCPClientBuilder) SetAddr(addr string) *TCPClientBuilder {
tc.addr = addr
return tc
}
// SetTLSConfig sets the tls config.
//
// Here's an example:
//
// tc := &tls.Config{
// InsecureSkipVerify: true,
// }
func (tc *TCPClientBuilder) SetTLSConfig(c *tls.Config) *TCPClientBuilder {
tc.tlsCfg = c
return tc
}
// Build builds and returns a new TCP ClientTransporter.
func (tc *TCPClientBuilder) Build() transport.ClientTransporter {
return func(ctx context.Context) (*transport.Transport, error) {
return transport.NewTCPClientTransportWithAddr(ctx, "tcp", tc.addr, tc.tlsCfg)
}
}
// TCPClient creates a new TCPClientBuilder
func TCPClient() *TCPClientBuilder {
return &TCPClientBuilder{
addr: fmt.Sprintf(":%d", DefaultPort),
}
}
// TCPServer creates a new TCPServerBuilder
func TCPServer() *TCPServerBuilder {
return &TCPServerBuilder{
addr: fmt.Sprintf(":%d", DefaultPort),
}
}
// WebsocketClient creates a new WebsocketClientBuilder.
func WebsocketClient() *WebsocketClientBuilder {
return &WebsocketClientBuilder{
url: fmt.Sprintf("ws://127.0.0.1:%d", DefaultPort),
proxy: http.ProxyFromEnvironment,
}
}
// WebsocketServer creates a new WebsocketServerBuilder.
func WebsocketServer() *WebsocketServerBuilder {
return &WebsocketServerBuilder{
addr: fmt.Sprintf(":%d", DefaultPort),
path: "/",
}
}
// UnixClient creates a new UnixClientBuilder.
func UnixClient() *UnixClientBuilder {
return &UnixClientBuilder{
path: DefaultUnixSockPath,
}
}
// UnixServer creates a new UnixServerBuilder.
func UnixServer() *UnixServerBuilder {
return &UnixServerBuilder{
path: DefaultUnixSockPath,
}
}