Skip to content

Commit

Permalink
upload issue
Browse files Browse the repository at this point in the history
  • Loading branch information
reZach committed Jul 2, 2022
1 parent c7fd875 commit 0708545
Show file tree
Hide file tree
Showing 144 changed files with 490 additions and 4,160 deletions.
2 changes: 0 additions & 2 deletions CODE_OF_CONDUCT.md

This file was deleted.

7 changes: 0 additions & 7 deletions CONTRIBUTING.md

This file was deleted.

19 changes: 0 additions & 19 deletions LICENSE

This file was deleted.

78 changes: 0 additions & 78 deletions README.md

This file was deleted.

217 changes: 3 additions & 214 deletions app/electron/main.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,22 @@
const {
app,
protocol,
BrowserWindow,
session,
ipcMain,
Menu
BrowserWindow
} = require("electron");
const {
default: installExtension,
REDUX_DEVTOOLS,
REACT_DEVELOPER_TOOLS
} = require("electron-devtools-installer");
const SecureElectronLicenseKeys = require("secure-electron-license-keys");
const Protocol = require("./protocol");
const MenuBuilder = require("./menu");
const i18nextBackend = require("i18next-electron-fs-backend");
const i18nextMainBackend = require("../localization/i18n.mainconfig");
const Store = require("secure-electron-store").default;
const ContextMenu = require("secure-electron-context-menu").default;
const path = require("path");
const fs = require("fs");
const crypto = require("crypto");
const isDev = process.env.NODE_ENV === "development";
const port = 40992; // Hardcoded; needs to match webpack.development.js and package.json
const selfHost = `http://localhost:${port}`;

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
let menuBuilder;

async function createWindow() {

// If you'd like to set up auto-updating for your app,
// I'd recommend looking at https://github.com/iffy/electron-updater-example
// to use the method most suitable for you.
// eg. autoUpdater.checkForUpdatesAndNotify();

if (!isDev) {
// Needs to happen before creating/loading the browser window;
// protocol is only used in prod
protocol.registerBufferProtocol(Protocol.scheme, Protocol.requestHandler); /* eng-disable PROTOCOL_HANDLER_JS_CHECK */
}

const store = new Store({
path: app.getPath("userData")
});

// Use saved config values for configuring your
// BrowserWindow, for instance.
// NOTE - this config is not passcode protected
// and stores plaintext values
//let savedConfig = store.mainInitialStore(fs);

// Create the browser window.
win = new BrowserWindow({
width: 800,
Expand All @@ -64,54 +28,12 @@ async function createWindow() {
nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false,
contextIsolation: true,
enableRemoteModule: false,
additionalArguments: [`--storePath=${store.sanitizePath(app.getPath("userData"))}`],
preload: path.join(__dirname, "preload.js"),
/* eng-disable PRELOAD_JS_CHECK */
disableBlinkFeatures: "Auxclick"
enableRemoteModule: false
}
});

// Sets up main.js bindings for our i18next backend
i18nextBackend.mainBindings(ipcMain, win, fs);

// Sets up main.js bindings for our electron store;
// callback is optional and allows you to use store in main process
const callback = function (success, initialStore) {
console.log(`${!success ? "Un-s" : "S"}uccessfully retrieved store in main process.`);
console.log(initialStore); // {"key1": "value1", ... }
};

store.mainBindings(ipcMain, win, fs, callback);

// Sets up bindings for our custom context menu
ContextMenu.mainBindings(ipcMain, win, Menu, isDev, {
"loudAlertTemplate": [{
id: "loudAlert",
label: "AN ALERT!"
}],
"softAlertTemplate": [{
id: "softAlert",
label: "Soft alert"
}]
});

// Setup bindings for offline license verification
SecureElectronLicenseKeys.mainBindings(ipcMain, win, fs, crypto, {
root: process.cwd(),
version: app.getVersion()
});

// Load app
if (isDev) {
win.loadURL(selfHost);
} else {
win.loadURL(`${Protocol.scheme}://rse/index.html`);
}

win.webContents.on("did-finish-load", () => {
win.setTitle(`Getting started with secure-electron-template (v${app.getVersion()})`);
});
win.loadURL(selfHost);

// Only do these things when in development
if (isDev) {
Expand All @@ -136,70 +58,8 @@ async function createWindow() {
// when you should delete the corresponding element.
win = null;
});

// https://electronjs.org/docs/tutorial/security#4-handle-session-permission-requests-from-remote-content
const ses = session;
const partition = "default";
ses.fromPartition(partition) /* eng-disable PERMISSION_REQUEST_HANDLER_JS_CHECK */
.setPermissionRequestHandler((webContents, permission, permCallback) => {
const allowedPermissions = []; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest

if (allowedPermissions.includes(permission)) {
permCallback(true); // Approve permission request
} else {
console.error(
`The application tried to request permission for '${permission}'. This permission was not whitelisted and has been blocked.`
);

permCallback(false); // Deny
}
});

// https://electronjs.org/docs/tutorial/security#1-only-load-secure-content;
// The below code can only run when a scheme and host are defined, I thought
// we could use this over _all_ urls
// ses.fromPartition(partition).webRequest.onBeforeRequest({urls:["http://localhost./*"]}, (listener) => {
// if (listener.url.indexOf("http://") >= 0) {
// listener.callback({
// cancel: true
// });
// }
// });

menuBuilder = MenuBuilder(win, app.name);

// Set up necessary bindings to update the menu items
// based on the current language selected
i18nextMainBackend.on("initialized", (loaded) => {
i18nextMainBackend.changeLanguage("en");
i18nextMainBackend.off("initialized"); // Remove listener to this event as it's not needed anymore
});

// When the i18n framework starts up, this event is called
// (presumably when the default language is initialized)
// BEFORE the "initialized" event is fired - this causes an
// error in the logs. To prevent said error, we only call the
// below code until AFTER the i18n framework has finished its
// "initialized" event.
i18nextMainBackend.on("languageChanged", (lng) => {
if (i18nextMainBackend.isInitialized){
menuBuilder.buildMenu(i18nextMainBackend);
}
});
}

// Needs to be called before app is ready;
// gives our scheme access to load relative files,
// as well as local storage, cookies, etc.
// https://electronjs.org/docs/api/protocol#protocolregisterschemesasprivilegedcustomschemes
protocol.registerSchemesAsPrivileged([{
scheme: Protocol.scheme,
privileges: {
standard: true,
secure: true
}
}]);

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
Expand All @@ -211,10 +71,6 @@ app.on("window-all-closed", () => {
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
} else {
i18nextBackend.clearMainBindings(ipcMain);
ContextMenu.clearMainBindings(ipcMain);
SecureElectronLicenseKeys.clearMainBindings(ipcMain);
}
});

Expand All @@ -224,71 +80,4 @@ app.on("activate", () => {
if (win === null) {
createWindow();
}
});

// https://electronjs.org/docs/tutorial/security#12-disable-or-limit-navigation
app.on("web-contents-created", (event, contents) => {
contents.on("will-navigate", (contentsEvent, navigationUrl) => {
/* eng-disable LIMIT_NAVIGATION_JS_CHECK */
const parsedUrl = new URL(navigationUrl);
const validOrigins = [selfHost];

// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
if (!validOrigins.includes(parsedUrl.origin)) {
console.error(
`The application tried to navigate to the following address: '${parsedUrl}'. This origin is not whitelisted and the attempt to navigate was blocked.`
);

contentsEvent.preventDefault();
}
});

contents.on("will-redirect", (contentsEvent, navigationUrl) => {
const parsedUrl = new URL(navigationUrl);
const validOrigins = [];

// Log and prevent the app from redirecting to a new page
if (!validOrigins.includes(parsedUrl.origin)) {
console.error(
`The application tried to redirect to the following address: '${navigationUrl}'. This attempt was blocked.`
);

contentsEvent.preventDefault();
}
});

// https://electronjs.org/docs/tutorial/security#11-verify-webview-options-before-creation
contents.on("will-attach-webview", (contentsEvent, webPreferences, params) => {
// Strip away preload scripts if unused or verify their location is legitimate
delete webPreferences.preload;
delete webPreferences.preloadURL;

// Disable Node.js integration
webPreferences.nodeIntegration = false;
});

// https://electronjs.org/docs/tutorial/security#13-disable-or-limit-creation-of-new-windows
// This code replaces the old "new-window" event handling;
// https://github.com/electron/electron/pull/24517#issue-447670981
contents.setWindowOpenHandler(({
url
}) => {
const parsedUrl = new URL(url);
const validOrigins = [];

// Log and prevent opening up a new window
if (!validOrigins.includes(parsedUrl.origin)) {
console.error(
`The application tried to open a new window at the following address: '${url}'. This attempt was blocked.`
);

return {
action: "deny"
};
}

return {
action: "allow"
};
});
});
Loading

0 comments on commit 0708545

Please sign in to comment.