-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
128 lines (119 loc) · 2.77 KB
/
index.js
File metadata and controls
128 lines (119 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const path = require('path')
const {app , BrowserWindow, ipcMain } = require('electron');
const fs = require('fs');
const Alert = require("electron-alert");
let alert = new Alert();
let win;
const CHANNEL_NAME = 'main';
const CHANNEL_NAME2 = 'deblock';
const redirect ="127.0.0.1"
function createWindow () {
win = new BrowserWindow({
title:'web blocker & deblocker',
width : 720,
height : 400,
center:true,
resizable:false,
maximizable:false,
icon: __dirname + '/icon2.png',
roundedCorners: false,
webPreferences:{
nodeIntegration: true,
contextIsolation: false,
preload:path.resolve("./preload.js")
}
})
//win.openDevTools();
win.loadFile(path.join(__dirname, 'index.html'));
win.setMenu(null)
win.on('closed',()=>{
win=null;
})
}
let hostfile = "";
function getOS(){
if (process.platform === "win32") {
hostfile = "c:\\Windows\\System32\\Drivers\\etc\\hosts";
} else {
hostfile = "/etc/hosts";
}
return hostfile;
}
ipcMain.on(CHANNEL_NAME, (event, data) => {
hostfile = getOS();
newData = redirect + " " +data +"\n";
fs.readFile(hostfile, function (err, searchData) {
if (err) {console.log(err);}
if(searchData.indexOf(newData) >= 0){
let swalOptions = {
icon: 'warning',
title: data +' alerdy has been blocked',
};
alert.fire(swalOptions);
}else {
fs.appendFile(hostfile, newData, (err)=> {
if(err){
let swalOptions = {
icon: 'error',
title: 'Oops...',
text: 'Something went wrong!',
footer:"we can't find the hosts file"
};
alert.fire(swalOptions);
}
else {
let swalOptions = {
title: data+" has been blocked",
icon: "success",
showConfirmButton: false,
timer: 3000
};
alert.fire(swalOptions);
}
});
}
});
});
ipcMain.on(CHANNEL_NAME2, (event, data) =>{
hostfile = getOS();
newData = redirect + " " +data +"\n" ;
fs.readFile(hostfile, function (err, searchData) {
if (err) throw err;
if(searchData.indexOf(newData) >= 0){
fs.readFile(hostfile, 'utf-8', (err)=>{
if (err) {
let swalOptions = {
icon: 'error',
title: 'Oops...',
text: 'Something went wrong!',
footer:"we can't find the hosts file"
};
alert.fire(swalOptions);
} else {
var d = fs.readFileSync(hostfile, 'utf-8');
var newValue = d.replace(redirect +" " +data+'\n', '');
fs.writeFileSync(hostfile, newValue, 'utf-8');
let swalOptions = {
title: data+" has been unlocked",
icon: "success",
showConfirmButton: false,
timer: 3000
};
alert.fire(swalOptions);
}
});
}else {
let swalOptions = {
icon: 'warning',
title:"We can't find "+ data +' in the blocked list',
};
alert.fire(swalOptions);
}
});
})
app.on('ready',createWindow)
app.on('activate', ()=> {
if(win === null){
createWindow()
}
})