Skip to content

Commit

Permalink
Proxy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jindrapetrik committed Jul 30, 2011
1 parent fd853e5 commit f5a1451
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 43 deletions.
36 changes: 23 additions & 13 deletions trunk/libsrc/jpproxy/src/com/jpexs/proxy/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ class Connection
Socket socket = null;
InputStream in = 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(){
SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault();
Expand All @@ -36,22 +57,11 @@ public void promoteToClientSSL(){
}

public void promoteToServerSSL(){
String ksName = "server.jks";
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();
try{
socket=sf.createSocket(socket,null,socket.getPort(),false);
((SSLSocket)socket).setUseClientMode(false);
}catch(Exception ex){

ex.printStackTrace();
}
try {
in = socket.getInputStream();
Expand Down
23 changes: 20 additions & 3 deletions trunk/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void run()
}
catch(SSLHandshakeException she)
{
client.close();
she.printStackTrace();
break;
}
catch (IOException e)
Expand All @@ -136,7 +136,7 @@ public void run()
}


if(request.getCommand().equals("CONNECT")){
if(request.getCommand().equals("CONNECT")){
secureServer=request.getHost();
securePort=request.getPort();
if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureServer)))){
Expand Down Expand Up @@ -392,7 +392,24 @@ HttpRelay createHttpsRelay(String secureHost,int securePort) throws IOException

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);
if(ProxyConfig.useHTTPSProxy){
Request connectReq=new Request(client);
Expand Down
29 changes: 17 additions & 12 deletions trunk/libsrc/jpproxy/src/com/jpexs/proxy/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public synchronized Reply recvReply(Request request)

try
{
return recv();
return recv();
}
catch (IOException e)
{
Expand Down Expand Up @@ -130,28 +130,33 @@ private void send(Request request) throws IOException
{

/* Prepare HTTP/1.1 request */
request.removeHeaderField("Proxy-Connection");
if(request.containsHeaderField("Connection")&&(request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))){
//request.removeHeaderField("Connection");
}else{
request.setHeaderField("Connection", "open");

request.removeHeaderField("Proxy-Connection");


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)
{
request.write(getOutputStream());
request.write(getOutputStream());
}
else
{
String oldStatusLine = request.statusLine;
StringBuffer head = new StringBuffer();
head.append(request.getCommand());
head.append(" ");
head.append(request.getPath());
head.append(request.getPath());
head.append(" ");
head.append("HTTP/1.0");
request.statusLine = head.toString();
Expand Down
15 changes: 1 addition & 14 deletions trunk/libsrc/jpproxy/src/com/jpexs/proxy/Https.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,7 @@ public Https(String host, int port) throws IOException

public Https(String host, int port, boolean isProxy) throws IOException
{
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();
super(host, port,isProxy);
}

private static String cacheKey(String host, int port)
Expand Down
2 changes: 1 addition & 1 deletion trunk/libsrc/jpproxy/src/com/jpexs/proxy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Main
public static boolean DEBUG_MODE = false;

public static void main(String argv[])
{
{
List<Replacement> replacements = new ArrayList<Replacement>();
if ((new File(REPLACEMENTSFILE)).exists()) {
try {
Expand Down
4 changes: 4 additions & 0 deletions trunk/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public class ProxyConfig {
public static String httpProxyHost="";
public static int httpProxyPort=0;

public static String httpsKeyStoreFile=null;
public static String httpsKeyStorePass=null;
public static String httpsKeyPass=null;

}

0 comments on commit f5a1451

Please sign in to comment.