Пример #1
0
        static void mlist_onClientConnect(ClientWebRequest request)
        {
            try
            {
            engine.NtfyConnection(request);
            }
            catch (Exception er)
            {

                MemoryStream errstream = new MemoryStream();
                StreamWriter mwriter = new StreamWriter(errstream);
                mwriter.Write("<html><head><title>An error has occured</title></head><body><pre><h2>Whoops! We've had a 500 Internal Server Error! To the embarassment of the developer, the full error is shown below</h2><hr />"+er.ToString()+"</pre></body></html>");
                mwriter.Flush();
                errstream.Position = 0;
                ClientHttpResponse response = new ClientHttpResponse();
                response.len = errstream.Length;
                response.StatusCode = "500 Internal Server Error";
                response.ContentType = "text/html";
                response.WriteHeader(request.stream);
                response.WriteStream(errstream, request.stream, 16384);
                try {
                request.stream.Dispose();
                }catch(Exception err) {

                }
            }
        }
Пример #2
0
 public void onRequest(ClientWebRequest request)
 {
     if(request.Method == "GET") {
     //Initiate session
     ClientHttpResponse response = new ClientHttpResponse();
         response.ContentType = "text/html";
         response.len = 1024*1024*300;
         response.StatusCode = "200 OK";
         response.WriteHeader(request.stream);
         ClientSession session = new ClientSession();
         session.sessionID = Guid.NewGuid();
         TrashyStream garbage = new TrashyStream(request.stream);
         session.writer = garbage;
         BinaryWriter mwriter = new BinaryWriter(garbage);
         mwriter.Write(session.sessionID.ToByteArray());
         mwriter.Flush();
         lock(ClientSession.sessions) {
         ClientSession.sessions.Add(session.sessionID,session);
         }
         session.WaitHandle.WaitOne();
     }else {
         try {
     TrashyStream reader = new TrashyStream(request.stream);
         BinaryReader mreader = new BinaryReader(reader);
         ClientSession currentSession = ClientSession.sessions[new Guid(mreader.ReadBytes(16))];
         currentSession.reader = reader;
         BinaryWriter mwriter = new BinaryWriter(currentSession.writer);
         byte[] ourprivatekey = db[0];
         byte[] ourpubkey = db.GetPublicKeyOnly(ourprivatekey);
         mwriter.Write(ourpubkey.Length);
         mwriter.Write(ourpubkey);
         mwriter.Flush();
         byte[] theirpubkey = mreader.ReadBytes(mreader.ReadInt32());
         if(!db.IsKeyTrusted(theirpubkey)) {
         db.AddPublicKey(theirpubkey);
             db.Commit();
         }
         currentSession.securedStream = new TrashyStream(db.CreateAuthenticatedStream(ourprivatekey,new DualStream(currentSession.writer,currentSession.reader),32));
         Console.WriteLine("Secure stream negotiated");
         currentSession.pubKey = BitConverter.ToString(theirpubkey);
         OpenNetProtocolDriver driver = new OpenNetProtocolDriver(currentSession);
         }catch(Exception er) {
         Console.WriteLine(er);
         }
     }
 }
Пример #3
0
        public string ProfilePicture(ClientWebRequest request)
        {
            SessionInformation session = sessions[request.QueryString["sessionID"]];
            if(request.QueryString.ContainsKey("GetProfilePic")) {
            foreach(string et in session.fs.OpenDir(request.QueryString["GetProfilePic"]).Files) {
            if(et == "pic") {
                    Bitmap tmap = new Bitmap(32,32);
                        Graphics aix = Graphics.FromImage(tmap);
                        Bitmap amage = new Bitmap(session.fs.OpenDir(request.QueryString["GetProfilePic"]).OpenFile("pic"));
                    aix.DrawImage(amage,new Rectangle(0,0,32,32));
                        aix.Dispose();
                        amage.Dispose();
                        MemoryStream ystream = new MemoryStream();
                        tmap.Save(ystream,System.Drawing.Imaging.ImageFormat.Jpeg);
                        ystream.Position = 0;
                        tmap.Dispose();
                        ClientHttpResponse _response = new ClientHttpResponse();
                        _response.len = ystream.Length;
                        _response.ContentType = "image/jpg";
                        _response.WriteHeader(request.stream);
                        _response.WriteStream(ystream,request.stream,16384);

                    }
            }
                Bitmap mmap = new Bitmap(32,32);
                Graphics mfix = Graphics.FromImage(mmap);
                        mfix.DrawLine(Pens.Red,new Point(0,0),new Point(32,32));
                mfix.DrawLine(Pens.Red,new Point(0,32),new Point(32,0));
                mfix.Dispose();
                MemoryStream mstream = new MemoryStream();
                mmap.Save(mstream,System.Drawing.Imaging.ImageFormat.Jpeg);
                mmap.Dispose();
                mstream.Position = 0;
                ClientHttpResponse response = new ClientHttpResponse();
                response.len = mstream.Length;
                response.ContentType = "image/jpg";
                response.WriteHeader(request.stream);
                response.WriteStream(mstream,request.stream,16384);
                request.ContinueProcessing = false;
                return "";
            }
            return "profile.html?GetProfilePic="+session.UserName+"&sessionID="+request.QueryString["sessionID"];
        }
Пример #4
0
        public string downloadTable(ClientWebRequest request)
        {
            if(request.QueryString.ContainsKey("action")) {
            if(request.QueryString["action"] == "pause") {
                Download.downloads[Convert.ToInt32(request.QueryString["inst"])].Pause();

                }
                if(request.QueryString["action"] == "resume") {
                Download.downloads[Convert.ToInt32(request.QueryString["inst"])].Resume();

                }
                if(request.QueryString["action"] == "abort") {
                Download.downloads[Convert.ToInt32(request.QueryString["inst"])].Abort();

                }
                return "";
            }
            if(request.QueryString.ContainsKey("downloadFile")) {
                Stream fstr = File.Open(request.QueryString["downloadFile"],FileMode.Open,FileAccess.Read,FileShare.ReadWrite);

                ClientHttpResponse response = new ClientHttpResponse();
                response.StatusCode = "200 OK";
                response.ContentType = ConfigManager.getMimeType(request.QueryString["downloadFile"]);
                response.len = fstr.Length;
                response.AddHeader("Content-Disposition: attachment; filename=\""+Path.GetFileName(request.QueryString["downloadFile"])+"\"");

                response.WriteHeader(request.stream);
                request.ContinueProcessing = false;
                try {

                Download mload = new Download(request.QueryString["downloadFile"],fstr,request.stream,fstr.Length);
                mload.Begin();
                }catch(Exception er) {

                }

                return "";
            }
            try {
            statusUpdateEvent.WaitOne(5000);
            statusUpdateEvent.Reset();
            }catch(Exception) {

            }
            StringBuilder mbuilder = new StringBuilder();
            mbuilder.AppendLine("<table border=\"1\" inst_id=\"maintable\">");
            mbuilder.AppendLine("<tr><td><b><u>Connection name</u></b></td><td><b><u>Progress</u></b></td><td><b><u>Status</u></b></td></tr>");
            try {
                int i = 0;
            lock(Download.downloads) {

            foreach(Download et in Download.downloads) {
                    string progress = "<div style=\"width:"+((int)(((float)et._progress/(float)et._size)*100f)).ToString()+"%;background-color:Green;\">PROGRESS</div>";
                string actions = "";
                        if(et.paused) {
                        actions = "PAUSED";
                        }else {
                        actions = "IN PROGRESS";
                        }
                        if(et.haserror) {
                    actions = "ERROR";
                        }

                    mbuilder.AppendLine("<tr dlID=\""+i.ToString()+"\"><td>"+et.name+"</td><td style=\"background-color:Red;\">"+progress+"</td><td inst_id=\"status\">"+actions+"</td></tr>");
                i++;
                    }

                mbuilder.AppendLine("</table>");
            }
                }catch(Exception) {
                }
            return mbuilder.ToString();
        }
Пример #5
0
        public void onRequest(ClientWebRequest request)
        {
            try {
            if(request.UnsanitizedRelativeURI.Contains("transparent.png")) {
            if(transparentBitmap == null) {
                Bitmap mmap = new Bitmap(4,4);
                    Graphics mfix = Graphics.FromImage(mmap);
                    mfix.Clear(Color.FromArgb(106,0,0,255));
                    mfix.Dispose();
                    MemoryStream mstream = new MemoryStream();
                    mmap.Save(mstream,ImageFormat.Png);
                    mstream.Position = 0;
                    transparentBitmap = mstream.ToArray();
                    mstream.Dispose();
                    mmap.Dispose();
                }
                ClientHttpResponse response = new ClientHttpResponse();
                response.ContentType = "image/png";
                response.len = transparentBitmap.Length;
                response.StatusCode = "200 OK";
                response.WriteHeader(request.stream);
                request.stream.Write(transparentBitmap,0,transparentBitmap.Length);

            }
            if(request.UnsanitizedRelativeURI.Contains("backgroundImg.jpg")) {
            if(bitmapData == null) {
                Bitmap mmap = new Bitmap(1024,1024);
                Graphics mfix = Graphics.FromImage(mmap);
                mfix.Clear(Color.Black);
                mfix.FillRectangle(new LinearGradientBrush(new Point(0,0),new Point(0,512),Color.Blue,Color.Black),new Rectangle(0,0,1024,512));
                mfix.DrawString("OpenServer 2012 - Administration Console\nApplication startup time: "+DateTime.Now.ToString(),new Font(FontFamily.GenericMonospace,24),Brushes.White,new Point(0,0));
                    mfix.Dispose();

                    MemoryStream mstream = new MemoryStream();
                    mmap.Save(mstream,ImageFormat.Jpeg);
                    mstream.Position = 0;
                    bitmapData = mstream.ToArray();
                    mstream.Dispose();

                }

                ClientHttpResponse response = new ClientHttpResponse();
                response.ContentType = "image/jpeg";
                response.len = bitmapData.Length;
                response.StatusCode = "200 OK";
                response.WriteHeader(request.stream);
                request.stream.Write(bitmapData,0,bitmapData.Length);
                request.stream.Flush();
                return;
            }
            string path = Environment.CurrentDirectory+request.UnsanitizedRelativeURI.Replace("/",Path.DirectorySeparatorChar.ToString());
            if(path.IndexOf("?")>1) {
            path = path.Substring(0,path.IndexOf("?"));
            }
            if(File.Exists(path)) {

            using(Stream fstr = File.Open(path,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)) {

                string mimetype;
                string str = path.Substring(path.LastIndexOf("."));
                if(engine.mimetypes.ContainsKey(str)) {
                mimetype = engine.mimetypes[str];
                }else {
                mimetype = "application/octet-stream";
                }

                    if(mimetype == "text/html") {
                    StreamReader mreader = new StreamReader(fstr);
                        string httext = mreader.ReadToEnd();
                        reqManager.ParseHTMLDocument(ref httext,request);
                        MemoryStream mstream = new MemoryStream();
                        StreamWriter mwriter = new StreamWriter(mstream);
                        mwriter.Write(httext);
                        mwriter.Flush();
                        mstream.Position = 0;
                        if(request.ContinueProcessing) {
                        ClientHttpResponse response = new ClientHttpResponse();
                            response.ContentType = mimetype;
                            response.len = mstream.Length;
                            response.StatusCode = "200 OK";
                            response.WriteHeader(request.stream);
                            response.WriteStream(mstream,request.stream,16384);
                        }else {
                        return;
                        }
                    }else {

                ClientHttpResponse response = new ClientHttpResponse();
                response.ContentType = mimetype;
                response.len = fstr.Length;
                        response.StatusCode = "200 OK";
                    response.WriteHeader(request.stream);
                    response.WriteStream(fstr,request.stream,16384);

                    }
                }
            }else {
            ClientHttpResponse response = new ClientHttpResponse();
                response.ContentType = "text/html";
                response.len = notfoundpage.Length;
                response.StatusCode = "404 Not Found";
                response.WriteHeader(request.stream);
                request.stream.Write(notfoundpage,0,notfoundpage.Length);
                request.stream.Flush();

            }
            }catch(Exception er) {
            Console.WriteLine(er);
            }
        }