我一直在尝试使用netsh advfirewall为一个电子JavaScript应用程序阻止一个特定网站的所有ip。这是我的代码。
const { app, BrowserWindow } = require('electron')
const { exec } = require('child_process')
const dns = require('dns')
const url = require('url')
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
})
win.loadFile('Screens/index.html')
blockWebsite('littlebigsnake.com')
win.setMenu(null)
}
app.whenReady().then(() => {
createWindow()
})
function blockWebsite(urlString) {
// grab host name from input
const { hostname } = url.parse(`http://${urlString}`)
// turn hostname into all ips
dns.resolve(hostname, 'A', (err, addresses) => {
if (err) {
console.error(`Error: ${err}`)
return
}
// block each ip address through the firewall
addresses.forEach(address => {
exec(`netsh advfirewall firewall add rule name="Block ${urlString}" dir=out action=block remoteip=${address}`, (error, stdout, stderr) => {
if (error) {
console.error(`${error}`)
return
}
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
})
})
})
}
这是我得到的错误netsh advfirewall firewall add rule name="Block littlebigsnak.com;Dir =out action=block remoteip=104.26.9.138
错误:Command failed: netsh advfirewall firewall add rule name="Block littlebigsnak.com "remoteip=172.67.71.90
错误:Command failed: netsh advfirewall firewall add rule name="Block littlebigsnak.com "Dir =out action=block remoteip=104.26.8.138
我明白了
const blockWebsite = (website) => {
const command = `echo 127.0.0.1 ${website} >> C:\Windows\System32\drivers\etc\hosts`
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`${error}`)
return
}
console.log(`Success: ${stdout}`)
})
}
把网址写在hosts文件里。然后,hosts文件将重定向到该网站的所有外发尝试,导致崩溃(不完全是,但足够接近)。我不能得到它的工作没有管理特权,但是。所以你需要以admin的身份运行。