Hi Pathik,
does the proxy you use allow HTTP CONNECT?
I did add a dirty hack to the Smack 3.x source code to do this (hard coded proxy, …) and it seems to work.
I’‘ll post it here so you can do what you want with it. I’'m not sure that it will be added to Smack.
Make sure to connect to :5223 (SSL-Port).
LG
//ConnectionConfiguration.java - new method:
public Socket setProxy(String proxyHost, String proxyPort, String host, int port)
{
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort);
SSLTunnelSocketFactory SSLTSF = new SSLTunnelSocketFactory(proxyHost,proxyPort);
Socket s = null;
try {
} catch (Exception e) {
e.printStackTrace();
}
return s;
} //XMPPConnection.java - connectUsingConfiguration()
//change
if (config.getSocketFactory() == null) {
this.socket = new Socket(host, port);
} else
{
this.socket = config.getSocketFactory().createSocket(host, port);
}
//to
this.socket = config.setProxy("10.1.1.1", "8080", host, port); //SSLTunnelSocketFactory.java - new class:
package org.jivesoftware.smack.util; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; public class SSLTunnelSocketFactory extends SSLSocketFactory
{
private SSLSocketFactory dfactory;
private String tunnelHost;
private int tunnelPort; public SSLTunnelSocketFactory(String proxyhost, String proxyport){
tunnelHost = proxyhost;
tunnelPort = Integer.parseInt(proxyport);
} public Socket createSocket(String host, int port)
throws IOException, UnknownHostException
{
return createSocket(null,host,port,true);
} public Socket createSocket(String host,int port,InetAddress clientHost,
int clientPort)
throws IOException,UnknownHostException
{
return createSocket(null,host,port,true);
} public Socket createSocket(InetAddress host,int port)
throws IOException
{
return createSocket(null,host.getHostName(),port,true);
} public Socket createSocket(InetAddress address,int port,
InetAddress clientAddress,int clientPort)
throws IOException
{
return createSocket(null,address.getHostName(),port,true);
}
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException,UnknownHostException
{
Socket tunnel = new Socket(tunnelHost,tunnelPort);
doTunnelHandshake(tunnel,host,port);
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (Exception e) {
}
dfactory = sslContext.getSocketFactory(); SSLSocket result = (SSLSocket)dfactory.createSocket(
tunnel,host,port,autoClose);
result.addHandshakeCompletedListener(
new HandshakeCompletedListener() {
public void handshakeCompleted(HandshakeCompletedEvent event) {
System.out.println("Handshake finished!");
System.out.println(
"\t CipherSuite:" + event.getCipherSuite());
System.out.println(
"\t SessionId " + event.getSession());
System.out.println(
"\t PeerHost " + event.getSession().getPeerHost());
}
}
);
result.startHandshake(); return result;
} private void doTunnelHandshake(Socket tunnel, String host, int port)
throws IOException
{
OutputStream out = tunnel.getOutputStream();
String msg = "CONNECT " + host + ":" + port + " HTTP/1.0\n"
+ "User-Agent: "
+ sun.net.www.protocol.http.HttpURLConnection.userAgent
+ "\r\n\r\n";
byte b[];
try {
b = msg.getBytes("ASCII7");
} catch (UnsupportedEncodingException ignored) {
b = msg.getBytes();
}
out.write(b);
out.flush();
byte reply[] = new byte[200];
int replyLen = 0;
int newlinesSeen = 0;
boolean headerDone = false; /* Done on first newline */ InputStream in = tunnel.getInputStream();
while (newlinesSeen < 2) {
int i = in.read();
if (i < 0) {
throw new IOException("Unexpected EOF from proxy");
}
if (i == ''\n'') {
headerDone = true;
++newlinesSeen;
} else if (i != ''\r'') {
newlinesSeen = 0;
if (!headerDone && replyLen < reply.length) {
reply[replyLen++] = (byte) i;
}
}
}
String replyStr;
try {
replyStr = new String(reply, 0, replyLen, "ASCII7");
} catch (UnsupportedEncodingException ignored) {
replyStr = new String(reply, 0, replyLen);
}
if(replyStr.toLowerCase().indexOf("200 connection established") == -1){
throw new IOException("Unable to tunnel through "
+ tunnelHost + ":" + tunnelPort
+ ". Proxy returns \"" + replyStr + "\"");
}
} public String[] getDefaultCipherSuites(){
return dfactory.getDefaultCipherSuites();
} public String[] getSupportedCipherSuites(){
return dfactory.getSupportedCipherSuites();
}
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
}