log("Connecting to:"+notMacIP+":"+notMacPort);
Socket sockOUT = new Socket(notMacIP,Integer.parseInt(notMacPort));
if (Integer.parseInt(notMacPort) == 443)
{
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) {}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
sockOUT = (SSLSocket)sc.getSocketFactory().createSocket(sockOUT,notMacIP,Integer.parseInt(notMacPort),true);
((SSLSocket)sockOUT).setUseClientMode(true);
((SSLSocket)sockOUT).startHandshake();
}
log("2.)Got connection:"+notMacIP+":"+notMacPort);
new Thread(new HttpTunnel(null,sockIN,sockOUT,notMacIP,notMacPort,"tunnel")).start();//sock1 to sock2 read & write
new Thread(new HttpTunnel(null,sockOUT,sockIN,notMacIP,notMacPort,"tunnel")).start();//and sock2 to sock1 read & write
}
}
catch(Exception e)
{
log(e);
}
}
else if (type.equalsIgnoreCase("mainServer"))
{
try
{
ServerSocket ss = null;
if (port == 443) ss = getServerSocket(port,ip,keystore,keystorepass,keypass);
else if (ip == null) ss = new ServerSocket(port);
else ss = new ServerSocket(port,1000,InetAddress.getByName(ip));
log("Listening on: "+type+" "+ip+":"+port);
while(true)
{
try
{
Socket sockIN = ss.accept();
BufferedInputStream bi = new BufferedInputStream(sockIN.getInputStream());
Vector headers = getHeaders(bi);
String host = "";
int skipBytes = 0;
for (int x=0; x<headers.size(); x++)
{
String data = headers.elementAt(x).toString();
if (data.toUpperCase().startsWith("HOST:"))
{
host = data.substring(data.indexOf(":")+1).trim();
if (host.indexOf(":") >= 0) host = host.substring(0,host.indexOf(":"));
}
}
//validate username. if ok, continue.
boolean ok = true;
if (ok && (host.toUpperCase().endsWith(".MAC.COM") || host.toUpperCase().endsWith(".APPLE.COM")))
{
bi.skip(skipBytes);
Socket sockOUT = new Socket("127.0.0.1",53443);
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) {}
}};