Solved

Proxy https-http

Posted on 2002-04-07
12
293 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now