diff --git a/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs b/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs index 89be527..2bebec2 100644 --- a/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs +++ b/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs @@ -138,6 +138,110 @@ namespace captainalm.network.oc } return toret; } + + public Boolean sendSmallNumber(Int32 numIn) { + if (numIn > -1 && numIn < 10 && connected) { + String numStr = numIn.ToString(); + try { + var bts = System.Text.Encoding.ASCII.GetBytes(numStr.Substring(0, 1)); + sock.Send(bts,bts.Length,SocketFlags.None); + return true; + } catch (SocketException e) { + connected = false; + } + } + return false; + } + + public Boolean sendNumber(Int32 numIn) { + if (connected) { + String numStr = numIn.ToString(); + try { + var bts = System.Text.Encoding.ASCII.GetBytes(numStr); + sock.Send(bts,bts.Length,SocketFlags.None); + return true; + } catch (SocketException e) { + connected = false; + } + } + return false; + } + + public Int32 receiveSmallNumber() { + if (connected) { + try { + var bts = new Byte[1]; + sock.Receive(bts,1,SocketFlags.None); + String sn = System.Text.Encoding.ASCII.GetString(bts); + return Int32.Parse(sn); + } catch (SocketException e) { + connected = false; + } catch (FormatException e) { + } catch (OverflowException e) { + } + } + return 0; + } + + public Int32 receiveNumber(Int32 lIn) { + Int32 toret = 0; + if (connected) { + try { + int len = lIn; + byte[] bufferIn = new byte[len]; + int pos = 0; + while (pos < len) { + int res = sock.Receive(bufferIn, pos, len - pos,SocketFlags.None); + if (res == -1) { + connected = false; + break; + } else { + pos += res; + connected = true; + } + } + toret = Int32.Parse(System.Text.Encoding.ASCII.GetString(bufferIn)); + } catch (SocketException e) { + connected = false; + toret = 0; + } catch (FormatException e) { + toret = 0; + }catch (OverflowException e) { + toret = 0; + } + } + return toret; + } + + public String receiveData(Int32 lIn) { + String toret = ""; + if (connected) { + try { + int len = lIn; + byte[] bufferIn = new byte[len]; + int pos = 0; + while (pos < len) { + int res = sock.Receive(bufferIn, pos, len - pos,SocketFlags.None); + if (res == -1) { + connected = false; + break; + } else { + pos += res; + connected = true; + } + } + toret = System.Text.Encoding.ASCII.GetString(bufferIn); + } catch (SocketException e) { + connected = false; + toret = ""; + } catch (FormatException e) { + toret = ""; + }catch (OverflowException e) { + toret = ""; + } + } + return toret; + } public void invokeConnectionCheck() { try { diff --git a/OCUploadDownloadServer/OCDaemonHoster/Program.cs b/OCUploadDownloadServer/OCDaemonHoster/Program.cs index 91ef282..0f89db0 100644 --- a/OCUploadDownloadServer/OCDaemonHoster/Program.cs +++ b/OCUploadDownloadServer/OCDaemonHoster/Program.cs @@ -29,7 +29,7 @@ namespace OCDaemonHoster public static List addrsv6; public static void Main(String[] args) { - writeLine("Open Computers Daemon Hoster (OCDH) : (C) Captain ALM 2019."); + writeLine("Open Computers Daemon Hoster (OCDH) : (C) Captain ALM 2020."); writeLine("License: BSD 2-Clause."); addrsv4 = getInterfaceAddresses(4); addrsv6 = getInterfaceAddresses(6); @@ -56,8 +56,6 @@ namespace OCDaemonHoster public static void hoster() { writeLine("Hosting Mode!"); - IPEndPoint address = new IPEndPoint(IPAddress.Parse(ipAddress),port); - writeLine("[INFO] : Address Setup!"); if (settings.ContainsKey("target")) { writeLine("[INFO] : Target File : " + settings["target"]); } @@ -69,6 +67,17 @@ namespace OCDaemonHoster } catch (IOException e) { } } + serverRuntime(); + } + + public static void accessor() { + writeLine("Accessor Mode!"); + serverRuntime(); + } + + public static void serverRuntime() { + IPEndPoint address = new IPEndPoint(IPAddress.Parse(ipAddress),port); + writeLine("[INFO] : Address Setup!"); List wl = new List(); if (settings.ContainsKey("whitelist")) { @@ -120,10 +129,6 @@ namespace OCDaemonHoster server = null; } - public static void accessor() { - throw new NotImplementedException("Method not Implemented."); - } - public static void handleProtocol(OCNetworkClient clientIn) { String prot = clientIn.receiveProtocol(); if (prot.Equals("1")) { @@ -176,6 +181,172 @@ namespace OCDaemonHoster } 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 enr = new List(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"); + } } } @@ -184,8 +355,30 @@ namespace OCDaemonHoster } public static void saveFile(String target, String contents) { + try { + var tp = System.IO.Path.GetDirectoryName(target); + if (! Directory.Exists(tp)) {Directory.CreateDirectory(tp);} + } catch (ArgumentException e) { + } System.IO.File.WriteAllText(target,contents,System.Text.Encoding.ASCII); } + + public static void createFile(String target) { + try { + var tp = System.IO.Path.GetDirectoryName(target); + if (! Directory.Exists(tp)) {Directory.CreateDirectory(tp);} + } catch (ArgumentException e) { + } + using (File.Create(target)); + } + + public static void deleteFile(String target) { + if (File.GetAttributes(target).HasFlag(FileAttributes.Directory)) { + System.IO.Directory.Delete(target); + } else { + System.IO.File.Delete(target); + } + } public static void decryptArgs(String[] args) { try { @@ -291,7 +484,7 @@ namespace OCDaemonHoster writeLine(""); writeLine("Usage:"); writeLine( - "OCDH.exe [-mode=] [-whitelist=] [-target=] [-cache] [-enumeration] [-creation] [-deletion]"); + "OCDH.exe [-mode=] [-whitelist=] [-target=] [-cache] [-enumeration] [-creation] [-deletion] [-writeonly] [-readonly]"); writeLine(""); writeLine("-mode= : allows to select a Hosting Mode."); writeLine("-whitelist= : allows IP Address to connect, if there is no whitelist switch then any IP Address can connect."); @@ -300,6 +493,8 @@ namespace OCDaemonHoster writeLine("-enumeration : allows for file/directory enumeration (File Access Mode Only)."); writeLine("-creation : allows for file/directory creation (File Access Mode Only)."); writeLine("-deletion : allows for file/directory deletion (File Access Mode Only)."); + writeLine("-readonly : disallows write access for files (File Access Mode Only)."); + writeLine("-writeonly : disallows read access for files (File Access Mode Only)."); writeLine(""); writeLine("MODE:"); writeLine("H : File Host Mode, Hosts a single file for access."); diff --git a/OCUploadDownloadServer/OCDaemonHoster/Properties/AssemblyInfo.cs b/OCUploadDownloadServer/OCDaemonHoster/Properties/AssemblyInfo.cs index 19df51b..12ddee5 100644 --- a/OCUploadDownloadServer/OCDaemonHoster/Properties/AssemblyInfo.cs +++ b/OCUploadDownloadServer/OCDaemonHoster/Properties/AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Captain ALM")] [assembly: AssemblyProduct("Open Computers Daemon Hoster")] -[assembly: AssemblyCopyright("Copyright (C) Captain ALM 2019")] +[assembly: AssemblyCopyright("Copyright (C) Captain ALM 2020")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -28,4 +28,4 @@ using System.Runtime.InteropServices; // // You can specify all the values or you can use the default the Revision and // Build Numbers by using the '*' as shown below: -[assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("2.0.*")] diff --git a/create.lua b/create.lua new file mode 100644 index 0000000..72da6b0 --- /dev/null +++ b/create.lua @@ -0,0 +1,166 @@ +local ic = require("internet") +local c = require("component") +local shell = require("shell") +local fs = require("filesystem") + +io.write("create Utility (C) Captain ALM 2020 :: BSD 2-Clause\n") + +if not c.isAvailable("internet") then + io.stderr:write("Internet Card Required!\n") + return 2 +end + +local i = c.internet + +if(i == nil or ic == nil) then + io.stderr:write("Internet Component/API Required!\n") + return 2 +end +if(not i.isTcpEnabled()) then + io.stderr:write("TCP Support Required!\n") + return 2 +end +local args, options = shell.parse(...) +local ipaddress = "127.0.0.1" +local port = 100 +if #args < 1 then + io.write("Usage: create \n") + return +elseif #args == 1 then + ipaddress = "127.0.0.1" + port = 100 +elseif #args == 2 then + ipaddress = args[2] + port = 100 +elseif #args >= 3 then + ipaddress = args[2] + port = math.floor(math.abs(args[3])) +end + +function catch(block) + return block[1] +end + +function try(block) + status, result = pcall(block[1]) + if not status then + block[2](result) + end + return result +end + +local function shandshake(con,val) + print("Sending Handshake...\n") + con:write(val) + return nil + end + +local function rhandshake(con) + local ret = nil + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = nil + end + } + } + if ret == nil then + print("Handshake Not Received!\n") + return false + else + if ret == "" or ret == "0" then + print("Handshake Not Received!\n") + return false + else + print("Handshake Received...\n") + return true + end + end +end + +local function senddata(con,data) + local datlen = tostring(string.len(data)) + con:write(tostring(string.len(datlen))) + con:write(datlen) + con:write(data) +end + +local function recsmallnum(con) + local ret = 0 + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = 0 + end + } + } + return ret + end + +local function recdata(con,datlen) + local ret = "" + try { + function() + ret = tostring(con:read(datlen)) + end, + catch { + function(ex) + ret = "" + end + } + } + if (ret == "" and datlen > 0) then + return nil + else + return ret + end +end + +local function executer(connection) + connection:setTimeout(5) + shandshake(connection,"3") + if not rhandshake(connection) then + connection:close() + return 1 + end + shandshake(connection,"3") + if not rhandshake(connection) then + connection:close() + return 1 + end + print("Sending File Name...\n") + senddata(connection,args[1]) + if not rhandshake(connection) then + connection:close() + return 1 + end +end + +print("Opening Connection!\n") +local connection = ic.open(ipaddress, port) + +if connection then + try { + function() + executer(connection) + end, + catch { + function(ex) + io.stderr:write("Error Caught: "..ex..".\n") + end + } + } + ::ci:: + print("Terminating...\n") + connection:close() + return +else + io.stderr:write("Connection Failed!\n") + return 1 +end \ No newline at end of file diff --git a/createdir.lua b/createdir.lua new file mode 100644 index 0000000..8e3ebc5 --- /dev/null +++ b/createdir.lua @@ -0,0 +1,166 @@ +local ic = require("internet") +local c = require("component") +local shell = require("shell") +local fs = require("filesystem") + +io.write("createdir Utility (C) Captain ALM 2020 :: BSD 2-Clause\n") + +if not c.isAvailable("internet") then + io.stderr:write("Internet Card Required!\n") + return 2 +end + +local i = c.internet + +if(i == nil or ic == nil) then + io.stderr:write("Internet Component/API Required!\n") + return 2 +end +if(not i.isTcpEnabled()) then + io.stderr:write("TCP Support Required!\n") + return 2 +end +local args, options = shell.parse(...) +local ipaddress = "127.0.0.1" +local port = 100 +if #args < 1 then + io.write("Usage: createdir \n") + return +elseif #args == 1 then + ipaddress = "127.0.0.1" + port = 100 +elseif #args == 2 then + ipaddress = args[2] + port = 100 +elseif #args >= 3 then + ipaddress = args[2] + port = math.floor(math.abs(args[3])) +end + +function catch(block) + return block[1] +end + +function try(block) + status, result = pcall(block[1]) + if not status then + block[2](result) + end + return result +end + +local function shandshake(con,val) + print("Sending Handshake...\n") + con:write(val) + return nil + end + +local function rhandshake(con) + local ret = nil + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = nil + end + } + } + if ret == nil then + print("Handshake Not Received!\n") + return false + else + if ret == "" or ret == "0" then + print("Handshake Not Received!\n") + return false + else + print("Handshake Received...\n") + return true + end + end +end + +local function senddata(con,data) + local datlen = tostring(string.len(data)) + con:write(tostring(string.len(datlen))) + con:write(datlen) + con:write(data) +end + +local function recsmallnum(con) + local ret = 0 + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = 0 + end + } + } + return ret + end + +local function recdata(con,datlen) + local ret = "" + try { + function() + ret = tostring(con:read(datlen)) + end, + catch { + function(ex) + ret = "" + end + } + } + if (ret == "" and datlen > 0) then + return nil + else + return ret + end +end + +local function executer(connection) + connection:setTimeout(5) + shandshake(connection,"3") + if not rhandshake(connection) then + connection:close() + return 1 + end + shandshake(connection,"6") + if not rhandshake(connection) then + connection:close() + return 1 + end + print("Sending Directory Name...\n") + senddata(connection,args[1]) + if not rhandshake(connection) then + connection:close() + return 1 + end +end + +print("Opening Connection!\n") +local connection = ic.open(ipaddress, port) + +if connection then + try { + function() + executer(connection) + end, + catch { + function(ex) + io.stderr:write("Error Caught: "..ex..".\n") + end + } + } + ::ci:: + print("Terminating...\n") + connection:close() + return +else + io.stderr:write("Connection Failed!\n") + return 1 +end \ No newline at end of file diff --git a/delete.lua b/delete.lua new file mode 100644 index 0000000..3fe8446 --- /dev/null +++ b/delete.lua @@ -0,0 +1,166 @@ +local ic = require("internet") +local c = require("component") +local shell = require("shell") +local fs = require("filesystem") + +io.write("delete Utility (C) Captain ALM 2020 :: BSD 2-Clause\n") + +if not c.isAvailable("internet") then + io.stderr:write("Internet Card Required!\n") + return 2 +end + +local i = c.internet + +if(i == nil or ic == nil) then + io.stderr:write("Internet Component/API Required!\n") + return 2 +end +if(not i.isTcpEnabled()) then + io.stderr:write("TCP Support Required!\n") + return 2 +end +local args, options = shell.parse(...) +local ipaddress = "127.0.0.1" +local port = 100 +if #args < 1 then + io.write("Usage: delete \n") + return +elseif #args == 1 then + ipaddress = "127.0.0.1" + port = 100 +elseif #args == 2 then + ipaddress = args[2] + port = 100 +elseif #args >= 3 then + ipaddress = args[2] + port = math.floor(math.abs(args[3])) +end + +function catch(block) + return block[1] +end + +function try(block) + status, result = pcall(block[1]) + if not status then + block[2](result) + end + return result +end + +local function shandshake(con,val) + print("Sending Handshake...\n") + con:write(val) + return nil + end + +local function rhandshake(con) + local ret = nil + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = nil + end + } + } + if ret == nil then + print("Handshake Not Received!\n") + return false + else + if ret == "" or ret == "0" then + print("Handshake Not Received!\n") + return false + else + print("Handshake Received...\n") + return true + end + end +end + +local function senddata(con,data) + local datlen = tostring(string.len(data)) + con:write(tostring(string.len(datlen))) + con:write(datlen) + con:write(data) +end + +local function recsmallnum(con) + local ret = 0 + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = 0 + end + } + } + return ret + end + +local function recdata(con,datlen) + local ret = "" + try { + function() + ret = tostring(con:read(datlen)) + end, + catch { + function(ex) + ret = "" + end + } + } + if (ret == "" and datlen > 0) then + return nil + else + return ret + end +end + +local function executer(connection) + connection:setTimeout(5) + shandshake(connection,"3") + if not rhandshake(connection) then + connection:close() + return 1 + end + shandshake(connection,"4") + if not rhandshake(connection) then + connection:close() + return 1 + end + print("Sending Path...\n") + senddata(connection,args[1]) + if not rhandshake(connection) then + connection:close() + return 1 + end +end + +print("Opening Connection!\n") +local connection = ic.open(ipaddress, port) + +if connection then + try { + function() + executer(connection) + end, + catch { + function(ex) + io.stderr:write("Error Caught: "..ex..".\n") + end + } + } + ::ci:: + print("Terminating...\n") + connection:close() + return +else + io.stderr:write("Connection Failed!\n") + return 1 +end \ No newline at end of file diff --git a/enumerate.lua b/enumerate.lua new file mode 100644 index 0000000..c5ae614 --- /dev/null +++ b/enumerate.lua @@ -0,0 +1,204 @@ +local ic = require("internet") +local c = require("component") +local shell = require("shell") +local fs = require("filesystem") + +io.write("enumerate Utility (C) Captain ALM 2020 :: BSD 2-Clause\n") + +if not c.isAvailable("internet") then + io.stderr:write("Internet Card Required!\n") + return 2 +end + +local i = c.internet + +if(i == nil or ic == nil) then + io.stderr:write("Internet Component/API Required!\n") + return 2 +end +if(not i.isTcpEnabled()) then + io.stderr:write("TCP Support Required!\n") + return 2 +end +local args, options = shell.parse(...) +local filename = "" +local file_parentpath = "" +local ipaddress = "127.0.0.1" +local port = 100 +if #args < 2 then + io.write("Usage: enumerate \n") + return +elseif #args == 2 then + ipaddress = "127.0.0.1" + port = 100 +elseif #args == 3 then + ipaddress = args[3] + port = 100 +elseif #args >= 4 then + ipaddress = args[3] + port = math.floor(math.abs(args[4])) +end + +filename = shell.resolve(args[1]) +file_parentpath = fs.path(filename) + +if fs.exists(file_parentpath) and not fs.isDirectory(file_parentpath) then + io.stderr:write("Invalid Folder Path!\n") + return 1 +end + +if fs.isDirectory(filename) then + io.stderr:write("File is a directory!\n") + return 1 +end + +function catch(block) + return block[1] +end + +function try(block) + status, result = pcall(block[1]) + if not status then + block[2](result) + end + return result +end + +local function shandshake(con,val) + print("Sending Handshake...\n") + con:write(val) + return nil + end + +local function rhandshake(con) + local ret = nil + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = nil + end + } + } + if ret == nil then + print("Handshake Not Received!\n") + return false + else + if ret == "" or ret == "0" then + print("Handshake Not Received!\n") + return false + else + print("Handshake Received...\n") + return true + end + end +end + +local function senddata(con,data) + local datlen = tostring(string.len(data)) + con:write(tostring(string.len(datlen))) + con:write(datlen) + con:write(data) +end + +local function recsmallnum(con) + local ret = 0 + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = 0 + end + } + } + return ret + end + +local function recdata(con,datlen) + local ret = "" + try { + function() + ret = tostring(con:read(datlen)) + end, + catch { + function(ex) + ret = "" + end + } + } + if (ret == "" and datlen > 0) then + return nil + else + return ret + end +end + +local function executer(connection) + connection:setTimeout(5) + shandshake(connection,"3") + if not rhandshake(connection) then + connection:close() + return 1 + end + shandshake(connection,"5") + if not rhandshake(connection) then + connection:close() + return 1 + end + print("Sending File Name...\n") + senddata(connection,args[2]) + shandshake(connection,"1") + print("Waiting For Data...\n") + local lld = math.floor(math.abs(recsmallnum(connection))) + local ld = math.floor(math.abs(recdata(connection,lld))) + local data = recdata(connection,ld) + if data == nil then + print("Message Not Received!\n") + connection:close() + return 1 + else + print("Message Received!\n") + end + print("Writing File...\n") + if not fs.exists(file_parentpath) then + fs.makeDirectory(file_parentpath) + end + local f, reason = io.open(filename, "w") + if f then + f:write(data) + f:flush() + f:close() + else + print("File Write Failed!\n") + connection:close() + return 1 + end + shandshake(connection,"1") +end + +print("Opening Connection!\n") +local connection = ic.open(ipaddress, port) + +if connection then + try { + function() + executer(connection) + end, + catch { + function(ex) + io.stderr:write("Error Caught: "..ex..".\n") + end + } + } + ::ci:: + print("Terminating...\n") + connection:close() + return +else + io.stderr:write("Connection Failed!\n") + return 1 +end \ No newline at end of file diff --git a/get.lua b/get.lua new file mode 100644 index 0000000..93b60ee --- /dev/null +++ b/get.lua @@ -0,0 +1,204 @@ +local ic = require("internet") +local c = require("component") +local shell = require("shell") +local fs = require("filesystem") + +io.write("get Utility (C) Captain ALM 2020 :: BSD 2-Clause\n") + +if not c.isAvailable("internet") then + io.stderr:write("Internet Card Required!\n") + return 2 +end + +local i = c.internet + +if(i == nil or ic == nil) then + io.stderr:write("Internet Component/API Required!\n") + return 2 +end +if(not i.isTcpEnabled()) then + io.stderr:write("TCP Support Required!\n") + return 2 +end +local args, options = shell.parse(...) +local filename = "" +local file_parentpath = "" +local ipaddress = "127.0.0.1" +local port = 100 +if #args < 2 then + io.write("Usage: get \n") + return +elseif #args == 2 then + ipaddress = "127.0.0.1" + port = 100 +elseif #args == 3 then + ipaddress = args[3] + port = 100 +elseif #args >= 4 then + ipaddress = args[3] + port = math.floor(math.abs(args[4])) +end + +filename = shell.resolve(args[1]) +file_parentpath = fs.path(filename) + +if fs.exists(file_parentpath) and not fs.isDirectory(file_parentpath) then + io.stderr:write("Invalid Folder Path!\n") + return 1 +end + +if fs.isDirectory(filename) then + io.stderr:write("File is a directory!\n") + return 1 +end + +function catch(block) + return block[1] +end + +function try(block) + status, result = pcall(block[1]) + if not status then + block[2](result) + end + return result +end + +local function shandshake(con,val) + print("Sending Handshake...\n") + con:write(val) + return nil + end + +local function rhandshake(con) + local ret = nil + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = nil + end + } + } + if ret == nil then + print("Handshake Not Received!\n") + return false + else + if ret == "" or ret == "0" then + print("Handshake Not Received!\n") + return false + else + print("Handshake Received...\n") + return true + end + end +end + +local function senddata(con,data) + local datlen = tostring(string.len(data)) + con:write(tostring(string.len(datlen))) + con:write(datlen) + con:write(data) +end + +local function recsmallnum(con) + local ret = 0 + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = 0 + end + } + } + return ret + end + +local function recdata(con,datlen) + local ret = "" + try { + function() + ret = tostring(con:read(datlen)) + end, + catch { + function(ex) + ret = "" + end + } + } + if (ret == "" and datlen > 0) then + return nil + else + return ret + end +end + +local function executer(connection) + connection:setTimeout(5) + shandshake(connection,"3") + if not rhandshake(connection) then + connection:close() + return 1 + end + shandshake(connection,"1") + if not rhandshake(connection) then + connection:close() + return 1 + end + print("Sending File Name...\n") + senddata(connection,args[2]) + shandshake(connection,"1") + print("Waiting For Data...\n") + local lld = math.floor(math.abs(recsmallnum(connection))) + local ld = math.floor(math.abs(recdata(connection,lld))) + local data = recdata(connection,ld) + if data == nil then + print("Message Not Received!\n") + connection:close() + return 1 + else + print("Message Received!\n") + end + print("Writing File...\n") + if not fs.exists(file_parentpath) then + fs.makeDirectory(file_parentpath) + end + local f, reason = io.open(filename, "w") + if f then + f:write(data) + f:flush() + f:close() + else + print("File Write Failed!\n") + connection:close() + return 1 + end + shandshake(connection,"1") +end + +print("Opening Connection!\n") +local connection = ic.open(ipaddress, port) + +if connection then + try { + function() + executer(connection) + end, + catch { + function(ex) + io.stderr:write("Error Caught: "..ex..".\n") + end + } + } + ::ci:: + print("Terminating...\n") + connection:close() + return +else + io.stderr:write("Connection Failed!\n") + return 1 +end \ No newline at end of file diff --git a/put.lua b/put.lua new file mode 100644 index 0000000..89ef0ff --- /dev/null +++ b/put.lua @@ -0,0 +1,221 @@ +local ic = require("internet") +local c = require("component") +local shell = require("shell") +local fs = require("filesystem") + +io.write("put Utility (C) Captain ALM 2020 :: BSD 2-Clause\n") + +if not c.isAvailable("internet") then + io.stderr:write("Internet Card Required!\n") + return 2 +end + +local i = c.internet + +if(i == nil or ic == nil) then + io.stderr:write("Internet Component/API Required!\n") + return 2 +end +if(not i.isTcpEnabled()) then + io.stderr:write("TCP Support Required!\n") + return 2 +end +local args, options = shell.parse(...) +local filename = "" +local file_parentpath = "" +local ipaddress = "127.0.0.1" +local port = 100 +if #args < 2 then + io.write("Usage: put \n") + return +elseif #args == 2 then + ipaddress = "127.0.0.1" + port = 100 +elseif #args == 3 then + ipaddress = args[3] + port = 100 +elseif #args >= 4 then + ipaddress = args[3] + port = math.floor(math.abs(args[4])) +end + +filename = shell.resolve(args[1]) +file_parentpath = fs.path(filename) + +if fs.exists(file_parentpath) and not fs.isDirectory(file_parentpath) then + io.stderr:write("Invalid Folder Path!\n") + return 1 +end + +if fs.isDirectory(filename) then + io.stderr:write("File is a directory!\n") + return 1 +end + +function catch(block) + return block[1] +end + +function try(block) + status, result = pcall(block[1]) + if not status then + block[2](result) + end + return result +end + +local function shandshake(con,val) + print("Sending Handshake...\n") + con:write(val) + return nil + end + +local function rhandshake(con) + local ret = nil + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = nil + end + } + } + if ret == nil then + print("Handshake Not Received!\n") + return false + else + if ret == "" or ret == "0" then + print("Handshake Not Received!\n") + return false + else + print("Handshake Received...\n") + return true + end + end +end + +local function senddata(con,data) + local datlen = tostring(string.len(data)) + con:write(tostring(string.len(datlen))) + con:write(datlen) + con:write(data) +end + +local function recsmallnum(con) + local ret = 0 + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = 0 + end + } + } + return ret + end + +local function recdata(con,datlen) + local ret = "" + try { + function() + ret = tostring(con:read(datlen)) + end, + catch { + function(ex) + ret = "" + end + } + } + if (ret == "" and datlen > 0) then + return nil + else + return ret + end +end + +local function rmsgs(con) + local ret = nil + try { + function() + ret = con:read(1) + end, + catch { + function(ex) + ret = nil + end + } + } + if ret == nil then + print("Message Failed!\n") + return false + else + if ret == "" or ret == "0" then + print("Message Failed!\n") + return false + else + print("Message Succeeded!\n") + return true + end + end +end + +local function executer(connection) + connection:setTimeout(5) + shandshake(connection,"3") + if not rhandshake(connection) then + connection:close() + return 1 + end + shandshake(connection,"2") + if not rhandshake(connection) then + connection:close() + return 1 + end + print("Sending File Name...\n") + senddata(connection,args[2]) + if not rhandshake(connection) then + connection:close() + return 1 + end + print("Reading File...\n") + local f, reason = io.open(filename, "r") + local data = "" + if f then + data = f:read("*a") + f:close() + else + print("File Read Failed!\n") + connection:close() + return 1 + end + print("Sending Data...\n") + senddata(connection,data) + rmsgs(connection) +end + +print("Opening Connection!\n") +local connection = ic.open(ipaddress, port) + +if connection then + try { + function() + executer(connection) + end, + catch { + function(ex) + io.stderr:write("Error Caught: "..ex..".\n") + end + } + } + ::ci:: + print("Terminating...\n") + connection:close() + return +else + io.stderr:write("Connection Failed!\n") + return 1 +end \ No newline at end of file