2023-01-21 290
问候,
我想问一下是否有一种方法可以动态地阻止网站从计算机上访问?我的意思是(在Java本机界面上)可以编码此功能吗?
您的回应非常感谢.
谢谢,
Cyril H.
是的,您可以使用Java代码简单的HTTP代理服务:
网络协议/asimpleproxyserver.htm
另外,有很多现有代理解决方案的大量可能适合您的需求:
您的代理可以根据想要编码的任何逻辑来限制对您想要的任何URL的访问.
如果您想获得真正的花哨/安全,并要求人们使用代理(并且不要选择绕过它),则可以做到这一点,但这可能比您需要的要多得多.
您可以将条目附加到您的 hosts file 使用 C0>类,如本文所示:如何如何将文本附加到Java中的现有文件?.
这在所有平台上都起作用(是的,所有这些平台:包括Windows,Mac,Linux,Android等),并阻止所有浏览器的访问,而无需代理或特殊浏览器扩展程序(可以删除这些浏览器在大多数情况下).
这是一些简单的代码来开始您.随时编辑以满足您的需求:
public void blockSite(String url) {
// Note that this code only works in Java 7+,
// refer to the above link about appending files for more info
// Get OS name
String OS = System.getProperty("os.name").toLowerCase();
// Use OS name to find correct location of hosts file
String hostsFile = "";
if ((OS.indexOf("win") >= 0)) {
// Doesn't work before Windows 2000
hostsFile = "C:\\Windows\\System32\\drivers\\etc\\hosts";
} else if ((OS.indexOf("mac") >= 0)) {
// Doesn't work before OS X 10.2
hostsFile = "etc/hosts";
} else if ((OS.indexOf("nux") >= 0)) {
hostsFile = "/etc/hosts";
} else {
// Handle error when platform is not Windows, Mac, or Linux
System.err.println("Sorry, but your OS doesn't support blocking.");
System.exit(0);
}
// Actually block site
Files.write(Paths.get(hostsFile),
("127.0.0.1 " + url).getBytes(),
StandardOpenOption.APPEND);
}
以上方法导入:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
示例用法:
blockSite("www.example.com");
注意:
这需要作为管理员(Windows)或使用sudo(Mac,Linux)运行.
这可能对某些平台不起作用,因为它仅在Ubuntu Linux上进行了测试.
P.S.如果您要制作父母控制软件,则还应该考虑阻止程序.并非您想阻止的所有内容都在互联网上.这是一些简单的代码:
/**
Blocks programs.
@param programs - The array of process names.
@param timeout - The time between blocks, in milliseconds.
This parameter should not be set below 100, to avoid slowdown.
@author https://stackoverflow.com/users/5905216/h-a-sanger
*/
public void blockPrograms(int timeout, String...programs) throws IOException {
// Get OS name
String OS = System.getProperty("os.name").toLowerCase();
// Identify correct blocking command for OS
String command = "";
if ((OS.indexOf("win") >= 0)) {
command = "taskkill /f /im ";
} else if ((OS.indexOf("mac") >= 0) || (OS.indexOf("nux") >= 0)) {
command = "killall ";
} else {
// Handle error when platform is not Windows, Mac, or Linux
System.err.println("Sorry, but your OS doesn't support blocking.");
System.exit(0);
}
// Start blocking!
while(true) {
// Cycle through programs list
for(int i = 0; i < programs.length; i++) {
// Block program
Runtime.getRuntime().exec(command + programs[i]);
}
// Timeout
try { Thread.sleep(timeout); } catch(InterruptedException e) {}
}
}
以上代码导入:
import java.io.IOException;
示例用法:
blockPrograms(100, "chrome", "firefox");
再次,让我注意这仅在Ubuntu Linux上进行了测试.
以上所述是小编给大家介绍的阻止所有浏览器访问一个网站,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/26238.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态