Esempio n. 1
0
        public static void Run()
        {
            var server = new DotNetServer();

            var pipe = server.Start();

            server.Host((env, respond, error) =>
            {
                var path = env["Owin.RequestUri"] as string;

                /*
                 * if (path == "/")
                 *  respond(
                 *          "200 OK",
                 *          new Dictionary<string, IList<string>>()
                 *          {
                 *              { "Content-Type",  new string[] { "text/plain" } }
                 *          },
                 *          new object[] { Encoding.ASCII.GetBytes("Hello world.") }
                 *      );
                 * else if (path == "/stream")
                 * {
                 *  StreamingApp(env, respond, error);
                 * }
                 * else if (path == "/bufferedecho")
                 * {
                 *  BufferedEcho(env, respond, error);
                 * }
                 */

                var urlParts = path.Substring(1).Split(new[] { '/' }, 2);

                if (urlParts.Length >= 1)
                {
                    if (urlParts[0] == "nick")
                    {
                        var steamID = long.Parse(urlParts[1]);
                        var client  = Client.Get(steamID);

                        Console.WriteLine(steamID);

                        if (client.GameVersion != 0)
                        {
                            var nickname = client.GamerTag;
                            respond(
                                "200 OK",
                                new Dictionary <string, IList <string> >()
                            {
                                { "Content-Type", new string[] { "text/plain" } }
                            },
                                new object[] { Encoding.ASCII.GetBytes(nickname) }
                                );
                            return;
                        }
                    }
                    else if (urlParts[0] == "stats")
                    {
                        var statistics   = Client.GetStatistics();
                        var retString    = "[Stats]\r\n";
                        var totalPlayers = 0;

                        foreach (var stat in statistics)
                        {
                            retString += string.Format("playerState{0}={1}\r\n", stat.StatisticID, stat.StatisticCount);

                            totalPlayers += stat.StatisticCount;
                        }

                        retString += string.Format("totalPlayers={0}\r\n", totalPlayers);

                        retString   += "[Lobbies]\r\n";
                        totalPlayers = 0;
                        foreach (var serverc in MatchServer.Servers)
                        {
                            var cnt = serverc.Value.Sessions.Count(sess => (DateTime.Now - sess.LastTouched).TotalSeconds < 60);
                            //var cnt = serverc.Value.Sessions.Count;
                            retString    += string.Format("lobbies{0}={1}\r\n", serverc.Key, cnt);
                            totalPlayers += cnt;
                        }

                        retString += string.Format("totalLobbies={0}", totalPlayers);
                        respond(
                            "200 OK",
                            new Dictionary <string, IList <string> >()
                        {
                            { "Content-Type", new string[] { "text/plain" } }
                        },
                            new object[] { Encoding.ASCII.GetBytes(retString) }
                            );
                        return;
                    }
                    else if (urlParts[0] == "clean")
                    {
                        long clientID = 0;
                        var valid     = false;

                        if (long.TryParse(urlParts[1], out clientID))
                        {
                            if (clientID != 0x110000100000002)
                            {
                                var client = Client.Get(clientID);

                                if ((DateTime.UtcNow - client.LastTouched).TotalSeconds < 300)
                                {
                                    var state = CIServer.IsUnclean(clientID, null);

                                    if (!state)
                                    {
                                        valid = true;
                                    }
                                }
                            }
                        }
                        respond(
                            "200 OK",
                            new Dictionary <string, IList <string> >()
                        {
                            { "Content-Type", new string[] { "text/plain" } }
                        },
                            new object[] { Encoding.ASCII.GetBytes((valid) ? "valid" : "invalid") }
                            );
                        return;
                    }
                    else if (urlParts[0] == "cleanExt")
                    {
                        long clientID = 0;
                        var valid     = false;
                        var reason    = "invalid-id";

                        if (long.TryParse(urlParts[1], out clientID))
                        {
                            if (clientID != 0x110000100000002)
                            {
                                var client = Client.Get(clientID);

                                if ((DateTime.UtcNow - client.LastTouched).TotalHours < 6)
                                {
                                    var state = CIServer.IsUnclean(clientID, null);

                                    if (state)
                                    {
                                        reason = CIServer.WhyUnclean(clientID);
                                    }
                                    else
                                    {
                                        reason = "actually-valid";
                                        valid  = true;
                                    }
                                }
                                else
                                {
                                    reason = "not-registered-at-lsp";
                                }
                            }
                            else
                            {
                                reason = "00002-IDGEN-error";
                            }
                        }
                        respond(
                            "200 OK",
                            new Dictionary <string, IList <string> >()
                        {
                            { "Content-Type", new string[] { "text/plain" } }
                        },
                            new object[] { Encoding.ASCII.GetBytes(((valid) ? "valid" : "invalid") + "\r\n" + reason) }
                            );

                        return;
                    }
                    else if (urlParts[0] == "pc")
                    {
                        var file = "pc/" + urlParts[1].Replace('\\', '/');

                        if (!File.Exists(file))
                        {
                            respond(
                                "200 OK",
                                new Dictionary <string, IList <string> >()
                            {
                                { "Content-Type", new string[] { "text/plain" } }
                            },
                                new object[] { Encoding.ASCII.GetBytes("Not found!") }
                                );
                        }
                        else
                        {
                            var stream = File.OpenRead(file);
                            var b      = new byte[stream.Length];
                            stream.Read(b, 0, (int)stream.Length);
                            stream.Close();

                            respond(
                                "200 OK",
                                new Dictionary <string, IList <string> >()
                            {
                                { "Content-Type", new string[] { "application/octet-stream" } }
                            },
                                new object[] { b }
                                );
                        }

                        return;
                    }
                    else if (urlParts[0] == "servers")
                    {
                        var filter = urlParts[1].Replace(".xml", "");
                        if (filter.Contains("yeQA4reD"))
                        {
                            respond(
                                "200 OK",
                                new Dictionary <string, IList <string> >()
                            {
                                { "Content-Type", new string[] { "text/xml" } }
                            },
                                new object[] { Encoding.ASCII.GetBytes(ProcessServers(filter)) }
                                );
                        }
                        else
                        {
                            respond(
                                "200 OK",
                                new Dictionary <string, IList <string> >()
                            {
                                { "Content-Type", new string[] { "text/html" } }
                            },
                                new object[] { Encoding.ASCII.GetBytes("Invalid") }
                                );
                        }
                        return;
                    }
                    else
                    {
                        respond(
                            "200 OK",
                            new Dictionary <string, IList <string> >()
                        {
                            { "Content-Type", new string[] { "text/html" } }
                        },
                            new object[] { Encoding.ASCII.GetBytes("Invalid") }
                            );
                    }
                }
            });
            Log.Info("Listening on " + server.ListenEndPoint);
        }
Esempio n. 2
0
        void server_PacketReceived(object sender, UdpPacketReceivedEventArgs e)
        {
            var packet = e.Packet;
            var reader = packet.GetReader();
            var type   = reader.ReadByte();

            if (type == 0xE)
            {
                var request = new LogRequestPacket1(reader);

                if (request.GameVersion == 0 || (request.InternalIP.Port < 28960 || request.InternalIP.Port > 28970))
                {
                    // update 'last touched', possibly re-set state
                    var client = Client.Get(request.XUID);
                    client.SetLastTouched();

                    if (!client.IsMatched)
                    {
                        client.CurrentState = 0x417D;
                    }

                    // only send 0E 02 00 here
                    var response = packet.MakeResponse();
                    var writer   = response.GetWriter();
                    writer.Write(new byte[] { 0x0E, 0x02, 0x00 });
                    response.Send();
                }
                else
                {
                    bool allowedVersion = true;
                    Log.Data(string.Format("Player connecting {0} from {1} version {2}.{3} XUID {4}", request.GamerTag, request.ExternalIP, request.GameVersion, request.GameBuild, request.XUID.ToString("X16")));

                    if (!Client.IsVersionAllowed(request.GameVersion, request.GameBuild))
                    {
                        // no return, we need to keep the version logged for later packets
                        Log.Warn(string.Format("Client {0} attempted to connect with disallowed version {1}.{2}.", request.GamerTag, request.GameVersion, request.GameBuild));

                        // to send 'fake' data
                        allowedVersion = false;
                    }

                    var client = Client.Get(request.XUID);
                    client.SetFromLog(request);

                    var tempStats = new List <LogStatistics>();

                    /*
                     * tempStats.Add(new LogStatistics(0x2ee0, 1));
                     * tempStats.Add(new LogStatistics(1, 2));
                     * tempStats.Add(new LogStatistics(0x417f, 3));
                     * tempStats.Add(new LogStatistics(0x417e, 4));
                     * tempStats.Add(new LogStatistics(5, 5));
                     * tempStats.Add(new LogStatistics(13, 6));
                     * tempStats.Add(new LogStatistics(0x3a98, 7));
                     * tempStats.Add(new LogStatistics(4, 8));
                     * tempStats.Add(new LogStatistics(3, 9));
                     * tempStats.Add(new LogStatistics(2, 10));
                     * tempStats.Add(new LogStatistics(17, 11));
                     * tempStats.Add(new LogStatistics(17, 12));
                     * tempStats.Add(new LogStatistics(6, 13));
                     * tempStats.Add(new LogStatistics(9, 14));
                     * tempStats.Add(new LogStatistics(15, 16));
                     * tempStats.Add(new LogStatistics(0x416e, 17));
                     * tempStats.Add(new LogStatistics(7, 18));
                     * tempStats.Add(new LogStatistics(0x417d, 19));
                     * tempStats.Add(new LogStatistics(8, 20));
                     * tempStats.Add(new LogStatistics(12, 21));
                     * tempStats.Add(new LogStatistics(16, 22));
                     * tempStats.Add(new LogStatistics(14, 23));
                     * tempStats.Add(new LogStatistics(0x2ee0, 24));
                     * tempStats.Add(new LogStatistics(11, 25));
                     * tempStats.Add(new LogStatistics(10, 26));
                     */

                    var responsePacket = new LogResponsePacket1(client.XUID);

                    if (allowedVersion)
                    {
                        responsePacket.SetStatistics(Client.GetStatistics());
                    }
                    else
                    {
                        var fakeStats = new List <LogStatistics>();
                        fakeStats.Add(new LogStatistics(1, 28789)); //up
                        fakeStats.Add(new LogStatistics(2, 24932)); //da
                        fakeStats.Add(new LogStatistics(3, 25972)); //te

                        for (short i = 4; i <= 19; i++)
                        {
                            fakeStats.Add(new LogStatistics(i, 1337));
                        }

                        responsePacket.SetStatistics(fakeStats);

                        /*if (request.GameBuild < 40)
                         * {
                         *  responsePacket.SetBetaClosed();
                         * }
                         * else
                         * {*/
                        responsePacket.SetOldBuild();
                        //}
                    }

                    if ((request.XUID & 0xFFFFFFFF) == 2)
                    {
                        Log.Info(string.Format("Non-allowed client (IDGEN) (XUID {0}) tried to connect", request.XUID));
                        responsePacket.SetBetaClosed();
                    }

                    if (!Client.IsAllowed(request.XUID))
                    {
                        Log.Info(string.Format("Non-allowed client (XUID {0}) tried to connect", request.XUID));
                        responsePacket.SetBanned();
                    }

                    if (!Client.IsAllowed(client.XUIDAlias))
                    {
                        Log.Info(string.Format("Non-allowed client (XUID {0}) tried to connect", request.XUID));
                        responsePacket.SetBanned();
                    }

                    var ipAddress = packet.GetSource().Address;

                    if (!Client.IsAllowed(ipAddress))
                    {
                        Log.Info(string.Format("Non-allowed client (IP {0}) tried to connect", ipAddress));
                        responsePacket.SetBanned();
                    }

                    if (!packet.Secure)
                    {
                        if (allowedVersion)
                        {
                            Log.Info(string.Format("Client (IP {0}) tried to connect with insecure packet.", ipAddress));
                        }

                        responsePacket.SetOldBuild();
                    }

                    var response = packet.MakeResponse();
                    responsePacket.Write(response.GetWriter());
                    response.Send();
                }
            }
            else if (type == 0xFD)
            {
                if (!packet.Secure)
                {
                    return;
                }

                long realID = (0x0110000100000000 | reader.ReadInt32());
                long fakeID = (0x0110000100000000 | reader.ReadInt32());

                if (realID == fakeID)
                {
                    return;
                }

                var client = Client.Get(realID);
                client.XUIDAlias = fakeID;
            }
        }
Esempio n. 3
0
        public void OnResponse(HttpListenerContext context)
        {
            try
            {
                var url = context.Request.Url.PathAndQuery;

                Log.Debug(string.Format("HTTP request for {0}", url));

                var urlParts = url.Substring(1).Split(new[] { '/' }, 2);

                if (urlParts.Length >= 1)
                {
                    if (urlParts[0] == "nick")
                    {
                        var steamID = long.Parse(urlParts[1]);
                        var client  = Client.Get(steamID);

                        if (client.GameVersion != 0)
                        {
                            var nickname = client.GamerTag;

                            //rp.status = (int)RespState.OK;
                            //rp.Headers["Content-Type"] = "text/plain";
                            var b = Encoding.ASCII.GetBytes(nickname);
                            context.Response.ContentLength64 = b.Length;
                            context.Response.ContentType     = "text/plain";
                            context.Response.OutputStream.Write(b, 0, b.Length);
                            context.Response.OutputStream.Close();
                            return;
                        }
                    }
                    else if (urlParts[0] == "stats")
                    {
                        var statistics   = Client.GetStatistics();
                        var retString    = "[Stats]\r\n";
                        var totalPlayers = 0;

                        foreach (var stat in statistics)
                        {
                            retString += string.Format("playerState{0}={1}\r\n", stat.StatisticID, stat.StatisticCount);

                            totalPlayers += stat.StatisticCount;
                        }

                        retString += string.Format("totalPlayers={0}\r\n", totalPlayers);

                        retString   += "[Lobbies]\r\n";
                        totalPlayers = 0;
                        foreach (var server in MatchServer.Servers)
                        {
                            var cnt = server.Value.Sessions.Count(sess => (DateTime.Now - sess.LastTouched).TotalSeconds < 60);

                            retString    += string.Format("lobbies{0}={1}\r\n", server.Key, cnt);
                            totalPlayers += cnt;
                        }

                        retString += string.Format("totalLobbies={0}", totalPlayers);

                        /*rp.status = (int)RespState.OK;
                         * rp.Headers["Content-Type"] = "text/plain";
                         * rp.BodyData = Encoding.ASCII.GetBytes(retString);*/
                        var b = Encoding.ASCII.GetBytes(retString);
                        context.Response.ContentLength64 = b.Length;
                        context.Response.ContentType     = "text/plain";
                        context.Response.OutputStream.Write(b, 0, b.Length);
                        context.Response.OutputStream.Close();
                        return;
                    }
                    else if (urlParts[0] == "clean")
                    {
                        long clientID = 0;
                        var  valid    = false;

                        if (long.TryParse(urlParts[1], out clientID))
                        {
                            var client = Client.Get(clientID);

                            if ((DateTime.UtcNow - client.LastTouched).TotalSeconds < 300)
                            {
                                var state = CIServer.IsUnclean(clientID, null);

                                if (!state)
                                {
                                    valid = true;
                                }
                            }
                        }

                        var b = Encoding.ASCII.GetBytes((valid) ? "valid" : "invalid");
                        context.Response.ContentLength64 = b.Length;
                        context.Response.ContentType     = "text/plain";
                        context.Response.OutputStream.Write(b, 0, b.Length);
                        context.Response.OutputStream.Close();
                        return;
                    }
                    else if (urlParts[0] == "cleanExt")
                    {
                        long clientID = 0;
                        var  valid    = false;
                        var  reason   = "invalid-id";

                        if (long.TryParse(urlParts[1], out clientID))
                        {
                            var client = Client.Get(clientID);

                            if ((DateTime.UtcNow - client.LastTouched).TotalHours < 6)
                            {
                                var state = CIServer.IsUnclean(clientID, null);

                                if (state)
                                {
                                    reason = CIServer.WhyUnclean(clientID);
                                }
                                else
                                {
                                    reason = "actually-valid";
                                    valid  = true;
                                }
                            }
                            else
                            {
                                reason = "not-registered-at-lsp";
                            }
                        }

                        var b = Encoding.ASCII.GetBytes(((valid) ? "valid" : "invalid") + "\r\n" + reason);
                        context.Response.ContentLength64 = b.Length;
                        context.Response.ContentType     = "text/plain";
                        context.Response.OutputStream.Write(b, 0, b.Length);
                        context.Response.OutputStream.Close();
                        return;
                    }

                    /*else if (urlParts[0] == "key_public.xml")
                     * {
                     *  if (_keyXML == string.Empty)
                     *  {
                     *      _keyXML = File.ReadAllText("key-public.xml");
                     *  }
                     *
                     *  var b = Encoding.ASCII.GetBytes(_keyXML);
                     *  context.Response.ContentLength64 = b.Length;
                     *  context.Response.ContentType = "text/plain";
                     *  context.Response.OutputStream.Write(b, 0, b.Length);
                     *  context.Response.OutputStream.Close();
                     *  return;
                     * }*/
                    else if (urlParts[0] == "pc")
                    {
                        var file = "pc/" + urlParts[1].Replace('\\', '/');

                        if (!File.Exists(file))
                        {
                            /*rp.status = (int)RespState.NOT_FOUND;
                             * rp.Headers["Content-Type"] = "text/plain";
                             * rp.BodyData = Encoding.ASCII.GetBytes("Not Found!");*/
                            var b = Encoding.ASCII.GetBytes("Not Found!");
                            context.Response.ContentLength64 = b.Length;
                            context.Response.ContentType     = "text/plain";
                            context.Response.OutputStream.Write(b, 0, b.Length);
                            context.Response.OutputStream.Close();
                        }
                        else
                        {
                            var stream = File.OpenRead(file);
                            var b      = new byte[stream.Length];
                            stream.Read(b, 0, (int)stream.Length);
                            stream.Close();

                            context.Response.ContentLength64 = b.Length;
                            context.Response.ContentType     = "application/octet-stream";
                            context.Response.OutputStream.Write(b, 0, b.Length);
                            context.Response.OutputStream.Close();
                        }

                        return;
                    }
                    else if (urlParts[0] == "servers")
                    {
                        var filter = urlParts[1].Replace(".xml", "");
                        if (filter.Contains("yeQA4reD"))
                        {
                            var b = Encoding.ASCII.GetBytes(ProcessServers(filter));
                            context.Response.ContentLength64 = b.Length;
                            context.Response.ContentType     = "text/xml";
                            context.Response.OutputStream.Write(b, 0, b.Length);
                            context.Response.OutputStream.Close();
                        }
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
                var b = Encoding.ASCII.GetBytes(e.Message);
                context.Response.ContentLength64 = b.Length;
                context.Response.ContentType     = "text/plain";
                context.Response.OutputStream.Write(b, 0, b.Length);
                context.Response.OutputStream.Close();
                return;
            }

            var bt = Encoding.ASCII.GetBytes("Unknown Player");

            context.Response.ContentLength64 = bt.Length;
            context.Response.ContentType     = "text/plain";
            context.Response.OutputStream.Write(bt, 0, bt.Length);
            context.Response.OutputStream.Close();
        }