/// <summary>
        /// When a server sends data initially, it needs to be validated with a validation code.
        /// Once that has happened, this method is called, and it allows the server to be seen in the Serverlist.
        /// This method also corrects local IP addresses by converting them to External IP
        /// addresses, so that external clients get a good IP to connect to.
        /// </summary>
        /// <param name="remote">The remote IP of the server</param>
        private bool ValidateServer(IPEndPoint remote)
        {
            string     key = String.Format("{0}:{1}", remote.Address, remote.Port);
            GameServer server;

            // try to fetch the existing server, if its not here... we have bigger problems
            if (!Servers.TryGetValue(key, out server))
            {
                Program.ErrorLog.Write("NOTICE: [MasterServer.ValidateServer] We encountered a strange error trying to fetch a connected server.");
                return(false);
            }

            // Parse our External IP Address
            IPAddress ExtAddress = IPAddress.Loopback;

            IPAddress.TryParse(Program.Config.GamespyExtAddress, out ExtAddress);
            bool ExtAddressIsLocal = Networking.IsLanIP(ExtAddress);

            // Parse Server address and see if its external or LAN
            IPAddress serverAddress = server.AddressInfo.Address;
            bool      isLocalServer = Networking.IsLanIP(server.AddressInfo.Address);

            // Check to make sure we allow external servers in our list
            if (!isLocalServer && !Program.Config.GamespyAllowExtServers)
            {
                DebugLog.Write("External Server not Allowed: " + key);
                return(false);
            }
            else if (isLocalServer && !ExtAddressIsLocal)
            {
                server.AddressInfo.Address = ExtAddress;
            }

            // Server is valid
            server.IsValidated   = true;
            server.LastRefreshed = DateTime.Now;
            server.LastPing      = DateTime.Now;
            server.country       = (serverAddress.AddressFamily == AddressFamily.InterNetwork)
                ? Ip2nation.GetCountryCode(serverAddress).ToUpperInvariant()
                : "??";

            // Update or add the new server
            DebugLog.Write("Adding Validated Server to Serverlist: " + key);
            Servers.AddOrUpdate(key, server, (k, old) => { return(server); });

            // Fire the event
            if (OnServerlistUpdate != null)
            {
                OnServerlistUpdate(null, EventArgs.Empty);
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Whenever the "newuser" command is recieved, this method is called to
        /// add the new users information into the database
        /// </summary>
        /// <param name="Recv">Array of parms sent by the server</param>
        private void CreateNewUser(Dictionary <string, string> Recv)
        {
            // Make sure the user doesnt exist already
            try
            {
                using (GamespyDatabase Database = new GamespyDatabase())
                {
                    // Check to see if user exists
                    if (Database.UserExists(Recv["nick"]))
                    {
                        Stream.SendAsync(@"\error\\err\516\fatal\\errmsg\This account name is already in use!\id\1\final\");
                        Disconnect(5);
                        return;
                    }

                    // We need to decode the Gamespy specific encoding for the password
                    string Password = GamespyUtils.DecodePassword(Recv["passwordenc"]);
                    string Cc       = (RemoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
                        ? Ip2nation.GetCountryCode(RemoteEndPoint.Address)
                        : Program.Config.ASP_LocalIpCountryCode;

                    // Attempt to create account. If Pid is 0, then we couldnt create the account
                    if ((PlayerId = Database.CreateUser(Recv["nick"], Password, Recv["email"], Cc)) == 0)
                    {
                        Stream.SendAsync(@"\error\\err\516\fatal\\errmsg\Error creating account!\id\1\final\");
                        Disconnect(6);
                        return;
                    }

                    Stream.SendAsync(@"\nur\\userid\{0}\profileid\{0}\id\1\final\", PlayerId);
                }
            }
            catch (Exception e)
            {
                // Check for invalid query params
                if (e is KeyNotFoundException)
                {
                    Stream.SendAsync(@"\error\\err\0\fatal\\errmsg\Invalid Query!\id\1\final\");
                }
                else
                {
                    Stream.SendAsync(@"\error\\err\516\fatal\\errmsg\Error creating account!\id\1\final\");
                    GpcmServer.Log("ERROR: [Gpcm.CreateNewUser] An error occured while trying to create a new User account :: " + e.Message);
                }

                Disconnect(7);
                return;
            }
        }