Torna al Thread

/* SirVer by Riccardo Cecchini */ using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; public class SirVer { //Variabili globali public string[,] vars = new string[4096, 3]; public int session = 0; public int numvars = 1; public static void Main() { //Lista degli errori string error404 = "<h1>404 File Not Found</h1>"; //Creazione/correzione cartelle neccessarie if (System.IO.Directory.Exists(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/www/") == false) System.IO.Directory.CreateDirectory(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/www/"); if (System.IO.Directory.Exists(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/temp/") == false) System.IO.Directory.CreateDirectory(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/temp/"); else { System.IO.Directory.Delete(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/temp/", true); System.IO.Directory.CreateDirectory(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/temp/"); } int recv; byte[] data = new byte[10240]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); while(true) { try{ Console.WriteLine("Waiting for a client..."); Socket client = newsock.Accept(); IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port); //Invia il primo pacchetto al client string startdata = "HTTP/1.0 SERVER WEB\r\n Server:SERverONE\r\n\r\n"; /*Invio dei primi dati data = Encoding.ASCII.GetBytes(startdata); client.Send(data, data.Length, SocketFlags.None);*/ //Variabile ordinaria (dipende il while) bool recdata = true; while (recdata) { //Dichiarazioni e preparazione al processo data = new byte[10240]; recv = client.Receive(data); if (recv == 0) break; Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); //Ottenere comando impartito dal client int i = 0; string result = ""; string edati = Encoding.UTF8.GetString(data, 0, recv); string[] split = edati.Split(new Char[] {' '}); foreach (string s in split) { i += 1; if (i == 2) result = s; } //Aprire il file selezionato in caso che non esista alcuna corrispondenza di comando string dires = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/www" + result; string hournow = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); //Lettura/scrittura file if(System.IO.File.Exists(dires) == true) { if(Path.GetExtension(dires) == ".sir") { FileStream stream = File.Open(dires, FileMode.Open); StreamReader reader = new StreamReader(stream); string opfile = reader.ReadToEnd(); reader.Close(); opfile = tocode(opfile); string dirtemp = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "/temp/" + hournow; //Conferma correzione Console.Write("\nResult:" + opfile + "\n\n"); StreamWriter writer = new StreamWriter(dirtemp); writer.WriteLine(opfile); writer.Close(); data = System.IO.File.ReadAllBytes(dirtemp); } else data = System.IO.File.ReadAllBytes(dires); } else data = Encoding.UTF8.GetBytes(error404); //Error 404 //Conversione dei byte (PASSAGGIO ELIMINATO) //data = Encoding.UTF8.GetBytes(startdata); client.Send(data, data.Length, SocketFlags.None); recdata = false; } Console.WriteLine("Disconnected from {0}", clientep.Address); client.Close(); //System.Threading.Thread.Sleep(data.Length); } catch (Exception e) { Console.WriteLine("\n{0} Exception caught.\n", e); //Console.WriteLine("\nThere is an error.\n", e); } } newsock.Close(); } //Analizza il tipo di richiesta (IN PROGRAMMAZIONE, INUTILIZZATO) int resultype (string result) { int i = 0; string indirizzo = ""; string[] split = result.Split(new Char[] { ':' }); foreach (string s in split) { i += 1; if (i == 1) indirizzo = s; } if (indirizzo == "/image") return 1; if (indirizzo == "/download") return 2; return 0; } public static string tocode (string result) { SirVer srv = new SirVer(); result = " " + result; string now = ""; result = result.Replace("&", "-|"); result = result.Replace("$", "|-"); result = result.Replace("<|", "&"); result = result.Replace("|>", "$"); bool start = false; string[] split = result.Split(new Char[] { '&' }); result = ""; foreach (string s in split) { if (start == true) { now = s; string[] splitnow = now.Split(new Char[] { '$' }); bool nowstart = true; string finalcode = ""; foreach (string snow in splitnow) { if (nowstart == true) finalcode = srv.read(snow); else if(snow != "") finalcode = finalcode + "$" + snow; nowstart = false; } finalcode = finalcode.Replace(">$", ">"); result = result + finalcode; } else result = result + s; start = true; } result = result.Replace ("\\VRG",","); result = result.Replace("-|", "&"); result = result.Replace("|-", "$"); return "<!--SirVer-->\r\n" + result; } //Coding! string read(string code) { session += 1; string ret = ""; //Replace code = code.Replace("\r\n", ""); string[] split = code.Split(new Char[] { ';' }); foreach (string s in split) { bool isfun = true; string[] funsplit = s.Split(new Char[] { '(' }); string function = ""; string request = "/"; foreach (string funs in funsplit) { if (isfun == true) function = funs; else request = request + "(" + funs; isfun = false; } request = request.Replace("/(", "") + "/"; request = request.Replace(")/", ""); ret = ret + functions(function, request); } return ret; } string functions(string function, string request) { string finalret = ""; function = function.Replace(" ", ""); request = request.Replace(",,", "\\VRG"); string[] requestes = request.Split(new Char[] { ',' }); //Casi di funzioni if (function == "print") finalret = print(requestes[0]); if (function == "var") finalret = var(requestes[0], requestes[1]); return finalret; } string jscriptcol(string text) { return "<script language=\"javascript\">" + text + "</script>"; } string totext(string text,int vari) { int idvar; if (vari == 1) if ((idvar = whatvar(text)) != -1) text = vars[idvar, 2]; text = text.Replace("\\\"", "||.||"); text = text.Replace("\"", ""); text = text.Replace("||.||", "\""); return text; } int whatvar(string name) { bool go = true; int numstat = 0; int idvar = -1; name = name.Replace(" ", ""); while (go) { if (vars[numstat, 1] == name) idvar = numstat; numstat += 1; if (numvars <= numstat) go = false; } return idvar; } //Le funzioni di Sir! string print(string text) { string retext = ""; string[] split = text.Split(new Char[] { '.' }); foreach (string s in split) { retext = retext + totext(s, 1); } return retext; } string var(string name, string text) { string sesstext = session.ToString(); text = totext(text, 0); vars[numvars, 0] = sesstext; vars[numvars, 1] = name; vars[numvars, 2] = text; numvars += 1; return ""; } }
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5