Skip to content

Commit

Permalink
Certificates Converted
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal Web committed Mar 23, 2023
1 parent 495fb60 commit bb176fb
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 298 deletions.
75 changes: 37 additions & 38 deletions server/api/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
module.exports = (server) => {
const {
logImprt,
utility: {
isPlainObject,
eachObjectAsync,
isArray,
eachAsync,
assign
},
app
} = server;
logImprt('SERVER APP API', __dirname);
async function add(method, methodName) {
app[methodName] = method;
console.log('Extended App API', methodName);
import { onMessage } from './onMessage.js';
import {
isPlainObject,
eachObjectAsync,
isArray,
eachAsync,
assign
} from 'Acid';
import { imported } from 'utilities/logs.js';
imported('SERVER APP API');
async function add(app, methodName, method) {
app.set(methodName, method);
console.log('Extended App API', methodName);
}
export async function addApi(methodName, method) {
const { app } = this;
if (isPlainObject(methodName)) {
return eachObjectAsync(methodName, (childMethod, childMethodName) => {
add(app, childMethodName, childMethod);
});
}
async function addApi(methodName, method) {
if (isPlainObject(methodName)) {
return eachObjectAsync(methodName, add);
}
return add(methodName, method);
return add(app, methodName, method);
}
async function remove(app, method, methodName) {
app.delete(methodName);
}
export async function removeApi(methods) {
const { app } = this;
if (isPlainObject(methods)) {
return eachObjectAsync(methods, (method, methodName) => {
remove(app, methodName);
});
}
async function remove(method, methodName) {
app[methodName] = null;
if (isArray(methods)) {
return eachAsync(methods, (methodName) => {
remove(app, methodName);
});
}
async function removeApi(methodName) {
if (isPlainObject(methodName)) {
return eachObjectAsync(methodName, remove);
}
if (isArray(methodName)) {
return eachAsync(methodName, remove);
}
return remove(methodName);
}
assign(server.api, {
add: addApi,
remove: removeApi
});
require('./onMessage')(server);
};
return remove(app, methods);
}
90 changes: 42 additions & 48 deletions server/api/onMessage/index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,48 @@
module.exports = (server) => {
import {
stringify,
hasValue
} from 'Acid';
import {
success, failed, imported, msgSent, info
} from 'utilities/logs.js';
imported('ON PUBLIC MESSAGE');
export async function onMessage(socket, message) {
const { app, } = this;
const {
cnsl,
logImprt,
error: logError,
success,
app,
utility: {
stringify,
hasValue
}
} = server;
logImprt('ON PUBLIC MESSAGE', __dirname);
const onMessage = async (socket, message) => {
const {
body,
sid,
api
} = message;
if (!api) {
return logError(`Invalid no method name given. ${stringify(message)}`);
}
const method = app[api];
if (method) {
if (body) {
if (hasValue(sid)) {
cnsl(`Request:${api} RequestID: ${sid}`);
console.log(message.body);
const response = {
sid
};
const hasResponse = await method(socket, message, response);
if (hasResponse) {
socket.send(response);
}
return;
} else {
const eid = message.eid;
if (hasValue(eid)) {
success(`Request:${method} Emit ID:${eid} ${stringify(message)}`);
return method(socket, body, message);
} else {
return logError(`Invalid Request type. No Emit ID was given. ${stringify(message)}`);
}
body,
sid,
api
} = message;
if (!api) {
return failed(`Invalid no method name given. ${stringify(message)}`);
}
const method = app[api];
if (method) {
if (body) {
if (hasValue(sid)) {
info(`Request:${api} RequestID: ${sid}`);
console.log(message.body);
const response = {
sid
};
const hasResponse = await method(socket, message, response);
if (hasResponse) {
socket.send(response);
}
return;
} else {
return logError(`Invalid Request no body was sent. ${stringify(message)}`);
const eid = message.eid;
if (hasValue(eid)) {
success(`Request:${method} Emit ID:${eid} ${stringify(message)}`);
return method(socket, body, message);
} else {
return failed(`Invalid Request type. No Emit ID was given. ${stringify(message)}`);
}
}
} else {
return logError(`Invalid method name given. ${stringify(message)}`);
return failed(`Invalid Request no body was sent. ${stringify(message)}`);
}
};
server.api.onMessage = onMessage;
};
} else {
return failed(`Invalid method name given. ${stringify(message)}`);
}
}
60 changes: 26 additions & 34 deletions server/app/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module.exports = async (server) => {
const path = require('path');
import {
keys,
assign,
cleanPath,
isString,
} from 'Acid';
import {
success, failed, imported, msgSent, info
} from 'utilities/logs.js';
import { toBase64 } from 'utilities/crypto.js';
import { read } from 'utilities/file.js';
import path from 'path';
export async function app() {
const {
logImprt,
cnsl,
alert,
utility: {
keys,
assign,
cleanPath,
isString,
},
configuration: {
resourceDirectory,
cacheMaxAge,
Expand All @@ -20,18 +22,12 @@ module.exports = async (server) => {
language,
onConnectMessage
},
crypto: {
toBase64,
},
file: {
read
}
} = server;
logImprt('APP', __dirname);
} = this;
imported('APP', __dirname);
const isValid = require('is-valid-path');
const api = {
async open(socket, request, response) {
cnsl(request);
info(request);
response.head = {};
if (cacheMaxAge) {
response.head.cacheMaxAge = cacheMaxAge;
Expand Down Expand Up @@ -59,21 +55,19 @@ module.exports = async (server) => {
return true;
},
async reKey(socket, body) {
cnsl(`${toBase64(body.certificate.key)}`);
info(`${toBase64(body.certificate.key)}`);
socket.reKey(body.certificate);
},
async file(socket, request, response) {
cnsl(request);
const {
path: location
} = request.body;
info(request);
const { path: requestPath } = request.body;
response.head = {};
if (!isString(location) || !location.length || !isValid(location)) {
if (!isString(requestPath) || !requestPath.length || !isValid(requestPath)) {
console.log('No valid state request recieved - Returning empty data');
response.head.status = 404;
return true;
}
const cleanedPath = cleanPath(`${resourceDirectory}/${location}`);
const cleanedPath = cleanPath(`${resourceDirectory}/${requestPath}`);
const data = await read(cleanedPath);
const ext = path.extname(cleanedPath);
console.log(`EXT => ${ext}`);
Expand All @@ -84,10 +78,8 @@ module.exports = async (server) => {
return true;
},
async state(socket, request, response) {
cnsl(request);
const {
state: fileName
} = request.body;
info(request);
const { state: fileName } = request.body;
response.head = {};
if (!isString(fileName) || !isValid(fileName)) {
console.log('No valid state request recieved - Returning empty data');
Expand All @@ -103,6 +95,6 @@ module.exports = async (server) => {
return true;
},
};
assign(server.app, api);
alert(`LOADED PUBLIC API: COUNT: ${keys(api).length} ||| ${keys(api).join(' , ')}`);
};
assign(this.app, api);
success(`LOADED PUBLIC API: COUNT: ${keys(api).length} ||| ${keys(api).join(' , ')}`);
}
7 changes: 4 additions & 3 deletions server/bind.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { promise } from 'Acid';
import { info } from '../utilities/logs.js';
export async function bind(server) {
export async function bindServer() {
const {
server: rawServer,
server,
configuration: {
port,
ip,
}
} = this;
info(`BIND SERVER`);
await promise((accept) => {
rawServer.bind(port, ip, accept);
server.bind(port, ip, accept);
info(`SERVER BOUND: IP:${ip} - PORT:${port}`);
});
return server;
}
4 changes: 3 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { onListen } from './onListen.js';
import { sendPacket } from './sendPacket.js';
import { processSocket } from './processSocket.js';
import { processMessage } from './processMessage.js';
import { bind } from './bind.js';
import { bindServer } from './bind.js';
import { parseMessage } from './parseMessage.js';
import { chunkMessage } from './chunkMessage.js';
import { emit } from './emit.js';
Expand All @@ -30,6 +30,7 @@ class Server {
onMessage = onMessage;
onListen = onListen;
sendPacket = sendPacket;
bindServer = bindServer;
async initialize(serverConfiguration) {
console.log('-------SERVER INITIALIZING-------');
this.configure(serverConfiguration);
Expand All @@ -39,6 +40,7 @@ class Server {
this.server.on('error', this.onError.bind(this));
this.server.on('listening', this.onListen.bind(this));
this.server.on('message', this.onMessage.bind(this));
await this.bindServer();
console.log('-------SERVER INITIALIZED-------');
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion utilities/certificate/getCertificateKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
keys,
remove
} from 'Acid';
export default function getCertificateKeys(certificate, exclude) {
export function getCertificateKeys(certificate, exclude) {
const certKeys = keys(certificate);
if (exclude) {
remove(certKeys, exclude);
Expand Down
2 changes: 1 addition & 1 deletion utilities/certificate/save.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { write } from '../file.js';
import { encode } from '../crypto.js';
export default async function saveCertificate(certificate, directory, certificateName = 'profile') {
export async function saveCertificate(certificate, directory, certificateName = 'profile') {
await write(`${directory}/${certificateName}.cert`, encode(certificate));
}
2 changes: 1 addition & 1 deletion utilities/certificate/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'Acid';
cnsl.imported('Certificate Signing');
import pluckCertificate from '../pluckCertificate.js';
export default function signCertificate(certificate, authority, exclude) {
export function signCertificate(certificate, authority, exclude) {
console.log('Sign Certificate');
const concatValues = pluckCertificate(certificate, exclude);
return hashSign(concatValues, authority.private);
Expand Down
25 changes: 19 additions & 6 deletions utilities/certificates/domain/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
Module for quickly generating domain certificates you must contact the team for official registration/signing.
*/
import cnsl from '../../logs/index.js';
import { read } from '../../file/index.js';
import { signVerify, encode } from '../../crypto/index.js';
import { read } from 'utilities/file/index.js';
import { signVerify, encode } from 'utilities/crypto/index.js';
import { jsonParse } from 'Acid';
import { createDomainProfile } from '../../certificate/createDomainProfile.js';
import { signCertificate } from '../../certificate/sign.js';
export default async function create(config) {
import { createDomainProfile } from 'utilities/certificate/createDomainProfile.js';
import { signCertificate } from 'utilities/certificate/sign.js';
import { success, cnsl } from 'utilities/logs.js';
export async function createDomain(config) {
const {
template,
templateLocation
Expand All @@ -33,3 +33,16 @@ export default async function create(config) {
cnsl.success(`TOTAL KEYPAIR SIZE: ${encode(ephemeral).length + encode(master).length}bytes`);
return domainCertificate;
}
export async function createDomainEphemeral(config) {
const {
template,
templateLocation,
master
} = config;
console.log(master);
const ephemeralTemplate = (template || jsonParse(await read(templateLocation || `${__dirname}/template.json`)));
const ephemeral = await createDomainEphemeral(ephemeralTemplate, master);
cnsl('------------MASTER KEY------------');
success('Ephemeral Certificate', `SIZE: ${encode(master).length}bytes`);
return ephemeral;
}
Loading

0 comments on commit bb176fb

Please sign in to comment.