-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaga_user.js
64 lines (58 loc) · 1.79 KB
/
naga_user.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
import fs from "fs";
export class NagaUser {
constructor(username, password, secret_md5, cookies) {
this.username = username;
this.password = password;
this.secret_md5 = secret_md5;
this.webPage = null;
this.cookies = cookies || [];
this.login = false;
}
}
export class NagaUserGroup {
constructor() {
this.users = []
if (fs.existsSync("naga_user.json")) {
const value = fs.readFileSync("./naga_user.json").toString();
const jsonData = JSON.parse(value);
for (const value of jsonData) {
this.users.push(new NagaUser(value.username, value.password, value.secret_md5, value.cookies));
}
}
else {
fs.writeFileSync("./naga_user.json", "[]");
}
}
addUser(username, password, secret_md5) {
const user = this.getByUsername(username);
if (user !== undefined) throw new Error('User already exists');
this.users.push(new NagaUser(username, password, secret_md5));
this.save();
}
save() {
const jsonData = [];
for (const user of this.users) {
jsonData.push({
username: user.username,
password: user.password,
secret_md5: user.secret_md5,
cookies: user.cookies
});
}
fs.writeFileSync("./naga_user.json", JSON.stringify(jsonData));
}
getByUsername(username) {
for (const user of this.users) {
if (user.username === username) {
return user;
}
}
}
getBySecret(secret_md5) {
for (const user of this.users) {
if (user.secret_md5 === secret_md5) {
return user;
}
}
}
}