Wednesday, February 25, 2009

HTTP and HTTPS Tunneling using java

/*
* HttpTunnel.java
* @author Jijo Mathew
*/


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Security;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
public class HttpTunnel {

public HttpTunnel() {
}
/**
*
* This method is used for HTTP tunneling
*/
static public Object transaction(Object data, String url) {

URL server = null;
HttpURLConnection con = null;

try {
server = new URL(url);

} catch(MalformedURLException e) {
System.out.println("URL exception: " + e );
return null;
}


// send request
ObjectInputStream response = null;
Object result = null;
try {

con = (HttpURLConnection) server.openConnection();


con.setDoOutput(true);


con.setUseCaches(false);
con.setConnectTimeout(20000);

con.setRequestProperty("Content-Type", "application/octet-stream");

OutputStream out = con.getOutputStream();
ObjectOutputStream request = new ObjectOutputStream(new BufferedOutputStream(con.getOutputStream()));
request.writeObject(data);
request.flush();
request.close();

// get the result input stream
response = new ObjectInputStream(new BufferedInputStream(con.getInputStream()));

// read response back from the server
result = response.readObject();

} catch(IOException ignored) {
ignored.printStackTrace();
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
finally {
try {

con=null;
server=null;

if( response!=null ) {
response.close();
}
} catch(IOException ex) {}
}



return result;
}

/***
* This method is used for HTTPS tunneling
* @param data (Object to send)
* @param url (domain name)
* @param keyPath(SSL key path)
* @return
*/
static public Object transaction(Object data, String url,String keyPath) {
URL server = null;
HttpsURLConnection con = null;
try {
System.setProperty("javax.net.ssl.trustStore", keyPath);
System.setProperty("java.protocol.handler.pkgs","javax.net.ssl");
Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider());

server = new URL(url);
} catch(MalformedURLException e) {
System.out.println("URL exception: " + e );
return null;
}
// send request
ObjectInputStream response = null;
Object result = null;
try {
con = (HttpsURLConnection) server.openConnection();
con.setHostnameVerifier(new HostnameVerifier()
{
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(20000);
con.setRequestProperty("Content-Type", "application/octet-stream");
OutputStream out = con.getOutputStream();
ObjectOutputStream request = new ObjectOutputStream(new BufferedOutputStream(con.getOutputStream()));
request.writeObject(data);
request.flush();
request.close();

// get the result input stream
response = new ObjectInputStream(new BufferedInputStream(con.getInputStream()));

// read response back from the server
result = response.readObject();
//System.out.println(result);
} catch(IOException ignored) {
ignored.printStackTrace();
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
finally {
try {

con=null;
server=null;

if( response!=null ) {
response.close();
}
} catch(IOException ex) {}
}


return result;
}

public static void main(String args[]) {
String object = "helooo";
//HTTP tunneling
transaction(object,"http://yourdomain.com");
//HTTPS tunneling
transaction(object,"https://yourdomain.com","C:/Program Files/Java/jdk1.6.0_10/jre/lib/security/cacerts");

}

}

1 comment:

Anonymous said...

Gr8 help from you