-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
342 lines (294 loc) · 10 KB
/
main.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
mflog "github.com/mainflux/mainflux/logger"
"github.com/mainflux/mproxy/examples/simple"
hproxy "github.com/mainflux/mproxy/pkg/http"
"github.com/mainflux/mproxy/pkg/mqtt"
"github.com/mainflux/mproxy/pkg/mqtt/websocket"
"github.com/mainflux/mproxy/pkg/session"
mptls "github.com/mainflux/mproxy/pkg/tls"
"github.com/mainflux/mproxy/pkg/websockets"
)
const (
// WS - MQTT
defMQTTWSHost = "0.0.0.0"
defMQTTWSPath = "/mqtt"
defMQTTWSPort = "8080"
defMQTTWSSPath = "/mqtt"
defMQTTWSSPort = "8081"
defMQTTWSTargetScheme = "ws"
defMQTTWSTargetHost = "localhost"
defMQTTWSTargetPort = "8888"
defMQTTWSTargetPath = "/mqtt"
envMQTTWSHost = "MPROXY_MQTT_WS_HOST"
envMQTTWSPort = "MPROXY_MQTT_WS_PORT"
envMQTTWSPath = "MPROXY_MQTT_WS_PATH"
envMQTTWSSPort = "MPROXY_MQTT_WSS_PORT"
envMQTTWSSPath = "MPROXY_MQTT_WSS_PATH"
envMQTTWSTargetScheme = "MPROXY_MQTT_WS_TARGET_SCHEME"
envMQTTWSTargetHost = "MPROXY_MQTT_WS_TARGET_HOST"
envMQTTWSTargetPort = "MPROXY_MQTT_WS_TARGET_PORT"
envMQTTWSTargetPath = "MPROXY_MQTT_WS_TARGET_PATH"
// MQTT
defMQTTHost = "0.0.0.0"
defMQTTPort = "1883"
defMQTTSPort = "8883"
defMQTTTargetHost = "0.0.0.0"
defMQTTTargetPort = "1884"
defCACerts = ""
defServerCert = ""
defServerKey = ""
envMQTTHost = "MPROXY_MQTT_HOST"
envMQTTPort = "MPROXY_MQTT_PORT"
envMQTTSPort = "MPROXY_MQTTS_PORT"
envMQTTTargetHost = "MPROXY_MQTT_TARGET_HOST"
envMQTTTargetPort = "MPROXY_MQTT_TARGET_PORT"
envCACerts = "MPROXY_CA_CERTS"
envServerCert = "MPROXY_SERVER_CERT"
envServerKey = "MPROXY_SERVER_KEY"
// WS
defWSHost = "0.0.0.0"
defWSPort = "8081"
defWSTargetHost = "ws://localhost"
defWSTargetPort = "8889"
envWSHost = "MPROXY_WS_HOST"
envWSPort = "MPROXY_WS_PORT"
envWSTargetHost = "MPROXY_MQTT_WS_TARGET_HOST"
envWSTargetPort = "MPROXY_MQTT_WS_TARGET_PORT"
defClientTLS = "false"
envClientTLS = "MPROXY_CLIENT_TLS"
defLogLevel = "debug"
envLogLevel = "MPROXY_LOG_LEVEL"
envHTTPHost = "MPROXY_HTTP_HOST"
envHTTPPort = "MPROXY_HTTP_PORT"
envHTTPargetHost = "MPROXY_HTTP_TARGET_HOST"
envHTTPTargetPort = "MPROXY_HTTP_TARGET_PORT"
envHTTPServerCert = "MPROXY_HTTP_SERVER_CERT"
envHTTPServerKey = "MPROXY_HTTP_SERVER_KEY"
defHTTPHost = "0.0.0.0"
defHTTPPort = "8888"
defHTTPTargetHost = "http://localhost"
defHTTPTargetPort = "8081"
defHTTPServerCert = ""
defHTTPServerKey = ""
)
type config struct {
clientTLS bool
caCerts string
serverCert string
serverKey string
httpConfig HTTPConfig
mqttConfig MQTTConfig
wsMQTTConfig WSMQTTConfig
wsConfig WSConfig
logLevel string
}
type WSConfig struct {
host string
port string
targetHost string
targetPort string
}
type WSMQTTConfig struct {
host string
port string
path string
wssPort string
wssPath string
targetScheme string
targetHost string
targetPort string
targetPath string
}
type MQTTConfig struct {
host string
port string
mqttsPort string
targetHost string
targetPort string
}
type HTTPConfig struct {
host string
port string
targetHost string
targetPort string
serverCert string
serverKey string
}
func main() {
cfg := loadConfig()
logger, err := mflog.New(os.Stdout, cfg.logLevel)
if err != nil {
log.Fatalf(err.Error())
}
h := simple.New(logger)
errs := make(chan error, 3)
ctx := context.Background()
if cfg.clientTLS {
tlsCfg, err := mptls.LoadTLSCfg(cfg.caCerts, cfg.serverCert, cfg.serverKey)
if err != nil {
errs <- err
}
// WSS - MQTT
logger.Info(fmt.Sprintf("Starting encrypted WebSocket proxy on port %s ", cfg.wsMQTTConfig.wssPort))
go proxyMQTTWSS(cfg, tlsCfg, logger, h, errs)
// MQTTS
logger.Info(fmt.Sprintf("Starting MQTTS proxy on port %s ", cfg.mqttConfig.mqttsPort))
go proxyMQTTS(ctx, cfg.mqttConfig, tlsCfg, logger, h, errs)
// WSS
logger.Info(fmt.Sprintf("Starting WSS proxy on port %s ", cfg.wsConfig.port))
go proxyWSS(ctx, cfg, logger, h, errs)
// HTTPS
logger.Info(fmt.Sprintf("Starting HTTPS proxy on port %s ", cfg.httpConfig.port))
go proxyHTTPS(ctx, cfg.httpConfig, logger, h, errs)
} else {
// WS - MQTT
logger.Info(fmt.Sprintf("Starting WebSocket proxy on port %s ", cfg.wsMQTTConfig.port))
go proxyMQTTWS(cfg.wsMQTTConfig, logger, h, errs)
// MQTT
logger.Info(fmt.Sprintf("Starting MQTT proxy on port %s ", cfg.mqttConfig.port))
go proxyMQTT(ctx, cfg.mqttConfig, logger, h, errs)
// WS
logger.Info(fmt.Sprintf("Starting WS proxy on port %s ", cfg.wsConfig.port))
go proxyWS(ctx, cfg.wsConfig, logger, h, errs)
// HTTP
logger.Info(fmt.Sprintf("Starting HTTP proxy on port %s ", cfg.httpConfig.port))
go proxyHTTP(ctx, cfg.httpConfig, logger, h, errs)
}
go func() {
c := make(chan os.Signal, 2)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()
err = <-errs
logger.Error(fmt.Sprintf("mProxy terminated: %s", err))
}
func env(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func loadConfig() config {
tls, err := strconv.ParseBool(env(envClientTLS, defClientTLS))
if err != nil {
log.Fatalf("Invalid value passed for %s\n", envClientTLS)
}
return config{
// WS
wsMQTTConfig: WSMQTTConfig{
host: env(envMQTTWSHost, defMQTTWSHost),
port: env(envMQTTWSPort, defMQTTWSPort),
path: env(envMQTTWSPath, defMQTTWSPath),
wssPort: env(envMQTTWSSPort, defMQTTWSSPort),
wssPath: env(envMQTTWSSPath, defMQTTWSSPath),
targetScheme: env(envMQTTWSTargetScheme, defMQTTWSTargetScheme),
targetHost: env(envMQTTWSTargetHost, defMQTTWSTargetHost),
targetPort: env(envMQTTWSTargetPort, defMQTTWSTargetPort),
targetPath: env(envMQTTWSTargetPath, defMQTTWSTargetPath),
},
// MQTT
mqttConfig: MQTTConfig{
host: env(envMQTTHost, defMQTTHost),
port: env(envMQTTPort, defMQTTPort),
mqttsPort: env(envMQTTSPort, defMQTTSPort),
targetHost: env(envMQTTTargetHost, defMQTTTargetHost),
targetPort: env(envMQTTTargetPort, defMQTTTargetPort),
},
clientTLS: tls,
caCerts: env(envCACerts, defCACerts),
serverCert: env(envServerCert, defServerCert),
serverKey: env(envServerKey, defServerKey),
// HTTP
httpConfig: HTTPConfig{
port: env(envHTTPPort, defHTTPPort),
host: env(envHTTPHost, defHTTPHost),
targetHost: env(envHTTPargetHost, defHTTPTargetHost),
targetPort: env(envHTTPTargetPort, defHTTPTargetPort),
serverCert: env(envHTTPServerCert, defHTTPServerCert),
serverKey: env(envHTTPServerKey, defHTTPServerKey),
},
// WS
wsConfig: WSConfig{
host: env(envWSHost, defWSHost),
port: env(envWSPort, defWSPort),
targetHost: env(envWSTargetHost, defWSTargetHost),
targetPort: env(envWSTargetPort, defWSTargetPort),
},
// Log
logLevel: env(envLogLevel, defLogLevel),
}
}
func proxyMQTTWS(cfg WSMQTTConfig, logger mflog.Logger, handler session.Handler, errs chan error) {
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
wp := websocket.New(target, cfg.targetPath, cfg.targetScheme, handler, logger)
http.Handle(cfg.path, wp.Handler())
errs <- wp.Listen(cfg.port)
}
func proxyMQTTWSS(cfg config, tlsCfg *tls.Config, logger mflog.Logger, handler session.Handler, errs chan error) {
target := fmt.Sprintf("%s:%s", cfg.wsMQTTConfig.targetHost, cfg.wsMQTTConfig.targetPort)
wp := websocket.New(target, cfg.wsMQTTConfig.targetPath, cfg.wsMQTTConfig.targetScheme, handler, logger)
http.Handle(cfg.wsMQTTConfig.wssPath, wp.Handler())
errs <- wp.ListenTLS(tlsCfg, cfg.serverCert, cfg.serverKey, cfg.wsMQTTConfig.wssPort)
}
func proxyMQTT(ctx context.Context, cfg MQTTConfig, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.port)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
mp := mqtt.New(address, target, handler, logger)
errs <- mp.Listen(ctx)
}
func proxyMQTTS(ctx context.Context, cfg MQTTConfig, tlsCfg *tls.Config, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.mqttsPort)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
mp := mqtt.New(address, target, handler, logger)
errs <- mp.ListenTLS(ctx, tlsCfg)
}
func proxyHTTP(ctx context.Context, cfg HTTPConfig, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.port)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
hp, err := hproxy.NewProxy(address, target, handler, logger)
if err != nil {
errs <- err
return
}
http.HandleFunc("/", hp.Handler)
errs <- hp.Listen()
}
func proxyHTTPS(ctx context.Context, cfg HTTPConfig, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.port)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
hp, err := hproxy.NewProxy(address, target, handler, logger)
if err != nil {
errs <- err
return
}
http.HandleFunc("/", hp.Handler)
errs <- hp.ListenTLS(cfg.serverCert, cfg.serverKey)
}
func proxyWS(ctx context.Context, cfg WSConfig, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.port)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
wp, err := websockets.NewProxy(address, target, logger, handler)
if err != nil {
errs <- err
}
errs <- wp.Listen()
}
func proxyWSS(ctx context.Context, cfg config, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.wsConfig.host, cfg.wsConfig.port)
target := fmt.Sprintf("%s:%s", cfg.wsConfig.targetHost, cfg.wsConfig.targetPort)
wp, err := websockets.NewProxy(address, target, logger, handler)
if err != nil {
errs <- err
}
errs <- wp.ListenTLS(cfg.serverCert, cfg.serverKey)
}