forked from PoroCoco/myaenetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountAux.lua
116 lines (109 loc) · 4.4 KB
/
accountAux.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local component = require("component")
local internet = require("internet")
local filesystem = require("filesystem")
local shell = require("shell")
local urlAccount = "http://192.168.3.30:5000/accountCreation"
local webIdPath = "/home/myaenetwork/webIdentification.txt"
local workDirectory = "/home/myaenetwork/"
local newDirectory = "/home/myaenetwork"
local bs = { [0] =
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/',
}
function encode(s)
local byte, rep = string.byte, string.rep
local pad = 2 - ((#s-1) % 3)
s = (s..rep('\0', pad)):gsub("...", function(cs)
local a, b, c = byte(cs, 1, 3)
return bs[a>>2] .. bs[(a&3)<<4|b>>4] .. bs[(b&15)<<2|c>>6] .. bs[c&63]
end)
return s:sub(1, #s-pad) .. rep('=', pad)
end
function isConfigCorrect(rid, rusername,rpassword) --checks if each line of the identification file as the right data
if rid == nil or rusername == nil or rpassword == nil then
return false
end
if rid == "" or rusername == "" or rpassword == "" then
return false
else
return true
end
end
function createAccount()
print("Let's configure your account")
print("The account is linked to the computer, you will use the account username and password to connect on the web page")
print("Choose your account username")
local id = math.floor(math.random(1000000))
local username = io.read()
while username == "" or string.match(username, ";") do
print("Your username cannot be empty or contain ';'")
print("Choose your account username.")
username = io.read()
end
print("Your username is ".. username)
print()
print("Choose your account password.")
print("DO NOT USE A SENSITIVE PASSWORD, THEY ARE NOT ENCRYPTED")
print("I RECOMMAND YOU TO USE A PIN/SMALL PASSWORD")
local password = io.read()
while password == "" or string.match(password, ";") do
print("Your password cannot be empty or contain ';'")
print("Choose your account password.")
password = io.read()
end
if accountToServer(id,username,password) then
print("Account creation accepted by the server")
else
print("Account creation denied by the server")
print("Server might be offline or account already registered")
print("Please try the account creation one more time WITH A DIFFERENT USERNAME. Otherwise contact PoroCoco#4636 on Discord")
return
end
local f = io.open(webIdPath,"w") -- writes the infos into the identification file
f:write("id = "..tostring(id), "\n")
f:write("username = "..username, "\n")
f:write("password = "..password)
f:close()
print("Configuration is done !")
end
function accountToServer(id, username, password)
local accountData = tostring(id)..";"..username..";"..password
shell.setWorkingDirectory("/home/") -- if the server is down, internet.request will give an error so before trying it's going back to the basic dir
if internet.request(urlAccount, encode(accountData))() == "Account accepted" then
shell.setWorkingDirectory(workDirectory)
return true
else
shell.setWorkingDirectory(workDirectory)
return false
end
end
if component.isAvailable("internet") then
filesystem.makeDirectory(newDirectory)
shell.setWorkingDirectory(workDirectory)
if filesystem.exists(webIdPath) then --tries to read the identification file, if it cannot start an account creation
local f = io.open(webIdPath,"r")
local rid = f:read('*l')
local rusername = f:read('*l')
local rpassword = f:read('*l')
if isConfigCorrect(rid,rusername,rpassword) then
print("Computer is already configured")
print("Your account "..rusername)
print("Show account password ? Yes/No")
if io.read() == "Yes" then
print("Your account "..rpassword)
end
else
print("config file isn't correct. Remaking one")
createAccount()
end
f:close()
shell.setWorkingDirectory("/home/")
else
createAccount()
shell.setWorkingDirectory("/home/")
end
else
print("Please insert the Internet Card into the computer")
end