public void listen() { listener = new TcpListener(port); listener.Start(); while (is_active) { TcpClient s = listener.AcceptTcpClient(); HttpProcessor processor = new HttpProcessor(s, this); Thread thread = new Thread(new ThreadStart(processor.process)); thread.Start(); Thread.Sleep(1); } }
public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData);
public override void handleGETRequest(HttpProcessor p) { p.writeSuccess(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Tentativa de acesso via GET"); p.outputStream.WriteLine("User not autenticated"); }
public abstract void handleGETRequest(HttpProcessor p);
public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData) { // Create //New_User //User //Pass //SSO //action //emu_type //emu_name //db_host //db_port //db_user //db_pass //db_name //host //tcp //mus //Figuredata //Furnidata // Others //key Dictionary<String, String> post = new Dictionary<string, string>(); if(HttpProcessor.debug) Console.WriteLine("POST request: {0}", p.http_url); string data = inputData.ReadToEnd(); if (data.Contains("&")) { foreach (string i in data.Split('&')) { string[] sp = i.Split('='); post[sp[0]] = DecodeUrlString(sp[1]); } } p.writeSuccess(); if (!post.ContainsKey("user") || !post.ContainsKey("pass")) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Tentativa de acesso direto ao POST sem autenticacao"); p.outputStream.WriteLine("User not autenticated"); } else { if (autenticate(post)) { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Tentativa de acesso inválida: {0}", post["user"]); p.outputStream.WriteLine("User not autenticated"); } else { Console.BackgroundColor = ConsoleColor.White; Dictionary<string, string> Data = new Dictionary<string, string>(); try { using (var streamReader = new StreamReader(Environment.CurrentDirectory + "/Emuladores/" + Emulator.name + "/fbrpan.ini")) { string text; while ((text = streamReader.ReadLine()) != null) { if (text.Length < 1 || text.StartsWith("#")) continue; var num = text.IndexOf('='); if (num == -1) continue; var key = text.Substring(0, num); var value = text.Substring((num + 1)); Data.Add(key, value); } streamReader.Close(); } } catch (Exception ex) { throw new ArgumentException(string.Format("Could not process configuration file: {0}", ex.Message)); } switch (post["action"]) { case "create": if(!emuExists(Emulator.name) && post["user"] == "admin"){ p.outputStream.WriteLine("1"); post["user"] = post["new_user"]; Emulator.name = post["user"]; Handles.handleCreate(post); } else p.outputStream.WriteLine("0"); break; case "login": p.outputStream.WriteLine("1"); break; case "start": if (Process.GetProcessesByName(Emulator.name).Length == 0) Handles.handleStart(post); p.outputStream.WriteLine(Process.GetProcessesByName(Emulator.name).Length); break; case "stop": foreach (var process in Process.GetProcessesByName(Emulator.name)) process.Kill(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Emulador {0} desligado", Emulator.name); p.outputStream.WriteLine(0); break; case "restart": try { foreach (var process in Process.GetProcessesByName(Emulator.name)) process.Kill(); Console.WriteLine("Emulador {0} desligado", Emulator.name); } finally { Handles.handleStart(post); } p.outputStream.WriteLine(Process.GetProcessesByName(Emulator.name).Length); break; case "changev": if (emuExists(Emulator.name)) { try { foreach (var process in Process.GetProcessesByName(Emulator.name)) process.Kill(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Emulador {0} desligado", Emulator.name); } finally { Functions.DelAll(new DirectoryInfo("Emuladores/" + Emulator.name)); string t = post["emu_type"]; foreach (string a in Emulator.block_edit) post[a] = Data[a]; post["emu_type"] = t; Handles.handleCreate(post); p.outputStream.WriteLine("1"); } } else p.outputStream.WriteLine("0"); break; case "verify": if (Process.GetProcessesByName(Emulator.name).Length == 0) p.outputStream.WriteLine("0"); else p.outputStream.WriteLine("1"); break; case "title": if (Process.GetProcessesByName(Emulator.name).Length != 0) { foreach (var process in Process.GetProcessesByName(Emulator.name)) { p.outputStream.WriteLine(process.MainWindowTitle); } } break; case "get_config": try { p.outputStream.WriteLine(Data[post["key"]]); } catch { p.outputStream.WriteLine(""); } break; case "get_versions": foreach (string v in Emulator.versions) { if (Data["emu_type"] == v) p.outputStream.WriteLine("<option value='{0}' selected='selected'>{0}</option>", v); else p.outputStream.WriteLine("<option value='{0}'>{0}</option>", v); } break; case "set_config": try { using (StreamWriter sw = File.CreateText("Emuladores/" + Emulator.name + "/fbrpan.ini")) { foreach (KeyValuePair<string, string> z in post) { string value = ""; try { value = z.Value; value = value.Replace("%3A", ":"); value = value.Replace("%2F", "/"); foreach (string s in Emulator.block_edit) { if (z.Key == s) { if (Data.ContainsKey(s)) value = Data[s]; else value = ""; } } } catch { } sw.WriteLine(z.Key + "=" + value); } foreach (string s in Emulator.block_edit) { if (!post.ContainsKey(s)) { if(Data.ContainsKey(s)) sw.WriteLine(s + "=" + Data[s]); } } } foreach (string a in Emulator.block_edit) { try { post[a] = Data[a]; } catch { post[a] = ""; } } Handles.emuCreateConfig(Data["emu_type"], Emulator.name, post); p.outputStream.WriteLine("1"); } catch(Exception e) { Console.WriteLine(e.ToString()); p.outputStream.WriteLine("0"); } break; case "emu_info": p.outputStream.WriteLine("<b>"+Emulator.name.ToUpper() + "</b><br>TCP: <b>" + Data["tcp"] + "</b> MUS: <b>" + Data["mus"] + "</b>"); break; } } } }