-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
217 lines (197 loc) · 6.55 KB
/
users.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const fs = require('fs');
const f = require('./functions.js');
module.exports = {
////////////////////////// USERS //////////////////////////
getUsers: function() {
// check if file ./config/users exists
if (!fs.existsSync("./config/users.json")) {
// if not, create it
fs.writeFileSync("./config/users.json", JSON.stringify([]));
}
// read the file
let users = fs.readFileSync("./config/users.json");
// parse the file
return JSON.parse(users);
},
init: async function() {
let newfile = false
// check if user file exists
if (!fs.existsSync("./config/users.json")) {
// if not, create it
fs.writeFileSync("./config/users.json", JSON.stringify([]));
}
// check user contain at least one admin
let users = this.getUsers();
let admin = users.filter(u => u.username == "root");
if (admin.length == 0) {
newfile = true;
}
if (newfile) {
let root_user_apikey = f.generateUUUIDV4();
this.createUser("root", root_user_apikey);
console.log("[+] Root user created with apikey: " + root_user_apikey);
} else {
console.log("[+] Root user already exists");
}
},
createUser(username, apikey) {
// get all the users
let users = this.getUsers();
// push the new user
users.push({
username: username,
apikey: apikey
});
// write the file
fs.writeFileSync("./config/users.json", JSON.stringify(users));
},
deleteUser(username) {
// get all the users
let users = this.getUsers();
// filter the user
users = users.filter(u => u.username != username);
// write the file
fs.writeFileSync("./config/users.json", JSON.stringify(users));
},
////////////////////////// CLIENTS //////////////////////////
getClient: function() {
// check if file ./config/users exists
if (!fs.existsSync("./config/clients.json")) {
// if not, create it
fs.writeFileSync("./config/clients.json", JSON.stringify([]));
}
// read the file
let clients = fs.readFileSync("./config/clients.json");
// parse the file
return JSON.parse(clients);
},
createClient: function(client_key, secret_key) {
// get all the clients
let clients = this.getClient();
// push the new client
clients.push({
client_key: client_key,
secret_key: secret_key,
name: f.genrate_fancy_name(),
owner: undefined,
shared: [],
tags: []
});
// write the file
fs.writeFileSync("./config/clients.json", JSON.stringify(clients));
},
claimServer: function(username, server_key) {
// get all the clients
let clients = this.getClient();
// filter the client
clients = clients.filter(c => c.client_key == server_key);
if (clients.length == 0) {
return false;
}
// get the client
let client = clients[0];
// check if the client is already claimed
if (client.owner != undefined) {
return false;
}
// set the owner
client.owner = username;
// write the file
fs.writeFileSync("./config/clients.json", JSON.stringify(clients));
return true;
},
shareServer: function(server_key, share_with) {
// get all the clients
let clients = this.getClient();
// filter the client
clients = clients.filter(c => c.client_key == server_key);
if (clients.length == 0) {
return false;
}
// get the client
let client = clients[0];
// check if the client is already claimed
if (client.owner == undefined) {
return false;
}
// check if the client is already shared
if (client.shared.includes(share_with)) {
return false;
}
// share the client
client.shared.push(share_with);
// write the file
fs.writeFileSync("./config/clients.json", JSON.stringify(clients));
return true;
},
getUserClient: function(username) {
// get all the clients
let clients = this.getClient();
// filter the client
clients = clients.filter(c => c.owner == username || c.shared.includes(username));
return clients.map(c => {
return {
client_key: c.client_key,
name: c.name,
tags: c.tags,
shared: c.shared,
}
});
},
addTag: function(server_key, tag) {
// get all the clients
let clients = this.getClient();
// filter the client
clients = clients.filter(c => c.client_key == server_key);
if (clients.length == 0) {
return false;
}
// get the client
let client = clients[0];
// check if the tag is already added
if (client.tags.includes(tag)) {
return false;
}
// add the tag
client.tags.push(tag);
// write the file
fs.writeFileSync("./config/clients.json", JSON.stringify(clients));
return true;
},
removeTag: function(server_key, tag) {
// get all the clients
let clients = this.getClient();
// filter the client
clients = clients.filter(c => c.client_key == server_key);
if (clients.length == 0) {
return false;
}
// get the client
let client = clients[0];
// check if the tag is already added
if (!client.tags.includes(tag)) {
return false;
}
// remove the tag
client.tags = client.tags.filter(t => t != tag);
// write the file
fs.writeFileSync("./config/clients.json", JSON.stringify(clients));
return true;
},
renameServer: function(server_key, new_name) {
// get all the clients
let clients = this.getClient();
// filter the client
clients = clients.filter(c => c.client_key == server_key);
if (clients.length == 0) {
return false;
}
// get the client
let client = clients[0];
// set the new name
client.name = new_name;
// write the file
fs.writeFileSync("./config/clients.json", JSON.stringify(clients));
return true;
}
}