This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
forked from dandanthedev/is-a.dev-webserver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
152 lines (132 loc) · 4.24 KB
/
functions.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
const fs = require("fs");
require("dotenv").config();
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const { MongoClient } = require('mongodb');
const mongoose = require('mongoose');
const userSchema = require('./data'); // Import your Mongoose schema definition
// bcrypt
const bcrypt = require('bcrypt');
const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/';
const dbName = process.env.DATABASE_NAME || 'your_database_name';
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connect(uri + "hosting-config", { useNewUrlParser: true, useUnifiedTopology: true });
async function generateConfigWithActivation(domain, email) {
let config = {};
config = {
domain: domain,
ACTIVATION_EMAIL: email,
FTP: true,
ACTIVATED: false,
HashedPassword:
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15),
};
let userDocument = new userSchema(config);
await userDocument.save();
return config.ACTIVATION_CODE;
}
async function generateConfig(domain) {
let config = {};
config = {
domain: domain,
FTP: true,
ACTIVATED: true,
HashedPassword:
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15),
};
let userDocument = new userSchema(config);
await userDocument.save();
return config;
}
async function LinkDiscord(domain, code) {
let finalize =`dh=${code}`
// make folder
fs.mkdirSync(`content/${domain}/.well-known`);
// make file
fs.writeFileSync(
`content/${domain}/.well-known/discord`,
finalize,
function (err) {
if (err) throw err;
}
);
// change permissions
return true;
}
async function activateDomain(domain, callback) {
const User = mongoose.model("hostingdata"); // Replace with your Mongoose model name
const user = await User.findOne({ domain }).exec();
let useremail = '';
try {
let config = user;
//console.log(config.activation_code);
//if (config.ACTIVATION_CODE === activation_code) {
// Remove activation code
useremail = config.ACTIVATION_EMAIL;
password = config.HashedPassword;
// hash password
const pas = await bcrypt.hash(password, 10);
await User.updateOne({ domain }, { $set: { ACTIVATED: true, HashedPassword: pas } });
const msg = {
to: useremail,
from: '[email protected]', // This email should be verified in your SendGrid settings
templateId: 'd-694e5d1edfca4cbca4958fb4fb4516f3', // Replace with your actual dynamic template ID
dynamic_template_data: {
username: domain,
password: password,
// Other dynamic data that your template requires
},
};
await sgMail
.send(msg)
.then(() => {
console.log('Email sent');
})
.catch((error) => {
console.error(error);
});
// Success: Activation code matched and config file updated
callback(null, true);
}
catch (err) {
// Error: Activation code did not match
callback(err, false);
}
}
function fetchDir(dir) {
let files = [];
let dirFiles = fs.readdirSync(dir);
for (let file of dirFiles) {
let stats = fs.statSync(`${dir}/${file}`);
if (stats.isDirectory()) {
files.push({
file: `${file}`,
type: "directory",
path: dir.replace(`content/${dir.split("/")[1]}`, ""),
});
let subFiles = fetchDir(`${dir}/${file}`);
subFiles.forEach((element) => {
files.push(element);
});
} else {
files.push({
file: `${file}`,
type: "file",
path: dir.replace(`content/${dir.split("/")[1]}`, ""),
});
}
}
return files;
}
function getUserFiles(domain) {
try {
//recursively get all files and make them an array with {file, type}
let files = fetchDir(`content/${domain}`);
return files;
} catch (err) {
return [];
}
}
module.exports = { generateConfig, getUserFiles, generateConfigWithActivation, activateDomain, LinkDiscord };