Add migrate
This commit is contained in:
src
@ -2,7 +2,7 @@ const express = require("express");
|
||||
const app = express.Router();
|
||||
module.exports.app = app;
|
||||
const { ExpressCheckToken } = require("../../auth");
|
||||
const UserManeger = require("../../Users_Maneger");
|
||||
const UserManeger = require("../../ofvp_users");
|
||||
module.exports.listRoutes = () => (require("../index")).GetExpressRoutes(app);
|
||||
const OfvpMongo = require("../../ofvp_mongo");
|
||||
const Wireguard = require("../../services/wireguard/index");
|
||||
|
@ -19,6 +19,7 @@ const Console = new (require("./lib/Console"))("OFVp Main Process");
|
||||
// Wait Mongo Database to be ready
|
||||
Console.log("Waiting for MongoDB to be ready...");
|
||||
await (require("./ofvp_mongo")).ConnectionStatus();
|
||||
await (require("./lib/Migrate")).Move();
|
||||
Console.log("MongoDB is ready!");
|
||||
|
||||
// Wait to Load all Services Process available
|
||||
|
182
src/lib/Migrate.js
Normal file
182
src/lib/Migrate.js
Normal file
@ -0,0 +1,182 @@
|
||||
const mongoose = require("mongoose"), Admin = mongoose.mongo.Admin;
|
||||
const ofvp_mongo = require("../ofvp_mongo");
|
||||
const { EncryptPassword } = require("./PasswordEncrypt");
|
||||
const { MongoDB_URL } = process.env;
|
||||
|
||||
async function Move() {
|
||||
const connection = mongoose.createConnection(MongoDB_URL);
|
||||
await new Promise(Complete => connection.on('open', Complete));
|
||||
const Data = (await new Promise((Complete, Reject) => {
|
||||
new Admin(connection.db).listDatabases(function(err, result) {
|
||||
if (err) return Reject(err);
|
||||
Complete(result.databases)
|
||||
});
|
||||
})).filter(Data => Data.name === "token" || Data.name === "users");
|
||||
console.log(Data);
|
||||
for (const {name} of Data) {
|
||||
if (name === "token") {
|
||||
const AuthToken = mongoose.createConnection(`${MongoDB_URL}/token`);
|
||||
await new Promise(Complete => AuthToken.on('open', Complete));
|
||||
const Token = AuthToken.model("AuthToken", new mongoose.Schema({
|
||||
// E-Mail Token
|
||||
token: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
// E-Mail
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
// Password
|
||||
password: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
}
|
||||
}));
|
||||
const Tokens = await Token.find({});
|
||||
for (const Token of Tokens) {
|
||||
const Password = EncryptPassword(Token.password);
|
||||
ofvp_mongo.AuthTokenSchema.create({
|
||||
token: Token.token,
|
||||
email: Token.email,
|
||||
password: Password,
|
||||
createdAt: Token.createdAt
|
||||
});
|
||||
}
|
||||
} else if (name === "users") {
|
||||
const Users = mongoose.createConnection(`${MongoDB_URL}/users`);
|
||||
await new Promise(Complete => Users.on('open', Complete));
|
||||
const User = UsersMongo.model("Users", new mongoose.Schema({
|
||||
// Username
|
||||
username: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
// SSH
|
||||
expire: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
ssh: {
|
||||
password: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
connections: {
|
||||
type: Number,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
v2ray: {
|
||||
load: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
id: {
|
||||
type: String,
|
||||
default: Crypto.randomUUID,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
level: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
alterId: {
|
||||
type: Number,
|
||||
default: 0
|
||||
}
|
||||
},
|
||||
// Wireguard Config
|
||||
wireguard: {
|
||||
load: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
keys: {
|
||||
Preshared: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
Private: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
Public: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
}
|
||||
},
|
||||
ip: {
|
||||
v4: {
|
||||
ip: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
mask: {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
v6: {
|
||||
ip: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
mask: {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
const Users = await User.find({});
|
||||
for (const User of Users) {
|
||||
const Password = EncryptPassword(User.ssh.password);
|
||||
ofvp_mongo.UsersSchema.create({
|
||||
username: User.username,
|
||||
expire: User.expire,
|
||||
password: Password,
|
||||
ssh: {
|
||||
connections: User.ssh.connections
|
||||
},
|
||||
wireguard: {
|
||||
load: User.wireguard.load === undefined ? false : User.wireguard.load,
|
||||
keys: {
|
||||
Preshared: User.wireguard.keys.Preshared,
|
||||
Private: User.wireguard.keys.Private,
|
||||
Public: User.wireguard.keys.Public
|
||||
},
|
||||
ip: {
|
||||
v4: {
|
||||
ip: User.wireguard.ip.v4.ip,
|
||||
mask: User.wireguard.ip.v4.mask
|
||||
},
|
||||
v6: {
|
||||
ip: User.wireguard.ip.v6.ip,
|
||||
mask: User.wireguard.ip.v6.mask
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
await connection.close();
|
||||
}
|
||||
module.exports.Move = Move;
|
||||
if (require.main === module) Move();
|
@ -2,7 +2,6 @@ const { MongoDB_URL } = process.env;
|
||||
if (!(/(mongodb|mongodb\+srv):\/\/([a-zA-Z0-9@:]+)[a-zA-Z0-9\.]+/gi.test(MongoDB_URL) && !/:[a-zA-Z]+$/gi.test(MongoDB_URL))) throw new Error("Invalid MongoDB URL: "+MongoDB_URL);
|
||||
const Mongoose = require("mongoose");
|
||||
const Console = new (require("./lib/Console"))("Mongo");
|
||||
const { EncryptPassword, DecryptPassword } = require("./lib/PasswordEncrypt");
|
||||
const ConnectionStatusObject = {Status: "Connecting", Error: null};
|
||||
const Connection = Mongoose.createConnection(`${MongoDB_URL}/OFVpServer`);
|
||||
Connection.on("connected", () => {
|
||||
@ -62,6 +61,34 @@ const TokenSchema = new Mongoose.Schema({
|
||||
}
|
||||
});
|
||||
|
||||
const AuthTokenSchema = Connection.model("AuthToken", TokenSchema);
|
||||
module.exports.AuthTokenSchema = AuthTokenSchema;
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {Promise<Array<{
|
||||
* token: String;
|
||||
* email: String;
|
||||
* password: {
|
||||
* iv: String;
|
||||
* Encrypt: String;
|
||||
* };
|
||||
* createdAt: String;
|
||||
* }>>}
|
||||
*/
|
||||
module.exports.GetTokens = async () => await AuthTokenSchema.find({});
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{
|
||||
* token: String;
|
||||
* email: String;
|
||||
* password: String;
|
||||
* }} BodyRecive
|
||||
* @returns {Promise<BodyRecive>}
|
||||
*/
|
||||
module.exports.AddToken = async (BodyRecive) => {await AuthTokenSchema.create(BodyRecive); return BodyRecive};
|
||||
|
||||
// Users Schema
|
||||
const AddUserSchema = new Mongoose.Schema({
|
||||
// Username
|
||||
@ -145,9 +172,6 @@ const AddUserSchema = new Mongoose.Schema({
|
||||
const UsersSchema = Connection.model("Users", AddUserSchema);
|
||||
module.exports.UsersSchema = UsersSchema;
|
||||
|
||||
const AuthTokenSchema = Connection.model("AuthToken", TokenSchema);
|
||||
module.exports.AuthTokenSchema = AuthTokenSchema;
|
||||
|
||||
/**
|
||||
* Get Array users config
|
||||
*
|
||||
@ -251,29 +275,4 @@ module.exports.AddUser = async (BodyRecive) => {await UsersSchema.create(BodyRec
|
||||
* }
|
||||
* }|undefined>}
|
||||
*/
|
||||
module.exports.findOneUser = async (username) => await UsersSchema.findOne({username: username});
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {Promise<Array<{
|
||||
* token: String;
|
||||
* email: String;
|
||||
* password: {
|
||||
* iv: String;
|
||||
* Encrypt: String;
|
||||
* };
|
||||
* createdAt: String;
|
||||
* }>>}
|
||||
*/
|
||||
module.exports.GetTokens = async () => await AuthTokenSchema.find({});
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{
|
||||
* token: String;
|
||||
* email: String;
|
||||
* password: String;
|
||||
* }} BodyRecive
|
||||
* @returns {Promise<BodyRecive>}
|
||||
*/
|
||||
module.exports.AddToken = async (BodyRecive) => {await AuthTokenSchema.create(BodyRecive); return BodyRecive};
|
||||
module.exports.findOneUser = async (username) => await UsersSchema.findOne({username: username});
|
Reference in New Issue
Block a user