Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ±β€πŸ‘€πŸ±β€πŸ‘€πŸ±β€πŸ‘€πŸ±β€πŸ‘€ #284

Open
wants to merge 13 commits into
base: react-ui
Choose a base branch
from
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/node_modules
**/package-lock.json
react-ui/nojsx
wsc-chrome.min.js
assets
package.json
package.zip
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ handlers is an array of 2 element arrays where the first item is a regular expre

```
cd web-server-chrome
mkdir assets
cd makedeps
npm install
npm run make # this builds the app dependencies such as react and material-ui into a bundle
Expand Down
6 changes: 5 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ function onchoosefolder(entry) {

function settings_ready(d) {
localOptions = d
console.log('settings:',d)
let dCpy = {};
Object.assign(dCpy, d);
delete dCpy.optPrivateKey;// dont fill logs with crypto info
delete dCpy.optCertificate;
console.log('settings:',dCpy)
setTimeout( maybeStartup, 2000 ) // give background accept handler some time to trigger
//chrome.alarms.getAll( onAllAlarms )
}
Expand Down
183 changes: 183 additions & 0 deletions chromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**
* @author Alexey Kuzmin <[email protected]>
* @fileoverview Promise based wrapper for Chrome Extension API.
* @see https://developer.chrome.com/extensions/api_index
* @license MIT
* @version 3.1.0
*/



;(function(global) {
'use strict';

let apiProxy = {
/**
* @param {!Object} apiObject
* @param {string} methodName
* @param {Arguments} callArguments Arguments to be passes to method call.
*/
callMethod(apiObject, methodName, callArguments) {
let originalMethod = apiObject[methodName];
let callArgumentsArray = Array.from(callArguments);

return new Promise((resolve, reject) => {
let callback = apiProxy.processResponse_.bind(null, resolve, reject);
callArgumentsArray.push(callback);
originalMethod.apply(apiObject, callArgumentsArray);
});
},

/**
* @param {!Function} callback
* @param {!Function} errback
* @param {!Array} response Response from Extension API.
* @private
*/
processResponse_(callback, errback, ...response) {
let error = global.chrome.runtime.lastError;
if (typeof error == 'object') {
errback(new Error(error.message));
return;
}

if (response.length < 2)
response = response[0]; // undefined if response is empty

callback(response);
}
};


let classifier = {
/**
* @param {string} letter
* @return {boolean}
* @private
*/
isCapitalLetter_(letter) {
return letter == letter.toUpperCase();
},

/**
* @param {string} string
* @return {boolean}
* @private
*/
startsWithCapitalLetter_(string) {
return classifier.isCapitalLetter_(string[0]);
},

/**
* We need to decide should given property be wrapped or not
* by its name only. Retrieving its value would cause API initialization,
* that can take a long time (dozens of ms).
* @param {string} propName
* @return {boolean}
*/
propertyNeedsWrapping(propName) {
if (classifier.startsWithCapitalLetter_(propName)) {
// Either constructor, enum, or constant.
return false;
}

if (propName.startsWith('on') &&
classifier.isCapitalLetter_(propName[2])) {
// Extension API event, e.g. 'onUpdated'.
return false;
}

// Must be a namespace or a method.
return true;
}
};


let wrapGuy = {
/**
* @param {!Object} api API object to wrap.
* @return {!Object}
*/
wrapApi(api) {
return wrapGuy.wrapObject_(api);
},

/**
* Wraps API object.
* @param {!Object} apiObject
* @return {!Object}
* @private
*/
wrapObject_(apiObject) {
let wrappedObject = {};

Object.keys(apiObject)
.filter(classifier.propertyNeedsWrapping)
.forEach(keyName => {
Object.defineProperty(wrappedObject, keyName, {
enumerable: true,
configurable: true,
get() {
return wrapGuy.wrapObjectField_(apiObject, keyName);
}
});
});

return wrappedObject;
},

/**
* @type {!Map}
* @private
*/
wrappedFieldsCache_: new Map(),

/**
* Wraps single object field.
* @param {!Object} apiObject
* @param {string} keyName
* @return {?|undefined}
* @private
*/
wrapObjectField_(apiObject, keyName) {
let apiEntry = apiObject[keyName];

if (wrapGuy.wrappedFieldsCache_.has(apiEntry)) {
return wrapGuy.wrappedFieldsCache_.get(apiEntry);
}

let entryType = typeof apiEntry;
let wrappedField;
if (entryType == 'function') {
wrappedField = wrapGuy.wrapMethod_(apiObject, keyName);
}
if (entryType == 'object') {
wrappedField = wrapGuy.wrapObject_(apiEntry);
}

if (wrappedField) {
wrapGuy.wrappedFieldsCache_.set(apiEntry, wrappedField);
return wrappedField;
}
},

/**
* Wraps API method.
* @param {!Object} apiObject
* @param {string} methodName
* @return {!Function}
* @private
*/
wrapMethod_(apiObject, methodName) {
return function() {
return apiProxy.callMethod(apiObject, methodName, arguments);
}
}
};


let chromise = wrapGuy.wrapApi(global.chrome);

global.chromise = chromise;

}(this));
7 changes: 4 additions & 3 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ function ui82arr(arr, startOffset) {
return outarr
}
function str2ab(s) {
var arr = []
var buf = new ArrayBuffer(s.length);
var bufView = new Uint8Array(buf);
for (var i=0; i<s.length; i++) {
arr.push(s.charCodeAt(i))
bufView[i] = s.charCodeAt(i);
}
return new Uint8Array(arr).buffer
return buf;
}
WSC.ui82str = ui82str
WSC.str2ab = str2ab
Expand Down
74 changes: 74 additions & 0 deletions crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
(function() {

// function to create certificate
var createCrypto = function(cn, data) {
console.log(
'Generating 1024-bit key-pair and certificate for \"' + cn + '\".');
var keys = forge.pki.rsa.generateKeyPair(1024);
console.log('key-pair created.');

var cert = forge.pki.createCertificate();
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(
cert.validity.notBefore.getFullYear() + 10);
var attrs = [{
name: 'commonName',
value: cn
}, {
name: 'countryName',
value: 'SE'
}, {
shortName: 'ST',
value: 'test-st'
}, {
name: 'localityName',
value: 'testing server'
}, {
name: 'organizationName',
value: 'Web server for chrome'
}, {
shortName: 'OU',
value: 'WSC'
}];
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions([{
name: 'basicConstraints',
cA: true
}, {
name: 'keyUsage',
keyCertSign: true,
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true
}, {
name: 'subjectAltName',
altNames: [{
type: 6, // URI
value: 'http://localhost'
}]
}]);
// FIXME: add subjectKeyIdentifier extension
// FIXME: add authorityKeyIdentifier extension
cert.publicKey = keys.publicKey;

// self-sign certificate
cert.sign(keys.privateKey, forge.md.sha256.create());

// save data
data[cn] = {
cert: forge.pki.certificateToPem(cert),
privateKey: forge.pki.privateKeyToPem(keys.privateKey)
};

return data;
console.log('certificate created for \"' + cn + '\": \n');
};

WSC.createCrypto = (name, data) => { return createCrypto(name, data || {}); }

})();

1 change: 1 addition & 0 deletions makedeps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run `yarn make` or `npm run make` to build the dependency bundle for the main app
21 changes: 21 additions & 0 deletions makedeps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var m

m = require('react')
window.React = m

m = require('react-dom')
window.ReactDOM = m

m = require('@material-ui/core')
window.MaterialUI = m

m = require('@material-ui/lab');
window.MaterialUILab = m

m = require('underscore')
window._ = m

m = require('node-forge')
window.forge = m

module.exports = {}
19 changes: 19 additions & 0 deletions makedeps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "makedeps",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"make": "./node_modules/.bin/browserify index.js > ../assets/bundle.js"
},
"dependencies": {
"@material-ui/core": "^4.6.1",
"@material-ui/icons": "^4.5.1",
"@material-ui/lab": "^4.0.0-alpha.57",
"browserify": "^16.5.0",
"node-forge": "^0.10.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"underscore": "^1.9.1"
}
}
4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"minimum_chrome_version": "45",
"app": {
"background": {
"scripts": ["underscore.js","encoding.js","common.js","log-full.js","mime.js","buffer.js","request.js","stream.js","chromesocketxhr.js","connection.js","webapp.js","websocket.js","handlers.js","httplib.js","upnp.js","background.js"]
"scripts": ["underscore.js","encoding.js","common.js","assets/bundle.js",
"log-full.js", "mime.js", "buffer.js","request.js","crypto.js","stream.js", "chromesocketxhr.js",
"connection.js","webapp.js","websocket.js","handlers.js","httplib.js","upnp.js","background.js"]
}
},
"permissions": [
Expand Down
2 changes: 1 addition & 1 deletion minimize.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cat "underscore.js" "encoding.js" "common.js" "log-full.js" "mime.js" "buffer.js" "request.js" "stream.js" "chromesocketxhr.js" "connection.js" "webapp.js" "websocket.js" "upnp.js" "handlers.js" "httplib.js" > wsc-chrome.min.js
cat "underscore.js" "encoding.js" "common.js" "log-full.js" "mime.js" "buffer.js" "request.js" "crypto.js" "stream.js" "chromesocketxhr.js" "connection.js" "webapp.js" "websocket.js" "upnp.js" "handlers.js" "httplib.js" > wsc-chrome.min.js
6 changes: 6 additions & 0 deletions react-ui/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
["@babel/plugin-transform-react-jsx",{useBuiltIns:true}],
"@babel/plugin-syntax-class-properties"
]
}
Loading