public void CloseCastingService() { Variables.castingAddress = ""; if (SetCastingAddress != null) { SetCastingAddress.Invoke(new object(), new EventArgs()); } isWorking = false; isTakingScreenshots = false; Console.WriteLine("Server Stoped."); return; }
private async Task StartServer() { //serv = serv??new HttpListener(); var selectedIP = ipList[1]; var url = string.Format("http://{0}:{1}", selectedIP, port.ToString()); Console.WriteLine("IP is: " + url); serv.Prefixes.Clear(); serv.Prefixes.Add("http://localhost:" + port.ToString() + "/"); //serv.Prefixes.Add("http://*:" + numPort.Value.ToString() + "/"); // Uncomment this to Allow Public IP Over Internet. [Commented for Security Reasons.] serv.Prefixes.Add(url + "/"); serv.Start(); Console.WriteLine("Server Started Successfuly!"); Variables.castingAddress = url; if (SetCastingAddress != null) { SetCastingAddress.Invoke(new object(), new EventArgs()); } Console.WriteLine("Private Network URL : " + url); Console.WriteLine("Localhost URL : " + "http://localhost:" + port.ToString() + "/"); while (isWorking) { var ctx = await serv.GetContextAsync(); //Screenshot(); var resPath = ctx.Request.Url.LocalPath; if (resPath == "/") // Route The Root Dir to the Index Page { resPath += "index.html"; } var page = Application.StartupPath + "/WebServer" + resPath; bool fileExist; lock (locker) fileExist = File.Exists(page); if (!fileExist) { var errorPage = Encoding.UTF8.GetBytes("<h1 style=\"color:red\">Error 404 , File Not Found </h1><hr><a href=\".\\\">Back to Home</a>"); ctx.Response.ContentType = "text/html"; ctx.Response.StatusCode = 404; try { await ctx.Response.OutputStream.WriteAsync(errorPage, 0, errorPage.Length); } catch (Exception ex) { } ctx.Response.Close(); continue; } if (isPrivateTask) { if (!ctx.Request.Headers.AllKeys.Contains("Authorization")) { ctx.Response.StatusCode = 401; ctx.Response.AddHeader("WWW-Authenticate", "Basic realm=Screen Task Authentication : "); ctx.Response.Close(); continue; } else { var auth1 = ctx.Request.Headers["Authorization"]; auth1 = auth1.Remove(0, 6); // Remove "Basic " From The Header Value auth1 = Encoding.UTF8.GetString(Convert.FromBase64String(auth1)); /*var auth2 = string.Format("{0}:{1}", txtUser.Text, txtPassword.Text); * if (auth1 != auth2) * { * // MessageBox.Show(auth1+"\r\n"+auth2); * Log(string.Format("Bad Login from {0} using {1}", ctx.Request.RemoteEndPoint.Address.ToString(), auth1)); * var errorPage = Encoding.UTF8.GetBytes("<h1 style=\"color:red\">Not Authorized !!! </h1><hr><a href=\"./\">Back to Home</a>"); * ctx.Response.ContentType = "text/html"; * ctx.Response.StatusCode = 401; * ctx.Response.AddHeader("WWW-Authenticate", "Basic realm=Screen Task Authentication : "); * try * { * await ctx.Response.OutputStream.WriteAsync(errorPage, 0, errorPage.Length); * } * catch (Exception ex) * { * * * } * ctx.Response.Close(); * continue; * }*/ } } //Everything OK! ??? Then Read The File From HDD as Bytes and Send it to the Client byte[] filedata; // Required for One-Time Access of the file {Reader\Writer Problem in OS} rwl.AcquireReaderLock(Timeout.Infinite); filedata = File.ReadAllBytes(page); rwl.ReleaseReaderLock(); var fileinfo = new FileInfo(page); if (fileinfo.Extension == ".css") // important for IE -> Content-Type must be defiend for CSS files unless will ignored !!! { ctx.Response.ContentType = "text/css"; } else if (fileinfo.Extension == ".html" || fileinfo.Extension == ".htm") { ctx.Response.ContentType = "text/html"; // Important For Chrome Otherwise will display the HTML as plain text. } ctx.Response.AddHeader("Access-Control-Allow-Origin", "*"); ctx.Response.AddHeader("Access-Control-Allow-Headers", "origin, x-requested-with, content-type"); ctx.Response.StatusCode = 200; try { await ctx.Response.OutputStream.WriteAsync(filedata, 0, filedata.Length); } catch (Exception ex) { /* * Do Nothing !!! this is the Only Effective Solution for this Exception : * the specified network name is no longer available * */ } ctx.Response.Close(); } }