Link to home
Start Free TrialLog in
Avatar of mdoland
mdoland

asked on

Proxy https-http

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





Avatar of msterjev
msterjev

If you increase the points I will give you the solution!
Avatar of mdoland

ASKER

how much 200 all together?
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){}
     }
}
ASKER CERTIFIED SOLUTION
Avatar of msterjev
msterjev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
Avatar of mdoland

ASKER

That was easy!! I think 110 was abit to much. What about 50?
Avatar of mdoland

ASKER

Just kidding!
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!
Avatar of mdoland

ASKER

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