Skip to content

Commit 29c2efc

Browse files
committed
v2.0 Big Update
1 parent b07f052 commit 29c2efc

167 files changed

Lines changed: 46685 additions & 12913 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ servers_old/
55
spigots.json
66
config.json
77
indiftpd*
8-
logs/
8+
logs/
9+
old/
10+
users.json

app.js

Lines changed: 66 additions & 1421 deletions
Large diffs are not rendered by default.

my_modules/additional.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
1+
var colors = require('colors');
2+
var fs = require('fs');
3+
var configManager = require("./config");
4+
var cfg = configManager.readConfig();
5+
16
exports.getTimeFormatted = () => {
27
date = new Date();
38
return "[" + date.getHours().toString().padStart(2, "0") + ":" + date.getMinutes().toString().padStart(2, "0") + ":" + date.getSeconds().toString().padStart(2, "0") + "." + date.getMilliseconds().toString().padStart(2, "0") + "]";
9+
}
10+
11+
exports.showRequestInLogs = (req, res) => {
12+
method = req.method.toString().toUpperCase();
13+
ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
14+
ip = ip.replace("::ffff:", "").replace("::1", "127.0.0.1");
15+
url = req.originalUrl;
16+
console.log(this.getTimeFormatted(), "[" + colors.yellow(ip) + "]", colors.green(req.method) + " - " + colors.cyan(res.statusCode) + " - " + url);
17+
if (cfg['save-logs'] == true) {
18+
if (!fs.existsSync("./logs/")) {
19+
fs.mkdirSync("./logs");
20+
}
21+
date = new Date();
22+
fname = date.getDate().toString().padStart(2, "0") + "-" + date.getMonth().toString().padStart(2, "0") + "-" + date.getFullYear().toString().padStart(2, "0") + ".log";
23+
if (fs.existsSync("./logs/" + fname)) {
24+
rf = fs.readFileSync("./logs/" + fname);
25+
rf = rf + "\n" + this.getTimeFormatted() + " [" + ip + "] " + req.method + " - " + res.statusCode + " - " + url;
26+
} else {
27+
rf = this.getTimeFormatted() + " [" + ip + "] " + req.method + " - " + res.statusCode + " - " + url;
28+
}
29+
fs.writeFileSync("./logs/" + fname, rf);
30+
}
31+
}
32+
33+
exports.showMyMessageInLogs = (req, res, msg) => {
34+
method = req.method.toString().toUpperCase();
35+
ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
36+
ip = ip.replace("::ffff:", "").replace("::1", "127.0.0.1");
37+
url = req.originalUrl;
38+
console.log(this.getTimeFormatted(), "[" + colors.yellow(ip) + "]", msg);
39+
if (cfg['save-logs'] == true) {
40+
if (!fs.existsSync("./logs/")) {
41+
fs.mkdirSync("./logs");
42+
}
43+
date = new Date();
44+
fname = date.getDate().toString().padStart(2, "0") + "-" + date.getMonth().toString().padStart(2, "0") + "-" + date.getFullYear().toString().padStart(2, "0") + ".log";
45+
if (fs.existsSync("./logs/" + fname)) {
46+
rf = fs.readFileSync("./logs/" + fname);
47+
rf = rf + "\n" + this.getTimeFormatted() + " [" + ip + "] " + msg;
48+
} else {
49+
rf = this.getTimeFormatted() + " [" + ip + "] " + msg;
50+
}
51+
fs.writeFileSync("./logs/" + fname, rf);
52+
}
453
}

my_modules/auth_manager.js

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
var config = require('./config');
2+
var usersConfig = config.readUsersConfig();
3+
var crypto = require("crypto");
4+
5+
const PASSWORD_REGEX = /^[a-zA-Z0-9_.-]{2,}$/g;
6+
const LOGIN_REGEX = /^[a-zA-Z0-9_.-]{4,16}$/g;
7+
const EMAIL_REGEX = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g;
8+
9+
exports.authorize = (hash, login) => {
10+
var authsucc = false;
11+
cfg = config.readConfig();
12+
this.reloadUsersConfig();
13+
users = usersConfig;
14+
if (cfg.auth == false) {
15+
authsucc = true;
16+
} else {
17+
if (typeof (hash) !== "undefined" && hash != "") {
18+
if (typeof users[login] !== "undefined" && users[login].username !== "undefined" && users[login].username.length > 0) {
19+
if (users[login].hash == hash) {
20+
authsucc = true;
21+
}
22+
}
23+
}
24+
}
25+
return authsucc;
26+
}
27+
28+
exports.login = (password, login) => {
29+
var authsucc = false;
30+
cfg = config.readConfig();
31+
this.reloadUsersConfig();
32+
users = usersConfig;
33+
if (cfg.auth == false) {
34+
authsucc = true;
35+
} else {
36+
if (typeof (password) !== "undefined" && password != "") {
37+
if (typeof users[login] !== "undefined" && users[login].username !== "undefined" && users[login].username.length > 0) {
38+
if (users[login].password == crypto.createHash('sha256').update(password).digest('hex')) {
39+
authsucc = true;
40+
}
41+
}
42+
}
43+
}
44+
return authsucc;
45+
}
46+
47+
exports.addNewUser = (password, login, permissions, mail) => {
48+
success = false;
49+
cfg = config.readConfig();
50+
this.reloadUsersConfig();
51+
users = usersConfig;
52+
53+
if (login == "kubek") {
54+
success = "You can`t use this function on admin account";
55+
} else {
56+
if (cfg.auth == false) {
57+
success = "Auth is disabled";
58+
} else {
59+
if(Object.keys(users).length >= 6){
60+
success = "Users count is limited to 5 users";
61+
} else {
62+
if (mail == null || typeof mail == "undefined" || mail.match(EMAIL_REGEX)) {
63+
if (login.match(LOGIN_REGEX) && password.match(PASSWORD_REGEX)) {
64+
if (typeof users[login] == "undefined") {
65+
newUserHash = crypto.randomUUID().toString();
66+
if (permissions[0] == "") {
67+
permissions = [];
68+
}
69+
users[login] = {
70+
username: login,
71+
password: crypto.createHash('sha256').update(password).digest('hex'),
72+
hash: newUserHash,
73+
permissions: permissions,
74+
mail: mail
75+
}
76+
config.writeUsersConfig(users);
77+
success = true;
78+
} else {
79+
success = "User already exists";
80+
}
81+
} else {
82+
success = "Not matching regex";
83+
}
84+
} else {
85+
success = "Not matching regex";
86+
}
87+
}
88+
}
89+
}
90+
return success;
91+
}
92+
93+
exports.regenUserHash = (login) => {
94+
success = false;
95+
cfg = config.readConfig();
96+
this.reloadUsersConfig();
97+
users = usersConfig;
98+
99+
if (cfg.auth == false) {
100+
success = "Auth is disabled";
101+
} else {
102+
if (login.match(LOGIN_REGEX)) {
103+
if (typeof users[login] !== "undefined") {
104+
newUserHash = crypto.randomUUID().toString();
105+
users[login]['hash'] = newUserHash;
106+
config.writeUsersConfig(users);
107+
success = true;
108+
} else {
109+
success = "User not exists";
110+
}
111+
} else {
112+
success = "Not matching regex";
113+
}
114+
}
115+
return success;
116+
}
117+
118+
exports.changeAdminPass = (oldPass, newPass) => {
119+
success = false;
120+
cfg = config.readConfig();
121+
this.reloadUsersConfig();
122+
users = usersConfig;
123+
124+
if (cfg.auth == false) {
125+
success = "Auth is disabled";
126+
} else {
127+
op_hash = crypto.createHash('sha256').update(oldPass).digest('hex');
128+
np_hash = crypto.createHash('sha256').update(newPass).digest('hex');
129+
if (users['kubek']['password'] == op_hash) {
130+
users['kubek']['password'] = np_hash;
131+
newUserHash = crypto.randomUUID().toString();
132+
users['kubek']['hash'] = newUserHash;
133+
config.writeUsersConfig(users);
134+
success = true;
135+
} else {
136+
success = "Old password is incorrect";
137+
}
138+
}
139+
return success;
140+
}
141+
142+
exports.deleteUser = (login) => {
143+
success = false;
144+
cfg = config.readConfig();
145+
this.reloadUsersConfig();
146+
users = usersConfig;
147+
148+
if (login == "kubek") {
149+
success = "You can`t use this function on admin account";
150+
} else {
151+
if (cfg.auth == false) {
152+
success = "Auth is disabled";
153+
} else {
154+
if (login.match(LOGIN_REGEX)) {
155+
if (typeof users[login] !== "undefined") {
156+
users[login] = "";
157+
delete users[login];
158+
config.writeUsersConfig(users);
159+
success = true;
160+
} else {
161+
success = "User not exists";
162+
}
163+
} else {
164+
success = "Not matching regex";
165+
}
166+
}
167+
}
168+
return success;
169+
}
170+
171+
exports.editUser = (login, permissions, mail) => {
172+
success = false;
173+
cfg = config.readConfig();
174+
this.reloadUsersConfig();
175+
users = usersConfig;
176+
177+
if (login == "kubek") {
178+
success = "You can`t use this function on admin account";
179+
} else {
180+
if (cfg.auth == false) {
181+
success = "Auth is disabled";
182+
} else {
183+
if (mail == null || mail == "" || typeof mail == "undefined" || mail.match(EMAIL_REGEX)) {
184+
if (login.match(LOGIN_REGEX)) {
185+
if (typeof users[login] !== "undefined") {
186+
users[login]['mail'] = mail;
187+
if (permissions[0] == "") {
188+
permissions = [];
189+
}
190+
newUserHash = crypto.randomUUID().toString();
191+
users[login]['permissions'] = permissions;
192+
users[login]['hash'] = newUserHash;
193+
config.writeUsersConfig(users);
194+
success = true;
195+
} else {
196+
success = "User not exists";
197+
}
198+
} else {
199+
success = "Not matching regex";
200+
}
201+
} else {
202+
success = "Not matching regex";
203+
}
204+
}
205+
}
206+
return success;
207+
}
208+
209+
exports.getUserPermissions = (hash, login) => {
210+
auth = this.authorize(hash, login);
211+
if (auth == true) {
212+
if (typeof usersConfig[login] !== "undefined" && typeof usersConfig[login].permissions !== "undefined") {
213+
return usersConfig[login].permissions;
214+
} else {
215+
return false;
216+
}
217+
} else {
218+
return false;
219+
}
220+
}
221+
222+
exports.reloadUsersConfig = () => {
223+
usersConfig = config.readUsersConfig();
224+
}

my_modules/config.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const fs = require('fs');
2-
const defaultConfig = '{"lang":"en", "ftpd":false,"ftpd-user":"kubek","ftpd-password":"kubek","auth":false,"owner-user":"kubek","owner-password":"kubek","internet-access":false,"save-logs":true}';
2+
const CONFIG_VERSION = 1;
3+
const defaultConfig = '{"lang":"en", "ftpd":false,"ftpd-user":"kubek","ftpd-password":"kubek","auth":false,"internet-access":false,"save-logs":true,"config-version":1}';
4+
const defaultUsersConfig = '{"kubek": {"username": "kubek","password": "72ba608dbfac8d46d4aaf40f428badf85af1f929fece7480e56602b4452a71fe","mail": "","hash": "","permissions": ["console", "plugins", "filemanager", "server_settings", "kubek_settings"]}}';
5+
var crypto = require('crypto');
36

47
exports.readConfig = () => {
58
if (!fs.existsSync("config.json")) {
@@ -9,26 +12,65 @@ exports.readConfig = () => {
912
} else {
1013
parse = JSON.parse(fs.readFileSync("config.json"));
1114
// FOR BACKWARD COMPABILITY
12-
if(typeof parse['internet-access'] === "undefined"){
15+
if (typeof parse['internet-access'] === "undefined") {
1316
parse['internet-access'] = false;
1417
}
15-
if(typeof parse['save-logs'] === "undefined"){
18+
if (typeof parse['config-version'] === "undefined") {
19+
parse['config-version'] = CONFIG_VERSION;
20+
}
21+
if (typeof parse['owner-password'] !== "undefined") {
22+
users__cfg = this.readUsersConfig();
23+
users__cfg['kubek']['password'] = crypto.createHash('sha256').update(parse['owner-password']).digest('hex');
24+
this.writeUsersConfig(users__cfg);
25+
parse['owner-password'] = "";
26+
parse['owner-user'] = "";
27+
delete parse['owner-password'];
28+
delete parse['owner-user'];
29+
this.writeConfig(parse);
30+
}
31+
if (typeof parse['save-logs'] === "undefined") {
1632
parse['save-logs'] = false;
1733
}
1834
return parse;
1935
}
2036
}
2137

38+
exports.readUsersConfig = () => {
39+
if (!fs.existsSync("users.json")) {
40+
ret = this.writeDefaultUsersConfig();
41+
return ret;
42+
} else {
43+
parsess = JSON.parse(fs.readFileSync("users.json"));
44+
return parsess;
45+
}
46+
}
47+
2248
exports.writeConfig = (config) => {
23-
fs.writeFileSync("config.json", config)
49+
fs.writeFileSync("config.json", JSON.stringify(config, null, "\t"))
50+
}
51+
52+
exports.writeUsersConfig = (config) => {
53+
fs.writeFileSync("users.json", JSON.stringify(config, null, "\t"))
54+
}
55+
56+
exports.writeServersJSON = (config) => {
57+
fs.writeFileSync("./servers/servers.json", JSON.stringify(config, null, "\t"))
2458
}
2559

2660
exports.writeDefaultConfig = () => {
2761
fs.writeFileSync("config.json", defaultConfig);
2862
}
2963

64+
exports.writeDefaultUsersConfig = () => {
65+
newHash = crypto.randomUUID().toString();
66+
du = JSON.parse(defaultUsersConfig);
67+
du['kubek']['hash'] = newHash;
68+
fs.writeFileSync("users.json", JSON.stringify(du, null, "\t"));
69+
return du;
70+
}
71+
3072
exports.readServersJSON = () => {
31-
if(fs.existsSync("./servers/servers.json")){
73+
if (fs.existsSync("./servers/servers.json")) {
3274
return JSON.parse(fs.readFileSync("./servers/servers.json"));
3375
} else {
3476
return false;

0 commit comments

Comments
 (0)