wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes blocking a website Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "blocking a website" Watch "blocking a website" New topic
Author

blocking a website

ritesh thakur
Greenhorn

Joined: Jul 08, 2013
Posts: 13

this is my code for blocking a website.

System.out.print("\nEnter the URL of the website you want to block:");
System.out.print("\n\n URL: ");
BufferedReader fr=new BufferedReader(new InputStreamReader
(System.in));
try{


blockedsite[x]=fr.readLine();
int checkurlindex=blockedsite[x].indexOf(".");
if(checkurlindex==-1)
{System.out.print("\n\n*********");
System.out.print("\n* ERROR *");
System.out.print("\n*********");
System.out.print("\n\nYou have entered an ");
System.out.print("invalid URL. Please ");
System.out.print("enter again.\n");
continue;}
char c='.';
String temp="";
int count = 0;
for (int i = 0 ; i < blockedsite[x].length() ; i++) {
if ( blockedsite[x].charAt(i) == c ) {
count += 1;
}
}
int firstindex=0,secondindex=0;
if(count==1)
{
firstindex=blockedsite[x].indexOf(".");
searchsite[x]=blockedsite[x].substring(0, firstindex);
searchsite[x]="www."+searchsite[x];
}
else
{
firstindex=blockedsite[x].indexOf(".");
temp=blockedsite[x].substring(firstindex+1);
secondindex=temp.indexOf(".");
searchsite[x]=blockedsite[x].substring(0, (secondindex+firstindex+1));

}
x+=1;
System.out.print("\n\n");
for(int i=1;i<60;i++)
System.out.print("=");
System.out.print("\n\n");
System.out.print("\n\nThe website has been blocked.\n ");

is the code correct.?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
The code doesn't seem to accept or monitor either incoming or outgoing connections, so I'm not sure how it could be blocking anything...?

What is the underlying problem you're trying to solve?


Ping & DNS - my free Android networking tools app
ritesh thakur
Greenhorn

Joined: Jul 08, 2013
Posts: 13

Ulf Dittmer wrote:The code doesn't seem to accept or monitor either incoming or outgoing connections, so I'm not sure how it could be blocking anything...?

What is the underlying problem you're trying to solve?


this is the server side of the quote. can this help me blocking a website.?

public static void internetserver() throws Exception{

Thread thread=new Thread(){public void run(){


try {String host = "127.0.0.1";
int remoteport = 8080;
int localport = 111;
// Print a start-up message
// And start running the server
runServer(host, remoteport, localport); // never returns
} catch (Exception e) {
System.err.println(e);
}}};
thread.start();}


/**
* runs a single-threaded proxy server on
* the specified local port. It never returns.
*/
static int flagchk=0;

public static void runServer(String host, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);

final byte[] request = new byte[1024];
byte[] reply = new byte[4096];

while (true) {
Socket client = null, server = null;
try { flagchk=0;
// Wait for a connection on the local port
client = ss.accept();

final InputStream streamFromClient = client.getInputStream();
final OutputStream streamToClient = client.getOutputStream();

// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try {
server = new Socket(host, remoteport);
} catch (IOException e) {
PrintWriter out = new PrintWriter(streamToClient);
out.print("Proxy server cannot connect to " + host + ":"
+ remoteport + ":\n" + e + "\n");
out.flush();
client.close();
continue;
}

// Get server streams.
final InputStream streamFromServer = server.getInputStream();
final OutputStream streamToServer = server.getOutputStream();

// a thread to read the client's requests and pass them
// to the server. A separate thread for asynchronous.
Thread t = new Thread() {
public void run() {
int bytesRead;
try {
while ((bytesRead = streamFromClient.read(request)) != -1) {

String value = new String(request);
int urlindex = value.indexOf("GET");
int urlindex1=urlindex+4;
String urlvalue=value.substring(urlindex1);
int urlindex2=urlvalue.indexOf(" ");
String url=urlvalue.substring(0,urlindex2); //url
String hostsite1="",url1="";
char c='.';
String temp="";
int count = 0;
for (int i = 0 ; i < url.length() ; i++) {
if ( url.charAt(i) == c ) {
count += 1;
}
}
int firstindex=0,secondindex=0;
if(count==1)
{
firstindex=url.indexOf(".");
url1=url.substring(0, firstindex);
url1="www."+url1;
}
else
{
firstindex=url.indexOf(".");
temp=url.substring(firstindex+1);
secondindex=temp.indexOf(".");
if(firstindex<0)
{firstindex=0;}
if(secondindex<0)
{secondindex=0;}
url1=url.substring(0, (secondindex+firstindex+1));

} url=url1;
int thirdindex=url.indexOf("//");
if(thirdindex<0)
{thirdindex=0;}
url1=url.substring(thirdindex+2);



int hostindex = value.indexOf("Host:");
int hostindex1=hostindex+6;
String hostvalue=value.substring(hostindex1);
int hostindex2=hostvalue.indexOf("\n");
String hostsite=hostvalue.substring(0,hostindex2); //host

count = 0;
for (int i = 0 ; i < hostsite.length() ; i++) {
if ( hostsite.charAt(i) == c ) {
count += 1;
}
}
firstindex=0;secondindex=0;
if(count==1)
{
firstindex=hostsite.indexOf(".");
hostsite1=hostsite.substring(0, firstindex);
hostsite1="www."+hostsite1;
}
else
{
firstindex=hostsite.indexOf(".");
temp=hostsite.substring(firstindex+1);
secondindex=temp.indexOf(".");
hostsite1=hostsite.substring(0, (secondindex+firstindex+1));

}
if(firewallstatus==1)
{for(int i=0;i<x;i++)
{
if((hostsite1.equals(searchsite[i]))||(url1.equals(searchsite[i])))
{
flagchk=1;

PrintWriter out = new PrintWriter(streamToClient);
out.print("This website has been blocked by the FIREWALL. ");
out.flush();out.close();break;
}

}
}
if(flagchk==0)
{streamToServer.write(request, 0, bytesRead);
streamToServer.flush();
} }
} catch (IOException e) {}

// the client closed the connection to us, so close our
// connection to the server.
try {
streamToServer.close();
} catch (IOException e) {
}
}
};

// Start the client-to-server request thread running
t.start();

// Read the server's responses
// and pass them back to the client.
int bytesRead;
try {
while ((bytesRead = streamFromServer.read(reply)) != -1) {
streamToClient.write(reply, 0, bytesRead);
streamToClient.flush();
}
} catch (IOException e) {
}

// The server closed its connection to us, so we close our
// connection to our client.
streamToClient.close();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (server != null)
server.close();
if (client != null)
client.close();
} catch (IOException e) {
}
}
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Can you describe in words what that code is supposed to be doing? Especially, are you trying to block outgoing connections or incoming connections? "Blocking a web site" only makes sense for outgoing connections (because incoming connections are not tied to web sites, they're tied to IP addresses), but hard to tell from the code without knowing what it does.
ritesh thakur
Greenhorn

Joined: Jul 08, 2013
Posts: 13

Ulf Dittmer wrote:Can you describe in words what that code is supposed to be doing? Especially, are you trying to block outgoing connections or incoming connections? "Blocking a web site" only makes sense for outgoing connections (because incoming connections are not tied to web sites, they're tied to IP addresses), but hard to tell from the code without knowing what it does.


i want it to bound all the outgoing connections. if url matches to the one entered, my browser should not open it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Oh, it's not the Java process starting the connections? So what you're talking about is a proxy. Why do you want to write your own? There are any number of filtering proxies out there, even open source ones written in Java, if that's what you want to use.
ritesh thakur
Greenhorn

Joined: Jul 08, 2013
Posts: 13

Ulf Dittmer wrote:Oh, it's not the Java process starting the connections? So what you're talking about is a proxy. Why do you want to write your own? There are any number of filtering proxies out there, even open source ones written in Java, if that's what you want to use.


working on a project.
anyways, what i m not getting is, the code i posted previously, just matches the string name, the other code matches and blocks the site, but when i run the program, in menu, i have to use the previous code to block it, while second code alone can do the job.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Well, there's no menu in the code you posted, thus we don't know what might or might not be happening - so we can't speculate on anything.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: blocking a website
 
Similar Threads
how can i separate string?
Large Numbers
BubbleSort on Matrix
use of substrings
How to get substrings between sentinel characters?