Proxy fixes

This commit is contained in:
Jindra Pet��k
2011-07-30 16:56:19 +02:00
parent fd853e51bf
commit f5a1451b9f
6 changed files with 66 additions and 43 deletions
@@ -22,6 +22,27 @@ class Connection
Socket socket = null; Socket socket = null;
InputStream in = null; InputStream in = null;
OutputStream out = null; OutputStream out = null;
static SSLSocketFactory sf;
static{
String ksName = ProxyConfig.httpsKeyStoreFile;
if(ksName!=null){
char ksPass[] = ProxyConfig.httpsKeyStorePass.toCharArray();
char ctPass[] = ProxyConfig.httpsKeyPass.toCharArray();
try{
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(ksName), ksPass);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ctPass);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
sf=sc.getSocketFactory();
}catch(Exception ex){
}
}
}
public void promoteToClientSSL(){ public void promoteToClientSSL(){
SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault();
@@ -36,22 +57,11 @@ class Connection
} }
public void promoteToServerSSL(){ public void promoteToServerSSL(){
String ksName = "server.jks"; try{
char ksPass[] = "ServerJKS".toCharArray();
char ctPass[] = "ServerKey".toCharArray();
try{
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(ksName), ksPass);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ctPass);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
SSLSocketFactory sf=sc.getSocketFactory();
socket=sf.createSocket(socket,null,socket.getPort(),false); socket=sf.createSocket(socket,null,socket.getPort(),false);
((SSLSocket)socket).setUseClientMode(false); ((SSLSocket)socket).setUseClientMode(false);
}catch(Exception ex){ }catch(Exception ex){
ex.printStackTrace();
} }
try { try {
in = socket.getInputStream(); in = socket.getInputStream();
@@ -126,7 +126,7 @@ class Handler implements Runnable
} }
catch(SSLHandshakeException she) catch(SSLHandshakeException she)
{ {
client.close(); she.printStackTrace();
break; break;
} }
catch (IOException e) catch (IOException e)
@@ -136,7 +136,7 @@ class Handler implements Runnable
} }
if(request.getCommand().equals("CONNECT")){ if(request.getCommand().equals("CONNECT")){
secureServer=request.getHost(); secureServer=request.getHost();
securePort=request.getPort(); securePort=request.getPort();
if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureServer)))){ if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureServer)))){
@@ -392,7 +392,24 @@ class Handler implements Runnable
if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureHost)))) if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureHost))))
{ {
http=Https.open(secureHost,securePort,ProxyConfig.useHTTPSProxy); if(ProxyConfig.useHTTPSProxy){
http=Https.open(ProxyConfig.httpsProxyHost,ProxyConfig.httpsProxyPort,true);
Request connectReq=new Request(null);
connectReq.setStatusLine("CONNECT "+secureHost+":"+securePort+" HTTP/1.1");
connectReq.setCommand("CONNECT");
connectReq.setURL(secureHost+":"+securePort);
connectReq.setProtocol("HTTP/1.1");
try {
http.sendRequest(connectReq);
Reply rep=http.recvReply(connectReq);
} catch (RetryRequestException ex) {
}
((Https)http).promoteToClientSSL();
}else{
http=Https.open(secureHost,securePort,false);
((Https)http).promoteToClientSSL();
}
/*http = new Http(request.getHost(),request.getPort(),ProxyConfig.useHTTPSProxy); /*http = new Http(request.getHost(),request.getPort(),ProxyConfig.useHTTPSProxy);
if(ProxyConfig.useHTTPSProxy){ if(ProxyConfig.useHTTPSProxy){
Request connectReq=new Request(client); Request connectReq=new Request(client);
@@ -86,7 +86,7 @@ class Http extends HttpConnection
try try
{ {
return recv(); return recv();
} }
catch (IOException e) catch (IOException e)
{ {
@@ -130,20 +130,25 @@ class Http extends HttpConnection
{ {
/* Prepare HTTP/1.1 request */ /* Prepare HTTP/1.1 request */
request.removeHeaderField("Proxy-Connection");
if(request.containsHeaderField("Connection")&&(request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))){ request.removeHeaderField("Proxy-Connection");
//request.removeHeaderField("Connection");
}else{
request.setHeaderField("Connection", "open"); if(!proxy){
if(request.containsHeaderField("Connection")&&(request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))){
}else{
request.setHeaderField("Connection", "open");
}
if (!request.containsHeaderField("Host"))
{
request.setHeaderField("Host", request.getHost());
}
} }
if (!request.containsHeaderField("Host"))
{
request.setHeaderField("Host", request.getHost());
}
if (proxy) if (proxy)
{ {
request.write(getOutputStream()); request.write(getOutputStream());
} }
else else
{ {
@@ -151,7 +156,7 @@ class Http extends HttpConnection
StringBuffer head = new StringBuffer(); StringBuffer head = new StringBuffer();
head.append(request.getCommand()); head.append(request.getCommand());
head.append(" "); head.append(" ");
head.append(request.getPath()); head.append(request.getPath());
head.append(" "); head.append(" ");
head.append("HTTP/1.0"); head.append("HTTP/1.0");
request.statusLine = head.toString(); request.statusLine = head.toString();
@@ -23,20 +23,7 @@ public class Https extends Http {
public Https(String host, int port, boolean isProxy) throws IOException public Https(String host, int port, boolean isProxy) throws IOException
{ {
super(host, port,isProxy); super(host, port,isProxy);
if(isProxy){
Request connectReq=new Request(null);
connectReq.setCommand("CONNECT");
connectReq.setURL(host+":"+port);
connectReq.setProtocol("HTTP/1.1");
try {
sendRequest(connectReq);
recvReply(connectReq);
} catch (RetryRequestException ex) {
}
}
promoteToClientSSL();
} }
private static String cacheKey(String host, int port) private static String cacheKey(String host, int port)
@@ -15,7 +15,7 @@ public class Main
public static boolean DEBUG_MODE = false; public static boolean DEBUG_MODE = false;
public static void main(String argv[]) public static void main(String argv[])
{ {
List<Replacement> replacements = new ArrayList<Replacement>(); List<Replacement> replacements = new ArrayList<Replacement>();
if ((new File(REPLACEMENTSFILE)).exists()) { if ((new File(REPLACEMENTSFILE)).exists()) {
try { try {
@@ -36,4 +36,8 @@ public class ProxyConfig {
public static String httpProxyHost=""; public static String httpProxyHost="";
public static int httpProxyPort=0; public static int httpProxyPort=0;
public static String httpsKeyStoreFile=null;
public static String httpsKeyStorePass=null;
public static String httpsKeyPass=null;
} }