public static void serverRuntime() { IPEndPoint address = new IPEndPoint(IPAddress.Parse(ipAddress), port); writeLine("[INFO] : Address Setup!"); List <String> wl = new List <String>(); if (settings.ContainsKey("whitelist")) { wl.AddRange(settings["whitelist"].Split(",".ToCharArray())); } OCNetworkListener server = new OCNetworkListener(address, wl); writeLine("[INFO] : Listener Started!"); writeLine("[INFO] : Listener 'Address:Port' : " + server.getListeningAddress().Address.ToString() + ":" + server.getListeningAddress().Port); writeLine("[INFO] : Open Addresses 'Address:Port' :"); if (version == 4 && ipAddress.Equals(IPAddress.Any.ToString())) { foreach (String c in addrsv4) { writeLine(c + ":" + port); } } else if (version == 6 && ipAddress.Equals(IPAddress.IPv6Any.ToString())) { foreach (String c in addrsv6) { writeLine(c + ":" + port); } } else if (version == 0 && ipAddress.Equals(IPAddress.IPv6Any.ToString())) { List <String> addrsT = new List <String>(); addrsT.AddRange(addrsv4); addrsT.AddRange(addrsv6); foreach (String c in addrsT) { writeLine(c + ":" + port); } addrsT.Clear(); addrsT = null; } else { writeLine(ipAddress + ":" + port); } Boolean exec = true; while (exec) { if (server.getIsThereAcceptedClient()) { OCNetworkClient client = server.getAcceptedClient(); writeLine("[INFO] : Client Accepted!"); writeLine("[INFO] : Client 'Address:Port' : " + client.getRemoteAddress().Address.ToString() + ":" + client.getRemoteAddress().Port); handleProtocol(client); server.returnAcceptedClient(client); writeLine("[INFO] : Client Disposed!"); } try { Thread.Sleep(100); } catch (ThreadInterruptedException e) { break; } } server.close(); server = null; }
public static void handleProtocol(OCNetworkClient clientIn) { String prot = clientIn.receiveProtocol(); if (prot.Equals("1")) { writeLine("[INFO] : Sending..."); String data = ""; if (settings.ContainsKey("target") && !settings.ContainsKey("cache")) { writeLine("[INFO] : Sending : Loading Data..."); try { data = loadFile(settings["target"]); } catch (IOException e) { data = ""; } } else if (settings.ContainsKey("cache")) { writeLine("[INFO] : Sending : Retrieving Data..."); data = cache; } writeLine("[INFO] : Sending : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Sending : Waiting For Handshake..."); Boolean hand1Succ = clientIn.receiveHandshake("1"); if (hand1Succ) { writeLine("[INFO] : Sending : Sending Data..."); clientIn.sendData(data); writeLine("[INFO] : Sending : Waiting For Handshake..."); clientIn.receiveHandshake("1"); } } else if (prot.Equals("2")) { writeLine("[INFO] : Receiving..."); writeLine("[INFO] : Receiving : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Receiving : Waiting For Data..."); String data = clientIn.receiveData(); writeLine("[INFO] : Receiving : Processing Data..."); if (data.Contains("\r") && !data.Contains("\n")) { data = data.Replace("\r", "\r\n"); } if (data.Contains("\n") && !data.Contains("\r")) { data = data.Replace("\n", "\r\n"); } if (settings.ContainsKey("cache")) { writeLine("[INFO] : Receiving : Caching Data..."); cache = data; } if (settings.ContainsKey("target")) { writeLine("[INFO] : Receiving : Saving Data..."); try { saveFile(settings["target"], data); } catch (IOException e) { } } writeLine("[INFO] : Receiving : Sending Handshake..."); clientIn.sendHandshake("1"); } else if (prot.Equals("3")) { write("[INFO] : Access Mode : "); clientIn.sendHandshake("1"); String protam = clientIn.receiveProtocol(); if (protam.Equals("1") && !settings.ContainsKey("writeonly")) { writeLine("Send"); writeLine("[INFO] : Sending : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Sending : Receiving Path..."); Int32 sl = clientIn.receiveSmallNumber(); if (sl != 0) { Int32 l = clientIn.receiveNumber(sl); if (l != 0) { String nom = clientIn.receiveData(l); writeLine("[INFO] : Reading : " + nom); try { String data = loadFile(nom); writeLine("[INFO] : Sending : Waiting For Handshake..."); if (clientIn.receiveHandshake("1")) { writeLine("[INFO] : Sending : " + nom); clientIn.sendSmallNumber(data.Length.ToString().Length); clientIn.sendNumber(data.Length); clientIn.sendData(data); } writeLine("[INFO] : Sending : Waiting For Handshake..."); clientIn.receiveHandshake("1"); } catch (IOException e) { } } } } else if (protam.Equals("2") && !settings.ContainsKey("readonly")) { writeLine("Receive"); writeLine("[INFO] : Receiving : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Receiving : Receiving Path..."); Int32 sl = clientIn.receiveSmallNumber(); if (sl != 0) { Int32 l = clientIn.receiveNumber(sl); if (l != 0) { String nom = clientIn.receiveData(l); writeLine("[INFO] : Receiving : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Receiving : " + nom); sl = clientIn.receiveSmallNumber(); if (sl != 0) { l = clientIn.receiveNumber(sl); if (l != 0) { String data = clientIn.receiveData(l); writeLine("[INFO] : Writing : " + nom); try { saveFile(nom, data); writeLine("[INFO] : Receiving : Sending Handshake..."); clientIn.sendHandshake("1"); } catch (IOException e) { } } } } } } else if (protam.Equals("3") && settings.ContainsKey("creation")) { writeLine("File Creation"); writeLine("[INFO] : Creating : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Creating : Receiving Path..."); Int32 sl = clientIn.receiveSmallNumber(); if (sl != 0) { Int32 l = clientIn.receiveNumber(sl); if (l != 0) { String nom = clientIn.receiveData(l); writeLine("[INFO] : Creating : " + nom); try { createFile(nom); writeLine("[INFO] : Creating : Sending Handshake..."); clientIn.sendHandshake("1"); } catch (IOException e) { } } } } else if (protam.Equals("4") && settings.ContainsKey("deletion")) { writeLine("Deletion"); writeLine("[INFO] : Deleting : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Deleting : Receiving Path..."); Int32 sl = clientIn.receiveSmallNumber(); if (sl != 0) { Int32 l = clientIn.receiveNumber(sl); if (l != 0) { String nom = clientIn.receiveData(l); writeLine("[INFO] : Deleting : " + nom); try { deleteFile(nom); writeLine("[INFO] : Deleting : Sending Handshake..."); clientIn.sendHandshake("1"); } catch (IOException e) { } } } } else if (protam.Equals("5") && settings.ContainsKey("enumeration")) { writeLine("Enumeration"); writeLine("[INFO] : Enumerating : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Enumerating : Receiving Path..."); Int32 sl = clientIn.receiveSmallNumber(); if (sl != 0) { Int32 l = clientIn.receiveNumber(sl); if (l != 0) { String nom = clientIn.receiveData(l); writeLine("[INFO] : Enumerating : " + nom); try { String result = ""; if (Directory.Exists(nom) || File.Exists(nom)) { if (File.GetAttributes(nom).HasFlag(FileAttributes.Directory)) { List <String> enr = new List <string>(Directory.GetFileSystemEntries(nom)); enr.Remove(nom); if (enr.Count > 0) { if (enr.Count == 1) { result = enr[0].ToString(); } else { for (int i = 0; i < (enr.Count - 1); i++) { result = result + enr[i].ToString() + "\r\n"; } result = result + enr[enr.Count - 1].ToString(); } } enr.Clear(); enr = null; } else { result = new FileInfo(nom).Length.ToString(); } } writeLine("[INFO] : Enumerating : Waiting For Handshake..."); if (clientIn.receiveHandshake("1")) { writeLine("[INFO] : Enumerating : Sending Enumeration..."); clientIn.sendSmallNumber(result.Length.ToString().Length); clientIn.sendNumber(result.Length); clientIn.sendData(result); } writeLine("[INFO] : Enumerating : Waiting For Handshake..."); clientIn.receiveHandshake("1"); } catch (IOException e) { } } } } else if (protam.Equals("6") && settings.ContainsKey("deletion")) { writeLine("Directory Creation"); writeLine("[INFO] : Creating : Sending Handshake..."); clientIn.sendHandshake("1"); writeLine("[INFO] : Creating : Receiving Path..."); Int32 sl = clientIn.receiveSmallNumber(); if (sl != 0) { Int32 l = clientIn.receiveNumber(sl); if (l != 0) { String nom = clientIn.receiveData(l); writeLine("[INFO] : Creating : " + nom); try { if (!Directory.Exists(nom)) { Directory.CreateDirectory(nom); } writeLine("[INFO] : Creating : Sending Handshake..."); clientIn.sendHandshake("1"); } catch (IOException e) { } } } } else { writeLine("Unknown"); clientIn.sendHandshake("0"); } } }