<commit_before>package webClient

import (
  "net/http"
  "github.com/gorilla/websocket"
  "github.com/satori/go.uuid"
  "github.com/M-O-S-E-S/mgm2/core"
)

type clientResponse struct {
  MessageID int
  MessageType string
  Message interface{}
}

type WebsocketConnector struct {
  httpConnector *HttpConnector
  session chan<- core.UserSession
  logger Logger
}

func NewWebsocketConnector(hc *HttpConnector, session chan<- core.UserSession, logger Logger) (*WebsocketConnector) {
  return &WebsocketConnector{hc, session, logger}
}

var upgrader = &websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}

func (wc WebsocketConnector) WebsocketHandler(w http.ResponseWriter, r *http.Request) {

  session, _ := wc.httpConnector.store.Get(r, "MGM")
  // test if session exists
  if len(session.Values) == 0 {
    wc.logger.Info("Websocket closed, no existing session")
    return
  }
  // test origin, etc for websocket security<commit_msg>Added json response to session test for websocket connections.  A sessionless client should pick this up and try to authenticate before connecting again.<commit_after>package webClient

import (
  "net/http"
  "github.com/gorilla/websocket"
  "github.com/satori/go.uuid"
  "github.com/M-O-S-E-S/mgm2/core"
  "encoding/json"
)

type clientResponse struct {
  MessageID int
  MessageType string
  Message interface{}
}

type WebsocketConnector struct {
  httpConnector *HttpConnector
  session chan<- core.UserSession
  logger Logger
}

func NewWebsocketConnector(hc *HttpConnector, session chan<- core.UserSession, logger Logger) (*WebsocketConnector) {
  return &WebsocketConnector{hc, session, logger}
}

var upgrader = &websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}

func (wc WebsocketConnector) WebsocketHandler(w http.ResponseWriter, r *http.Request) {

  // test if session exists
  session, _ := wc.httpConnector.store.Get(r, "MGM")
  if len(session.Values) == 0 {
    wc.logger.Info("Websocket closed, no existing session")

    response := clientResponse{ MessageType: "AccessDenied", Message: "No Session Found"}
    js, err := json.Marshal(response)
    if err != nil {
      http.Error(w, err.Error(), http.StatusInternalServerError)
      return
    }
    w.Header().Set("Content-Type", "application/json")
    w.Write(js)
    return
  }
  // test origin, etc for websocket security
{'fraction_non_alphanumeric': 0.1096528365791702, 'fraction_numerical': 0.00846740050804403, 'mean_word_length': 4.346153846153846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4716501', 'n_tokens_mistral': 770, 'n_tokens_neox': 759, 'n_words': 191}