示例#1
0
func (c *Command) Execute(message []byte) {

	var commandDetector CommandDetector
	json.Unmarshal(message, &commandDetector)

	// inner connection auth
	if commandDetector.ServerUUID != nil {
		ServerUUID, _ := gocql.ParseUUID(*commandDetector.ServerUUID)
		// todo: check err
		server := model_server.Get(ServerUUID)
		answer := "error"
		if server.Exists && c.connection.GetRemoteAddr() == server.IP {
			c.connection.SetServerAuthState()
			answer = "ok"
		}

		c.connection.Send(`{"command":"` + answer + `"}`)
		return
	}

	// ---------------
	var session *model2_auth_session.Fields

	sessionUUID, err := gocql.ParseUUID(commandDetector.SessionUUID)
	if err == nil {
		session, err = model2_auth_session.Load(sessionUUID)
		if session != nil {
			if session.Exists {
				if session.RemoteAddr != c.connection.GetRemoteAddr() || session.UserAgent != c.connection.GetUserAgent() {
					session = nil
				}
			} else {
				session = nil
			}
		}
	}

	if err != nil || session == nil {
		session, err = model2_auth_session.Create()
		session.Update(model2.Fields{
			"RemoteAddr": c.connection.GetRemoteAddr(),
			"UserAgent":  c.connection.GetUserAgent(),
		})
	}

	if session.IsLock {
		b, err := star.Send(session.LockServerUUID, model.Fields{
			"command":      "get_session_lock_state",
			"session_uuid": session.UUID,
		})

		var commandCheckSessionDetector CommandCheckSessionDetector
		if err == nil {
			json.Unmarshal(b, &commandCheckSessionDetector)
		}

		if err != nil || commandCheckSessionDetector.IsLock {
			// session.Create(c.connection.GetRemoteAddr(), c.connection.GetUserAgent())
			session, err = model2_auth_session.Create()
			session.Update(model2.Fields{
				"RemoteAddr": c.connection.GetRemoteAddr(),
				"UserAgent":  c.connection.GetUserAgent(),
			})
		}

		logger.String(string(b))
	}

	// session.Lock()
	c.connection.SetSession(session)

	sendCommandAuth := SendCommandAuth{
		Command:      "auth",
		SessionUUID:  session.UUID.String(),
		AuthMethods:  c.ctx.Config.Auth.Methods,
		IsAuth:       session.IsAuth,
		PlayerExists: false,
	}

	if session.IsAuth {

		/*
			user := model2_user.New()
			user.UUID = session.UserUUID
			user.Load()
		*/
		user, _ := model2_user.Load(session.UserUUID)
		logger.String(fmt.Sprintf("user: %+v", user))

		// check for user lock
		if user.IsLock {

			b, err := star.Send(user.LockServerUUID, model.Fields{
				"command":   "get_user_lock_state",
				"user_uuid": user.UUID.String(),
			})

			if err != nil {

			} else {

				type CommandCheckUserDetector struct {
					IsLock bool `json:"is_lock"`
				}

				var commandCheckUserDetector CommandCheckUserDetector
				json.Unmarshal(b, &commandCheckUserDetector)

				if commandCheckUserDetector.IsLock {

					_, _ = star.Send(user.LockServerUUID, model.Fields{
						"command":      "star_user_logout",
						"user_uuid":    user.UUID,
						"session_uuid": session.UUID.String(),
					})

					user.Load()
				}

			}
		}

		// user.Lock()

		if user.PlayerUUID != nil {
			sendCommandAuth.PlayerExists = true
		}

		sendCommandAuth.User = SendCommandAuthUser{
			Name:        user.Name,
			SectionName: user.SectionName,
		}
	}

	b, err := json.Marshal(sendCommandAuth)
	if err != nil {
		logger.Error(errors.New(err))
		return
	}

	c.connection.Send(string(b))
}
示例#2
0
func NewServer(serverUUID gocql.UUID) *Server {
	s := &Server{isLocal: false}

	e := model_server.Get(serverUUID)
	if !e.Exists {
		logger.String(fmt.Sprintf("server %s not found in storage", serverUUID.String()))
		return nil
	}

	path := fmt.Sprintf("http://%s:%d/ws", e.IP, e.Port)
	logger.String(fmt.Sprintf("connect to: %s", path))
	u, err := url.Parse(path)
	if err != nil {

		logger.String(fmt.Sprintf("%v", err))

		// return err
		return nil
	}

	rawConn, err := net.Dial("tcp", u.Host)
	if err != nil {
		logger.String(fmt.Sprintf("%v", err))
		// return err
		return nil
	}

	wsHeaders := http.Header{
		"Origin": {"http://local.host:80"},
		// your milage may differ
		"Sec-WebSocket-Extensions": {"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"},
		"User-Agent":               {"spacecrfat-agent"},
	}

	wsConn, _, err := websocket.NewClient(rawConn, u, wsHeaders, 4096, 4096)
	if err != nil {
		logger.String(fmt.Sprintf("%v", err))
		// return fmt.Errorf("websocket.NewClient Error: %s\nResp:%+v", err, resp)
		return nil
	}

	s.ws = wsConn

	s.ws.SetWriteDeadline(time.Now().Add(10 * time.Second))
	err = s.ws.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"command":"auth","server_uuid":"%s"}`, localServer.UUID.String())))
	if err != nil {
		logger.String(fmt.Sprintf("%v", err))
		return nil
	}

	_, message, err := s.ws.ReadMessage()
	// s.ws.ReadMessage()

	if err != nil {
		if err != io.EOF {
			logger.Error(errors.New(err))
		}
	}

	smessage := string(message)
	logger.String(fmt.Sprintf("star auth answer: %v", smessage))

	go s.Reading()
	go s.Pinger()

	return s
}