Ejemplo n.º 1
0
func SocketHandler(w http.ResponseWriter, r *http.Request) {
	token, err := chelpers.GetToken(r)
	if err != nil && err != http.ErrNoCookie { //invalid jwt token
		logrus.Errorf("Error reading JWT: %v", err)
		token = nil
	}

	//check if player is in the whitelist
	if config.Constants.SteamIDWhitelist != "" {
		if token == nil {
			// player isn't logged in,
			// and access is restricted to logged in people
			http.Error(w, "Not logged in", http.StatusForbidden)
			return
		}

		if !chelpers.IsSteamIDWhitelisted(token.Claims.(*chelpers.TF2StadiumClaims).SteamID) {
			http.Error(w, "you're not in the beta", http.StatusForbidden)
			return
		}
	}

	var so *wsevent.Client

	if token != nil { //received valid jwt
		so, err = socket.AuthServer.NewClient(upgrader, w, r)
	} else {
		so, err = socket.UnauthServer.NewClient(upgrader, w, r)
	}

	if err != nil {
		return
	}

	so.Token = token

	//logrus.Debug("Connected to Socket")
	err = SocketInit(so)
	if err != nil {
		logrus.Error(err)
		so.Close()
		return
	}
}