This commit is contained in:
Benjamin Toby
2024-12-05 08:03:33 +01:00
parent 9686a22c96
commit 3ffd2d6777
821 changed files with 3251 additions and 91996 deletions
@@ -1,204 +1,16 @@
"use strict";
(() => {
var exports = {};
exports.id = 1176;
exports.ids = [1176];
exports.modules = {
/***/ 6517:
/***/ ((module) => {
module.exports = require("lodash");
/***/ }),
/***/ 2261:
/***/ ((module) => {
module.exports = require("serverless-mysql");
/***/ }),
/***/ 4300:
/***/ ((module) => {
module.exports = require("buffer");
/***/ }),
/***/ 2081:
/***/ ((module) => {
module.exports = require("child_process");
/***/ }),
/***/ 6113:
/***/ ((module) => {
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
module.exports = require("http");
/***/ }),
/***/ 1017:
/***/ ((module) => {
module.exports = require("path");
/***/ }),
/***/ 3802:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6517);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(2224);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1017);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(2081);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(child_process__WEBPACK_IMPORTED_MODULE_5__);
// @ts-check
const fs = __webpack_require__(7147);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
"use strict";(()=>{var e={};e.id=4105,e.ids=[4105],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},46127:(e,r,t)=>{t.r(r),t.d(r,{config:()=>y,default:()=>E,routeModule:()=>b});var s={};t.r(s),t.d(s,{default:()=>h});var a=t(89947),n=t(2706),i=t(96762),o=t(6420),l=t.n(o),c=t(77538),u=t.n(c);t(27825);var d=t(51348),p=t.n(d),f=t(33873),S=t.n(f),_=t(79646);let m=t(29021);async function h(e,r){if("POST"!==e.method)return r.json({user:null,msg:"Registration Failed!"});let t=await l()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let s=e.body.schema,a=e.body.dbId;try{let e=await p()("SELECT * FROM user_databases WHERE user_id = ? AND id = ?",[t.id,a]);if(!e?.[0])throw Error("Invalid Database");let n=e[0],i=S().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/main.json`),o=JSON.parse(m.readFileSync(i,"utf-8")),l=o.find(e=>e.dbFullName==n.db_full_name);if(!l)throw Error("This Database doesn't exist");if(l.tables=s.tables,l.childrenDatabases)for(let e=0;e<l.childrenDatabases.length;e++){let{dbFullName:r}=l.childrenDatabases[e],t=o.find(e=>e.dbFullName===r);if(!t||!t.tables){l.childrenDatabases?.splice(e,1);continue}t.tables=s.tables}m.writeFileSync(`${i}`,JSON.stringify(o),"utf8");let c=S().join(process.cwd(),"/shell"),u=(0,_.execSync)(`node createDbFromSchema.js --user ${t.id} --database ${n.db_full_name}`,{cwd:c});console.log(u.toString()),r.json({success:!0})}catch(s){u()({component:"/api/acceptUserInvitation/catch-error",message:s.message,user:t,req:e}),r.json({success:!1})}}let E=(0,i.M)(s,"default"),y=(0,i.M)(s,"config"),b=new a.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/user-schema/replace-database-schema",pathname:"/api/user-schema/replace-database-schema",bundlePath:"",filename:""},userland:s})},77538:(e,r,t)=>{let s=t(29021),{IncomingMessage:a}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:a,req:n}){let i=new Date,o=(()=>{if(!n)return null;try{let e=n.headers["x-forwarded-for"],r=n.headers["x-real-ip"],t=n.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],a=t||s||r||n.socket.remoteAddress;if(!a)return null;return String(a)}catch(e){return null}})();try{let a=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),n?.url&&(a+=`
URL: ${n.url}`),n?.body&&(a+=`
Request Body: ${JSON.stringify(n.body,null,4)}`),o&&(a+=`
IP: ${o}`),a+=`
Date: ${i.toDateString()}
========================================`,s.existsSync("./.tmp/error.log")||s.writeFileSync("./.tmp/error.log","","utf-8");let l=s.readFileSync("./.tmp/error.log","utf-8");s.writeFileSync("./.tmp/error.log",a),s.appendFileSync("./.tmp/error.log",`
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* API handler
* ==============================================================================
* @type {import("next").NextApiHandler}
*/ async function handler(req, res) {
/**
* Check method
*
* @description Check request method and return if invalid
*/ if (req.method !== "POST") return res.json({
user: null,
msg: "Registration Failed!"
});
/**
* User Auth
*
* @description User Auth
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default()(req, res, true);
if (!user) {
return res.json({
success: false,
msg: "Unauthorized"
});
}
/**
* Validate Form
*
* @description Check if request body is valid
*/ /** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType} */ const schema = req.body.schema;
const dbId = req.body.dbId;
/**
* Validate Form
*
* @description Check if request body is valid
*/ try {
/**
* @type {import("@/package-shared/types").DSQL_MYSQL_user_databases_Type[] | null}
*/ const targetDBArray = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default()(`SELECT * FROM user_databases WHERE user_id = ? AND id = ?`, [
user.id,
dbId
]);
if (!targetDBArray?.[0]) {
throw new Error("Invalid Database");
}
const targetDb = targetDBArray[0];
const userSchemaPath = path__WEBPACK_IMPORTED_MODULE_4___default().resolve(process.cwd(), `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user.id}/main.json`);
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType[]} */ const userSchema = JSON.parse(fs.readFileSync(userSchemaPath, "utf-8"));
const targetDatabase = userSchema.find((db)=>db.dbFullName == targetDb.db_full_name);
if (!targetDatabase) {
throw new Error("This Database doesn't exist");
}
targetDatabase.tables = schema.tables;
if (targetDatabase.childrenDatabases) {
for(let i = 0; i < targetDatabase.childrenDatabases.length; i++){
const childDb = targetDatabase.childrenDatabases[i];
const { dbFullName } = childDb;
const targetChildDb = userSchema.find((db)=>db.dbFullName === dbFullName);
if (!targetChildDb || !targetChildDb.tables) {
targetDatabase.childrenDatabases?.splice(i, 1);
continue;
}
targetChildDb.tables = schema.tables;
}
}
fs.writeFileSync(`${userSchemaPath}`, JSON.stringify(userSchema), "utf8");
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const targetShellPath = path__WEBPACK_IMPORTED_MODULE_4___default().join(process.cwd(), "/shell");
const dbShellUpdate = (0,child_process__WEBPACK_IMPORTED_MODULE_5__.execSync)(`node createDbFromSchema.js --user ${user.id} --database ${targetDb.db_full_name}`, {
cwd: targetShellPath
});
console.log(dbShellUpdate.toString());
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
res.json({
success: true
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {any} */ error) {
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
component: "/api/acceptUserInvitation/catch-error",
message: error.message,
user: user
});
res.json({
success: false
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/***/ })
};
;
// load runtime
var __webpack_require__ = require("../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [2224,2163,6825], () => (__webpack_exec__(3802)));
module.exports = __webpack_exports__;
})();
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},6420:(e,r,t)=>{t(81630);let s=t(51348),a=t(72907),n=t(29021);async function i(e,r,t,i){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let o=a(e.cookies.datasquirelAuthKey);if(!o)return null;let l=JSON.parse(o);if(!l.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${l.csrf_k}`)))return null;let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!n.existsSync(`${c}/${l.csrf_k}`))return null;if(0==l.verification_status&&!t){let e=await s(`SELECT verification_status FROM users WHERE id='${l.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return l?.date&&Date.now()-l.date>6048e5?null:l}e.exports=i},72907:(e,r,t)=>{let{scryptSync:s,createDecipheriv:a}=t(55511),{Buffer:n}=t(79428);e.exports=e=>{let r=a("aes-192-cbc",s(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),n.alloc(16,0));try{let t=r.update(e,"hex","utf8");return t+=r.final("utf8")}catch(e){return null}}},51348:(e,r,t)=>{let s=t(9973),a=t(21580),n=s({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:a()}});async function i(...e){try{let r=await n.query(...e);return await n.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}}e.exports=i},21580:(e,r,t)=>{let s=t(29021);e.exports=function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let r=`${e}/ca-cert.pem`;if(!s.existsSync(r)){console.log(`${r} does not exist`);return}return{ca:s.readFileSync(`${e}/ca-cert.pem`)}}},2706:(e,r)=>{var t;Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE"}(t||(t={}))},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../../webpack-api-runtime.js");r.C(e);var t=r(r.s=46127);module.exports=t})();
File diff suppressed because one or more lines are too long