コード例 #1
0
        //[Produces("application/octet-stream")]
        public async Task <IActionResult> IndexPOST(
            [FromHeader(Name = "osu-token")] string clientToken = null
            )
        {
            try
            {
                Response.Headers["cho-protocol"] = "19";
                Response.Headers["Connection"]   = "keep-alive";
                Response.Headers["Keep-Alive"]   = "timeout=60, max=100";
                Response.Headers["cho-server"]   = "Sora (https://github.com/Mempler/Sora)";

                Response.StatusCode = 200;

                await using var body = new MemoryStream();
                await Request.Body.CopyToAsync(body);

                body.Position = 0;

                await using var mw = MStreamWriter.New();
                using var mr       = new MStreamReader(body);

                Presence pr;
                try
                {
                    if (string.IsNullOrEmpty(clientToken))
                    {
                        pr = new Presence(_cs);
                        Response.Headers["cho-token"] = pr.Token;
                        string ip = Response.Headers["X-Forwarded-For"];

                        if (string.IsNullOrEmpty(ip))
                        {
                            ip = "127.0.0.1";
                        }

                        await _evmng.RunEvent(
                            EventType.BanchoLoginRequest,
                            new BanchoLoginRequestArgs { pr = pr, Reader = mr, Writer = mw, IPAddress = ip }
                            );

                        mw.Flush();

                        return(await RetOut(mw.BaseStream));
                    }
                } catch (Exception ex)
                {
                    Logger.Err(ex);
                    mw.Write(new LoginResponse(LoginResponses.Exception));
                    return(await RetOut(mw.BaseStream));
                }

                pr = _ps.GetPresence(Request.Headers["osu-token"]);
                if (pr == null)
                {
                    Logger.Warn(
                        "Presence of token%#F94848%", Request.Headers["osu-token"], "%#FFFFFF%hasn't been found!"
                        );
                    Response.StatusCode = 403; // Presence is not known, force the client to send login info.
                    return(await RetOut(mw.BaseStream));
                }

                while (true)
                {
                    try
                    {
                        pr.LastRequest.Restart();
                        if (Request.ContentLength - body.Position < 7)
                        {
                            break; // Dont handle any invalid packets! (less then bytelength of 7)
                        }
                        var packetId = (PacketId)mr.ReadInt16();
                        mr.ReadBoolean();
                        var packetData = mr.ReadBytes();

                        await using var packetDataStream = new MemoryStream(packetData);
                        using var packetDataReader       = new MStreamReader(packetDataStream);

                        await _evmng.RunEvent(
                            EventType.BanchoPacket,
                            new BanchoPacketArgs { pr = pr, PacketId = packetId, Data = packetDataReader }
                            );
                    } catch (Exception ex)
                    {
                        Logger.Err(ex);
                        break;
                    }
                }

                try
                {
                    if (Response.Body.CanWrite)
                    {
                        pr.GetOutput()
                        .WriteTo(Response.Body);
                    }
                    return(await RetOut(mw.BaseStream));
                } catch
                {
                    // Ignored because it may throw an exception.
                }

                if (pr.Get <bool>("IS_LAST_REQUEST"))
                {
                    _ps.EndPresence(pr, true);
                }
            } catch (Exception ex)
            {
                Logger.Err(ex);
            }

            return(Ok());
        }
コード例 #2
0
ファイル: IndexController.cs プロジェクト: aisuru-pw/Sora
        public async Task <IActionResult> IndexPost([FromHeader(Name = "osu-token")] string clientToken = null)
        {
            try
            {
                Response.Headers["cho-protocol"] = "19";
                Response.Headers["Connection"]   = "keep-alive";
                Response.Headers["Keep-Alive"]   = "timeout=60, max=100";
                Response.Headers["cho-server"]   = "Sora (https://github.com/Mempler/Sora)";

                Response.StatusCode = 200;

                await using var body = new MemoryStream();
                await Request.Body.CopyToAsync(body);

                body.Position = 0;

                await using var mw = MStreamWriter.New();
                using var mr       = new MStreamReader(body);
                var pr = new Presence(new User());
                if (string.IsNullOrEmpty(clientToken))
                {
                    Response.Headers["cho-token"] = pr.Token.ToString();
                    string ip = Request.Headers["X-Real-IP"];

                    if (string.IsNullOrEmpty(ip))
                    {
                        ip = "127.0.0.1";
                    }

                    await _evManager.RunEvent(EventType.BanchoLoginRequest, new BanchoLoginRequestArgs
                    {
                        Reader    = mr,
                        Writer    = mw,
                        Pr        = pr,
                        IpAddress = ip
                    });

                    mw.Flush();

                    return(await RetOut(mw.BaseStream));
                }

                if (_presenceService.TryGet(new Token(clientToken.Trim()), out pr))
                {
                    while (true)
                    {
                        try
                        {
                            pr["LAST_PONG"] = DateTime.Now;

                            if (Request.ContentLength - body.Position < 7)
                            {
                                break; // Dont handle any invalid packets! (less then bytelength of 7)
                            }
                            var packetId = (PacketId)mr.ReadInt16();
                            mr.ReadBoolean();
                            var packetData = mr.ReadBytes();

                            await using var packetDataStream = new MemoryStream(packetData);
                            using var packetDataReader       = new MStreamReader(packetDataStream);
                            await _evManager.RunEvent(
                                EventType.BanchoPacket,
                                new BanchoPacketArgs { Pr = pr, PacketId = packetId, Data = packetDataReader }
                                );
                        }
                        catch (Exception ex)
                        {
                            Logger.Err(ex);
                            break;
                        }
                    }
                    try
                    {
                        await using var m = new MemoryStream();
                        if (Response.Body.CanWrite)
                        {
                            pr.WritePackets(m);
                        }

                        return(await RetOut(m));
                    }
                    catch (Exception ex)
                    {
                        Logger.Err(ex);
                        // Ignored because it may throw an exception.
                    }
                }
                else
                {
                    return(StatusCode(403));
                }
            }
            catch (Exception ex)
            {
                Logger.Err(ex);
            }

            return(Ok());
        }