Example #1
0
        public NPSocket()
        {
            _socket          = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _acceptEventArgs = new SocketAsyncEventArgs();

            _clients     = new List <NPServerClient>();
            _clientsByID = new Dictionary <long, NPHandler>();
            _instance    = this;
        }
Example #2
0
        private static void Run()
        {
            while (true)
            {
                Thread.Sleep(1);

                var clients = NPSocket.GetClients();
                foreach (var client in clients)
                {
                    client.Process();
                }
            }
        }
Example #3
0
        private void HandleAuthentication(object cliento)
        {
            var client   = (NPHandler)cliento;
            var database = XNP.Create();
            var keys     = from key in database.LicenseKeys
                           where key.Key == Message.licenseKey
                           select key;

            var  valid  = (keys.Count() == 1);
            var  result = (keys.Count() == 1) ? 0 : 1;
            long npid   = 0;

            if (valid)
            {
                npid = 0x120000100000000 | keys.First().ID;

                NPHandler existingClient = NPSocket.GetClient(npid);

                if (existingClient != null)
                {
                    existingClient.CloseConnection(true);
                }
            }

            var reply = MakeResponse <AuthenticateResultMessage>(client);

            reply.Message.result = result;

            if (valid)
            {
                reply.Message.npid         = (ulong)npid;
                reply.Message.sessionToken = new byte[16] {
                    0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };
            }
            else
            {
                reply.Message.npid         = (ulong)0;
                reply.Message.sessionToken = new byte[16] {
                    0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };
            }

            reply.Send();

            client.Authenticated = valid;
            client.NPID          = npid;

            database.Dispose();
        }
Example #4
0
        public override void Process(NPHandler client)
        {
            if (!client.Authenticated)
            {
                return;
            }

            var npidTo   = (long)Message.npid;
            var clientTo = NPSocket.GetClient(npidTo);

            var response = new NPRPCResponse <MessagingSendDataMessage>(clientTo);

            response.Message.npid = (ulong)client.NPID;
            response.Message.data = Message.data;
            response.Send();

            Log.Info("sent instant message from " + client.NPID.ToString("X16") + " to " + npidTo.ToString("X16"));
        }
Example #5
0
        private void HandleAuthentication(object cliento)
        {
            var client = (NPHandler)cliento;

            using (var wc = new WebClient())
            {
                var authString = string.Format("{0}&&{1}", Message.username, Message.password);

                // TODO: properly implement all of this (error checking, ...)
                var resultString = wc.UploadString("http://auth.iw4.prod.fourdeltaone.net/remauth.php", authString);
                var result       = resultString.Split('#');
                var success      = false;
                var npid         = 0L;

                Log.Data(resultString);

                if (result[0] == "ok")
                {
                    success = true;
                    npid    = (0x110000100000000 | uint.Parse(result[2]));
                }

                var reply = MakeResponse <AuthenticateResultMessage>(client);
                reply.Message.result       = (success) ? 0 : 1;
                reply.Message.npid         = (ulong)npid;
                reply.Message.sessionToken = new byte[16] {
                    0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };

                if (resultString.Contains("banned from this board"))
                {
                    reply.Message.result = 3;
                }

                client.Authenticated = success;
                client.NPID          = npid;
                NPSocket.SetClient(npid, client);

                reply.Send();
            }
        }
Example #6
0
        private void SendAuthResult(RPCAuthenticateWithTokenMessage message, NPHandler client, int userID, int group)
        {
            var reply = message.MakeResponse <AuthenticateResultMessage>(client);

            reply.Message.npid         = (ulong)0;
            reply.Message.sessionToken = new byte[16];

            if (userID > 0)
            {
                var npid = (0x110000100000000 | (uint)userID);

                var existingClient = NPSocket.GetClient(npid);
                //NPHandler existingClient = null;
                if (existingClient != null)
                {
                    //reply.Message.result = 4;
                    existingClient.CloseConnection(true);
                }

                reply.Message.result       = 0;
                reply.Message.npid         = (ulong)npid;
                reply.Message.sessionToken = message.Message.token;

                client.Authenticated = true;
                client.NPID          = npid;
                client.GroupID       = group;
                client.SessionToken  = Encoding.UTF8.GetString(message.Message.token);

                NPSocket.SetClient(npid, client);
            }
            else if (userID == 0)
            {
                reply.Message.result = 1;
            }
            else if (userID == -1)
            {
                reply.Message.result = 2;
            }

            reply.Send();

            if (group > 0)
            {
                var groupReply = new NPRPCResponse <AuthenticateUserGroupMessage>(client);
                groupReply.Message.groupID = group;
                groupReply.Send();
            }

            ThreadPool.QueueUserWorkItem(delegate(object cliento)
            {
                try
                {
                    var cclient = (NPHandler)cliento;
                    var uid     = (int)(cclient.NPID & 0xFFFFFFFF);

                    var db     = XNP.Create();
                    var result = from platform in db.ExternalPlatforms
                                 where platform.UserID == uid && platform.PlatformAuthenticated == 1
                                 select platform.PlatformID;

                    var value = 1;

                    if (result.Count() > 0)
                    {
                        value = 0;
                    }

                    Thread.Sleep(600);

                    var linkReply            = new NPRPCResponse <AuthenticateExternalStatusMessage>(cclient);
                    linkReply.Message.status = value;
                    linkReply.Send();
                }
                catch (Exception ex)
                {
                    Log.Error(ex.ToString());
                }
            }, client);
        }
Example #7
0
        public override void Process(NPHandler client)
        {
            var ipNum  = (uint)IPAddress.NetworkToHostOrder(Message.clientIP);
            var ip     = new IPAddress(BitConverter.GetBytes(ipNum));
            var ticket = ReadTicket(Message.ticket);
            var npid   = Message.npid;

            if (npid == 0)
            {
                npid = ticket.clientID;
            }

            var valid   = false;
            var groupID = 0;

            if (ticket.version == 1)
            {
                if (client.NPID == (long)ticket.serverID)
                {
                    if (npid == ticket.clientID)
                    {
                        var remoteClient = NPSocket.GetClient((long)ticket.clientID);
                        remoteClient.CurrentServer = client.NPID;

                        if (remoteClient != null && !remoteClient.Unclean)
                        {
                            Log.Debug("Ticket auth: remote address " + remoteClient.Address.Address.ToString());
                            Log.Debug("Ticket auth: message address " + ip.ToString());

                            if (ipNum == 0 || remoteClient.Address.Address.Equals(ip))
                            {
                                valid = true;

                                groupID = remoteClient.GroupID;

                                Log.Debug("Successfully authenticated a ticket for client " + remoteClient.NPID.ToString("x16"));
                            }
                            else
                            {
                                Log.Debug("Ticket auth: IP address didn't match.");
                            }
                        }
                        else
                        {
                            Log.Debug("Ticket auth: no such client");
                        }
                    }
                    else
                    {
                        Log.Debug("Ticket auth: NPID didn't match.");
                    }
                }
                else
                {
                    Log.Debug("Ticket auth: server NPID didn't match.");
                }
            }
            else
            {
                Log.Debug("Ticket auth: version didn't match.");
            }

            var reply = MakeResponse <AuthenticateValidateTicketResultMessage>(client);

            reply.Message.result  = (valid) ? 0 : 1;
            reply.Message.groupID = groupID;
            reply.Message.npid    = npid;
            reply.Send();
        }
Example #8
0
        public static void Handle(NPHandler client, string data)
        {
            var reason = int.Parse(data.Split(' ')[1]);

            if (reason == 0 || reason == 50001)
            {
                client.LastCI = DateTime.UtcNow;
                return;
            }

            ThreadPool.QueueUserWorkItem(stateo =>
            {
                var state = (CIReportState)stateo;

                if (client.Unclean)
                {
                    return;
                }

                // first, ban the player globally
                try
                {
                    using (var wc = new WebClient())
                    {
                        var url = string.Format("http://auth.iw4.prod.fourdeltaone.net/log.php?s={0}&r={1}", state.Client.SessionToken, state.Reason);
                        Log.Debug(url);
                        Log.Info(wc.DownloadString(url));
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.ToString());
                }

                // then try to send a message to the server they're on
                Thread.Sleep(500);

                var serverID = state.Client.CurrentServer;
                var npID     = state.Client.NPID;

                try
                {
                    var remote                   = NPSocket.GetClient(serverID);
                    var message                  = new NPRPCResponse <AuthenticateKickUserMessage>(remote);
                    message.Message.npid         = (ulong)npID;
                    message.Message.reason       = 1;
                    message.Message.reasonString = string.Format("Cheat detected (#{0})", state.Reason);
                    message.Send();
                }
                catch (Exception e)
                {
                    Log.Error(e.ToString());
                }

                state.Client.Unclean = true;
            }, new CIReportState()
            {
                Client = client,
                Reason = reason
            });
        }
Example #9
0
        static void Main(string[] args)
        {
#if !DEBUG
            Log.Initialize("NPx.log", LogLevel.Data | LogLevel.Info | LogLevel.Error, true);
#else
            Log.Initialize("NPx.log", LogLevel.All, true);
#endif
            Log.Info("NPServer starting...");

            // connect to the database
            Database = XNP.Create();

            if (Database == null)
            {
                return;
            }

            // start the NP socket
            NPSocket streamServer = new NPSocket();
            streamServer.Start();

            // start the NP auth API
            NPAuthAPI authAPI = new NPAuthAPI();
            authAPI.Start();

            // start the Steam friend linker
            SteamFriendsLinker steamLinker = new SteamFriendsLinker();
            steamLinker.Start();

            // start the profile data handler
            ProfileData profileData = new ProfileData();
            profileData.Start();

            // start the matchdata parser
            //MatchData matchData = new MatchData();
            //matchData.Start();

            // start the matchdata converter
            //MatchDataConverter matchDataConverter = new MatchDataConverter();
            //matchDataConverter.Start();

            // start aux threads
            NPHandler.Start();
            TaskScheduler.Start();
            HttpHandler.Start();

            StatFileHandler.Register();

            TaskScheduler.EnqueueTask(Servers.CleanSessions, 300);

            // main idle loop
            while (true)
            {
                try
                {
                    Log.WriteAway();
                }
                catch (Exception e) { Log.Error(e.ToString()); }

                Thread.Sleep(5000);
            }
        }