3

Greetings,

I would like to ask if there's a way to block website(s) from access on a computer(s) dynamically? I mean could this functionality be coded (on java native interface)?

Your response is highly appreciated.

Thanks, Cyril H.

Cyril Horad
  • 1,555
  • 3
  • 23
  • 35
  • what do you mean by blocking website from access on computer? do you want to implement firewall software or somehing similar to parential control? – gigadot Jan 13 '11 at 11:46
  • You need to word this question better. Do you want something that will run on the user's machine, or on the web server, or on a router between them? Do you want to prevent Java from accessing the web site, or prevent any web browser, or any program at all? – Dan R. Jan 13 '11 at 11:54
  • @Jigar: Ahhmm... Sorry about the insufficient data. The OS would be Windows XP Professional 32-bit SP3. – Cyril Horad Jan 13 '11 at 12:00
  • @gigadot: Implementing a parental control using Java is my objective. – Cyril Horad Jan 13 '11 at 12:00
  • @Dan R.: I would need to run it on the user's machine. I would want to prevent a website accessing to all browsers(example... facebook.com) I wouldn't like that computer accessing it... – Cyril Horad Jan 13 '11 at 12:02

2 Answers2

1

Yes, you can code a simple HTTP proxy service with Java:

http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm

Alternatively, there are plenty of existing proxy solutions out there might suit your needs out of the box:

http://www.roseindia.net/opensource/freeproxyservers.php

You would then configure the software/devices that access websites (e.g., your browser) to point to that proxy, so that all HTTP communication passed through it.

Your proxy could then restrict access to whatever URL(s) you wanted to, based on whatever logic you wanted to code up.

If you wanted to get really fancy/secure and require folks to use the proxy (and not to choose to bypass it), you could do that, but that's probably more than you need to, given your question.

kvista
  • 5,039
  • 1
  • 23
  • 25
  • Are they alternative other than using proxy server? – Cyril Horad Jan 13 '11 at 12:06
  • +1 but this would also rely on not allowing the user to change their browser settings or install a new browser (and probably lots of other circumventions) to get around it. – Qwerky Jan 13 '11 at 12:23
  • @Qwerky: I can't to that to meet the objectives. Is there a way? – Cyril Horad Jan 13 '11 at 12:54
  • @Cyril: Well, there's no other alternative than the concept of "intercepting network communications". Whether you do that at the HTTP layer or below is another story. The software on your network card could conceivably even allow such a setting, but I've never seen one. I'm a bit confused though -- if you're interested in alternatives to what is really not a common case, why do you want to code it (as your question suggests)? – kvista Jan 13 '11 at 12:56
  • Let me add one other option that is still in the realm of intercepting network communications -- implementing your own DNS server. In this case, just like the proxy, you would configure your software/devices (e.g., your O/S) to use your DNS server as a source for mapping URLs to IP addresses. You could then simply return a bogus or local IP address for any domains you want to block. But this isn't as flexible as the proxy case. You could code the DNS server in Java though, too. – kvista Jan 13 '11 at 12:59
  • @Kelly: I am building a computer management system for internet cafe. This was a big problem for the cafe about some customer visits unwanted website. Banning the domain on the switch/router wasn't a use. If I'm going to use the proxy case, would it be flexible to all browsers on a particular computer? – Cyril Horad Jan 13 '11 at 13:35
  • Yes, all of the major browsers support the notion of proxy-based HTTP because management of HTTP by an ISP (or the company itself) is a much requested feature. – kvista Jan 14 '11 at 13:41
0

You could append entries to your hosts file using the Files class, as shown in this post: How to append text to an existing file in Java?.

This works on all platforms (yes, all of them: including Windows, Mac, Linux, Android, and more), and blocks access for all browsers, without the need for a proxy or special browser extensions (which can be deleted in most cases).

Here is some simple code to start you off. Feel free to edit it to fit your needs:

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);
}

Imports for above method:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

Sample usage:

blockSite("www.example.com");

Note:

This needs to be run as an administrator (Windows) or using sudo (Mac, Linux).

This might not work for some platforms, as it was only tested on Ubuntu Linux.

P.S. If you're making parental control software, you should also look into blocking programs. Not all things you would want to block are on the Internet. Here is some simple code for that:

/**
    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) {}
    }
}

Imports for above code:

import java.io.IOException;

Sample usage:

blockPrograms(100, "chrome", "firefox");

Again, let me note this was only tested on Ubuntu Linux.

Henry Sanger
  • 143
  • 2
  • 15