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
/Mike
If you increase the points I will give you the solution!
ASKER
how much 200 all together?
300
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){}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
import java.io.*;
import java.util.*;
public class HttpProxyServer
{
public static void main(String args[]) throws Exception
{
int port=Integer.parseInt(args
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!
ASKER
That was easy!! I think 110 was abit to much. What about 50?
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=(SSLServerSocketFact ory)SSLSer verSocketF actory.get Default();
SSLServerSocket ss=(SSLServerSocket)sslsf. createServ erSocket(p ort);
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=s erver -Djavax.net.ssl.keyStorePa ssword=key storepassw ord 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!
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
SSLServerSocketFactory sslsf=(SSLServerSocketFact
SSLServerSocket ss=(SSLServerSocket)sslsf.
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=s
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!
ASKER
Great!
The enforcing of the browser will not be as complicated since I intend to write it myself (or actually already have)
The enforcing of the browser will not be as complicated since I intend to write it myself (or actually already have)