· 5 years ago · Nov 29, 2020, 02:34 PM
1version = "v0.4"
2-- RUNNING ON BEDROCK GUI API ---------------------------------------------------------------------------------------------------------
3local bedrockPath='/' if OneOS then OneOS.LoadAPI('/System/API/Bedrock.lua', false)elseif fs.exists(bedrockPath..'/Bedrock')then os.loadAPI(bedrockPath..'/Bedrock')else if http then print('Downloading Bedrock...')local h=http.get('http://pastebin.com/raw.php?i=0MgKNqpN')if h then local f=fs.open(bedrockPath..'/Bedrock','w')f.write(h.readAll())f.close()h.close()os.loadAPI(bedrockPath..'/Bedrock')else error('Failed to download Bedrock. Is your internet working?') end else error('This program needs to download Bedrock to work. Please enable HTTP.') end end if Bedrock then Bedrock.BasePath = bedrockPath Bedrock.ProgramPath = shell.getRunningProgram() end
4-- ENCRYPTION -------------------------------------------------------------------------------------------------------------------------
5-- Encryption code created by "Signature Abuser" on the computer craft forums, https://pastebin.com/WRTfH0yx
6local function zfill(N)
7 N=string.format("%X",N)
8 Zs=""
9 if #N==1 then
10 Zs="0"
11 end
12 return Zs..N
13end
14
15local function serializeImpl(t)
16 local sType = type(t)
17 if sType == "table" then
18 local lstcnt=0
19 for k,v in pairs(t) do
20 lstcnt = lstcnt + 1
21 end
22 local result = "{"
23 local aset=1
24 for k,v in pairs(t) do
25 if k==aset then
26 result = result..serializeImpl(v)..","
27 aset=aset+1
28 else
29 result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
30 end
31 end
32 result = result.."}"
33 return result
34 elseif sType == "string" then
35 return string.format("%q",t)
36 elseif sType == "number" or sType == "boolean" or sType == "nil" then
37 return tostring(t)
38 elseif sType == "function" then
39 local status,data=pcall(string.dump,t)
40 if status then
41 return 'func('..string.format("%q",data)..')'
42 else
43 error()
44 end
45 else
46 error()
47 end
48end
49
50local function split(T,func)
51 if func then
52 T=func(T)
53 end
54 local Out={}
55 if type(T)=="table" then
56 for k,v in pairs(T) do
57 Out[split(k)]=split(v)
58 end
59 else
60 Out=T
61 end
62 return Out
63end
64
65local function serialize( t )
66 t=split(t)
67 return serializeImpl( t, tTracking )
68end
69
70local function unserialize( s )
71 local func, e = loadstring( "return "..s, "serialize" )
72 local funcs={}
73 if not func then
74 return e
75 end
76 setfenv( func, {
77 func=function(S)
78 local new={}
79 funcs[new]=S
80 return new
81 end,
82 })
83 return split(func(),function(val)
84 if funcs[val] then
85 return loadstring(funcs[val])
86 else
87 return val
88 end
89 end)
90end
91
92local function sure(N,n)
93 if (l2-n)<1 then N="0" end
94 return N
95end
96
97local function splitnum(S)
98 Out=""
99 for l1=1,#S,2 do
100 l2=(#S-l1)+1
101 CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
102 Out=string.char(CNum)..Out
103 end
104 return Out
105end
106
107local function wrap(N)
108 return N-(math.floor(N/256)*256)
109end
110
111function checksum(S,num)
112 local sum=0
113 for char in string.gmatch(S,".") do
114 for l1=1,(num or 1) do
115 math.randomseed(string.byte(char)+sum)
116 sum=sum+math.random(0,9999)
117 end
118 end
119 math.randomseed(sum)
120 return sum
121end
122
123local function genkey(len,psw)
124 checksum(psw)
125 local key={}
126 local tKeys={}
127 for l1=1,len do
128 local num=math.random(1,len)
129 while tKeys[num] do
130 num=math.random(1,len)
131 end
132 tKeys[num]=true
133 key[l1]={num,math.random(0,255)}
134 end
135 return key
136end
137
138function encrypt(data,psw)
139 data=serialize(data)
140 local chs=checksum(data)
141 local key=genkey(#data,psw)
142 local out={}
143 local cnt=1
144 for char in string.gmatch(data,".") do
145 table.insert(out,key[cnt][1],zfill(wrap(string.byte(char)+key[cnt][2])),chars)
146 cnt=cnt+1
147 end
148 return string.sub(serialize({chs,table.concat(out)}),2,-3)
149end
150
151function decrypt(data,psw)
152 local oData=data
153 data=unserialize("{"..data.."}")
154 if type(data)~="table" then
155 return oData
156 end
157 local chs=data[1]
158 data=data[2]
159 local key=genkey((#data)/2,psw)
160 local sKey={}
161 for k,v in pairs(key) do
162 sKey[v[1]]={k,v[2]}
163 end
164 local str=splitnum(data)
165 local cnt=1
166 local out={}
167 for char in string.gmatch(str,".") do
168 table.insert(out,sKey[cnt][1],string.char(wrap(string.byte(char)-sKey[cnt][2])))
169 cnt=cnt+1
170 end
171 out=table.concat(out)
172 if checksum(out or "")==chs then
173 return unserialize(out)
174 end
175 return oData,out,chs
176end
177-- GUI --------------------------------------------------------------------------------------------------------------------------------
178local w, h = term.getSize()
179
180-- Convenience Functions
181function theme(themeBase)
182 themes = {
183 titleFG = colors.green , titleBG = colors.white,
184 passFG = colors.green , passBG = colors.black,
185 failedFG = colors.red , failedBG = colors.black
186 }
187 term.setBackgroundColor(themes[themeBase.."BG"])
188 term.setTextColor(themes[themeBase.."FG"])
189end
190
191function drawFor(text,number,ypos)
192 for i = 0, number do
193 term.setCursorPos(i,ypos)
194 tostring(text)
195 print(text)
196 end
197end
198
199function printCentered(str,ypos)
200 term.setCursorPos((w-#str) / 2, ypos)
201 print(str)
202end
203
204-- Drawing Functions
205
206function drawHeader()
207 term.clear()
208 theme("title")
209 printCentered(" K.K.O.S. "..version.." ",1)
210 drawFor("=", w, 2)
211end
212
213function drawWindow()
214 theme("title")
215 printCentered(" PLEASE INPUT PASSWORD ",8)
216 printCentered(" ",9)
217 printCentered(" ",10)
218 printCentered(" ",11)
219 printCentered(" ",12)
220end
221
222-- modular text box
223function textbox(lineNumber,lineSpaces,length,protected)
224 theme("pass")
225 term.setCursorPos(lineNumber,lineSpaces)
226 if protected then this="*" else this=nil end
227 input=read(this) -- get the input
228 if string.len(input) > length then
229 return string.sub(input, 1, length) -- return chars from 1 to length
230 else
231 return input -- returns the whole thing
232 end
233end
234-- OPERATING SYSTEM FUNCTIONS ---------------------------------------------------------------------------------------------------------
235function login()
236 term.setBackgroundColor(colors.cyan)
237 term.clear()
238 drawHeader()
239 drawWindow()
240 if textbox(w/4,10,10,true) == password then
241 term.clear()
242 theme("pass")
243 printCentered(" ACCESS GRANTED ",8)
244 else
245 term.clear()
246 theme("failed")
247 printCentered(" ACCESS DENIED ",8)
248 os.sleep(5)
249 login() end
250 os.sleep(1)
251end
252
253function newPassword()
254 print("Please create a password")
255 password = read(this)
256 print("Are you sure you want '"..password.."' to be your password? (Y/N)")
257 while(true) do
258 local event,char = os.pullEvent("char")
259 if char == "y" then
260 term.clearLine()
261 if fs.exists("K/.password") then fs.delete("KKOS/.password") end
262 local file = fs.open("K/.password","w")
263 file.writeLine(encrypt(password,"password"))
264 file.close()
265 return end
266 if char == "n" then term.clearLine() newPassword() end
267 end
268end
269
270function fakeStartup()
271 term.setCursorPos(1,h)
272 term.clear()
273 term.write("Booting")
274 os.sleep(1)
275 term.write(".")
276 os.sleep(1)
277 term.write(".")
278 os.sleep(1)
279 term.write(".")
280 os.sleep(1)
281 print()
282end
283
284-- MAIN -------------------------------------------------------------------------------------------------------------------------------
285
286fakeStartup()
287
288-- CHECK IF MAIN DIRECTORY EXISTS
289if fs.isDir("K") then print("Previous data found")
290else fs.makeDir("K") end
291
292-- CHECK FOR MAIN GUI FILES -----------------------------------------------------------------------------------------------------------
293
294if fs.isDir("Views") then print("Views Found")
295else fs.makeDir("Views") end
296if fs.exists("Views/main.view") then print("Main View Found")
297else
298shell.run("pastebin get a8RLGPAd Views/main.view") end
299
300-- CHECK IF PASSWORD EXISTS, SET ONE IF NEEDED
301if fs.exists("K/.password") then
302 local file = fs.open("K/.password","r")
303 password = file.readAll()
304 password = decrypt(password,"password")
305 file.close()
306else newPassword() end
307-- LOGIN ------------------------------------------------------------------------------------------------------------------------------
308login()
309
310if pocket then
311 print("POCKET COMPUTER")
312 -- POCKET COMPUTER INTERFACE ----------------------------------------------------------------------------------------------------------
313 else print("NORMAL COMPUTER")
314 local program = Bedrock:Initialise()
315 program:Run(function()end)
316 -- NORMAL COMPUTER INTERFACE ----------------------------------------------------------------------------------------------------------
317end