Add support for accessor.

This commit is contained in:
Captain ALM 2020-04-02 14:08:46 +01:00
parent 68f047c397
commit 0bdbe32c30
9 changed files with 1436 additions and 10 deletions

View File

@ -138,6 +138,110 @@ namespace captainalm.network.oc
} }
return toret; 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() { public void invokeConnectionCheck() {
try { try {

View File

@ -29,7 +29,7 @@ namespace OCDaemonHoster
public static List<String> addrsv6; public static List<String> addrsv6;
public static void Main(String[] args) { 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."); writeLine("License: BSD 2-Clause.");
addrsv4 = getInterfaceAddresses(4); addrsv4 = getInterfaceAddresses(4);
addrsv6 = getInterfaceAddresses(6); addrsv6 = getInterfaceAddresses(6);
@ -56,8 +56,6 @@ namespace OCDaemonHoster
public static void hoster() { public static void hoster() {
writeLine("Hosting Mode!"); writeLine("Hosting Mode!");
IPEndPoint address = new IPEndPoint(IPAddress.Parse(ipAddress),port);
writeLine("[INFO] : Address Setup!");
if (settings.ContainsKey("target")) { if (settings.ContainsKey("target")) {
writeLine("[INFO] : Target File : " + settings["target"]); writeLine("[INFO] : Target File : " + settings["target"]);
} }
@ -69,6 +67,17 @@ namespace OCDaemonHoster
} catch (IOException e) { } 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<String> wl = new List<String>(); List<String> wl = new List<String>();
if (settings.ContainsKey("whitelist")) { if (settings.ContainsKey("whitelist")) {
@ -120,10 +129,6 @@ namespace OCDaemonHoster
server = null; server = null;
} }
public static void accessor() {
throw new NotImplementedException("Method not Implemented.");
}
public static void handleProtocol(OCNetworkClient clientIn) { public static void handleProtocol(OCNetworkClient clientIn) {
String prot = clientIn.receiveProtocol(); String prot = clientIn.receiveProtocol();
if (prot.Equals("1")) { if (prot.Equals("1")) {
@ -176,6 +181,172 @@ namespace OCDaemonHoster
} }
writeLine("[INFO] : Receiving : Sending Handshake..."); writeLine("[INFO] : Receiving : Sending Handshake...");
clientIn.sendHandshake("1"); 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");
}
} }
} }
@ -184,8 +355,30 @@ namespace OCDaemonHoster
} }
public static void saveFile(String target, String contents) { 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); 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) { public static void decryptArgs(String[] args) {
try { try {
@ -291,7 +484,7 @@ namespace OCDaemonHoster
writeLine(""); writeLine("");
writeLine("Usage:"); writeLine("Usage:");
writeLine( writeLine(
"OCDH.exe <listening IP Address> <listening Port> [-mode=<MODE>] [-whitelist=<IP Address [Seperated By ,]>] [-target=<target file path>] [-cache] [-enumeration] [-creation] [-deletion]"); "OCDH.exe <listening IP Address> <listening Port> [-mode=<MODE>] [-whitelist=<IP Address [Seperated By ,]>] [-target=<target file path>] [-cache] [-enumeration] [-creation] [-deletion] [-writeonly] [-readonly]");
writeLine(""); writeLine("");
writeLine("-mode=<MODE> : allows to select a Hosting Mode."); writeLine("-mode=<MODE> : allows to select a Hosting Mode.");
writeLine("-whitelist=<IP Address [Seperated By ,]> : allows IP Address to connect, if there is no whitelist switch then any IP Address can connect."); writeLine("-whitelist=<IP Address [Seperated By ,]> : 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("-enumeration : allows for file/directory enumeration (File Access Mode Only).");
writeLine("-creation : allows for file/directory creation (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("-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("");
writeLine("MODE:"); writeLine("MODE:");
writeLine("H : File Host Mode, Hosts a single file for access."); writeLine("H : File Host Mode, Hosts a single file for access.");

View File

@ -14,7 +14,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Captain ALM")] [assembly: AssemblyCompany("Captain ALM")]
[assembly: AssemblyProduct("Open Computers Daemon Hoster")] [assembly: AssemblyProduct("Open Computers Daemon Hoster")]
[assembly: AssemblyCopyright("Copyright (C) Captain ALM 2019")] [assembly: AssemblyCopyright("Copyright (C) Captain ALM 2020")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [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 // You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below: // Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("2.0.*")]

166
create.lua Normal file
View File

@ -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 <remote filename> <ipaddress> <port>\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

166
createdir.lua Normal file
View File

@ -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 <remote directory name> <ipaddress> <port>\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

166
delete.lua Normal file
View File

@ -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 <remote file/folder name> <ipaddress> <port>\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

204
enumerate.lua Normal file
View File

@ -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 <local output file> <remote file/folder name> <ipaddress> <port>\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

204
get.lua Normal file
View File

@ -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 <local filename> <remote filename> <ipaddress> <port>\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

221
put.lua Normal file
View File

@ -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 <local filename> <remote filename> <ipaddress> <port>\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