feat(controller): connect to websockets via tls
This commit is contained in:
parent
7d1df69a69
commit
4c0da5ab71
|
|
@ -36,4 +36,5 @@ WS_TLS=""
|
||||||
WS_AUTH=""
|
WS_AUTH=""
|
||||||
WS_ROUTE=""
|
WS_ROUTE=""
|
||||||
WS_DISABLE=""
|
WS_DISABLE=""
|
||||||
|
WS_SKIP_VERIFY=""
|
||||||
# ---------------------------------------------------------------------------- #
|
# ---------------------------------------------------------------------------- #
|
||||||
|
|
@ -77,6 +77,7 @@ func main() {
|
||||||
flWsPort := flag.String("ws_port", lookupEnvOrString("WS_PORT", "8080"), "Websocket server port")
|
flWsPort := flag.String("ws_port", lookupEnvOrString("WS_PORT", "8080"), "Websocket server port")
|
||||||
flWsRoute := flag.String("ws_route", lookupEnvOrString("WS_ROUTE", "/ws/controller"), "Websocket server route")
|
flWsRoute := flag.String("ws_route", lookupEnvOrString("WS_ROUTE", "/ws/controller"), "Websocket server route")
|
||||||
flWsTls := flag.Bool("ws_tls", lookupEnvOrBool("WS_TLS", false), "Websocket server tls")
|
flWsTls := flag.Bool("ws_tls", lookupEnvOrBool("WS_TLS", false), "Websocket server tls")
|
||||||
|
flWsSkipVerify := flag.Bool("ws_skip_verify", lookupEnvOrBool("WS_SKIP_VERIFY", false), "Websocket skip tls certificate verify")
|
||||||
flDisableWs := flag.Bool("ws_disable", lookupEnvOrBool("WS_DISABLE", false), "Disable WS MTP")
|
flDisableWs := flag.Bool("ws_disable", lookupEnvOrBool("WS_DISABLE", false), "Disable WS MTP")
|
||||||
flDisableStomp := flag.Bool("stomp_disable", lookupEnvOrBool("STOMP_DISABLE", false), "Disable STOMP MTP")
|
flDisableStomp := flag.Bool("stomp_disable", lookupEnvOrBool("STOMP_DISABLE", false), "Disable STOMP MTP")
|
||||||
flDisableMqtt := flag.Bool("mqtt_disable", lookupEnvOrBool("MQTT_DISABLE", false), "Disable MQTT MTP")
|
flDisableMqtt := flag.Bool("mqtt_disable", lookupEnvOrBool("MQTT_DISABLE", false), "Disable MQTT MTP")
|
||||||
|
|
@ -169,14 +170,15 @@ func main() {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
wsClient = ws.Ws{
|
wsClient = ws.Ws{
|
||||||
Addr: *flWsAddr,
|
Addr: *flWsAddr,
|
||||||
Port: *flWsPort,
|
Port: *flWsPort,
|
||||||
Token: *flWsToken,
|
Token: *flWsToken,
|
||||||
Route: *flWsRoute,
|
Route: *flWsRoute,
|
||||||
Auth: *flWsAuth,
|
Auth: *flWsAuth,
|
||||||
TLS: *flWsTls,
|
TLS: *flWsTls,
|
||||||
DB: database,
|
InsecureSkipVerify: *flWsSkipVerify,
|
||||||
Ctx: ctx,
|
DB: database,
|
||||||
|
Ctx: ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
wsDone = make(chan os.Signal, 1)
|
wsDone = make(chan os.Signal, 1)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package ws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
@ -17,16 +18,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Ws struct {
|
type Ws struct {
|
||||||
Addr string
|
Addr string
|
||||||
Port string
|
Port string
|
||||||
Token string
|
Token string
|
||||||
Route string
|
Route string
|
||||||
Auth bool
|
Auth bool
|
||||||
TLS bool
|
TLS bool
|
||||||
Ctx context.Context
|
InsecureSkipVerify bool
|
||||||
NewDeviceQueue map[string]string
|
Ctx context.Context
|
||||||
NewDevQMutex *sync.Mutex
|
NewDeviceQueue map[string]string
|
||||||
DB db.Database
|
NewDevQMutex *sync.Mutex
|
||||||
|
DB db.Database
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -47,9 +49,14 @@ type deviceStatus struct {
|
||||||
var wsConn *websocket.Conn
|
var wsConn *websocket.Conn
|
||||||
|
|
||||||
func (w *Ws) Connect() {
|
func (w *Ws) Connect() {
|
||||||
|
log.Println("Connecting to WS endpoint...")
|
||||||
|
|
||||||
// communication with devices
|
prefix := "ws://"
|
||||||
wsUrl := "ws://" + w.Addr + ":" + w.Port + w.Route
|
if w.TLS {
|
||||||
|
prefix = "wss://"
|
||||||
|
}
|
||||||
|
|
||||||
|
wsUrl := prefix + w.Addr + ":" + w.Port + w.Route
|
||||||
|
|
||||||
if w.Auth {
|
if w.Auth {
|
||||||
log.Println("WS token:", w.Token)
|
log.Println("WS token:", w.Token)
|
||||||
|
|
@ -57,10 +64,16 @@ func (w *Ws) Connect() {
|
||||||
wsUrl = wsUrl + "?token=" + w.Token
|
wsUrl = wsUrl + "?token=" + w.Token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dialer := websocket.Dialer{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: w.InsecureSkipVerify,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Keeps trying to connect to the WS endpoint until it succeeds or receives a stop signal
|
// Keeps trying to connect to the WS endpoint until it succeeds or receives a stop signal
|
||||||
go func() {
|
go func(dialer websocket.Dialer) {
|
||||||
for {
|
for {
|
||||||
c, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
|
c, _, err := dialer.Dial(wsUrl, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error to connect to %s, err: %s", wsUrl, err)
|
log.Printf("Error to connect to %s, err: %s", wsUrl, err)
|
||||||
time.Sleep(WS_CONNECTION_RETRY)
|
time.Sleep(WS_CONNECTION_RETRY)
|
||||||
|
|
@ -72,7 +85,7 @@ func (w *Ws) Connect() {
|
||||||
go w.Subscribe()
|
go w.Subscribe()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}()
|
}(dialer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Ws) Disconnect() {
|
func (w *Ws) Disconnect() {
|
||||||
|
|
@ -100,13 +113,18 @@ func (w *Ws) Subscribe() {
|
||||||
w.NewDeviceQueue = make(map[string]string)
|
w.NewDeviceQueue = make(map[string]string)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
//TODO: deal with message in new go routine
|
|
||||||
msgType, wsMsg, err := wsConn.ReadMessage()
|
msgType, wsMsg, err := wsConn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("read:", err)
|
if websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||||
|
log.Printf("websocket error: %v", err)
|
||||||
|
w.Connect()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("websocket unexpected error:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: deal with message in new go routine
|
||||||
if msgType == websocket.TextMessage {
|
if msgType == websocket.TextMessage {
|
||||||
var deviceStatus deviceStatus
|
var deviceStatus deviceStatus
|
||||||
err = json.Unmarshal(wsMsg, &deviceStatus)
|
err = json.Unmarshal(wsMsg, &deviceStatus)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user