Solved

Proxy https-http

Posted on 2002-04-07
12
296 Views
Last Modified: 2010-03-31
I want a simple java program that can act as a proxy. I should be able to surf to it with https and it should reconnect it with http to the destination server. Does anyone have the source to such a program? Preferable it should be pretty simple to use.

/Mike





0
Comment
Question by:mdoland
  • 8
  • 4
12 Comments
 
LVL 3

Expert Comment

by:msterjev
ID: 6924044
If you increase the points I will give you the solution!
0
 

Author Comment

by:mdoland
ID: 6924065
how much 200 all together?
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6924129
300
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:msterjev
ID: 6924198
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6924199
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6924203
If you grade this I will give you https(SSL Socket) solution.

import java.io.*;

public class StreamCopier extends Thread
{
     InputStream in;
     OutputStream out;
     
     public StreamCopier(InputStream in,OutputStream out)
     {
          this.in=in;
          this.out=out;
          start();
     }
     
     public void run()
     {
          byte[] buffer=new byte[1024];
          int cb=0;
          try
          {
               do
               {
                    cb=in.read(buffer,0,1024);
                    if(cb>0)
                    {
                         out.write(buffer,0,cb);
                         out.flush();
                    }
               }
               while(cb!=-1);
               in.close();
               out.close();
          }
          catch(Exception e){}
     }
}
0
 
LVL 3

Accepted Solution

by:
msterjev earned 300 total points
ID: 6924204
import java.net.*;
import java.io.*;

public class HttpRequest extends Thread
{
     protected Socket clientSocket;
     
     public HttpRequest(Socket s)
     {
          this.clientSocket=s;
          start();
     }
     
     public void run()
     {
          DataInputStream clientIn=null;
          DataOutputStream clientOut=null;
          Socket webSocket=null;
          DataInputStream webIn=null;
          DataOutputStream webOut=null;
          String commandLine=null;
          String parseLine=null;
          String host;
          String strPort;
          int pos,port;
          try
          {
               clientIn=new DataInputStream(clientSocket.getInputStream());
               clientOut=new DataOutputStream(clientSocket.getOutputStream());
               commandLine=clientIn.readLine();
               if(commandLine!=null)
               {
                    parseLine=commandLine;
                    pos=parseLine.indexOf("//");
                    if(pos!=-1)
                    {
                         parseLine=parseLine.substring(pos+2);
                         pos=parseLine.indexOf(" ");
                         if(pos!=-1)
                              parseLine=parseLine.substring(0,pos);
                         pos=parseLine.indexOf("/");
                         if(pos!=-1)
                              parseLine=parseLine.substring(0,pos);
                         host=parseLine;
                         pos=parseLine.indexOf(":");
                         if(pos!=-1)
                         {
                              strPort=parseLine.substring(pos+1).trim();
                              host=parseLine.substring(0,pos);
                              port=Integer.parseInt(strPort);
                         }
                         else
                              port=80;
                         webSocket=new Socket(host,port);
                         webIn=new DataInputStream(webSocket.getInputStream());
                         webOut=new DataOutputStream(webSocket.getOutputStream());
                         webOut.writeBytes(commandLine+"\r\n");
                         StreamCopier clientCopier=new StreamCopier(clientIn,webOut);
                         StreamCopier webCopier=new StreamCopier(webIn,clientOut);
                         clientCopier.join();
                         webCopier.join();
                         webSocket.close();
                         clientSocket.close();
                    }
               }
          }
          catch(Exception e){}
          finally
          {
               try
               {
                    if(clientSocket!=null) clientSocket.close();
                    if(webSocket!=null) webSocket.close();
               }
               catch(Exception ignored){}
          }
     }
}
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6924205
import java.net.*;
import java.io.*;
import java.util.*;

public class HttpProxyServer
{
     public static void main(String args[]) throws Exception
     {
          int port=Integer.parseInt(args[0]);
          ServerSocket ss=new ServerSocket(port);
          System.out.println(new Date().toGMTString()+"> Http Proxy Server started on port "+port+" . . . ");
          while(true)
          {
               new HttpRequest(ss.accept());
          }
     }
}

P.S.

I'm just connected and writing this through above proxy code!
0
 

Author Comment

by:mdoland
ID: 6924628
That was easy!! I think 110 was abit to much. What about 50?
0
 

Author Comment

by:mdoland
ID: 6924629
Just kidding!
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6924745
About ssl:

First at all you must install JSSE. Download it from Sun and follow instalattion instructions(there are three files: jsse.jar,jnet.jar and jcert.jar , put them into jre\lib\ext and modify security.properties as the documentation says).

You must generate certificate for your server:

keytool -keystore server -genkey -keyalg rsa

and follow the questions on the screen!

The modified HttpProxyServer code would be:

import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import java.util.Date;



public class HttpProxyServer
{
    public static void main(String args[]) throws Exception
    {

           int port=Integer.parseInt(args[0]);
           SSLServerSocketFactory sslsf=(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
           SSLServerSocket ss=(SSLServerSocket)sslsf.createServerSocket(port);
          System.out.println(new Date().toGMTString()+"> Http Proxy Server started on port "+port+". . . ");
         while(true)
         {
                new HttpRequest(ss.accept());
         }
    }
}


Run the server with following command:

java -Djavax.net.ssl.keyStore=server -Djavax.net.ssl.keyStorePassword=keystorepassword HttpProxyServer 443

The question is how you would enforce your browser to have an SSL proxy?
The code presented above can be ServerProxy like https entra for existing server for example IIS (for filtering or something else). In that case you need to remove commandLine parsing in HttpRequest because both proxy and original server runs on the same machine!
0
 

Author Comment

by:mdoland
ID: 6925450
Great!
The enforcing of the browser will not be as complicated since I intend to write it myself (or actually already have)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question