public static Response From(Request request) { if (request == null) { return(MakeNullRequest()); } if (request.Type == "EXT" || request.Type == "GET" || request.Type == "CMD1" || request.Type == "CMD2" || request.Type == "CMD3" || request.Type == "CLR1" || request.Type == "CLR2" || request.Type == "CLR3" || request.Type == "CMDED1" || request.Type == "CMDED2" || request.Type == "CMDED3") { if (request.Type == "GET") { String file = HTTPServer.WEB_DIR + request.URL; Console.WriteLine(file); FileInfo f = new FileInfo(file); if (f.Exists && f.Extension.Contains(".")) { return(MakeFromFile(f)); } else { DirectoryInfo di = new DirectoryInfo(f + "/"); if (!di.Exists) { return(MakePageNotFound()); } FileInfo[] files = di.GetFiles(); foreach (FileInfo ff in files) { string n = ff.Name; if (n.Contains("default.html") || n.Contains("default.htm") || n.Contains("index.htm") || n.Contains("index.html") || n.Contains("Index.htm") || n.Contains("Index.html") || n.Contains("Default.htm") || n.Contains("Default.html")) { return(MakeFromFile(ff)); } } } if (f.Exists == false) { return(MakePageNotFound()); } } if (HTTPServer.CMDBool) { if (isCMDED) { if (HTTPServer.CMDEDIN5.Contains("shutdown") || HTTPServer.CMDEDIN6.Contains("shutdown") || HTTPServer.CMDEDIN7.Contains("shutdown") || HTTPServer.CMDEDIN5.Contains("del") || HTTPServer.CMDEDIN6.Contains("del") || HTTPServer.CMDEDIN7.Contains("del")) { return(MakeCustomres("Why are you trying to destroy this server . (Stop it, Get some help)")); } else { if (request.Type == "CMDED1") { if (HTTPServer.CMDEDIN6.Contains("CMD1")) { string CMDED11 = CharRem(HTTPServer.CMDEDIN6).Replace("CMD1", " ").Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED11); HTTPServer.CMDET(0, CMDED11); } if (HTTPServer.CMDEDIN7.Contains("CMD1")) { string CMDED12 = CharRem(HTTPServer.CMDEDIN7).Replace("CMD1", " ").Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED12); HTTPServer.CMDET(0, CMDED12); } if (HTTPServer.CMDEDIN5.Contains("CMD1")) { string CMDED13 = CharRem(HTTPServer.CMDEDIN5).Replace("CMD1", " ").Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("command is " + CMDED13); HTTPServer.CMDET(0, CMDED13); } return(MakeCustomres("CMDED1")); } if (request.Type == "CMDED2") { if (HTTPServer.CMDEDIN6.Contains("CMD2")) { string CMDED21 = CharRem(HTTPServer.CMDEDIN6).Replace("CMD2", " ").Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED21); HTTPServer.CMDET(1, CMDED21); } if (HTTPServer.CMDEDIN7.Contains("CMD2")) { string CMDED22 = CharRem(HTTPServer.CMDEDIN7).Replace("CMD2", " ").Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED22); HTTPServer.CMDET(1, CMDED22); } if (HTTPServer.CMDEDIN5.Contains("CMD2")) { string CMDED23 = CharRem(HTTPServer.CMDEDIN5).Replace("CMD2", " ").Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED23); HTTPServer.CMDET(1, CMDED23); } return(MakeCustomres("CMDED2")); } if (request.Type == "CMDED3") { if (HTTPServer.CMDEDIN6.Contains("CMD3")) { string CMDED31 = CharRem(HTTPServer.CMDEDIN6).Replace("CMD3", string.Empty).Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED31); HTTPServer.CMDET(2, CMDED31); } if (HTTPServer.CMDEDIN7.Contains("CMD3")) { string CMDED32 = CharRem(HTTPServer.CMDEDIN7).Replace("CMD3", string.Empty).Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED32); HTTPServer.CMDET(2, CMDED32); } if (HTTPServer.CMDEDIN5.Contains("CMD3")) { string CMDED33 = CharRem(HTTPServer.CMDEDIN5).Replace("CMD3", string.Empty).Replace("slash", "/").Replace("andd", "&&").Replace("SPC", " ").Replace("COL", ":").Replace("HYP", "-"); Console.WriteLine("Command is " + CMDED33); HTTPServer.CMDET(2, CMDED33); } return(MakeCustomres("CMDED3")); } } } else { Console.WriteLine("The client is trying to change the commands in slots"); } if (request.Type == "CMD1") { HTTPServer.CMD(0); return(MakeCustomres("CMD1")); } if (request.Type == "CMD2") { HTTPServer.CMD(1); return(MakeCustomres("CMD2")); } if (request.Type == "CMD3") { HTTPServer.CMD(2); return(MakeCustomres("CMD3")); } if (request.Type == "CLR1") { HTTPServer.CLR(0); return(MakeCustomres("CLR1")); } if (request.Type == "CLR2") { HTTPServer.CLR(1); return(MakeCustomres("CLR2")); } if (request.Type == "CLR3") { HTTPServer.CLR(2); return(MakeCustomres("CLR3")); } if (request.Type == "EXT") { Task.Factory.StartNew(() => { Thread.Sleep(3000); HTTPServer.stop(); Environment.Exit(3); }); return(MakeCustomres("EXT")); } } else { Console.WriteLine("Someone is trying to use Commands without permission"); } } else { return(MakeMethodNotAllowed()); } return(MakePageNotFound()); }
static void Main(string[] args) { string Set = "Settings.ini"; // Settings file HTTPServer.SetTimer(); string CMD_REQ_EN = File.ReadLines(Set).Skip(29).Take(1).First(); string CMD_REQ_1 = ReadSET(Set, 32); string CMD_REQ_2 = ReadSET(Set, 35); string CMD_REQ_3 = ReadSET(Set, 38); string line = File.ReadLines(Set).Skip(8).Take(1).First(); string dir = File.ReadLines(Set).Skip(11).Take(1).First(); string msgdir = File.ReadLines(Set).Skip(14).Take(1).First(); string logdir = File.ReadLines(Set).Skip(17).Take(1).First(); string Logbool = File.ReadLines(Set).Skip(20).Take(1).First(); string Pbool = File.ReadLines(Set).Skip(23).Take(1).First(); string ELine = File.ReadLines(Set).Skip(26).Take(1).First(); string cBool = File.ReadLines(Set).Skip(41).Take(1).First(); bool CanPF; bool logbool = bool.Parse(Logbool); bool UPNPF = bool.Parse(Pbool); int PT = int.Parse(line); int EPT; HTTPServer.CMDBool = bool.Parse(CMD_REQ_EN); HTTPServer.Lbool = logbool; HTTPServer.CMD1 = CMD_REQ_1; HTTPServer.CMD2 = CMD_REQ_2; HTTPServer.CMD3 = CMD_REQ_3; Response.isCMDED = bool.Parse(cBool); if (args.Length > 0) { if (args[0] == "-h") { Console.WriteLine("C# HTTP Web Server " + HTTPServer.NAME); Console.WriteLine(" "); Console.WriteLine("-h :To get the arguments of the program "); Console.WriteLine(" "); Console.WriteLine("-sc :To start the server with the configuration of settings file"); Console.WriteLine(" "); Console.WriteLine("-spf <EPort> :To start the server with port forwarding. Args <Eport>:External port"); Console.WriteLine(" "); Console.WriteLine("-s <Iport> <WEBDir> <ERRDir> <PRTFWD> <Eport> :To start the server with user configuration. Args <Iport>:Internal port <WEBDir>:The Directory where web pages are stored <ERRDir>:The directory where Error pages are stored <PRTFWD>:true or false wheather to port forward or not <Eport>:External port if port forward is enabled "); Environment.Exit(0); } if (cBool == "false") { Console.WriteLine("CMDED enabled is " + Response.isCMDED); Console.WriteLine("CMDED is disabled"); } else { Console.WriteLine("CMDED enabled is " + Response.isCMDED); Console.WriteLine("CMDED is enabled"); } if (args[0] == "-sc") { if (dir == "") // To set directory of webpages { HTTPServer.WEB_DIR = HTTPServer.DEF_WEB_DIR; Console.WriteLine("Web root directory is set to " + HTTPServer.DEF_WEB_DIR); } else { HTTPServer.SET_WEB_DIR = dir; HTTPServer.WEB_DIR = HTTPServer.SET_WEB_DIR; Console.WriteLine("Web root directory is set to " + HTTPServer.SET_WEB_DIR); } if (msgdir == "") // To set directory of error pages { HTTPServer.MSG_DIR = HTTPServer.DEF_MSG_DIR; Console.WriteLine("Msg root directory is set to " + HTTPServer.DEF_MSG_DIR); } else { HTTPServer.SET_MSG_DIR = msgdir; HTTPServer.MSG_DIR = HTTPServer.SET_MSG_DIR; Console.WriteLine("Msg root directory is set to " + HTTPServer.SET_MSG_DIR); } if (logdir == "") // To set directory of logs { HTTPServer.LOG_DIR = HTTPServer.DEF_LOG_DIR; Console.WriteLine("Log root directory is set to " + HTTPServer.DEF_LOG_DIR); } else { HTTPServer.SET_LOG_DIR = logdir; HTTPServer.LOG_DIR = HTTPServer.SET_LOG_DIR; Console.WriteLine("Log root directory is set to " + HTTPServer.SET_LOG_DIR); } if (!logbool) // To set logging is disabled or enabled { Console.WriteLine("Logging is disabled"); } else { Console.WriteLine("Logging is enabled"); } HTTPServer serve = new HTTPServer(PT); Console.WriteLine("$ Server starting"); serve.start(); Console.WriteLine("$ Server started on port :" + PT); if (Console.ReadKey().Key == ConsoleKey.Escape) { Console.WriteLine("Stopping server"); HTTPServer.stop(); Console.WriteLine("Server stopped"); } } } Console.WriteLine("press ESC key to stop the server"); if (dir == "") // To set directory of webpages { HTTPServer.WEB_DIR = HTTPServer.DEF_WEB_DIR; Console.WriteLine("Web root directory is set to " + HTTPServer.DEF_WEB_DIR); } else { HTTPServer.SET_WEB_DIR = dir; HTTPServer.WEB_DIR = HTTPServer.SET_WEB_DIR; Console.WriteLine("Web root directory is set to " + HTTPServer.SET_WEB_DIR); } if (msgdir == "") // To set directory of error pages { HTTPServer.MSG_DIR = HTTPServer.DEF_MSG_DIR; Console.WriteLine("Msg root directory is set to " + HTTPServer.DEF_MSG_DIR); } else { HTTPServer.SET_MSG_DIR = msgdir; HTTPServer.MSG_DIR = HTTPServer.SET_MSG_DIR; Console.WriteLine("Msg root directory is set to " + HTTPServer.SET_MSG_DIR); } if (logdir == "") // To set directory of logs { HTTPServer.LOG_DIR = HTTPServer.DEF_LOG_DIR; Console.WriteLine("Log root directory is set to " + HTTPServer.DEF_LOG_DIR); } else { HTTPServer.SET_LOG_DIR = logdir; HTTPServer.LOG_DIR = HTTPServer.SET_LOG_DIR; Console.WriteLine("Log root directory is set to " + HTTPServer.SET_LOG_DIR); } if (!logbool) // To set logging is disabled or enabled { Console.WriteLine("Logging is disabled"); } else { Console.WriteLine("Logging is enabled"); } HTTPServer server = new HTTPServer(PT); if (UPNPF) { if (ELine == "" || ELine == "0") { Console.WriteLine("External port is not specified in Settings.ini file or you are trying to port forward to 0"); CanPF = false; } else { CanPF = true; EPT = int.Parse(ELine); HTTPServer.EPort = EPT; HTTPServer.CanFWD = CanPF; } } else { Console.WriteLine("Port forwarding is disabled. You have to manually port forward"); } if (line == "" || line == "0") // To stop server from listening to 0 { Console.WriteLine("Port is not specified or you are using reserved port 0. Specify a port in which the server should listen in Settings.ini file"); } else { Console.WriteLine("$ Server starting"); server.start(); Console.WriteLine("$ Server started on port :" + PT); } if (Console.ReadKey().Key == ConsoleKey.Escape) { Console.WriteLine("Stopping server"); HTTPServer.stop(); Console.WriteLine("Server stopped"); } if (Console.ReadKey().Key == ConsoleKey.Enter) { PortForward.REMport(int.Parse(ELine)); PortForward.Makeport(PT, int.Parse(ELine)); Console.WriteLine("Request sussceded"); } }