This code sets up an HTTPS server using Express.js, configures it to use a private key and certificate, and initializes a sockify server to handle incoming connections. It also imports specific functions from the Core module, injects new modules into the sockify server, and calls various functions to set up the search provider and selenium server.
// TODO: move this to web extension code?
var importer = require('../Core');
var fs = require('fs');
var https = require('https');
var app = require('express')();
var {
searchProvider, sockify, seleniumServer
} = importer.import("mock properties rewire",
"search notebook provider",
"socket.io server",
"selenium http server");
var httpsOptions = {
key: fs.readFileSync('../Utilities/.ca/intermediate/private/localhost.key.pem'),
cert: fs.readFileSync('../Utilities/.ca/intermediate/certs/localhost.cert.pem'),
passphrase: 'x',
//requestCert: true,
rejectUnauthorized: false
};
var secureServer = require('https').createServer(httpsOptions, app);
var trustedCa = ['../Utilities/ca/intermediate/certs/localhost.cert.pem'];
https.globalAgent.options.ca = [];
for (const ca of trustedCa) {
https.globalAgent.options.ca.push(httpsOptions.cert);
}
sockify = r[1];
searchProvider = r[0];
seleniumServer = r[2];
var listener = secureServer.listen(8000);
sockify.sockifyServer(listener);
sockify.sockifyRequire({
prototype: {
search: function () {
},
results: function () {
}
}
}, 'SearchService');
sockify.sockifyRequire({
prototype: {
chrome: function () {
}
}
}, 'BrowserService');
searchProvider();
$.async();
seleniumServer()
.catch(e => $.sendError(e));
// var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
// var keyChain = path.join(PROFILE_PATH, '/Library/Keychains/login.keychain');
// security add-trusted-cert -d -r trustAsRoot -k ../Library/Keychains/login.keychain ../server.crt
///
/* TODO: integrate with native messaging?
{
"name": "com.my_company.my_application",
"description": "My Application",
"path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
*/
// Import required modules
const { importer, https, fs } = require('node-modules');
const express = require('express');
const { promisify } = require('util');
const { createServer } = require('https');
const { join } = require('path');
// Import custom modules
const {
SearchProvider,
SocketIOserver,
SeleniumServer,
} = importer.import([
'search notebook provider',
'socket.io server',
'selenium http server',
]);
// Define HTTPS server options
const httpsOptions = {
key: fs.readFileSync(join(__dirname, '../Utilities/.ca/intermediate/private/localhost.key.pem')),
cert: fs.readFileSync(join(__dirname, '../Utilities/.ca/intermediate/certs/localhost.cert.pem')),
passphrase: 'x',
rejectUnauthorized: false,
};
// Create HTTPS server
const secureServer = createServer(httpsOptions, express());
// Define trusted CA certificates
const trustedCa = [join(__dirname, '../Utilities/ca/intermediate/certs/localhost.cert.pem')];
// Set trusted CA certificates in the global agent
https.globalAgent.options.ca = [];
trustedCa.forEach((ca) => {
https.globalAgent.options.ca.push(httpsOptions.cert);
});
// Set up Socket.io server
const sockify = new SocketIOserver();
sockify.sockifyServer(secureServer);
sockify.sockifyRequire({
prototype: {
search: async () => {},
results: async () => {},
},
}, 'SearchService');
sockify.sockifyRequire({
prototype: {
chrome: async () => {},
},
}, 'BrowserService');
// Start the HTTPS server
const listener = secureServer.listen(8000);
// Start SearchProvider
SearchProvider();
// Start SeleniumServer asynchronously
promisify(seleniumServer)();
.catch((e) => console.error(e));
// TODO: Integrate with native messaging using the following config
// {
// "name": "com.my_company.my_application",
// "description": "My Application",
// "path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe",
// "type": "stdio",
// "allowed_origins": [
// "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
// ]
// }
var importer = require('../Core');
: Imports a module named Core
from a directory two levels up from the current directory.var fs = require('fs');
: Imports the built-in fs
(file system) module.var https = require('https');
: Imports the built-in https
module.var app = require('express')();
: Creates a new instance of the Express.js framework.var { searchProvider, sockify, seleniumServer } = importer.import([...]);
: Imports specific functions from the Core
module using the import
function.var httpsOptions = {... };
: Defines options for the HTTPS server, including a private key, certificate, passphrase, and trust settings.var secureServer = require('https').createServer(httpsOptions, app);
: Creates a new HTTPS server instance using the Express.js app and the defined options.var trustedCa = [...];
: Defines an array of trusted certificates.https.globalAgent.options.ca = [];
: Resets the global HTTPS agent's certificate cache.for (const ca of trustedCa) {... }
: Iterates over the trusted certificates and adds them to the global HTTPS agent's certificate cache.sockify = r[1];
: Extracts the sockify
module from an array r
.searchProvider = r[0];
: Extracts the searchProvider
module from the array r
.seleniumServer = r[2];
: Extracts the seleniumServer
module from the array r
.var listener = secureServer.listen(8000);
: Starts the HTTPS server and listens on port 8000.sockify.sockifyServer(listener);
: Configures sockify to use the HTTPS server.sockify.sockifyRequire({...}, 'SearchService');
: Injects a new module into the sockify server.sockify.sockifyRequire({...}, 'BrowserService');
: Injects another new module into the sockify server.searchProvider();
: Calls the searchProvider
function.$.async();
: Calls an asynchronous function.seleniumServer().catch(e => $.sendError(e));
: Calls the seleniumServer
function and catches any errors that occur.