Skip to content

Commit

Permalink
Test read hosts file from main script, implement safe api
Browse files Browse the repository at this point in the history
  • Loading branch information
LouWii committed Sep 5, 2021
1 parent 998eaf8 commit 76c438d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

`npm run watch`

`npm run typecheck`
`npm run typecheck`

`npm run lint`
48 changes: 48 additions & 0 deletions packages/main/src/hosts-file-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { accessSync, constants, readFile } from 'fs';

//const hostsFilePath = '/etc/hosts';
//const hostsDelimiterStart = '# ----- HostsFileEdit config - Do not delete -----';
//const hostsDelimiterEnd = '# ----- HostsFileEdit config - End -----';
//const regex = /(?:# ----- HostsFileEdit config - Do not delete -----\n)([\s\S]*)(?:# ----- HostsFileEdit config - End -----\n?)/g;
const hostsFilePaths: Record<string, string> = {
'linux': '/etc/hosts',
'darwin': '/etc/hosts',
'win32': 'C:\\Windows\\System32\\drivers\\etc\\hosts', // TODO: What if Windows is not on C:?
// Might be able to support others? https://nodejs.org/api/process.html#process_process_platform
};

export const readHostsFile = function(): Promise<string> {
return new Promise((resolve, reject) => {
const path = hostsFilePaths[process.platform];
if (!path) {
reject(`Platform ${process.platform} not supported`);
} else {
try {
accessSync(path, constants.R_OK);
} catch (error) {
reject(`Tried to read file ${path} but file is not readable`);
}

readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
}

// try {
// accessSync(path, constants.R_OK);
// } catch (error) {
// throw `Tried to read file ${path} but file is not readable`;
// }

// const content = readFileSync(path);

// return content;



});
};
9 changes: 7 additions & 2 deletions packages/main/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {app, BrowserWindow} from 'electron';
import {app, BrowserWindow, ipcMain} from 'electron';
import {join} from 'path';
import {URL} from 'url';

import {readHostsFile} from '/@/hosts-file-helper';

const isSingleInstance = app.requestSingleInstanceLock();

Expand Down Expand Up @@ -99,3 +99,8 @@ if (import.meta.env.PROD) {
.catch((e) => console.error('Failed check updates:', e));
}


// Events
ipcMain.handle('app:read-hosts-file', () => {
return readHostsFile();
});
8 changes: 7 additions & 1 deletion packages/preload/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {contextBridge} from 'electron';
import {contextBridge, ipcRenderer } from 'electron';

const apiKey = 'electron';
/**
Expand All @@ -9,6 +9,12 @@ const api: ElectronApi = {
platform: process.platform,
};

contextBridge.exposeInMainWorld('fileHelper', {
readFile: () => {
return ipcRenderer.invoke('app:read-hosts-file');
},
});

/**
* If contextIsolated enabled use contextBridge
* Else use fallback
Expand Down
5 changes: 5 additions & 0 deletions packages/preload/types/electron-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ interface ElectronApi {
readonly platform: Readonly<string>
}

interface FileHelper {
readonly readFile: () => Promise
}

declare interface Window {
electron: Readonly<ElectronApi>
electronRequire?: NodeRequire
fileHelper: FileHelper
}
8 changes: 6 additions & 2 deletions packages/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ export default defineComponent({
this.$store.commit('addNewHost');
},
saveHostsFile() {
let mm = 'tt';
mm.charAt(4);
const p = window.fileHelper.readFile();
p.then((result: string) => {
console.log(result);
}).catch((error: Error) => {
console.log('something was wrong', error);
});
},
importHostsList() {
let mm = 'tt';
Expand Down
13 changes: 0 additions & 13 deletions packages/renderer/src/hosts-file-helper.ts

This file was deleted.

0 comments on commit 76c438d

Please sign in to comment.