Solved

Proxy https-http

Posted on 2002-04-07
12
292 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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

10 Experts available now in Live!

Get 1:1 Help Now