Solved

Proxy https-http

Posted on 2002-04-07
12
297 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
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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 80
jmeter usage 4 31
learn programming 8 66
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 16
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

685 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