Пример #1
0
        public HttpResponseMessage Get()
        {
            var api  = Api.INSTANCE;
            var user = api.RequireAuthentication(Request);

            var result = new XMLList <AvatarData>("The-Sims-Online");

            using (var db = api.DAFactory.Get())
            {
                var avatars = db.Avatars.GetSummaryByUserId(user.UserID);

                foreach (var avatar in avatars)
                {
                    result.Add(new AvatarData
                    {
                        ID             = avatar.avatar_id,
                        Name           = avatar.name,
                        ShardName      = api.Shards.GetById(avatar.shard_id).Name,
                        HeadOutfitID   = avatar.head,
                        BodyOutfitID   = avatar.body,
                        AppearanceType = (AvatarAppearanceType)Enum.Parse(typeof(AvatarAppearanceType), avatar.skin_tone.ToString()),
                        Description    = avatar.description,
                        LotId          = avatar.lot_id,
                        LotName        = avatar.lot_name,
                        LotLocation    = avatar.lot_location
                    });
                }
            }

            return(ApiResponse.Xml(HttpStatusCode.OK, result));
        }
Пример #2
0
        public IActionResult Get()
        {
            var api = Api.INSTANCE;

            var result = new XMLList <ShardStatusItem>("Shard-Status-List");
            var shards = api.Shards.All;

            foreach (var shard in shards)
            {
                result.Add(shard);
            }
            return(ApiResponse.Xml(HttpStatusCode.OK, result));
        }
        /// <summary>
        /// Example for the useage of the new XML library
        /// Use the hashtable if you don't need nested XML (like the standard xml responses)
        /// If you need nested XML, use the XMLPair class. The Key-parameter is String.
        /// As value the following types can be used to achieve nesting: XMLPair, XMLPair[] and Hashtable
        /// </summary>
        /// <param name="e"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        private void MultipleXML(Request e, Hashtable returnHashtable)
        {
            returnHashtable.Add("UseTheHashtable", "If you don't need nested XML");

            XMLList Phones = new XMLList("Phones");
            Phones.Attributes.Add("ExampleAttribute1", "NeonMika");
            Phones.Attributes.Add("ExampleAttribute2", 1992);
            XMLList BluePhones = new XMLList("BluePhones");
            XMLList BlackPhones = new XMLList("BlackPhones");
            XMLList MokiaRumia = new XMLList("Phone");
            XMLList LangsumTalaxy = new XMLList("Phone");
            MokiaRumia.Add(new XMLPair("Name", "Mokia Rumia"));
            MokiaRumia.Add(new XMLPair("PhoneNumber", 436603541897));
            XMLList WirelessConnections = new XMLList("WirelessConnections");
            WirelessConnections.Add(new XMLPair("WLAN", true));
            WirelessConnections.Add(new XMLPair("Bluetooth", false));
            MokiaRumia.Add(WirelessConnections);
            WirelessConnections.Clear( );
            WirelessConnections.Add(new XMLPair("WLAN", false));
            WirelessConnections.Add(new XMLPair("Bluetooth", true));
            LangsumTalaxy.Add(new XMLPair("Name", "Langsum Talaxy"));
            LangsumTalaxy.Add(new XMLPair("PhoneNumber", 436603541122));
            LangsumTalaxy.Add(WirelessConnections);

            Phones.Add(MokiaRumia);
            Phones.Add(LangsumTalaxy);

            returnHashtable.Add("Phones", Phones);
        }
Пример #4
0
        public CitySelectorController(IDAFactory DAFactory, ApiServerConfiguration config, JWTFactory jwt, IShardsDomain shardsDomain) : base("/cityselector")
        {
            JsonWebToken.JWTTokenAuthentication.Enable(this, jwt);

            var str   = GetServerVersion();
            var split = str.LastIndexOf('-');

            VersionName = str;
            if (split != -1)
            {
                VersionName   = str.Substring(0, split);
                VersionNumber = str.Substring(split + 1);
            }

            try
            {
                using (var file = File.Open("updateUrl.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var reader = new StreamReader(file);
                    DownloadURL = reader.ReadLine();
                    reader.Close();
                }
            } catch (Exception)
            {
                DownloadURL = ""; // couldn't find info from the watchdog
            }

            //Take the auth ticket, establish trust and then create a cookie (reusing JWT)
            this.Get["/app/InitialConnectServlet"] = _ =>
            {
                var ticketValue = this.Request.Query["ticket"];
                var version     = this.Request.Query["version"];

                if (ticketValue == null)
                {
                    return(Response.AsXml(new XMLErrorMessage(ERROR_MISSING_TOKEN_CODE, ERROR_MISSING_TOKEN_MSG)));
                }

                using (var db = DAFactory.Get())
                {
                    var ticket = db.AuthTickets.Get((string)ticketValue);
                    if (ticket == null)
                    {
                        return(Response.AsXml(new XMLErrorMessage(ERROR_MISSING_TOKEN_CODE, ERROR_MISSING_TOKEN_MSG)));
                    }


                    db.AuthTickets.Delete((string)ticketValue);
                    if (ticket.date + config.AuthTicketDuration < Epoch.Now)
                    {
                        return(Response.AsXml(new XMLErrorMessage(ERROR_EXPIRED_TOKEN_CODE, ERROR_EXPIRED_TOKEN_MSG)));
                    }

                    /** Is it a valid account? **/
                    var user = db.Users.GetById(ticket.user_id);
                    if (user == null)
                    {
                        return(Response.AsXml(new XMLErrorMessage(ERROR_MISSING_TOKEN_CODE, ERROR_MISSING_TOKEN_MSG)));
                    }

                    //Use JWT to create and sign an auth cookies
                    var session = new JWTUserIdentity()
                    {
                        UserID   = user.user_id,
                        UserName = user.username
                    };

                    var token = jwt.CreateToken(session);
                    return(Response.AsXml(new UserAuthorized()
                    {
                        FSOBranch = VersionName,
                        FSOVersion = VersionNumber,
                        FSOUpdateUrl = DownloadURL
                    })
                           .WithCookie("fso", token.Token));
                }
            };

            //Return a list of the users avatars
            this.Get["/app/AvatarDataServlet"] = _ =>
            {
                this.RequiresAuthentication();
                var user = (JWTUserIdentity)this.Context.CurrentUser;

                var result = new XMLList <AvatarData>("The-Sims-Online");

                using (var db = DAFactory.Get())
                {
                    var avatars = db.Avatars.GetSummaryByUserId(user.UserID);

                    foreach (var avatar in avatars)
                    {
                        result.Add(new AvatarData {
                            ID             = avatar.avatar_id,
                            Name           = avatar.name,
                            ShardName      = shardsDomain.GetById(avatar.shard_id).Name,
                            HeadOutfitID   = avatar.head,
                            BodyOutfitID   = avatar.body,
                            AppearanceType = (AvatarAppearanceType)Enum.Parse(typeof(AvatarAppearanceType), avatar.skin_tone.ToString()),
                            Description    = avatar.description,
                            LotId          = avatar.lot_id,
                            LotName        = avatar.lot_name,
                            LotLocation    = avatar.lot_location
                        });
                    }
                }

                return(Response.AsXml(result));
            };

            this.Get["/app/ShardSelectorServlet"] = _ =>
            {
                this.RequiresAuthentication();
                var user = (JWTUserIdentity)this.Context.CurrentUser;

                var shardName = this.Request.Query["shardName"];
                var avatarId  = this.Request.Query["avatarId"];
                if (avatarId == null)
                {
                    //Using 0 to mean no avatar for CAS
                    avatarId = "0";
                }

                using (var db = DAFactory.Get())
                {
                    ShardStatusItem shard = shardsDomain.GetByName(shardName);
                    if (shard != null)
                    {
                        var tryIP = Request.Headers["X-Forwarded-For"].FirstOrDefault();
                        if (tryIP != null)
                        {
                            tryIP = tryIP.Substring(tryIP.LastIndexOf(',') + 1).Trim();
                        }
                        var ip = tryIP ?? this.Request.UserHostAddress;

                        uint avatarDBID = uint.Parse(avatarId);

                        if (avatarDBID != 0)
                        {
                            var avatar = db.Avatars.Get(avatarDBID);
                            if (avatar == null)
                            {
                                //can't join server with an avatar that doesn't exist
                                return(Response.AsXml(new XMLErrorMessage(ERROR_AVATAR_NOT_FOUND_CODE, ERROR_AVATAR_NOT_FOUND_MSG)));
                            }
                            if (avatar.user_id != user.UserID || avatar.shard_id != shard.Id)
                            {
                                //make sure we own the avatar we're trying to connect with
                                LOG.Info("SECURITY: Invalid avatar login attempt from " + ip + ", user " + user.UserID);
                                return(Response.AsXml(new XMLErrorMessage(ERROR_AVATAR_NOT_YOURS_CODE, ERROR_AVATAR_NOT_YOURS_MSG)));
                            }
                        }

                        var ban = db.Bans.GetByIP(ip);
                        if (ban != null || db.Users.GetById(user.UserID)?.is_banned != false)
                        {
                            return(Response.AsXml(new XMLErrorMessage(ERROR_BANNED_CODE, ERROR_BANNED_MSG)));
                        }

                        /** Make an auth ticket **/
                        var ticket = new ShardTicket
                        {
                            ticket_id = Guid.NewGuid().ToString().Replace("-", ""),
                            user_id   = user.UserID,
                            avatar_id = avatarDBID,
                            date      = Epoch.Now,
                            ip        = ip
                        };

                        db.Users.UpdateConnectIP(ticket.user_id, ip);
                        db.Shards.CreateTicket(ticket);

                        var result = new ShardSelectorServletResponse();
                        result.PreAlpha = false;

                        result.Address      = shard.PublicHost;
                        result.PlayerID     = user.UserID;
                        result.Ticket       = ticket.ticket_id;
                        result.ConnectionID = ticket.ticket_id;
                        result.AvatarID     = avatarId;

                        return(Response.AsXml(result));
                    }
                    else
                    {
                        return(Response.AsXml(new XMLErrorMessage(ERROR_SHARD_NOT_FOUND_CODE, ERROR_SHARD_NOT_FOUND_MSG)));
                    }
                }
            };

            //Get a list of shards (cities)
            this.Get["/shard-status.jsp"] = _ =>
            {
                var result = new XMLList <ShardStatusItem>("Shard-Status-List");
                var shards = shardsDomain.All;

                foreach (var shard in shards)
                {
                    var status = Protocol.CitySelector.ShardStatus.Down;

                    /*switch (shard.Status)
                     * {
                     *  case Database.DA.Shards.ShardStatus.Up:
                     *      status = Protocol.CitySelector.ShardStatus.Up;
                     *      break;
                     *  case Database.DA.Shards.ShardStatus.Full:
                     *      status = Protocol.CitySelector.ShardStatus.Full;
                     *      break;
                     *  case Database.DA.Shards.ShardStatus.Frontier:
                     *      status = Protocol.CitySelector.ShardStatus.Frontier;
                     *      break;
                     *  case Database.DA.Shards.ShardStatus.Down:
                     *      status = Protocol.CitySelector.ShardStatus.Down;
                     *      break;
                     *  case Database.DA.Shards.ShardStatus.Closed:
                     *      status = Protocol.CitySelector.ShardStatus.Closed;
                     *      break;
                     *  case Database.DA.Shards.ShardStatus.Busy:
                     *      status = Protocol.CitySelector.ShardStatus.Busy;
                     *      break;
                     * }*/

                    result.Add(shard);
                }

                return(Response.AsXml(result));
            };
        }