Răsfoiți Sursa

tentative unauth broadcast

reid 2 ani în urmă
părinte
comite
cddf37a45c
2 a modificat fișierele cu 16 adăugiri și 10 ștergeri
  1. 3 3
      auth/auth.go
  2. 13 7
      ws/ws.go

+ 3 - 3
auth/auth.go

@@ -53,11 +53,11 @@ func WsIsAuthenticated(conn *websocket.Conn, token string) bool {
 }
 
 // quick check if websocket is authed at all for unauth broadcast (not for actual auth)
-func WsAuthCheck(conn *websocket.Conn, token string) bool {
+func WsAuthCheck(conn *websocket.Conn) bool {
 	AuthenticatedClients.RLock()         // Acquire read lock
 	defer AuthenticatedClients.RUnlock() // Release read lock
-	for _, con := range AuthenticatedClients.Conn {
-		if val == con {
+	for _, con := range AuthenticatedClients.Conns {
+		if con == conn {
 			return true
 		}
 	}

+ 13 - 7
ws/ws.go

@@ -175,14 +175,20 @@ func loginHandler(conn *websocket.Conn, msg []byte, payload structs.WsPayload) e
 
 // broadcast the unauth payload
 func unauthHandler(conn *websocket.Conn, r *http.Request) {
-	blob := structs.UnauthBroadcast
-	blob.Type = "structure"
-	blob.AuthLevel = "unauthorized"
-	blob.Login = struct{
-		Remainder: 0
+	blob := structs.UnauthBroadcast{
+        Type:      "structure",
+        AuthLevel: "unauthorized",
+        Login: struct {
+            Remainder int `json:"remainder"`
+        }{
+            Remainder: 0,
+        },
+    }
+	resp, err := json.Marshal(blob)
+	if err != nil {
+		config.Logger.Error(fmt.Sprintf("Error unmarshalling message: %v", err))
 	}
-	resp := json.Marshal(blob)
-	if err := conn.WriteMessage(websocket.TextMessage, respJson); err != nil {
+	if err := conn.WriteMessage(websocket.TextMessage, resp); err != nil {
 		config.Logger.Error(fmt.Sprintf("Error writing response: %v", err))
 	}
 }