Exemplo n.º 1
0
        public override void OnOperation(HttpListenerContext context, Authentication authentication)
        {
            string id = context.Request.QueryString["id"];

            if (!UploadedModsManager.Instance.HasModWithIdBeenUploaded(id))
            {
                Utils.SendErrorPage(context.Response, "No mod with the id \"" + id + "\" has been uploaded", true, HttpStatusCode.NotFound);
                return;
            }

            string path = UploadedModsManager.Instance.GetZippedFileForMod(id);

            byte[] data = File.ReadAllBytes(path);

            string displayName = UploadedModsManager.Instance.GetModInfoFromId(id).DisplayName + ".zip";

            displayName = displayName.Replace(' ', '_');
            foreach (char c in Path.GetInvalidFileNameChars())
            {
                displayName = displayName.Replace(c, '_');
            }

            SpecialModData specialModData = UploadedModsManager.Instance.GetSpecialModInfoFromId(id);

            specialModData.Downloads++;
            specialModData.Save();

            HttpStream httpStream = new HttpStream(context.Response);

            httpStream.SendFile(data, displayName);
            httpStream.Close();
        }
Exemplo n.º 2
0
        public override void OnOperation(HttpListenerContext context, Authentication authentication)
        {
            context.Response.ContentType = "application/json";

            byte[] data = Misc.ToByteArray(context.Request.InputStream);
            string json = Encoding.UTF8.GetString(data);

            if (!authentication.IsSignedIn)
            {
                HttpStream stream = new HttpStream(context.Response);
                stream.Send(new SignOutResponse()
                {
                    Error = "You are not signed in."
                }.ToJson());
                stream.Close();
                return;
            }

            SessionsManager.Instance.RemoveSession(authentication.SessionID);

            // Delete the sessionID cookie
            Cookie cookie = context.Request.Cookies["SessionID"];

            cookie.Expires = DateTime.Now.AddDays(-10);
            cookie.Value   = null;
            context.Response.SetCookie(cookie);

            HttpStream httpStream = new HttpStream(context.Response);

            httpStream.Send(new SignOutResponse()
            {
                message = "signed out!"
            }.ToJson());
            httpStream.Close();
        }
Exemplo n.º 3
0
        public override void OnOperation(HttpListenerContext context, Authentication authentication)
        {
            context.Response.ContentType = "text/plain";
            HttpStream stream = new HttpStream(context.Response);

            stream.Send(authentication.IP);
            stream.Close();
        }
Exemplo n.º 4
0
        public override void OnOperation(HttpListenerContext context, Authentication authentication)
        {
            if (!context.Request.IsWebSocketRequest)
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                HttpStream httpStream = new HttpStream(context.Response);
                httpStream.Send("<html><body>websocket only!</body></html>");
                httpStream.Close();
            }

            if (!authentication.HasAtLeastAuthenticationLevel(Users.AuthenticationLevel.Admin))
            {
                HttpListenerWebSocketContext webNoSocket = context.AcceptWebSocketAsync(null).ConfigureAwait(true).GetAwaiter().GetResult();
                byte[] buffer = Encoding.UTF8.GetBytes("You do not have the requierd permissions to use this.");
                webNoSocket.WebSocket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, System.Threading.CancellationToken.None).ConfigureAwait(true).GetAwaiter().GetResult();
                webNoSocket.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "You do not have the requierd permissions to use this.", System.Threading.CancellationToken.None).ConfigureAwait(true).GetAwaiter().GetResult();
                webNoSocket.WebSocket.Dispose();
                return;
            }

            Task.Factory.StartNew(() => handleWebSocket(context, authentication));
        }
Exemplo n.º 5
0
        public override void OnOperation(HttpListenerContext context, Authentication authentication)
        {
            string key = Utils.GenerateSecureKey();

            NotesManager.Instance.SetNote(key, "Operation still loading....");

            HttpStream stream = new HttpStream(context.Response);

            stream.Send("Link: https://modbot.org/api?operation=noteOperation&key=" + key);
            stream.Close();

            string output;

            try
            {
                output = OnFrontendPushed();
            }
            catch (Exception e)
            {
                output = "Error!\n" + e.ToString();
            }

            NotesManager.Instance.SetNote(key, output);
        }
Exemplo n.º 6
0
        public static void AsyncDownloadFile(object o)
        {
            var             Param = (ThreadParam)o;
            HttpWebRequest  Request;
            HttpWebResponse Response;
            Stream          HttpStream;
            FileStream      OutStream;
            Int32           ReadBytes = 0;

            Byte[] Buffer          = new Byte[1024];
            int    statictime      = System.Environment.TickCount;
            int    CountTime       = 0;
            int    CountingBytenum = 0;

            while (true)
            {
                Param.mStartEvent.WaitOne();
                if (-1 == Param.mBeginBlock)
                {
                    Param.mStartEvent.Reset();
                    Param.mDoneEvent.Set();
                    continue;
                }

                try
                {
                    Request = WebRequest.Create(Param.url /* + "//" + Param.mFileName*/) as HttpWebRequest;
                    Request.AddRange(Param.mBeginBlock, Param.mEndBlock);

                    Response = Request.GetResponse() as HttpWebResponse;
                    //Response.StatusCode; 这个值为200表示一切OK
                    HttpStream = Response.GetResponseStream();
                    OutStream  = new FileStream(Param.mFileName + Param.mId.ToString(), FileMode.Create);
                    while (true)
                    {
                        ReadBytes = HttpStream.Read(Buffer, 0, Buffer.Length);
                        if (ReadBytes <= 0)
                        {
                            break;
                        }
                        Param.mDownloadSize += ReadBytes;
                        int atime     = Environment.TickCount;
                        int deltatime = atime - statictime;
                        CountTime += deltatime;
                        if (CountTime >= 1000)
                        {
                            Param.mSpeed    = CountingBytenum;
                            CountTime       = 0;
                            CountingBytenum = 0;
                        }
                        else
                        {
                            CountingBytenum += ReadBytes;
                        }
                        statictime = atime;

                        OutStream.Write(Buffer, 0, ReadBytes);
                    }
                    OutStream.Close();
                    HttpStream.Close();
                    Response.Close();
                    Request.Abort();
                    Param.mStartEvent.Reset();
                    Param.mDoneEvent.Set();
                }
                catch (WebException e)
                {
                }
            }
        }