?
Solved

Proxy https-http

Posted on 2002-04-07
12
Medium Priority
?
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 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