Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Proxy Server Bypass Help

    I'm creating a simple proxy server that is multi-threaded and will bypass all the host in the host array.
    Please help me to my code.

    I only want to use socket and streams for this:

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class ProxyServer extends Thread{
    
    	private ServerSocket ss;
    	private String[] host = {"www.google.com", "www.facebook.com"};
    	
    	private int remotePort = 80;
    	public static long timeout = 30000;
    		
        public ProxyServer(ServerSocket server) {
        	this.ss = server;
        }
        
        public static void main(String[] args){
        	try{
        		
        		System.out.println("Starting the Proxy Server..");
        		ServerSocket ss = new ServerSocket(50005);
        		System.out.println("Proxy server started at port " + ss.getLocalPort() + ".\r\n");
        		for(int i = 0; i < 5; i++){
        			ProxyServer proxy = new ProxyServer(ss);
        			proxy.start();
        		}
        	}catch(Exception e){
        		System.out.println(e);
        	}
        }
        
        public void run(){
        	
        	try{
        		Socket clientSocket = ss.accept();
        		InputStream clientIn = clientSocket.getInputStream();
        		OutputStream clientOut = new BufferedOutputStream(clientSocket.getOutputStream());
        		for(int i = 0; i < host.length; i++){
        			try{
        				Socket serverSocket = new Socket(host[i], remotePort);
    	    			System.out.println("Open connection to: " + serverSocket + "(timeout= " + timeout + " ms)\r\n");
    	    			InputStream serverIn = serverSocket.getInputStream();
    	    			OutputStream serverOut = new BufferedOutputStream(serverSocket.getOutputStream());
    	    			OpenConnection oc = new OpenConnection(clientIn, serverIn, clientOut, serverOut, clientSocket, serverSocket);
    	    			oc.run();
        			}catch(Exception e){
        				System.out.println(e);
        			}		
        		}	
        	}catch(Exception e){
        		System.out.println(e);
        	}
        }
    }

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class OpenConnection extends Thread{
    	
    	InputStream clientIn;
    	InputStream serverIn;
    	OutputStream clientOut;
    	OutputStream serverOut;
    	Socket clientSocket;
    	Socket serverSocket;
    	int timeout = 30000;
    	
        public OpenConnection(InputStream clientIn, InputStream serverIn, OutputStream clientOut, OutputStream serverOut, Socket c, Socket s) {
        	this.clientIn = clientIn;
        	this.serverIn = serverIn;
        	this.clientOut = clientOut;
        	this.serverOut = serverOut;
        	this.clientSocket = c;
        	this.serverSocket = s;
        }
        
        public void run(){
        	int r0 = -1, r1 = -1, ch = -1, i = -1;
        	long time0 = new Date().getTime();
        	long time1 = new Date().getTime();
        	try{
        		while(r0 != 0 || r1 != 0 || (time1 - time0) <= timeout) {
    				while((r0 = clientIn.available()) > 0) {
    					ch = clientIn.read();
    						if(ch != -1) {
    							serverOut.write(ch);
    							System.out.print((char)ch);
    						}
    					time0 = new Date().getTime();
    					serverOut.flush();
    				}
    				while((r1 = serverIn.available()) > 0) {
    					try{
    						ch = serverIn.read();
    						if(ch != -1) {
    							clientOut.write(ch);
    						}
    						time0 = new Date().getTime();
    						clientOut.flush();
    					}catch(Exception e){
    					}	
    				}
    			}
        	}catch(Exception e){
        		System.out.println(e);
        	}finally{
        		try{
        		clientIn.close();
        		clientOut.close();
        		serverIn.close();
        		serverOut.close();
        		clientSocket.close();
        		serverSocket.close();
        		}catch(Exception e){
        			System.out.println(e);
        		}
        	}
        } 
    }
    Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    Can you explain what the problem is with your code?
    Last edited by Norm; 12-04-2011 at 06:10 PM.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help


  4. #4
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    The problem is that the only site I can go is Google and not on Facebook. Maybe because of the for loop in the main method or the for loop in the ProxyServer.run :)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    Have you tried going only to the one site and does it work then?

    Why do you start 5 threads?

  6. #6
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    Yes, but even if i type Welcome to Facebook - Log In, Sign Up or Learn More, the browser only goes to Google..

    That for loop is for the multi thread, so that it can accept connections, the 5 is just for testing.. I can't make it while(true) {} :/

  7. #7
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    The proxy only accepts Google.com, even if I type Facebook.com . The for loop is for the threading so that it can accept multiple connection, the 5 is only for testing. I can't use while(true).

    When I put the oc.run() outside the try catch, it only now allows Facebook.com . :/
    Last edited by jrdncchr; 12-05-2011 at 04:31 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    You only need one ServerSocket for all your connections. When you get a connection, create a new thread and pass the socket from the connection to that thread.

  9. #9
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    Well, as you can see I only have 1 ServerSocket. :/

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    Why do you have more than one accept call for that one server socket?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    Can you explain what your code is supposed to do?
    The browsers are set to use your server as a proxy.
    The server is started.
    A browser tries to connect to a site: httP://somesite.com
    The connect is sent to your server
    What does your server do now???

  12. #12
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    I'm planning to thread or allow the 2 host "Google.com" and "Facebook.com" bypass the proxy. :/ but it turns out that it only allows 1?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    Can you explain what your code is supposed to do?
    The browsers are set to use your server as a proxy.
    The server is started.
    A browser tries to connect to a site: httP://somesite.com
    The connect is sent to your server
    What does your server do now???

  14. #14
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    The browsers are set to use my server as a proxy.
    I start the server at port 50005;
    Wait for a browser to connect;
    When the browser connect and typed any of the sites in the host[], it will allow it or bypass.
    The OpenConnection will start and start streaming or reading bytes from that site.
    But when the the typed site is not in the host[] it will not allow it.

    That's all, all I want is to allow the server to bypass all the sites in the host[] in my proxy. :/

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    What parts of your code works? Have you tested any of the parts by themselves to be sure that they work?

  16. #16
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    Yes they work, I think the error is that. When the socket of the server was created with the host name Google.com, the next socket of the server which is Facebook.com is not being recognized as a server that is allowed to connect to. I tried to put the oc.run() outside the try catch, and it turns out that it only allows Facebook.com :/

    I can't make it that multiple servers are allowed to be bypass from the proxy.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    Where does your code test what the site is that the browser is trying to connect to?
    It seems that it always tries to connect to the first site in the host array.
    Why call oc.run() which blocks the loop instead of creating a thread and starting it?

  18. #18
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    Hmm, i'm just debugging and turns that when i put the "www.google.com" as host in the Socket serverSocket = new Socket("www.google.com", 80), it allows to bypass the site Google. That's why i tried to loop it so that all the sites in the host[] will make a socket for that site. But it turns out that it only allows that it blocks the loop.

    Can you think of a code that will return the inputted site by the user in the browser into a string? Like:
    String inputtedHost = clientSocket.getInetAddress().getHostName() ? I like to store the value that the user inputted in the browser, like "www.google.com"

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Proxy Server Bypass Help

    Print out everything about the socket connection and what you read from it to see what URL the user entered in the browser. The original URL must be there somewhere.

  20. #20
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Re: Proxy Server Bypass Help

    I'll try, but please help me try bypass the host in the host[], using only streams and socket. thank you

Page 1 of 2 12 LastLast

Similar Threads

  1. proxy server
    By mahamudsust in forum Networking
    Replies: 3
    Last Post: 03-04-2013, 07:04 AM
  2. Proxy Server Development
    By Comediant in forum Java Servlet
    Replies: 0
    Last Post: 10-26-2011, 12:21 PM
  3. Trying to bypass proxy
    By dipanshu.agrawal in forum Networking
    Replies: 2
    Last Post: 04-16-2010, 03:46 PM
  4. proxy server
    By guneet singh in forum Networking
    Replies: 1
    Last Post: 01-18-2009, 02:06 PM
  5. Jakarta FTP via proxy server
    By megalon in forum Networking
    Replies: 0
    Last Post: 07-24-2008, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •