display angular | Cell 2 | Cell 4 | Search

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.

Cell 3

// 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/"
  ]
}

*/

What the code could have been:

// 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/"
//   ]
// }

Code Breakdown

Importing Modules

HTTPS Server Configuration

Configuring Global HTTPS Agent

Sockify Configuration

Listening and Sockify Initialization

Comments