· 6 years ago · Apr 26, 2020, 04:42 AM
1local net = require "internet"
2local c= require "component"
3local fs= require "filesystem"
4local shell= require "shell"
5local open=io.open
6host ="10.0.0.14:8080" --no HTTP and no slashes after ip
7local export={} -- you can require "nodebin" in other scripts
8local shellargs= shell.parse(...)
9local combined=""
10
11--Example API calls from other scripts
12--export.get("xx4455","myfile.lua")
13--local key = export.put('myfile.lua')
14
15function encode(code,chunk)
16 if chunk then print("Encoding data, chunk "..chunk)
17 else print("Encoding data...") end
18 if code then
19 if string.find(code, "%%") ~= nil then code = string.gsub(code, "%%","JQ==") end
20 code = string.gsub(code, "([^%w ])", function (c)
21 return string.format("%%%02X", string.byte(c))
22 end)
23 code = string.gsub(code, " ", "+")
24
25 end
26 return code
27 end
28
29function clean_encoding(code)
30code = string.gsub(code, "JQ==", "%%")
31code = string.gsub(code, "%%0A", "\n")
32code = string.gsub(code, "%%22", '"')
33code = string.gsub(code, "%%5F", '_')
34code = string.gsub(code, "%%28", '(')
35code = string.gsub(code, "%%2C", ',')
36code = string.gsub(code, "%%2D", '-')
37code = string.gsub(code, "%%2B", '+')
38code = string.gsub(code, "%%5D", ']')
39return code
40end
41
42
43function export.get(token,filename)
44if not token then token="" end
45if not filename then filename=token end
46if not c.isAvailable("internet") then
47 io.stderr:write("This program requires an internet card to run.")
48 return
49else
50 if fs.exists(filename) then shell.execute("del "..filename) end
51 local f = open(filename,"w")
52 local datanum=1
53 local lastnumdata=0
54 for data in net.request("http://"..host.."/?type=GET&body="..token) do
55 if data == "invalid_token" or data == "invalid_request" or data == nil or data == "" then
56 print(data.." Aborting...")
57 else
58 print("Downloading Chunk "..datanum)
59 datanum=datanum+1
60 f:write(clean_encoding(data))
61 lastnumdata=#data
62 end
63 local mem=0
64 local byte="bytes"
65 local total=(2048*(datanum-1))+lastnumdata
66 if(total > 1000) then
67 mem=total/1000
68 byte="KB"
69 elseif(total > 1000000) then
70 mem=total/1000000
71 byte="MB"
72 else mem=#data end
73
74 end
75 f:close()
76 if mem and byte then print("-> Recieved Response "..mem.." "..byte) end
77 print(fs.size(shell.resolve(filename)).." bytes written to "..filename)
78 if(alias) then print("Also saved as alias token "..alias) end
79 end
80end
81
82function export.put(filename,alias)
83 local rawsize=fs.size(shell.resolve(filename))
84 local filesize=math.ceil(rawsize/1000)
85 local chunk_total = math.ceil(filesize/2)
86 netdata="";
87 print("Rawsize is "..rawsize)
88 print("Chunksize is "..chunk_total)
89 if filename then
90 filename=shell.resolve(filename)
91 if not c.isAvailable("internet") then
92 io.stderr:write("This program requires an internet card to run.")
93 return
94 elseif fs.exists(filename) and chunk_total == 1 then
95 local f = io.open(filename,"r")
96 local data = f:read("*all")
97 f:close()
98
99 local d2 = encode(data)
100
101 combined_request="http://"..host.."/?type=PUT&body="..d2
102 if alias then combined_request=combined_request.."&alias="..alias end
103 local resp = net.request(combined_request)
104 tt1 = resp.read() --Reads http header
105 netdata = resp.read() --reads message body
106 tt2 = resp.read() -- Ends / the connection
107 if netdata == "invalid_token" or netdata == "invalid_request" or netdata == nil then
108 print("--------------------------------------")
109 local err = io.open("error.log","w")
110 err:write("FILESIZE: \n"..filesize.." KB\n")
111 err:write("DATA\n-------------------------\n")
112 err:write(d2)
113 err:close()
114 print("\n Invalid Response -> Aborting...")
115 print("\nCheck error.log for request that failed")
116 print("---------------------------------------")
117 else
118 local mem=0
119 local byte="bytes"
120 if(#data > 1000) then mem=#data/1000 byte="KB"
121 elseif(#data > 1000000) then mem=#data/1000000 byte ="MB"
122 else mem=#data end
123 print("Uploaded "..mem.." "..byte.." as token "..netdata)
124 if alias then print("Saved additional token as alias "..alias) end
125 return netdata
126 end
127 elseif not fs.exists(filename) then
128 print("Invalid Filename given as input")
129 elseif fs.exists(filename) and chunk_total > 1 then
130 local f = io.open(filename,"r")
131 local data = f:read("*all")
132 f:close()
133 local quarter=math.ceil(chunk_total/4)
134 local d2=""
135 for i=1,chunk_total do os.sleep(.01) d2=d2..encode(string.sub(data,(2000*(i-1)), (i*2000)-1),i) end
136
137 rawsize=string.len(d2)
138 filesize=math.ceil(rawsize/1000)
139 chunk_total = math.ceil(filesize/2)
140 combined_request="http://"..host.."/?type=PUTCHUNK&chunknum=1&chunktotal="..chunk_total.."&body="..string.sub(d2,0, 1999)
141 if alias then combined_request=combined_request.."&alias="..alias end
142 local resp = net.request(combined_request)
143 local key = resp.read() if key==nil or key=="" then key=resp.read() end if key==nil or key=="" then key=resp.read() end
144 print("RETURNED KEY: "..key)
145 if key==nil or key=="" or string.sub(key,1,14)=="Not Authorized" then print("ERROR: Failed to return PUT key!")
146 return nil
147 else
148 for z=2,chunk_total do
149 local val=(z*2000)-1
150 if( val >= rawsize ) then val = rawsize end
151 local clip=string.sub(d2,(z-1)*2000, val)
152 combined_request = "http://"..host.."/?type=PUTCHUNK&chunknum="..tostring(z).."&key="..key.."&chunktotal="..chunk_total.."&body="..clip
153 if alias then combined_request=combined_request.."&alias="..alias end
154 local scan = net.request(combined_request)
155 resp.read() resp.read() resp.read()
156 os.sleep(.01)
157 print("Chunk Head "..val)
158 print("Uploding chunk -> "..z.." / "..chunk_total)
159 end
160 print("Uploaded "..filesize.." KB as token "..key)
161 if(alias) then print("Saved additional token as alias '"..alias.."'") end
162 return key
163 end
164 end
165
166
167 else print("No filename specified. Aborting...") return nil
168 end
169 end
170
171function export.run(code)
172export.get(code,"/tmp/.tmprun")
173local file=io.open("/tmp/.tmprun","r")
174local txt=file:read("*all")
175file:close()
176load(txt)()
177end
178
179
180 if #shellargs > 1 then
181 for i=1, #shellargs do combined=combined..shellargs[i].." " end
182 print("Arguments-> "..combined)
183 if shellargs[1] == "get" and #shellargs==3 or shellargs[1] == "GET" and #shellargs==3 then export.get(shellargs[2],shellargs[3])
184 elseif shellargs[1] == "put" or shellargs[1] == "PUT" then export.put(shellargs[2],shellargs[3])
185 elseif shellargs[1] == "run" or shellargs[1] == "RUN" then export.run(shellargs[2])
186 else print("Unrecognized command "..combined) print("Valid Options are, put, get or run") end
187 end
188
189
190return export