From ed53ba9d0f71c2ca8480a23e7e95105ccac10f56 Mon Sep 17 00:00:00 2001 From: "honfika@gmail.com" Date: Mon, 1 Sep 2014 16:56:55 +0200 Subject: [PATCH] format proxy code, allow to change swf on the fly --- .gitignore | 4 +- examples/DeobfuscatorSample.java | 4 + lib/jpproxy.jar | Bin 86040 -> 85995 bytes .../src/com/jpexs/decompiler/flash/SWF.java | 3 + .../com/jpexs/decompiler/flash/abc/ABC.java | 5 + .../deobfuscation/ActionDeobfuscator.java | 5 + .../ActionDeobfuscatorSimple.java | 5 + .../flash/helpers/SWFDecompilerListener.java | 2 + .../flash/helpers/SWFDecompilerPlugin.java | 16 + .../flash/ActionScript2DeobfuscatorTest.java | 4 +- .../src/com/jpexs/proxy/ByteArray.java | 1 + .../src/com/jpexs/proxy/CatchedListener.java | 40 +- .../src/com/jpexs/proxy/Cleanable.java | 4 +- .../jpproxy/src/com/jpexs/proxy/Client.java | 42 +- .../src/com/jpexs/proxy/Connection.java | 161 ++- libsrc/jpproxy/src/com/jpexs/proxy/Copy.java | 44 +- .../jpproxy/src/com/jpexs/proxy/Handler.java | 998 ++++++++---------- libsrc/jpproxy/src/com/jpexs/proxy/Http.java | 477 ++++----- .../src/com/jpexs/proxy/HttpConnection.java | 67 +- .../src/com/jpexs/proxy/HttpError.java | 96 +- .../src/com/jpexs/proxy/HttpRelay.java | 6 +- libsrc/jpproxy/src/com/jpexs/proxy/Https.java | 185 ++-- .../src/com/jpexs/proxy/HttpsThrough.java | 54 +- .../jpproxy/src/com/jpexs/proxy/Janitor.java | 51 +- libsrc/jpproxy/src/com/jpexs/proxy/Key.java | 3 +- libsrc/jpproxy/src/com/jpexs/proxy/Main.java | 31 +- .../jpproxy/src/com/jpexs/proxy/Message.java | 383 +++---- .../src/com/jpexs/proxy/ProxyConfig.java | 44 +- .../src/com/jpexs/proxy/ReplacedListener.java | 11 +- libsrc/jpproxy/src/com/jpexs/proxy/Reply.java | 359 +++---- .../jpproxy/src/com/jpexs/proxy/Request.java | 370 +++---- .../jpexs/proxy/RetryRequestException.java | 16 +- .../src/com/jpexs/proxy/ReusableThread.java | 87 +- .../jpproxy/src/com/jpexs/proxy/Server.java | 194 ++-- .../src/com/jpexs/proxy/ThreadPool.java | 64 +- .../src/com/jpexs/proxy/WorkerThread.java | 237 ++--- .../flash/gui/proxy/ProxyFrame.java | 41 +- 37 files changed, 1847 insertions(+), 2267 deletions(-) diff --git a/.gitignore b/.gitignore index 1a5d150fd..95570ebb6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,6 @@ hs_err_pid*.log /libsrc/ffdec_lib/testdata/decompile/ /libsrc/ffdec_lib/testdata/recompile/ /libsrc/ffdec_lib/revision.txt -/libsrc/jpproxy/nbproject/private/ \ No newline at end of file +/libsrc/jpproxy/nbproject/private/ +/libsrc/jpproxy/dist/ +/libsrc/jpproxy/build/ \ No newline at end of file diff --git a/examples/DeobfuscatorSample.java b/examples/DeobfuscatorSample.java index 5d9bf6089..ab5e3dce2 100644 --- a/examples/DeobfuscatorSample.java +++ b/examples/DeobfuscatorSample.java @@ -6,6 +6,10 @@ import com.jpexs.decompiler.flash.helpers.SWFDecompilerListener; public class DeobfuscatorSample implements SWFDecompilerListener { + @Override + public byte[] proxyFileCatched(byte[] data) { + } + @Override public void actionListParsed(ActionList actions, SWF swf) { } diff --git a/lib/jpproxy.jar b/lib/jpproxy.jar index 082c393cc4ac633826100216d06b7f4d49503868..ec749fd302c4718aca824745130b3008b4c89ec9 100644 GIT binary patch delta 17029 zcmaKT2YgjU7WX;x`pZpwsgDpyAq3JxAV3Hqp@ou!E`%N-bP{?Kc!XjDQ5f|R1VjW8 zY(OA1MNmM+T@}lU*mc+LT6WiU)m^^-xi0~;-+o{6dvoW`J$L5xbEe#FyKP@TXbTxS z*kW}PIf;(eB=*}9k{aBA>z_8h@zxte^v0InWu3ZbbqaR(Sx{ZIsCsH<@8tyvxmn$^ zawg^GX5O6JDWSuV1yxh#R44SQT`;e9LDiy}wKW~L{H1K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8JI;jm?0CQk_BnZfr`pSCvBlP+kyMqLs52sYmw)>tqnizDS-AUg!br) zW2zPJa=v>^x&GjwK<>g(AAp8WMKt3yUVjkkhM54nucrDGq)-52JtsxP4 z(8y)*V@4yIFcxe+9*+1V2xMVfK8TM*Z8X{Ub6YS&Kps1^!w8waA?Dxoz9l>yi+ zKxX5=4Ja?4*|r0D+xyNU9Bi80HeZ~!9K~ixe#s&OM{)|dg*~?u8g>D25$IV6{s3p= zzb9s2hBX?3+;FVLI8b6L)?qG{_*VDi6Sf;L&RAb@FaELLbl@@*Vlf*tm<2?tf#@74 zjk%!iJg~%kP;>$MUkHA?32V0qy)MD3EyJoUMbFEj)x2o6!ndq9SJi({8VW*2QmF4A zy*b8o3Vis|w_`}7Z>4=@eYit&+4W;3uKiiB<-+O5?(KZ{_u!R*Rh{%jq_24~2PaE!^362dC2^S`W7q$7uY3f( z>C3Ns^aT#`%_wFgQQ#YWaeN(|cCSQaa*h$HX9wfM?=m(5vwf`?V-7R$Cdn7vx4w_- z-y0KXeEo0t%7hR=M)~^pj;Ah5;tzuhadd@K5Lj){_dS^AQNNPer`e3vH7SG))RfaybB~mA57#V*jUZZ2yMvXrAjqk^;O+@5? zz>WZm{8}^u8nYTb$M{b6hDV(eav^NKc|~e*8@X;hx*~TK*b_f zWcRf$t>>S=2SpkN-@WqiNdb@X%_wEJ*>j`)Y6BdeyH~DwNkAig2TD24RA#bw@x4*X zF{;}akdYK$@HH-uF9ZFYa|%F6okH<9|&0-LX(oZTjPBj=$JczBZl`11XczO4W^ zrzK8<@9}buv1B!^f8E~jWjBRT|5(n3+2wWn_;>pMk<7je$()e=cV-gR-vxe%{1O{s makvAMuX`tM;yc-agZ?i`YNWs$HO%%M7{DEZS|YYd^nU 0) - { - out.write(buffer, 0, n); - out.flush(); - } - out.flush(); - }catch(SocketException e){ - } - catch (IOException e) - { - //Ignore errors - } + try { + while ((n = in.read(buffer, 0, buffer.length)) > 0) { + out.write(buffer, 0, n); + out.flush(); + } + out.flush(); + } catch (SocketException e) { + } catch (IOException e) { + //Ignore errors + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java b/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java index 0b41b11bf..2b8b20e35 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java @@ -14,10 +14,10 @@ import java.util.Locale; import java.util.TimeZone; import javax.net.ssl.SSLHandshakeException; -class Handler implements Runnable -{ +class Handler implements Runnable { + static final boolean DEBUG = false; - + Client client = null; Socket socket = null; Request request = null; @@ -33,16 +33,15 @@ class Handler implements Runnable List catchedContentTypes; ReplacedListener replacedListener; - static int curId=0; + static int curId = 0; int id; /** * Create a Handler. */ - Handler(Socket socket,List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) - { + Handler(Socket socket, List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) { curId++; - id=curId; + id = curId; this.socket = socket; this.replacements = replacements; this.catchedListener = catchedListener; @@ -53,439 +52,346 @@ class Handler implements Runnable /** * Close all connections associated with this handler. */ - synchronized void close() - { - if (client != null) - { - client.close(); - client = null; - } - if (http != null) - { - http.close(); - http = null; - } + synchronized void close() { + if (client != null) { + client.close(); + client = null; + } + if (http != null) { + http.close(); + http = null; + } } /** * Flush all data to the client. */ - void flush() - { - if (client != null) - { - try - { - client.getOutputStream().flush(); - } - catch (IOException e) - { - - } - } - } - - public void run() - { - boolean keepAlive = false; - Exception reason = null; - - Thread.currentThread().setName("Handler(" - + socket.getInetAddress().getHostAddress() - + ")"); - - try - { - client = new Client(socket); - client.setTimeout(ProxyConfig.readTimeout); - } - catch (IOException e) - { - - - return; - } - - try - { - boolean secure=false; - int securePort=443; - String secureServer=""; - do - { - request = null; - reply = null; - idle = System.currentTimeMillis(); - - try - { - request = client.read(); - if(secure){ - request.addSecureHostToURL(secureServer); - } - } - catch(SSLHandshakeException she) - { - she.printStackTrace(); - break; - } - catch (IOException e) - { - - break; - } - - - 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)))){ - secure=true; - reply=new Reply(); - reply.statusLine = "HTTP/1.0 200 Connection established"; - reply.setHeaderField("Proxy-agent", ProxyConfig.appName); + void flush() { + if (client != null) { try { - client.write(reply); - } catch (IOException ex) { + client.getOutputStream().flush(); + } catch (IOException e) { } - client.promoteToServerSSL(); - keepAlive=true; - continue; - } - } - - idle = 0; - - try - { - keepAlive = processRequest(secure,secureServer,securePort); - } - catch (IOException ioe) - { - reason = ioe; - keepAlive = false; - } - - if (request != null && reply != null) - { - // XXX insert the number of bytes read into the - // reply content-length for logging. - if (reply != null && currentLength > 0) - { - reply.setHeaderField("Content-length", currentLength); - } - - - } - } - while (keepAlive); - } - finally - { - - } - - if (reason != null && reason.getMessage().indexOf("Broken pipe") == -1) - { - if (client != null && request != null) - { - error(client.getOutputStream(), reason, request); - } - - } - - close(); + } } - boolean processRequest(boolean secure,String secureHost,int securePort) throws IOException - { - boolean keepAlive = false; - - while (reply == null) - { - //boolean secure = false; - boolean uncompress = false; + public void run() { + boolean keepAlive = false; + Exception reason = null; + Thread.currentThread().setName("Handler(" + + socket.getInetAddress().getHostAddress() + + ")"); - if (request.getCommand().equals("CONNECT")) - { - secure = true; - } - else if (request.getURL().startsWith("/")) - { - - } - else if (request.getURL().startsWith("https://")&&(secure)) - { + try { + client = new Client(socket); + client.setTimeout(ProxyConfig.readTimeout); + } catch (IOException e) { - } - else if (! request.getURL().startsWith("http://")) - { + return; + } - return false; - } + try { + boolean secure = false; + int securePort = 443; + String secureServer = ""; + do { + request = null; + reply = null; + idle = System.currentTimeMillis(); - /* Client wants Keep-Alive */ - if (ProxyConfig.proxyKeepAlive) - { - keepAlive = (request.containsHeaderField("Proxy-Connection") - && request.getHeaderField("Proxy-Connection").equals("Keep-Alive")); - } + try { + request = client.read(); + if (secure) { + request.addSecureHostToURL(secureServer); + } + } catch (SSLHandshakeException she) { + she.printStackTrace(); + break; + } catch (IOException e) { - /* Filter the request. */ - //deleted + break; + } - /* None found. Use http or https relay. */ - if (secure) - { - http = createHttpsRelay(secureHost,securePort); - } - else - { - http = createHttpRelay(); - } - try - { - http.sendRequest(request); - if (http instanceof Http) - { - ((Http)http).setTimeout(ProxyConfig.readTimeout); - } - reply = http.recvReply(request); - } - catch (RetryRequestException e) - { - http.close(); - http = null; - continue; /* XXX */ - } + 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)))) { + secure = true; + reply = new Reply(); + reply.statusLine = "HTTP/1.0 200 Connection established"; + reply.setHeaderField("Proxy-agent", ProxyConfig.appName); + try { + client.write(reply); + } catch (IOException ex) { - /* Guess content-type if there aren't any headers. - Probably an upgraded HTTP/0.9 reply. */ - if (reply.headerCount() == 0) - { - String url = request.getURL(); - if (url.endsWith("/") - || url.endsWith(".html") || url.endsWith(".htm")) - { - reply.setHeaderField("Content-type", "text/html"); - } - } + } + client.promoteToServerSSL(); + keepAlive = true; + continue; + } + } + idle = 0; + try { + keepAlive = processRequest(secure, secureServer, securePort); + } catch (IOException ioe) { + reason = ioe; + keepAlive = false; + } - /* Filter the reply. */ - if (false) - { - /* uncompress gzip encoded html so it can be filtered */ - if (!ProxyConfig.dontUncompress - && "text/html".equals(reply.getHeaderField("Content-type"))) - { - String encoding = reply.getHeaderField("Content-Encoding"); - if (encoding != null && encoding.indexOf("gzip") != -1) - { - reply.removeHeaderField("Content-Encoding"); - reply.removeHeaderField("Content-length"); - uncompress = true; - } - } + if (request != null && reply != null) { + // XXX insert the number of bytes read into the + // reply content-length for logging. + if (reply != null && currentLength > 0) { + reply.setHeaderField("Content-length", currentLength); + } - //filter(reply); - } + } + } while (keepAlive); + } finally { + } - if(request.containsHeaderField("Connection")&&(request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))&& reply.containsHeaderField("Connection") - && reply.getHeaderField("Connection").equals("Keep-Alive")){ - keepAlive=true; - } - reply.removeHeaderField("Proxy-Connection"); - if (keepAlive && reply.containsHeaderField("Content-length")) - { - reply.setHeaderField("Proxy-Connection", "Keep-Alive"); - } - else - { - keepAlive = false; - } + if (reason != null && reason.getMessage().indexOf("Broken pipe") == -1) { + if (client != null && request != null) { + error(client.getOutputStream(), reason, request); + } - currentLength = -1; - contentLength = -1; - try - { - contentLength = Integer.parseInt(reply.getHeaderField("Content-length")); - } - catch (NumberFormatException e) - { - } + } - - - if (http instanceof HttpsThrough) - { - HttpsThrough https = (HttpsThrough) http; - int timeout = ProxyConfig.readTimeout; - - client.write(reply); - - - try - { - client.setTimeout(timeout); - https.setTimeout(timeout); - - Copy cp = new Copy(client.getInputStream(), https.getOutputStream()); - ReusableThread thread = Server.getThread(); - thread.setRunnable(cp); - flushCopy(https.getInputStream(), client.getOutputStream(), -1, true); - client.close(); - } - catch (InterruptedIOException iioe) - { - // ignore socket timeout exceptions - } - } - else if (reply.hasContent()) - { - try - { - processContent(uncompress); - } - catch (IOException e) - { - if (http instanceof Http) - { - ((Http)http).reallyClose(); - } - else - { - http.close(); - } - http = null; - - client.close(); - client = null; - - throw e; - //return false; /* XXX */ - } - /* Document contains no data. */ - if (contentLength == 0) - { - client.close(); - } - } - else - { - client.write(reply); - } - - http.close(); - } - - return keepAlive; + close(); } - HttpRelay createHttpsRelay(String secureHost,int securePort) throws IOException - { - HttpRelay http; + boolean processRequest(boolean secure, String secureHost, int securePort) throws IOException { + boolean keepAlive = false; - if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureHost)))) - { - 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"); + while (reply == null) { + //boolean secure = false; + boolean uncompress = false; + + if (request.getCommand().equals("CONNECT")) { + secure = true; + } else if (request.getURL().startsWith("/")) { + + } else if (request.getURL().startsWith("https://") && (secure)) { + + } else if (!request.getURL().startsWith("http://")) { + + return false; + } + + /* Client wants Keep-Alive */ + if (ProxyConfig.proxyKeepAlive) { + keepAlive = (request.containsHeaderField("Proxy-Connection") + && request.getHeaderField("Proxy-Connection").equals("Keep-Alive")); + } + + /* Filter the request. */ + //deleted + + /* None found. Use http or https relay. */ + if (secure) { + http = createHttpsRelay(secureHost, securePort); + } else { + http = createHttpRelay(); + } try { - http.sendRequest(connectReq); - Reply rep=http.recvReply(connectReq); - } catch (RetryRequestException ex) { + http.sendRequest(request); + if (http instanceof Http) { + ((Http) http).setTimeout(ProxyConfig.readTimeout); + } + reply = http.recvReply(request); + } catch (RetryRequestException e) { + http.close(); + http = null; + continue; /* XXX */ } - ((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){ + + /* Guess content-type if there aren't any headers. + Probably an upgraded HTTP/0.9 reply. */ + if (reply.headerCount() == 0) { + String url = request.getURL(); + if (url.endsWith("/") + || url.endsWith(".html") || url.endsWith(".htm")) { + reply.setHeaderField("Content-type", "text/html"); + } + } + + /* Filter the reply. */ + if (false) { + /* uncompress gzip encoded html so it can be filtered */ + if (!ProxyConfig.dontUncompress + && "text/html".equals(reply.getHeaderField("Content-type"))) { + String encoding = reply.getHeaderField("Content-Encoding"); + if (encoding != null && encoding.indexOf("gzip") != -1) { + reply.removeHeaderField("Content-Encoding"); + reply.removeHeaderField("Content-length"); + uncompress = true; + } + } + + //filter(reply); + } + + if (request.containsHeaderField("Connection") && (request.getHeaderField("Connection").toLowerCase().equals("keep-alive")) && reply.containsHeaderField("Connection") + && reply.getHeaderField("Connection").equals("Keep-Alive")) { + keepAlive = true; + } + reply.removeHeaderField("Proxy-Connection"); + if (keepAlive && reply.containsHeaderField("Content-length")) { + reply.setHeaderField("Proxy-Connection", "Keep-Alive"); + } else { + keepAlive = false; + } + + currentLength = -1; + contentLength = -1; + try { + contentLength = Integer.parseInt(reply.getHeaderField("Content-length")); + } catch (NumberFormatException e) { + } + + if (http instanceof HttpsThrough) { + HttpsThrough https = (HttpsThrough) http; + int timeout = ProxyConfig.readTimeout; + + client.write(reply); + + try { + client.setTimeout(timeout); + https.setTimeout(timeout); + + Copy cp = new Copy(client.getInputStream(), https.getOutputStream()); + ReusableThread thread = Server.getThread(); + thread.setRunnable(cp); + flushCopy(https.getInputStream(), client.getOutputStream(), -1, true); + client.close(); + } catch (InterruptedIOException iioe) { + // ignore socket timeout exceptions + } + } else if (reply.hasContent()) { + try { + processContent(uncompress); + } catch (IOException e) { + if (http instanceof Http) { + ((Http) http).reallyClose(); + } else { + http.close(); + } + http = null; + + client.close(); + client = null; + + throw e; + //return false; /* XXX */ + } + /* Document contains no data. */ + if (contentLength == 0) { + client.close(); + } + } else { + client.write(reply); + } + + http.close(); + } + + return keepAlive; + } + + HttpRelay createHttpsRelay(String secureHost, int securePort) throws IOException { + HttpRelay http; + + if ((ProxyConfig.httpsMode == ProxyConfig.HTTPS_FILTER) || ((ProxyConfig.httpsMode == ProxyConfig.HTTPS_FILTERLIST) && (ProxyConfig.enabledHttpsServers.contains(secureHost)))) { + 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); connectReq.setCommand("CONNECT"); connectReq.setURL(secureHost+":"+securePort); connectReq.setProtocol("HTTP/1.1"); - try { - http.sendRequest(connectReq); - http.recvReply(connectReq); - } catch (RetryRequestException ex) { + try { + http.sendRequest(connectReq); + http.recvReply(connectReq); + } catch (RetryRequestException ex) { + } + } + ((Http)http).promoteToClientSSL();*/ + } else { + if (ProxyConfig.useHTTPSProxy) { + http = new HttpsThrough(ProxyConfig.httpsProxyHost, + ProxyConfig.httpsProxyPort, + true); + } else { + http = new HttpsThrough(secureHost, securePort); } - } - ((Http)http).promoteToClientSSL();*/ - }else{ - if(ProxyConfig.useHTTPSProxy) - { - http = new HttpsThrough(ProxyConfig.httpsProxyHost, - ProxyConfig.httpsProxyPort, - true); - }else{ - http = new HttpsThrough(secureHost,securePort); - } - } + } - - - - return http; - } - - HttpRelay createHttpRelay() throws IOException - { - HttpRelay http; - - if (ProxyConfig.useHTTPProxy) - { - http = Http.open(ProxyConfig.httpProxyHost, - ProxyConfig.httpProxyPort, - true); - } - else - { - http = Http.open(request.getHost(), request.getPort()); - } - - return http; + return http; } - InputStream readChunkedTransfer(InputStream in) throws IOException - { - ByteArrayOutputStream chunks = new ByteArrayOutputStream(8192); - int size = 0; + HttpRelay createHttpRelay() throws IOException { + HttpRelay http; - contentLength = 0; - while ((size = reply.getChunkSize(in)) > 0) - { - contentLength += size; - copy(in, chunks, size, true); - reply.readLine(in); - } - reply.getChunkedFooter(in); + if (ProxyConfig.useHTTPProxy) { + http = Http.open(ProxyConfig.httpProxyHost, + ProxyConfig.httpProxyPort, + true); + } else { + http = Http.open(request.getHost(), request.getPort()); + } - reply.removeHeaderField("Transfer-Encoding"); - reply.setHeaderField("Content-length", contentLength); - - return new ByteArrayInputStream(chunks.toByteArray()); + return http; } -private static DateFormat httpDateFormat() { + InputStream readChunkedTransfer(InputStream in) throws IOException { + ByteArrayOutputStream chunks = new ByteArrayOutputStream(8192); + int size = 0; + + contentLength = 0; + while ((size = reply.getChunkSize(in)) > 0) { + contentLength += size; + copy(in, chunks, size, true); + reply.readLine(in); + } + reply.getChunkedFooter(in); + + reply.removeHeaderField("Transfer-Encoding"); + reply.setHeaderField("Content-length", contentLength); + + return new ByteArrayInputStream(chunks.toByteArray()); + } + + private static DateFormat httpDateFormat() { DateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); return httpDateFormat; } -void disableReplyCaching() { + void disableReplyCaching() { reply.removeHeaderField("Cache-Control"); reply.removeHeaderField("Last-Modified"); reply.removeHeaderField("Expires"); @@ -500,33 +406,26 @@ void disableReplyCaching() { reply.setHeaderField("Last-Modified", "Sat, 26 Jul 1997 05:00:00 GMT"); } -void processContent(boolean uncompress) throws IOException - { - InputStream in; - boolean chunked = false; + void processContent(boolean uncompress) throws IOException { + InputStream in; + boolean chunked = false; - if (reply.containsHeaderField("Transfer-Encoding") - && reply.getTransferEncoding().equals("chunked")) - { - in = readChunkedTransfer(reply.getContent()); - chunked = true; - } - else - { - in = reply.getContent(); - } + if (reply.containsHeaderField("Transfer-Encoding") + && reply.getTransferEncoding().equals("chunked")) { + in = readChunkedTransfer(reply.getContent()); + chunked = true; + } else { + in = reply.getContent(); + } - if (in == null) - { + if (in == null) { - return; - } - else if (uncompress) - { - in = new GZIPInputStream(in); - } + return; + } else if (uncompress) { + in = new GZIPInputStream(in); + } - String url = request.getURL(); + String url = request.getURL(); boolean replaced = false; for (Replacement r : replacements) { if (r.matches(url)) { @@ -534,7 +433,7 @@ void processContent(boolean uncompress) throws IOException ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { FileInputStream fis = new FileInputStream(r.targetFile); - byte[] buf = new byte[2048]; + byte[] buf = new byte[4096]; int pos = 0; while ((pos = fis.read(buf)) > 0) { buffer.write(buf, 0, pos); @@ -542,7 +441,7 @@ void processContent(boolean uncompress) throws IOException fis.close(); buffer.close(); } catch (IOException ex) { - + } byte[] bytes = buffer.toByteArray(); contentLength = bytes.length; @@ -566,16 +465,23 @@ void processContent(boolean uncompress) throws IOException if (contentType != null) { for (String ct : catchedContentTypes) { String convContentType = contentType; - if (convContentType.contains(";")) + if (convContentType.contains(";")) { convContentType = convContentType.substring(0, convContentType.indexOf(";")); + } if (ct.toLowerCase().equals(convContentType.toLowerCase())) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(in, baos, contentLength, true); + byte[] data = baos.toByteArray(); if (catchedListener != null) { - catchedListener.catched(ct, request.getURL(), new ByteArrayInputStream(baos.toByteArray())); + byte[] newData = catchedListener.catched(ct, request.getURL(), new ByteArrayInputStream(data)); + if (newData != null) { + data = newData; + contentLength = data.length; + reply.setHeaderField("Content-length", contentLength); + } } - in = new ByteArrayInputStream(baos.toByteArray()); + in = new ByteArrayInputStream(data); disableReplyCaching(); break; } @@ -587,22 +493,18 @@ void processContent(boolean uncompress) throws IOException } } - - /** * Return the content length. */ - int getTotalBytes() - { - return contentLength > 0 ? contentLength : 0; + int getTotalBytes() { + return contentLength > 0 ? contentLength : 0; } /** * Return the number of bytes read so far. */ - int getCurrentBytes() - { - return currentLength > 0 ? currentLength : 0; + int getCurrentBytes() { + return currentLength > 0 ? currentLength : 0; } /** @@ -612,21 +514,17 @@ void processContent(boolean uncompress) throws IOException * @param e exception that occurred * @param r request */ - void error(OutputStream out, Exception e, Request r) - { - StringBuffer buf = new StringBuffer(); - buf.append("While trying to retrieve the URL: "+r.getURL()+"\r\n"); - buf.append("

\r\nThe following error was encountered:\r\n

\r\n"); - buf.append("

  • " + e.toString() + "
\r\n"); - String s = new HttpError(400, buf.toString()).toString(); - try - { - out.write(s.getBytes(), 0, s.length()); - out.flush(); - } - catch (Exception ex) - { - } + void error(OutputStream out, Exception e, Request r) { + StringBuffer buf = new StringBuffer(); + buf.append("While trying to retrieve the URL: " + r.getURL() + "\r\n"); + buf.append("

\r\nThe following error was encountered:\r\n

\r\n"); + buf.append("

  • " + e.toString() + "
\r\n"); + String s = new HttpError(400, buf.toString()).toString(); + try { + out.write(s.getBytes(), 0, s.length()); + out.flush(); + } catch (Exception ex) { + } } /** @@ -637,64 +535,55 @@ void processContent(boolean uncompress) throws IOException * @param monitored Update the Monitor */ void copy(InputStream in, OutputStream out, int length, boolean monitored) - throws IOException - { - if (length == 0) - { - return; - } + throws IOException { + if (length == 0) { + return; + } - int n; - byte[] buffer = new byte[8192]; - long start = System.currentTimeMillis(); - long now = 0, then = start; - - bytesPerSecond = 0; + int n; + byte[] buffer = new byte[8192]; + long start = System.currentTimeMillis(); + long now = 0, then = start; - if (monitored) - { - currentLength = 0; - } - - for (;;) - { - n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; - n = in.read(buffer, 0, n); - if (n < 0) - { - break; - } - - out.write(buffer, 0, n); + bytesPerSecond = 0; - if (monitored) - { - currentLength += n; + if (monitored) { + currentLength = 0; + } - } + for (;;) { + n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; + n = in.read(buffer, 0, n); + if (n < 0) { + break; + } - now = System.currentTimeMillis(); - bytesPerSecond = currentLength / ((now - start) / 1000.0); + out.write(buffer, 0, n); - // flush after 1 second - if (now - then > 1000) - { - out.flush(); - } + if (monitored) { + currentLength += n; - if (length != -1) - { - length -= n; - if (length == 0) - { - break; - } - } + } - then = now; - } + now = System.currentTimeMillis(); + bytesPerSecond = currentLength / ((now - start) / 1000.0); - out.flush(); + // flush after 1 second + if (now - then > 1000) { + out.flush(); + } + + if (length != -1) { + length -= n; + if (length == 0) { + break; + } + } + + then = now; + } + + out.flush(); } @@ -706,86 +595,71 @@ void processContent(boolean uncompress) throws IOException * @param monitored Update the Monitor */ void flushCopy(InputStream in, OutputStream out, int length, boolean monitored) - throws IOException - { - if (length == 0) - { - return; - } + throws IOException { + if (length == 0) { + return; + } - int n; - byte[] buffer = new byte[8192]; - long start = System.currentTimeMillis(); - bytesPerSecond = 0; - - if (monitored) - { - currentLength = 0; - } - - for (;;) - { - n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; - n = in.read(buffer, 0, n); - if (n < 0) - { - break; - } + int n; + byte[] buffer = new byte[8192]; + long start = System.currentTimeMillis(); + bytesPerSecond = 0; - out.write(buffer, 0, n); - out.flush(); - if (monitored) - { - currentLength += n; + if (monitored) { + currentLength = 0; + } - } - bytesPerSecond = currentLength / ((System.currentTimeMillis() - start) / 1000.0); - if (length != -1) - { - length -= n; - if (length == 0) - { - break; - } - } - } - out.flush(); + for (;;) { + n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; + n = in.read(buffer, 0, n); + if (n < 0) { + break; + } + + out.write(buffer, 0, n); + out.flush(); + if (monitored) { + currentLength += n; + + } + bytesPerSecond = currentLength / ((System.currentTimeMillis() - start) / 1000.0); + if (length != -1) { + length -= n; + if (length == 0) { + break; + } + } + } + out.flush(); } - /** * Return a string represenation of the hander's state. */ - public String toString() - { - StringBuffer str = new StringBuffer(); - str.append("CLIENT "); - str.append(socket.getInetAddress().getHostAddress()); - str.append(":"); - str.append(socket.getPort()); - str.append(" - "); - if (request == null) - { - str.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); - } - else - { - if (reply != null && currentLength > 0) - { - str.append("("); - str.append(currentLength); - if (contentLength > 0) - { - str.append("/"); - str.append(contentLength); - } - str.append(" "); - str.append(((int)bytesPerSecond / 1024) + " kB/s"); - str.append(") "); - } - str.append(request.getURL()); - } - return str.toString(); + public String toString() { + StringBuffer str = new StringBuffer(); + str.append("CLIENT "); + str.append(socket.getInetAddress().getHostAddress()); + str.append(":"); + str.append(socket.getPort()); + str.append(" - "); + if (request == null) { + str.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); + } else { + if (reply != null && currentLength > 0) { + str.append("("); + str.append(currentLength); + if (contentLength > 0) { + str.append("/"); + str.append(contentLength); + } + str.append(" "); + str.append(((int) bytesPerSecond / 1024) + " kB/s"); + str.append(") "); + } + str.append(request.getURL()); + } + return str.toString(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Http.java b/libsrc/jpproxy/src/com/jpexs/proxy/Http.java index 19bf98417..cca9bc177 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Http.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Http.java @@ -6,15 +6,13 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; -class Http extends HttpConnection -{ +class Http extends HttpConnection { /* XXX - more than 1 should work now. */ static final int MAX_PENDING_REQUESTS = 1; - + static Hashtable cache = new Hashtable(33); private static Object httpLock = new Object(); - String host; int port; @@ -24,334 +22,263 @@ class Http extends HttpConnection long idle = 0; Vector queue = new Vector(); - public Http(String host, int port) throws IOException - { - this(host, port, false); - } - - public Http(String host, int port, boolean isProxy) throws IOException - { - super(host, port); - this.host = host; - this.port = port; - this.proxy = isProxy; + public Http(String host, int port) throws IOException { + this(host, port, false); } - public Http(String host, int port, boolean isProxy,Socket sock) throws IOException - { - super(sock); - this.host = host; - this.port = port; - this.proxy = isProxy; + public Http(String host, int port, boolean isProxy) throws IOException { + super(host, port); + this.host = host; + this.port = port; + this.proxy = isProxy; + } + + public Http(String host, int port, boolean isProxy, Socket sock) throws IOException { + super(sock); + this.host = host; + this.port = port; + this.proxy = isProxy; } public synchronized void sendRequest(Request request) - throws IOException, RetryRequestException - { - queue.addElement(request); - - try - { - send(request); - } - catch (IOException e) - { - if (persistent) - { - persistent = false; - throw new RetryRequestException(); - } - throw e; - } + throws IOException, RetryRequestException { + queue.addElement(request); + + try { + send(request); + } catch (IOException e) { + if (persistent) { + persistent = false; + throw new RetryRequestException(); + } + throw e; + } } public synchronized Reply recvReply(Request request) - throws IOException, RetryRequestException - { - while (queue.firstElement() != request) - { - try - { - wait(); - } - catch (InterruptedException e) - { - } - } + throws IOException, RetryRequestException { + while (queue.firstElement() != request) { + try { + wait(); + } catch (InterruptedException e) { + } + } - if (closed) - { - throw new RetryRequestException(); - } + if (closed) { + throw new RetryRequestException(); + } - try - { - return recv(); - } - catch (IOException e) - { - if (persistent) - { - persistent = false; - throw new RetryRequestException(); - } - throw e; - } + try { + return recv(); + } catch (IOException e) { + if (persistent) { + persistent = false; + throw new RetryRequestException(); + } + throw e; + } } - public void reallyClose() - { - persistent = false; - close(); + public void reallyClose() { + persistent = false; + close(); } - public synchronized void close() - { - if (persistent) - { - idle = System.currentTimeMillis(); - } - else - { - cacheRemove(host, port, this); - super.close(); - closed = true; - } + public synchronized void close() { + if (persistent) { + idle = System.currentTimeMillis(); + } else { + cacheRemove(host, port, this); + super.close(); + closed = true; + } - if (queue.size() > 0) - { - queue.removeElementAt(0); - - notify(); - } + if (queue.size() > 0) { + queue.removeElementAt(0); + + notify(); + } } - private void send(Request request) throws IOException - { - - /* Prepare HTTP/1.1 request */ - - request.removeHeaderField("Proxy-Connection"); - + private void send(Request request) throws IOException { - if(!proxy){ - if(request.containsHeaderField("Connection")&&(request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))){ + /* Prepare HTTP/1.1 request */ + request.removeHeaderField("Proxy-Connection"); - }else{ - request.setHeaderField("Connection", "open"); - } - if (!request.containsHeaderField("Host")) - { - request.setHeaderField("Host", request.getHost()); - } - } + if (!proxy) { + if (request.containsHeaderField("Connection") && (request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))) { - if (proxy) - { - request.write(getOutputStream()); - } - else - { - String oldStatusLine = request.statusLine; - StringBuffer head = new StringBuffer(); - head.append(request.getCommand()); - head.append(" "); - head.append(request.getPath()); - head.append(" "); - head.append("HTTP/1.0"); - request.statusLine = head.toString(); + } else { + request.setHeaderField("Connection", "open"); + } + if (!request.containsHeaderField("Host")) { + request.setHeaderField("Host", request.getHost()); + } + } - request.write(getOutputStream()); + if (proxy) { + request.write(getOutputStream()); + } else { + String oldStatusLine = request.statusLine; + StringBuffer head = new StringBuffer(); + head.append(request.getCommand()); + head.append(" "); + head.append(request.getPath()); + head.append(" "); + head.append("HTTP/1.0"); + request.statusLine = head.toString(); - /* flush? */ - - request.statusLine = oldStatusLine; - } + request.write(getOutputStream()); + + /* flush? */ + request.statusLine = oldStatusLine; + } } - private Reply recv() throws IOException - { - Reply reply = new Reply(getInputStream()); - reply.read(); + private Reply recv() throws IOException { + Reply reply = new Reply(getInputStream()); + reply.read(); - String conn = reply.getHeaderField("Connection"); + String conn = reply.getHeaderField("Connection"); - - if (reply.containsHeaderField("Connection") - && reply.getHeaderField("Connection").equals("close")) - { - persistent = false; - } - else if (reply.getProtocol().equals("HTTP/1.1")) - { - persistent = true; - } - else - { - persistent = false; - } + if (reply.containsHeaderField("Connection") + && reply.getHeaderField("Connection").equals("close")) { + persistent = false; + } else if (reply.getProtocol().equals("HTTP/1.1")) { + persistent = true; + } else { + persistent = false; + } - /* Received HTTP/1.1 "Continue". Read another Reply. */ - if (reply.getStatusCode() == 100) - { - reply = recv(); - } + /* Received HTTP/1.1 "Continue". Read another Reply. */ + if (reply.getStatusCode() == 100) { + reply = recv(); + } - return reply; + return reply; } - protected boolean isBusy() - { - return queue.size() >= MAX_PENDING_REQUESTS; + protected boolean isBusy() { + return queue.size() >= MAX_PENDING_REQUESTS; } - protected boolean isPersistent() - { - return persistent; + protected boolean isPersistent() { + return persistent; } - private static String cacheKey(String host, int port) - { - return host.toLowerCase() + ":" + port; + private static String cacheKey(String host, int port) { + return host.toLowerCase() + ":" + port; } - private static Vector cacheLookup(String host, int port) - { - Vector v = (Vector) cache.get(cacheKey(host, port)); - return v; + private static Vector cacheLookup(String host, int port) { + Vector v = (Vector) cache.get(cacheKey(host, port)); + return v; } - private static boolean cacheContains(Http http) - { - Vector v = (Vector) cache.get(cacheKey(http.host, http.port)); - return v != null ? v.contains(http) : false; + private static boolean cacheContains(Http http) { + Vector v = (Vector) cache.get(cacheKey(http.host, http.port)); + return v != null ? v.contains(http) : false; } - private static void cacheInsert(String host, int port, Http http) - { - String key = cacheKey(host, port); - Vector v = (Vector) cache.get(key); - if (v == null) - { - v = new Vector(); - } - v.addElement(http); - cache.put(key, v); + private static void cacheInsert(String host, int port, Http http) { + String key = cacheKey(host, port); + Vector v = (Vector) cache.get(key); + if (v == null) { + v = new Vector(); + } + v.addElement(http); + cache.put(key, v); } - private static void cacheRemove(String host, int port, Http http) - { - Vector v = (Vector) cache.get(cacheKey(host, port)); - if (v != null) - { - v.removeElement(http); - if (v.isEmpty()) - { - cache.remove(cacheKey(host, port)); - } - } + private static void cacheRemove(String host, int port, Http http) { + Vector v = (Vector) cache.get(cacheKey(host, port)); + if (v != null) { + v.removeElement(http); + if (v.isEmpty()) { + cache.remove(cacheKey(host, port)); + } + } } - private static void cacheClean() - { - long now = System.currentTimeMillis(); - Enumeration e = cache.keys(); - while (e.hasMoreElements()) - { - Vector v = (Vector) cache.get(e.nextElement()); - for (int i = 0; i < v.size(); i++) - { - Http http = (Http) v.elementAt(i); - if (http.idle > 0 && now - http.idle > 30000) /* 30 seconds */ - { - http.persistent = false; - http.close(); - } - } - } + private static void cacheClean() { + long now = System.currentTimeMillis(); + Enumeration e = cache.keys(); + while (e.hasMoreElements()) { + Vector v = (Vector) cache.get(e.nextElement()); + for (int i = 0; i < v.size(); i++) { + Http http = (Http) v.elementAt(i); + if (http.idle > 0 && now - http.idle > 30000) /* 30 seconds */ { + http.persistent = false; + http.close(); + } + } + } } static Http open(String host, int port, boolean isProxy) - throws IOException - { - Http http = null; + throws IOException { + Http http = null; - synchronized (httpLock) - { - Vector v = cacheLookup(host, port); - if (v != null) - { - for (int i = 0; i < v.size(); i++) - { - Http pick = (Http) v.elementAt(i); + synchronized (httpLock) { + Vector v = cacheLookup(host, port); + if (v != null) { + for (int i = 0; i < v.size(); i++) { + Http pick = (Http) v.elementAt(i); - /* find an http connection that isn't busy */ - if (pick.isPersistent() && !pick.isBusy()) - { - http = pick; - break; - } - } + /* find an http connection that isn't busy */ + if (pick.isPersistent() && !pick.isBusy()) { + http = pick; + break; + } + } - if (http != null) - { - http.idle = 0; - } - } - } - - if (http == null) - { - http = new Http(host, port, isProxy); - cacheInsert(host, port, http); - } - - return http; + if (http != null) { + http.idle = 0; + } + } + } + + if (http == null) { + http = new Http(host, port, isProxy); + cacheInsert(host, port, http); + } + + return http; } - static Http open(String host, int port) throws IOException - { - return open(host, port, false); + static Http open(String host, int port) throws IOException { + return open(host, port, false); } - static Enumeration enumerate() - { - Vector list = new Vector(); - Enumeration e = cache.keys(); - while (e.hasMoreElements()) - { - Vector v = (Vector) cache.get(e.nextElement()); - for (int i = 0; i < v.size(); i++) - { - list.addElement(v.elementAt(i)); - } - } - return list.elements(); + static Enumeration enumerate() { + Vector list = new Vector(); + Enumeration e = cache.keys(); + while (e.hasMoreElements()) { + Vector v = (Vector) cache.get(e.nextElement()); + for (int i = 0; i < v.size(); i++) { + list.addElement(v.elementAt(i)); + } + } + return list.elements(); } - static synchronized void clean() - { - cacheClean(); + static synchronized void clean() { + cacheClean(); } - public String toString() - { - StringBuffer buf = new StringBuffer(); - buf.append("SERVER "); - buf.append(super.toString()); - if (isPersistent()) - { - buf.append(" - "); - if (queue.size() > 0) - { - buf.append(queue.size()); - buf.append(" pending"); - } - else - { - buf.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); - } - } - return buf.toString(); + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("SERVER "); + buf.append(super.toString()); + if (isPersistent()) { + buf.append(" - "); + if (queue.size() > 0) { + buf.append(queue.size()); + buf.append(" pending"); + } else { + buf.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); + } + } + return buf.toString(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java index 685c68dfb..14e944fd5 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java @@ -5,54 +5,45 @@ import java.io.OutputStream; import java.io.IOException; import java.net.Socket; -abstract class HttpConnection extends Connection implements HttpRelay -{ - HttpConnection(String host, int port) throws IOException - { - super(host, port); +abstract class HttpConnection extends Connection implements HttpRelay { + + HttpConnection(String host, int port) throws IOException { + super(host, port); } - HttpConnection(Socket s) throws IOException - { - super(s); + HttpConnection(Socket s) throws IOException { + super(s); } public void sendRequest(Request request) - throws IOException, RetryRequestException - { - request.write(getOutputStream()); + throws IOException, RetryRequestException { + request.write(getOutputStream()); } - + public Reply recvReply(Request request) - throws IOException, RetryRequestException - { - Reply reply = new Reply(getInputStream()); - reply.read(); - return reply; - } - - public void setInputStream(InputStream in) - { - super.setInputStream(in); - } - - public void setOutputStream(OutputStream out) - { - super.setOutputStream(out); + throws IOException, RetryRequestException { + Reply reply = new Reply(getInputStream()); + reply.read(); + return reply; } - public InputStream getInputStream() - { - return super.getInputStream(); - } - - public OutputStream getOutputStream() - { - return super.getOutputStream(); + public void setInputStream(InputStream in) { + super.setInputStream(in); } - public void close() - { - super.close(); + public void setOutputStream(OutputStream out) { + super.setOutputStream(out); + } + + public InputStream getInputStream() { + return super.getInputStream(); + } + + public OutputStream getOutputStream() { + return super.getOutputStream(); + } + + public void close() { + super.close(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java index ec0dc8c64..f136473d6 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java @@ -1,70 +1,62 @@ package com.jpexs.proxy; -class HttpError -{ +class HttpError { + StringBuffer content = null; Reply reply = null; - - HttpError(int code, String message) - { - String error; - switch (code) - { - case 400: - error = "Bad Request"; - break; - case 403: - error = "Forbidden"; - break; + HttpError(int code, String message) { + String error; + switch (code) { + case 400: + error = "Bad Request"; + break; - case 404: - error = "Not found"; - break; + case 403: + error = "Forbidden"; + break; - case 503: - error = "Service Unavailable"; - break; + case 404: + error = "Not found"; + break; - default: - error = "Error"; - break; - } + case 503: + error = "Service Unavailable"; + break; - reply = new Reply(); - reply.statusLine = "HTTP/1.0 " + code + " " + error; - reply.setHeaderField("Content-type", "text/html"); - reply.setHeaderField("Server", ProxyConfig.appName+"/" +ProxyConfig.appVersion); + default: + error = "Error"; + break; + } - content = new StringBuffer(); - content.append(message); + reply = new Reply(); + reply.statusLine = "HTTP/1.0 " + code + " " + error; + reply.setHeaderField("Content-type", "text/html"); + reply.setHeaderField("Server", ProxyConfig.appName + "/" + ProxyConfig.appVersion); + + content = new StringBuffer(); + content.append(message); } - Reply getReply() - { - return reply; + Reply getReply() { + return reply; } - String getContent() - { - if (content == null) - { - return null; - } - return content.toString(); + String getContent() { + if (content == null) { + return null; + } + return content.toString(); } - public String toString() - { - StringBuffer buf = new StringBuffer(); - if (reply != null) - { - buf.append(reply.toString()); - } - if (content != null) - { - buf.append(content.toString()); - } - return buf.toString(); + public String toString() { + StringBuffer buf = new StringBuffer(); + if (reply != null) { + buf.append(reply.toString()); + } + if (content != null) { + buf.append(content.toString()); + } + return buf.toString(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java index f2808e42c..aed61531c 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java @@ -4,9 +4,11 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; -public interface HttpRelay -{ +public interface HttpRelay { + void sendRequest(Request request) throws IOException, RetryRequestException; + Reply recvReply(Request request) throws IOException, RetryRequestException; + void close(); } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Https.java b/libsrc/jpproxy/src/com/jpexs/proxy/Https.java index 0bb7b7571..27da62bd8 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Https.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Https.java @@ -10,143 +10,118 @@ import java.util.Vector; * @author JPEXS */ public class Https extends Http { - /* XXX - more than 1 should work now. */ + /* XXX - more than 1 should work now. */ + static final int MAX_PENDING_REQUESTS = 1; static Hashtable cache = new Hashtable(33); private static Object httpLock = new Object(); - public Https(String host, int port) throws IOException - { - this(host, port, false); + public Https(String host, int port) throws IOException { + this(host, port, false); } - public Https(String host, int port, boolean isProxy) throws IOException - { - super(host, port,isProxy); + public Https(String host, int port, boolean isProxy) throws IOException { + super(host, port, isProxy); } - private static String cacheKey(String host, int port) - { - return host.toLowerCase() + ":" + port; + private static String cacheKey(String host, int port) { + return host.toLowerCase() + ":" + port; } - private static Vector cacheLookup(String host, int port) - { - Vector v = (Vector) cache.get(cacheKey(host, port)); - return v; + private static Vector cacheLookup(String host, int port) { + Vector v = (Vector) cache.get(cacheKey(host, port)); + return v; } - private static boolean cacheContains(Https http) - { - Vector v = (Vector) cache.get(cacheKey(http.host, http.port)); - return v != null ? v.contains(http) : false; + private static boolean cacheContains(Https http) { + Vector v = (Vector) cache.get(cacheKey(http.host, http.port)); + return v != null ? v.contains(http) : false; } - private static void cacheInsert(String host, int port, Https http) - { - String key = cacheKey(host, port); - Vector v = (Vector) cache.get(key); - if (v == null) - { - v = new Vector(); - } - v.addElement(http); - cache.put(key, v); + private static void cacheInsert(String host, int port, Https http) { + String key = cacheKey(host, port); + Vector v = (Vector) cache.get(key); + if (v == null) { + v = new Vector(); + } + v.addElement(http); + cache.put(key, v); } - private static void cacheRemove(String host, int port, Https http) - { - Vector v = (Vector) cache.get(cacheKey(host, port)); - if (v != null) - { - v.removeElement(http); - if (v.isEmpty()) - { - cache.remove(cacheKey(host, port)); - } - } + private static void cacheRemove(String host, int port, Https http) { + Vector v = (Vector) cache.get(cacheKey(host, port)); + if (v != null) { + v.removeElement(http); + if (v.isEmpty()) { + cache.remove(cacheKey(host, port)); + } + } } - private static void cacheClean() - { - long now = System.currentTimeMillis(); - Enumeration e = cache.keys(); - while (e.hasMoreElements()) - { - Vector v = (Vector) cache.get(e.nextElement()); - for (int i = 0; i < v.size(); i++) - { - Https http = (Https) v.elementAt(i); - if (http.idle > 0 && now - http.idle > 30000) /* 30 seconds */ - { - http.persistent = false; - http.close(); - } - } - } + private static void cacheClean() { + long now = System.currentTimeMillis(); + Enumeration e = cache.keys(); + while (e.hasMoreElements()) { + Vector v = (Vector) cache.get(e.nextElement()); + for (int i = 0; i < v.size(); i++) { + Https http = (Https) v.elementAt(i); + if (http.idle > 0 && now - http.idle > 30000) /* 30 seconds */ { + http.persistent = false; + http.close(); + } + } + } } static Https open(String host, int port, boolean isProxy) - throws IOException - { - Https http = null; + throws IOException { + Https http = null; - synchronized (httpLock) - { - Vector v = cacheLookup(host, port); - if (v != null) - { - for (int i = 0; i < v.size(); i++) - { - Https pick = (Https) v.elementAt(i); + synchronized (httpLock) { + Vector v = cacheLookup(host, port); + if (v != null) { + for (int i = 0; i < v.size(); i++) { + Https pick = (Https) v.elementAt(i); - /* find an http connection that isn't busy */ - if (pick.isPersistent() && !pick.isBusy()) - { - http = pick; - break; - } - } + /* find an http connection that isn't busy */ + if (pick.isPersistent() && !pick.isBusy()) { + http = pick; + break; + } + } - if (http != null) - { - http.idle = 0; - } - } - } + if (http != null) { + http.idle = 0; + } + } + } - if (http == null) - { - http = new Https(host, port, isProxy); - cacheInsert(host, port, http); - } + if (http == null) { + http = new Https(host, port, isProxy); + cacheInsert(host, port, http); + } - return http; + return http; } - static Https open(String host, int port) throws IOException - { - return open(host, port, false); + static Https open(String host, int port) throws IOException { + return open(host, port, false); } - static Enumeration enumerate() - { - Vector list = new Vector(); - Enumeration e = cache.keys(); - while (e.hasMoreElements()) - { - Vector v = (Vector) cache.get(e.nextElement()); - for (int i = 0; i < v.size(); i++) - { - list.addElement(v.elementAt(i)); - } - } - return list.elements(); + static Enumeration enumerate() { + Vector list = new Vector(); + Enumeration e = cache.keys(); + while (e.hasMoreElements()) { + Vector v = (Vector) cache.get(e.nextElement()); + for (int i = 0; i < v.size(); i++) { + list.addElement(v.elementAt(i)); + } + } + return list.elements(); } - static synchronized void clean() - { - cacheClean(); + static synchronized void clean() { + cacheClean(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java index 1dff75fec..ebf3fa6ef 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java @@ -4,36 +4,36 @@ import java.io.IOException; class HttpsThrough extends HttpConnection { - boolean proxy = false; + boolean proxy = false; - HttpsThrough(String host, int port) throws IOException { - super(host, port); - } + HttpsThrough(String host, int port) throws IOException { + super(host, port); + } - HttpsThrough(String host, int port, boolean isProxy) throws IOException { - this(host, port); - proxy = isProxy; - } + HttpsThrough(String host, int port, boolean isProxy) throws IOException { + this(host, port); + proxy = isProxy; + } - public void sendRequest(Request request) - throws IOException, RetryRequestException { - if (proxy) { - super.sendRequest(request); - } else { - /* nothing */ - } - } + public void sendRequest(Request request) + throws IOException, RetryRequestException { + if (proxy) { + super.sendRequest(request); + } else { + /* nothing */ + } + } - public Reply recvReply(Request request) - throws java.io.IOException, RetryRequestException { - Reply reply = new Reply(getInputStream()); - if (proxy) { - reply.read(); - } else { - reply.statusLine = "HTTP/1.0 200 Connection established"; - reply.setHeaderField("Proxy-agent", ProxyConfig.appName); - } + public Reply recvReply(Request request) + throws java.io.IOException, RetryRequestException { + Reply reply = new Reply(getInputStream()); + if (proxy) { + reply.read(); + } else { + reply.statusLine = "HTTP/1.0 200 Connection established"; + reply.setHeaderField("Proxy-agent", ProxyConfig.appName); + } - return reply; - } + return reply; + } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java b/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java index de73453e7..53b2382bb 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java @@ -3,39 +3,32 @@ package com.jpexs.proxy; import java.util.Enumeration; import java.util.Vector; -class Janitor implements Runnable -{ +class Janitor implements Runnable { + private Vector cleanable = new Vector(); - - public void add(Cleanable c) - { - cleanable.addElement(c); + + public void add(Cleanable c) { + cleanable.addElement(c); } - - public void run() - { - Thread.currentThread().setName("Janitor"); - - for (;;) - { - try - { - Thread.sleep(30 * 1000); /* 30 seconds */ - } - catch (Exception e) - { - - } - for (Enumeration e = cleanable.elements(); - e.hasMoreElements(); ) - { - ((Cleanable)e.nextElement()).clean(); - } + public void run() { + Thread.currentThread().setName("Janitor"); - Http.clean(); + for (;;) { + try { + Thread.sleep(30 * 1000); /* 30 seconds */ - - } + } catch (Exception e) { + + } + + for (Enumeration e = cleanable.elements(); + e.hasMoreElements();) { + ((Cleanable) e.nextElement()).clean(); + } + + Http.clean(); + + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Key.java b/libsrc/jpproxy/src/com/jpexs/proxy/Key.java index 79c552550..18fca5321 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Key.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Key.java @@ -1,6 +1,7 @@ package com.jpexs.proxy; class Key { + private String name = null; /** @@ -31,4 +32,4 @@ class Key { public String toString() { return name; } -} \ No newline at end of file +} diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Main.java b/libsrc/jpproxy/src/com/jpexs/proxy/Main.java index 2b41fa0c1..f690e36b6 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Main.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Main.java @@ -8,22 +8,22 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; -public class Main -{ +public class Main { public static final String REPLACEMENTSFILE = "." + File.separator + "config" + File.separator + "replacements.ini"; public static boolean DEBUG_MODE = false; - public static void main(String[] argv) - { - List replacements = new ArrayList(); + public static void main(String[] argv) { + List replacements = new ArrayList(); if ((new File(REPLACEMENTSFILE)).exists()) { try { BufferedReader br = new BufferedReader(new FileReader(REPLACEMENTSFILE)); String s = ""; while ((s = br.readLine()) != null) { String fileName = br.readLine(); - if (fileName == null) break; + if (fileName == null) { + break; + } fileName = fileName.replaceAll("[\\\\/]", File.separator); Replacement r = new Replacement(s, fileName); if (DEBUG_MODE) { @@ -40,25 +40,26 @@ public class Main System.out.println("WARNING:REPLACEMENTS FILE NOT FOUND."); } } - Server.startServer(ProxyConfig.port, replacements, new ArrayList(), new CatchedListener() { + Server.startServer(ProxyConfig.port, replacements, new ArrayList(), new CatchedListener() { /** * Method called when specified contentType is received * * @param contentType Content type - * @param url URL of the method - * @param data Data stream + * @param url URL of the method + * @param data Data stream */ - public void catched(String contentType, String url, InputStream data) { + public byte[] catched(String contentType, String url, InputStream data) { + return null; } - }, new ReplacedListener() { + }, new ReplacedListener() { public void replaced(Replacement replacement, String url, String contentType) { - if (DEBUG_MODE) { - System.out.println("REPLACED:" + url + " (Content-type:" + contentType + ") WITH FILE " + replacement.targetFile); - } + if (DEBUG_MODE) { + System.out.println("REPLACED:" + url + " (Content-type:" + contentType + ") WITH FILE " + replacement.targetFile); + } } - }); + }); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Message.java b/libsrc/jpproxy/src/com/jpexs/proxy/Message.java index b3cca4456..42c34aabb 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Message.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Message.java @@ -5,8 +5,8 @@ import java.util.Enumeration; import java.util.Vector; import java.io.*; -public abstract class Message -{ +public abstract class Message { + /** * Hashtable used to store message headers. */ @@ -17,199 +17,155 @@ public abstract class Message */ String statusLine = null; - public String readLine(InputStream in) throws IOException - { - char[] buf = new char[128]; - int offset = 0; - int ch; + public String readLine(InputStream in) throws IOException { + char[] buf = new char[128]; + int offset = 0; + int ch; - for (;;) - { - ch = in.read(); - if (ch == -1 || ch == '\n') - { - break; - } - else if (ch == '\r') - { - int tmpch = in.read(); - if (tmpch != '\n') - { - if (! (in instanceof PushbackInputStream)) - { - in = new PushbackInputStream(in); - } - ((PushbackInputStream) in).unread(tmpch); - } - break; - } - else - { - if (offset == buf.length) - { - char[] tmpbuf = buf; - buf = new char[tmpbuf.length * 2]; - System.arraycopy(tmpbuf, 0, buf, 0, offset); - } - buf[offset++] = (char) ch; - } - } - return String.copyValueOf(buf, 0, offset); + for (;;) { + ch = in.read(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch == '\r') { + int tmpch = in.read(); + if (tmpch != '\n') { + if (!(in instanceof PushbackInputStream)) { + in = new PushbackInputStream(in); + } + ((PushbackInputStream) in).unread(tmpch); + } + break; + } else { + if (offset == buf.length) { + char[] tmpbuf = buf; + buf = new char[tmpbuf.length * 2]; + System.arraycopy(tmpbuf, 0, buf, 0, offset); + } + buf[offset++] = (char) ch; + } + } + return String.copyValueOf(buf, 0, offset); } /** * Read headers and store them in the hashtable. */ - void readHeaders(InputStream in) throws IOException - { - int i; - Key key = null; - - for (;;) - { - String s = readLine(in); - if (s == null) - { - break; - } - i = s.indexOf(':'); - if (i == -1) - { - /* end of header */ - if (s.length() == 0) - { - break; - } - /* multi-line headers */ - else if (key != null - && (s.startsWith(" ") || s.startsWith("\t"))) - { - int index = getHeaderValueCount(key.toString()); - index--; - Vector v = (Vector) headers.get(key); - v.setElementAt(v.elementAt(index) + "\n" + s, index); - } - } - else - { - key = new Key(s.substring(0, i)); - Vector v; - if (headers.containsKey(key)) - { - v = (Vector) headers.get(key); - } - else - { - v = new Vector(); - } - v.addElement(s.substring(i+1).trim()); - headers.put(key, v); - } - } + void readHeaders(InputStream in) throws IOException { + int i; + Key key = null; + + for (;;) { + String s = readLine(in); + if (s == null) { + break; + } + i = s.indexOf(':'); + if (i == -1) { + /* end of header */ + if (s.length() == 0) { + break; + } /* multi-line headers */ else if (key != null + && (s.startsWith(" ") || s.startsWith("\t"))) { + int index = getHeaderValueCount(key.toString()); + index--; + Vector v = (Vector) headers.get(key); + v.setElementAt(v.elementAt(index) + "\n" + s, index); + } + } else { + key = new Key(s.substring(0, i)); + Vector v; + if (headers.containsKey(key)) { + v = (Vector) headers.get(key); + } else { + v = new Vector(); + } + v.addElement(s.substring(i + 1).trim()); + headers.put(key, v); + } + } } - public int headerCount() - { - return headers.size(); + public int headerCount() { + return headers.size(); } /** * Set the Status line. */ - public void setStatusLine(String l) - { + public void setStatusLine(String l) { statusLine = l; } - public int getHeaderValueCount(String name) - { - Vector v = (Vector) headers.get(new Key(name)); - return v.size(); - } - - public String getHeaderField(String name) - { - return getHeaderField(name, 0); - } - - public String getHeaderField(String name, int index) - { - Vector v = (Vector) headers.get(new Key(name)); - if (v == null) - { - return null; - } - return (String) v.elementAt(index); + public int getHeaderValueCount(String name) { + Vector v = (Vector) headers.get(new Key(name)); + return v.size(); } - public void setHeaderField(String name, String value) - { - setHeaderField(name, value, 0); + public String getHeaderField(String name) { + return getHeaderField(name, 0); } - public void setHeaderField(String name, String value, int index) - { - Vector v; - Key key = new Key(name); - - if (headers.containsKey(key)) - { - v = (Vector) headers.get(key); - } - else - { - v = new Vector(); - if (index == 0) - { - v.addElement(""); - } - headers.put(key, v); - } - v.setElementAt(value, index); - } - - public void setHeaderField(String name, int value) - { - setHeaderField(name, value, 0); + public String getHeaderField(String name, int index) { + Vector v = (Vector) headers.get(new Key(name)); + if (v == null) { + return null; + } + return (String) v.elementAt(index); } - public void setHeaderField(String name, int value, int index) - { - setHeaderField(name, new Integer(value).toString(), index); + public void setHeaderField(String name, String value) { + setHeaderField(name, value, 0); + } + + public void setHeaderField(String name, String value, int index) { + Vector v; + Key key = new Key(name); + + if (headers.containsKey(key)) { + v = (Vector) headers.get(key); + } else { + v = new Vector(); + if (index == 0) { + v.addElement(""); + } + headers.put(key, v); + } + v.setElementAt(value, index); + } + + public void setHeaderField(String name, int value) { + setHeaderField(name, value, 0); + } + + public void setHeaderField(String name, int value, int index) { + setHeaderField(name, new Integer(value).toString(), index); } /** - * Set all header fields with the give name to the - * specified value. + * Set all header fields with the give name to the specified value. */ - public void setHeaderFields(String name, String value) - { - Vector v; - Key key = new Key(name); + public void setHeaderFields(String name, String value) { + Vector v; + Key key = new Key(name); - v = (Vector) headers.get(key); - if (v != null) - { - for (int i = 0; i < v.size(); i++) - { - v.setElementAt(value, i); - } - } + v = (Vector) headers.get(key); + if (v != null) { + for (int i = 0; i < v.size(); i++) { + v.setElementAt(value, i); + } + } } - public void appendHeaderField(String name, String value) - { - appendHeaderField(name, value, 0); + public void appendHeaderField(String name, String value) { + appendHeaderField(name, value, 0); } - - public void appendHeaderField(String name, String value, int index) - { - setHeaderField(name, getHeaderField(name, index) + value, index); + + public void appendHeaderField(String name, String value, int index) { + setHeaderField(name, getHeaderField(name, index) + value, index); } - - public void removeHeaderField(String name) - { - headers.remove(new Key(name)); + + public void removeHeaderField(String name) { + headers.remove(new Key(name)); } /** @@ -217,81 +173,70 @@ public abstract class Message * * @param name header name */ - public boolean containsHeaderField(String name) - { - return headers.containsKey(new Key(name)); + public boolean containsHeaderField(String name) { + return headers.containsKey(new Key(name)); } /** * @return an Enumeration of Strings */ - public Enumeration getHeaders() - { - Vector v = new Vector(); + public Enumeration getHeaders() { + Vector v = new Vector(); - for (Enumeration e = headers.keys(); e.hasMoreElements(); ) - { - v.addElement(e.nextElement().toString()); - } + for (Enumeration e = headers.keys(); e.hasMoreElements();) { + v.addElement(e.nextElement().toString()); + } - return v.elements(); + return v.elements(); } private final static byte[] COLON_SPACE = ": ".getBytes(); private final static byte[] CRLF = "\r\n".getBytes(); - - private ByteArray toByteArray(byte[] sep) - { - ByteArray buf = new ByteArray(); - Key key; - String value; - Vector v; - int i = 0; - - buf.append(statusLine); - buf.append(sep); - - for (Enumeration e = headers.keys(); e.hasMoreElements(); ) - { - key = (Key) e.nextElement(); - v = (Vector) headers.get(key); - for (i = 0; i < v.size(); i++) - { - buf.append(key.toString()); - buf.append(COLON_SPACE); - buf.append(v.elementAt(i).toString()); - buf.append(sep); - } - } - buf.append(sep); - return buf; + private ByteArray toByteArray(byte[] sep) { + ByteArray buf = new ByteArray(); + Key key; + String value; + Vector v; + int i = 0; + + buf.append(statusLine); + buf.append(sep); + + for (Enumeration e = headers.keys(); e.hasMoreElements();) { + key = (Key) e.nextElement(); + v = (Vector) headers.get(key); + for (i = 0; i < v.size(); i++) { + buf.append(key.toString()); + buf.append(COLON_SPACE); + buf.append(v.elementAt(i).toString()); + buf.append(sep); + } + } + buf.append(sep); + + return buf; } - private ByteArray toByteArray() - { - return toByteArray(CRLF); + private ByteArray toByteArray() { + return toByteArray(CRLF); } - private ByteArray toByteArray(String sep) - { - return toByteArray(sep.getBytes()); + private ByteArray toByteArray(String sep) { + return toByteArray(sep.getBytes()); } - - public String toString() - { - return toByteArray().toString(); + + public String toString() { + return toByteArray().toString(); } - - public String toString(String sep) - { - return toByteArray(sep).toString(); + + public String toString(String sep) { + return toByteArray(sep).toString(); } public void write(OutputStream out) - throws IOException - { - toByteArray().writeTo(out); - out.flush(); + throws IOException { + toByteArray().writeTo(out); + out.flush(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java b/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java index f470d1bf6..4b3ac637d 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java @@ -9,35 +9,35 @@ import java.util.List; */ public class ProxyConfig { - public static boolean dontLogFilters=false; + public static boolean dontLogFilters = false; - public static String appVersion="1.1"; - public static String appName="JPProxy"; - public static int port=55555; - public static String bindAddress; + public static String appVersion = "1.1"; + public static String appName = "JPProxy"; + public static int port = 55555; + public static String bindAddress; - public static int readTimeout=50000; - public static boolean proxyKeepAlive=false; - public static boolean dontUncompress=false; + public static int readTimeout = 50000; + public static boolean proxyKeepAlive = false; + public static boolean dontUncompress = false; - public static final int HTTPS_PASSTHRU=0; - public static final int HTTPS_FILTER=1; - public static final int HTTPS_FILTERLIST=2; + public static final int HTTPS_PASSTHRU = 0; + public static final int HTTPS_FILTER = 1; + public static final int HTTPS_FILTERLIST = 2; - public static int httpsMode=HTTPS_PASSTHRU; + public static int httpsMode = HTTPS_PASSTHRU; - public static List enabledHttpsServers=new ArrayList(); + public static List enabledHttpsServers = new ArrayList(); - public static boolean useHTTPSProxy=false; - public static String httpsProxyHost=""; - public static int httpsProxyPort=0; + public static boolean useHTTPSProxy = false; + public static String httpsProxyHost = ""; + public static int httpsProxyPort = 0; - public static boolean useHTTPProxy=false; - public static String httpProxyHost=""; - public static int httpProxyPort=0; + public static boolean useHTTPProxy = false; + public static String httpProxyHost = ""; + public static int httpProxyPort = 0; - public static String httpsKeyStoreFile=null; - public static String httpsKeyStorePass=null; - public static String httpsKeyPass=null; + public static String httpsKeyStoreFile = null; + public static String httpsKeyStorePass = null; + public static String httpsKeyPass = null; } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java b/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java index b23576348..3304e76e4 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java @@ -1,5 +1,6 @@ -package com.jpexs.proxy; - -public interface ReplacedListener { - public void replaced(Replacement replacement, String url, String contentType); -} +package com.jpexs.proxy; + +public interface ReplacedListener { + + public void replaced(Replacement replacement, String url, String contentType); +} diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java b/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java index 0d7fc4da8..ce1f6b8d8 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java @@ -8,238 +8,195 @@ import java.io.SequenceInputStream; import java.util.Hashtable; import java.util.StringTokenizer; -public class Reply extends Message -{ +public class Reply extends Message { + InputStream in = null; int statusCode = -1; - public Reply() - { + public Reply() { } - public Reply(InputStream in) - { - setContent(in); + public Reply(InputStream in) { + setContent(in); } - public void setContent(InputStream in) - { - this.in = in; + public void setContent(InputStream in) { + this.in = in; } - public InputStream getContent() - { - return in; - } - - void read() throws IOException - { - if (in != null) - { - read(in); - } - } - - void read(InputStream in) throws IOException - { - statusLine = readLine(in); - if (statusLine == null || statusLine.length() == 0) - { - throw new IOException("Missing HTTP status line"); - } - - /* Look for HTTP/0.9 */ - if (!statusLine.startsWith("HTTP")) - { - /* Put back the line */ - if (this.in != null) - { - String putback = new String(statusLine + "\n"); - this.in = new SequenceInputStream(new StringBufferInputStream(putback), in); - } - /* Fake a status line and upgrade to HTTP/1.0 */ - statusLine = "HTTP/1.0 200 OK"; - return; - } - - readHeaders(in); - int code = getStatusCode(); - - /* RFC 2068: 204 and 304 MUST NOT contain a message body. */ - switch (code) - { - case 204: /* No Content */ - case 304: /* Not Modified */ - /* Ignore the message body if it exists */ - if (containsHeaderField("Content-length")) - { - int contentLength = 0; - try - { - contentLength = Integer.parseInt(getHeaderField("Content-length")); - } - catch (NumberFormatException e) - { - } - int n; - byte[] buffer = new byte[8192]; - while ((n = in.read(buffer, 0, buffer.length)) > 0) - { - /* ignore */ - } - removeHeaderField("Content-length"); - } - break; - } + public InputStream getContent() { + return in; } - public boolean hasContent() - { - switch (getStatusCode()) - { - case 204: - case 304: - return false; - - default: - return true; - } + void read() throws IOException { + if (in != null) { + read(in); + } } - public String getProtocol() - { - StringTokenizer st = new StringTokenizer(statusLine); - String protocol = (String) st.nextToken(); - return protocol; + void read(InputStream in) throws IOException { + statusLine = readLine(in); + if (statusLine == null || statusLine.length() == 0) { + throw new IOException("Missing HTTP status line"); + } + + /* Look for HTTP/0.9 */ + if (!statusLine.startsWith("HTTP")) { + /* Put back the line */ + if (this.in != null) { + String putback = new String(statusLine + "\n"); + this.in = new SequenceInputStream(new StringBufferInputStream(putback), in); + } + /* Fake a status line and upgrade to HTTP/1.0 */ + statusLine = "HTTP/1.0 200 OK"; + return; + } + + readHeaders(in); + int code = getStatusCode(); + + /* RFC 2068: 204 and 304 MUST NOT contain a message body. */ + switch (code) { + case 204: /* No Content */ + + case 304: /* Not Modified */ + /* Ignore the message body if it exists */ + + if (containsHeaderField("Content-length")) { + int contentLength = 0; + try { + contentLength = Integer.parseInt(getHeaderField("Content-length")); + } catch (NumberFormatException e) { + } + int n; + byte[] buffer = new byte[8192]; + while ((n = in.read(buffer, 0, buffer.length)) > 0) { + /* ignore */ + } + removeHeaderField("Content-length"); + } + break; + } } - public int getStatusCode() - { - if (statusCode == -1) - { - StringTokenizer st = new StringTokenizer(statusLine); - String protocol = (String) st.nextToken(); - String status = (String) st.nextToken(); + public boolean hasContent() { + switch (getStatusCode()) { + case 204: + case 304: + return false; - try - { - statusCode = Integer.parseInt(status); - } - catch (NumberFormatException e) - { - System.out.println("Malformed or missing status code"); - statusCode = 0; - } - } - - return statusCode; + default: + return true; + } } - private Hashtable headerParser(String header) - { - Hashtable table = new Hashtable(); - String type = getHeaderField(header); - if (type == null) - { - return table; - } - - StringTokenizer st = new StringTokenizer(type, ";"); - int count = 0; - while (st.hasMoreTokens()) - { - String token = st.nextToken(); - token = token.trim(); - String name; - String value; - int i = token.indexOf('='); - if (i != -1) - { - name = token.substring(0, i); - value = token.substring(i+1); - } - else - { - name = token; - value = ""; - } - - if (count == 0) - { - table.put(header, name); - } - else - { - table.put(name, value); - } - - count++; - } - - return table; + public String getProtocol() { + StringTokenizer st = new StringTokenizer(statusLine); + String protocol = (String) st.nextToken(); + return protocol; } - public String getContentType() - { - Hashtable table = headerParser("Content-type"); - return(String) table.get("Content-type"); + public int getStatusCode() { + if (statusCode == -1) { + StringTokenizer st = new StringTokenizer(statusLine); + String protocol = (String) st.nextToken(); + String status = (String) st.nextToken(); + + try { + statusCode = Integer.parseInt(status); + } catch (NumberFormatException e) { + System.out.println("Malformed or missing status code"); + statusCode = 0; + } + } + + return statusCode; } - public String getBoundary() - { - Hashtable table = headerParser("Content-type"); - return(String) table.get("boundary"); + private Hashtable headerParser(String header) { + Hashtable table = new Hashtable(); + String type = getHeaderField(header); + if (type == null) { + return table; + } + + StringTokenizer st = new StringTokenizer(type, ";"); + int count = 0; + while (st.hasMoreTokens()) { + String token = st.nextToken(); + token = token.trim(); + String name; + String value; + int i = token.indexOf('='); + if (i != -1) { + name = token.substring(0, i); + value = token.substring(i + 1); + } else { + name = token; + value = ""; + } + + if (count == 0) { + table.put(header, name); + } else { + table.put(name, value); + } + + count++; + } + + return table; } - public String getTransferEncoding() - { - Hashtable table = headerParser("Transfer-Encoding"); - return(String) table.get("Transfer-Encoding"); + public String getContentType() { + Hashtable table = headerParser("Content-type"); + return (String) table.get("Content-type"); } - public int getChunkSize(InputStream in) throws IOException - { - String line = readLine(in); - line = line.trim(); /* apache can have trailing spaces */ - int size = -1; - try - { - size = Integer.valueOf(line, 16).intValue(); - } - catch (NumberFormatException e) - { - System.out.println(e); - } - return size; + public String getBoundary() { + Hashtable table = headerParser("Content-type"); + return (String) table.get("boundary"); } - public void getChunkedFooter(InputStream in) throws IOException - { - for (;;) - { - String line = readLine(in); - if (line == null) - { - break; - } - int i = line.indexOf(':'); - if (i == -1) - { - break; - } - } + public String getTransferEncoding() { + Hashtable table = headerParser("Transfer-Encoding"); + return (String) table.get("Transfer-Encoding"); } - public void setStatusLine(String line) - { - this.statusLine = line; + public int getChunkSize(InputStream in) throws IOException { + String line = readLine(in); + line = line.trim(); /* apache can have trailing spaces */ + + int size = -1; + try { + size = Integer.valueOf(line, 16).intValue(); + } catch (NumberFormatException e) { + System.out.println(e); + } + return size; } - public static Reply createRedirect(String url) - { - Reply r = new Reply(); - r.setStatusLine("HTTP/1.0 302 Moved Temporarily"); - r.setHeaderField("Location", url); - return r; + public void getChunkedFooter(InputStream in) throws IOException { + for (;;) { + String line = readLine(in); + if (line == null) { + break; + } + int i = line.indexOf(':'); + if (i == -1) { + break; + } + } + } + + public void setStatusLine(String line) { + this.statusLine = line; + } + + public static Reply createRedirect(String url) { + Reply r = new Reply(); + r.setStatusLine("HTTP/1.0 302 Moved Temporarily"); + r.setHeaderField("Location", url); + return r; } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Request.java b/libsrc/jpproxy/src/com/jpexs/proxy/Request.java index e227a96a4..b7630e106 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Request.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Request.java @@ -8,9 +8,8 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; -public class Request extends Message -{ - +public class Request extends Message { + private String command = null; private String url = null; private String protocol = null; @@ -18,264 +17,207 @@ public class Request extends Message private Client client = null; private Hashtable log; private Vector logHeaders; - public boolean hadKeepalive=false; + public boolean hadKeepalive = false; - - Request(Client c) - { - client = c; + Request(Client c) { + client = c; } - void read(InputStream in) throws IOException - { - statusLine = readLine(in); - if (statusLine == null || statusLine.length() == 0) - { - throw new IOException("Empty request"); - } + void read(InputStream in) throws IOException { + statusLine = readLine(in); + if (statusLine == null || statusLine.length() == 0) { + throw new IOException("Empty request"); + } - StringTokenizer st = new StringTokenizer(statusLine); - command = (String) st.nextToken(); - url = (String) st.nextToken(); - protocol = (String) st.nextToken(); + StringTokenizer st = new StringTokenizer(statusLine); + command = (String) st.nextToken(); + url = (String) st.nextToken(); + protocol = (String) st.nextToken(); - if (!url.startsWith("http")) - { - //TODO do something here - } + if (!url.startsWith("http")) { + //TODO do something here + } - readHeaders(in); + readHeaders(in); - if ("POST".equals(command) || "PUT".equals(command)) - { - try - { - int n = Integer.parseInt(getHeaderField("Content-length")); - data = new byte[n]; - int offset = 0; - while (offset < data.length) - { - n = in.read(data, offset, data.length - offset); - if (n < 0) - { - throw new IOException("Not enough " + command + " data"); - } - offset += n; - } - } - catch (NumberFormatException e) - { - - } - } + if ("POST".equals(command) || "PUT".equals(command)) { + try { + int n = Integer.parseInt(getHeaderField("Content-length")); + data = new byte[n]; + int offset = 0; + while (offset < data.length) { + n = in.read(data, offset, data.length - offset); + if (n < 0) { + throw new IOException("Not enough " + command + " data"); + } + offset += n; + } + } catch (NumberFormatException e) { + + } + } } public void write(OutputStream out) - throws IOException - { - super.write(out); - if (data != null) - { - out.write(data); - out.flush(); - } + throws IOException { + super.write(out); + if (data != null) { + out.write(data); + out.flush(); + } } - public String getRequest() - { - return statusLine; + public String getRequest() { + return statusLine; } - public String getCommand() - { - return command; - } - - public void setCommand(String command) - { - this.command = command; + public String getCommand() { + return command; } - public String getURL() - { - return url; + public void setCommand(String command) { + this.command = command; } - public void setURL(String url) - { - this.url = url; + public String getURL() { + return url; } - public String getProtocol() - { - return protocol; + public void setURL(String url) { + this.url = url; } - public void setProtocol(String protocol) - { - this.protocol = protocol; + public String getProtocol() { + return protocol; } - public void addSecureHostToURL(String host){ - url="https://"+host+url; + public void setProtocol(String protocol) { + this.protocol = protocol; } - public String getHost() - { - String url = getURL(); - String s; - - if (url.startsWith("http://")) - { - s = url.substring(7, url.indexOf('/', 7)); - } - else if (url.startsWith("https://")) - { - s = url.substring(8, url.indexOf('/', 8)); - } - else - { - s = url; - } - - int at = s.indexOf('@'); - if (at != -1 ) - { - s = s.substring(at+1); - } - - if (s.indexOf(':') != -1) - { - return s.substring(0, s.indexOf(':')); - } - - return s; + public void addSecureHostToURL(String host) { + url = "https://" + host + url; } - public int getPort() - { - int port = 80; - String url = getURL(); - String s; + public String getHost() { + String url = getURL(); + String s; - if (url.startsWith("http://")) - { - s = url.substring(7, url.indexOf('/', 7)); - } - else if (url.startsWith("https://")) - { - s = url.substring(8, url.indexOf('/', 8)); - port=443; - } - else - { - s = url; - } + if (url.startsWith("http://")) { + s = url.substring(7, url.indexOf('/', 7)); + } else if (url.startsWith("https://")) { + s = url.substring(8, url.indexOf('/', 8)); + } else { + s = url; + } - int at = s.indexOf('@'); - if (at != -1 ) - { - s = s.substring(at+1); - } + int at = s.indexOf('@'); + if (at != -1) { + s = s.substring(at + 1); + } - if (s.indexOf(':') != -1) - { - try - { - port = Integer.parseInt(s.substring(s.indexOf(':') + 1)); - } - catch (NumberFormatException e) - { - - } - } - return port; + if (s.indexOf(':') != -1) { + return s.substring(0, s.indexOf(':')); + } + + return s; } - public String getData() - { - if (data == null) - { - return null; - } - return new String(data); + public int getPort() { + int port = 80; + String url = getURL(); + String s; + + if (url.startsWith("http://")) { + s = url.substring(7, url.indexOf('/', 7)); + } else if (url.startsWith("https://")) { + s = url.substring(8, url.indexOf('/', 8)); + port = 443; + } else { + s = url; + } + + int at = s.indexOf('@'); + if (at != -1) { + s = s.substring(at + 1); + } + + if (s.indexOf(':') != -1) { + try { + port = Integer.parseInt(s.substring(s.indexOf(':') + 1)); + } catch (NumberFormatException e) { + + } + } + return port; } - public String getPath() - { - String str = getURL(); - int pos = 0; - for (int i = 0; i < 3; i++) - { - pos = str.indexOf('/', pos); - pos++; - } - pos--; - return str.substring(pos); - } - - public String getDocument() - { - String path = getPath(); - int n = path.lastIndexOf('/'); - if (n == path.length() - 1) - { - n = path.lastIndexOf('/', n - 1); - } - if (n < 0) - { - return "/"; - } - else - { - return path.substring(n + 1); - } + public String getData() { + if (data == null) { + return null; + } + return new String(data); } - public Client getClient() - { - return client; + public String getPath() { + String str = getURL(); + int pos = 0; + for (int i = 0; i < 3; i++) { + pos = str.indexOf('/', pos); + pos++; + } + pos--; + return str.substring(pos); } - public String getQueryString() - { - String path = getPath(); - int n = path.indexOf('?'); - if (n < 0) - { - return null; - } - return path.substring(n + 1); + public String getDocument() { + String path = getPath(); + int n = path.lastIndexOf('/'); + if (n == path.length() - 1) { + n = path.lastIndexOf('/', n - 1); + } + if (n < 0) { + return "/"; + } else { + return path.substring(n + 1); + } + } + + public Client getClient() { + return client; + } + + public String getQueryString() { + String path = getPath(); + int n = path.indexOf('?'); + if (n < 0) { + return null; + } + return path.substring(n + 1); } public synchronized void addLogEntry(String header, - String message) - { - if (log == null) - { - log = new Hashtable(); - logHeaders = new Vector(); - } + String message) { + if (log == null) { + log = new Hashtable(); + logHeaders = new Vector(); + } - Vector v = (Vector)log.get(header); - if (log.get(header) == null) - { - v = new Vector(); - log.put(header, v); - logHeaders.addElement(header); - } - v.addElement(message); + Vector v = (Vector) log.get(header); + if (log.get(header) == null) { + v = new Vector(); + log.put(header, v); + logHeaders.addElement(header); + } + v.addElement(message); } - public Enumeration getLogHeaders() - { - return logHeaders != null ? logHeaders.elements() : null; + public Enumeration getLogHeaders() { + return logHeaders != null ? logHeaders.elements() : null; } - public Enumeration getLogEntries(String header) - { - return log != null ? ((Vector)log.get(header)).elements() : null; + public Enumeration getLogEntries(String header) { + return log != null ? ((Vector) log.get(header)).elements() : null; } - } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java b/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java index 9d2d20235..873b5eb7e 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java @@ -1,14 +1,12 @@ package com.jpexs.proxy; -class RetryRequestException extends Exception -{ - RetryRequestException() - { - super(); +class RetryRequestException extends Exception { + + RetryRequestException() { + super(); } - - RetryRequestException(String message) - { - super(message); + + RetryRequestException(String message) { + super(message); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java b/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java index c2621772c..090ec7888 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java @@ -1,70 +1,57 @@ package com.jpexs.proxy; -public class ReusableThread extends Thread -{ +public class ReusableThread extends Thread { + private ThreadPool pool = null; private Runnable runnable = null; private boolean alive = true; private long lastrun = 0; private int used = 0; - public ReusableThread(ThreadPool pool) - { - this.pool = pool; + public ReusableThread(ThreadPool pool) { + this.pool = pool; } - public synchronized void setRunnable(Runnable runnable) - { - this.runnable = runnable; - notify(); + public synchronized void setRunnable(Runnable runnable) { + this.runnable = runnable; + notify(); } - public synchronized void terminate() - { - alive = false; - notify(); + public synchronized void terminate() { + alive = false; + notify(); } - public long getLastRunTime() - { - return lastrun; + public long getLastRunTime() { + return lastrun; } - public int useCount() - { - return used; + public int useCount() { + return used; } - - public void run() - { - while (alive) - { - setName("ReusableThread: idle"); - - while (runnable == null && alive) - { - synchronized (this) - { - try - { - wait(); - } - catch (InterruptedException ie) - { - } - } - } - if (alive) - { - setName("ReusableThread: busy"); - setPriority(Thread.NORM_PRIORITY); - lastrun = System.currentTimeMillis(); - used++; - runnable.run(); - runnable = null; - pool.put(this); - } - } + public void run() { + while (alive) { + setName("ReusableThread: idle"); + + while (runnable == null && alive) { + synchronized (this) { + try { + wait(); + } catch (InterruptedException ie) { + } + } + } + + if (alive) { + setName("ReusableThread: busy"); + setPriority(Thread.NORM_PRIORITY); + lastrun = System.currentTimeMillis(); + used++; + runnable.run(); + runnable = null; + pool.put(this); + } + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Server.java b/libsrc/jpproxy/src/com/jpexs/proxy/Server.java index 70d9f69e8..00ba753a7 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Server.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Server.java @@ -7,8 +7,8 @@ import java.io.IOException; import java.io.DataOutputStream; import java.util.List; -public class Server implements Runnable -{ +public class Server implements Runnable { + ServerSocket server = null; boolean running = false; @@ -17,145 +17,121 @@ public class Server implements Runnable private ReplacedListener replacedListener; private List replacements; - static ThreadPool pool; + static ThreadPool pool; static Server myServer; - static boolean serverRunning=false; + static boolean serverRunning = false; - static boolean stopping=false; + static boolean stopping = false; - public static ReusableThread getThread() - { - return pool.get(); + public static ReusableThread getThread() { + return pool.get(); } /** * Starts proxy server * - * @param port Listening port - * @param replacements List of replacements + * @param port Listening port + * @param replacements List of replacements * @param catchedContentTypes Content types to sniff - * @param catchedListener Catched listener + * @param catchedListener Catched listener */ public static boolean startServer(int port, List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) { stopServer(); - try { - myServer = new Server(port, replacements, catchedContentTypes, catchedListener, replacedListener); - } catch (IOException ex) { - return false; - } - pool = new ThreadPool(ProxyConfig.appName+" Threads"); + try { + myServer = new Server(port, replacements, catchedContentTypes, catchedListener, replacedListener); + } catch (IOException ex) { + return false; + } + pool = new ThreadPool(ProxyConfig.appName + " Threads"); /* Startup the Janitor */ - Janitor j = new Janitor(); - j.add(pool); - getThread().setRunnable(j); - serverRunning=true; - getThread().setRunnable(myServer); - return true; + Janitor j = new Janitor(); + j.add(pool); + getThread().setRunnable(j); + serverRunning = true; + getThread().setRunnable(myServer); + return true; } + public static void stopServer() { + if (serverRunning) { + serverRunning = false; + try { + myServer.server.close(); + } catch (IOException ex) { - public static void stopServer() - { - if(serverRunning){ - serverRunning=false; - try { - myServer.server.close(); - } catch (IOException ex) { - - } - pool.clean(); - } + } + pool.clean(); + } } - Server(int port,List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) throws IOException - { - this.replacements = replacements; - this.catchedContentTypes = catchedContentTypes; + Server(int port, List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) throws IOException { + + this.replacements = replacements; + this.catchedContentTypes = catchedContentTypes; this.catchedListener = catchedListener; this.replacedListener = replacedListener; - - try - { - String bindaddr = ProxyConfig.bindAddress; - if (bindaddr != null && bindaddr.length() > 0) - { - server = new ServerSocket(port, 512, - InetAddress.getByName(bindaddr)); - } - else - { - server = new ServerSocket(port, 512); - } - } - catch (IOException e) - { - throw e; - } - /* Initialize internal Httpd */ + try { + String bindaddr = ProxyConfig.bindAddress; + if (bindaddr != null && bindaddr.length() > 0) { + server = new ServerSocket(port, 512, + InetAddress.getByName(bindaddr)); + } else { + server = new ServerSocket(port, 512); + } + } catch (IOException e) { + throw e; + } + + /* Initialize internal Httpd */ } - synchronized void suspend() - { - running = false; + synchronized void suspend() { + running = false; } - synchronized void resume() - { - running = true; + synchronized void resume() { + running = true; } + public void run() { + Thread.currentThread().setName(ProxyConfig.appName + " Server"); + running = true; + for (;;) { + Socket socket; - public void run() - { - Thread.currentThread().setName(ProxyConfig.appName+" Server"); - running = true; - for (;;) - { - Socket socket; + try { + socket = server.accept(); + } catch (IOException e) { + if (stopping) { + break; + } + continue; + } - try - { - socket = server.accept(); - } - catch (IOException e) - { - if(stopping){ - break; - } - continue; - } + if (stopping) { + break; + } - if(stopping){ - break; - } - - if (running) - { - Handler h = new Handler(socket,replacements,catchedContentTypes,catchedListener,replacedListener); - ReusableThread rt = getThread(); - rt.setRunnable(h); - } - else - { - error(socket, 503, ProxyConfig.appName+" proxy service is suspended."); - } - } + if (running) { + Handler h = new Handler(socket, replacements, catchedContentTypes, catchedListener, replacedListener); + ReusableThread rt = getThread(); + rt.setRunnable(h); + } else { + error(socket, 503, ProxyConfig.appName + " proxy service is suspended."); + } + } } - void error(Socket socket, int code, String message) - { - try - { - DataOutputStream out = new DataOutputStream(socket.getOutputStream()); - out.writeBytes((new HttpError(code, message)).toString()); - out.close(); - socket.close(); - } - catch (IOException e) - { - e.printStackTrace(); - } + void error(Socket socket, int code, String message) { + try { + DataOutputStream out = new DataOutputStream(socket.getOutputStream()); + out.writeBytes((new HttpError(code, message)).toString()); + out.close(); + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java b/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java index 1568f7000..86409ddcb 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java @@ -2,52 +2,44 @@ package com.jpexs.proxy; import java.util.*; -public class ThreadPool implements Cleanable -{ +public class ThreadPool implements Cleanable { + private String name; private Vector pool = new Vector(); - - public ThreadPool(String name) - { - this.name = name; + + public ThreadPool(String name) { + this.name = name; } - public synchronized ReusableThread get() - { - ReusableThread rt = null; + public synchronized ReusableThread get() { + ReusableThread rt = null; - if (pool.size() > 0) - { - rt = (ReusableThread)pool.firstElement(); - pool.removeElement(rt); - } + if (pool.size() > 0) { + rt = (ReusableThread) pool.firstElement(); + pool.removeElement(rt); + } - if (rt == null) - { - rt = new ReusableThread(this); - rt.start(); - } + if (rt == null) { + rt = new ReusableThread(this); + rt.start(); + } - return rt; + return rt; } - public synchronized void put(ReusableThread rt) - { - pool.addElement(rt); + public synchronized void put(ReusableThread rt) { + pool.addElement(rt); } - public synchronized void clean() - { - long now = System.currentTimeMillis(); - - for (Enumeration e = pool.elements(); e.hasMoreElements(); ) - { - ReusableThread rt = (ReusableThread) e.nextElement(); - if (now - rt.getLastRunTime() >= 30000) - { - rt.terminate(); - pool.removeElement(rt); - } - } + public synchronized void clean() { + long now = System.currentTimeMillis(); + + for (Enumeration e = pool.elements(); e.hasMoreElements();) { + ReusableThread rt = (ReusableThread) e.nextElement(); + if (now - rt.getLastRunTime() >= 30000) { + rt.terminate(); + pool.removeElement(rt); + } + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java b/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java index 991dfb885..ddc2d4075 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java @@ -2,132 +2,133 @@ package com.jpexs.proxy; import java.util.*; - public class WorkerThread extends Thread { -private Runnable task; -private String name; + private Runnable task; + private String name; -private static int nactive = 0; -private static Vector list = new Vector(); -private static int max = 10; -private static long lifetime = 900 * 1000; /* 15 minute default */ + private static int nactive = 0; + private static Vector list = new Vector(); + private static int max = 10; + private static long lifetime = 900 * 1000; /* 15 minute default */ -private -WorkerThread() { - setDaemon(true); -} -/** - * Sets the lifetime of an idle WorkerThread (in ms). A WorkerThread - * will remain on the idle list for this much time before exiting. This - * does not affect WorkerThreads currently idling. - */ -synchronized static void -setLifetime(long time) { - lifetime = time; -} + private + WorkerThread() { + setDaemon(true); + } -/** - * Sets the maximum number of WorkerThreads that can exist at any given - * time. If this value is decreased below the current number of - * WorkerThreads, this will not take effect immediately. - */ -synchronized static void -setMaxThreads(int maxThreads) { - max = maxThreads; -} + /** + * Sets the lifetime of an idle WorkerThread (in ms). A WorkerThread will + * remain on the idle list for this much time before exiting. This does not + * affect WorkerThreads currently idling. + */ + synchronized static void + setLifetime(long time) { + lifetime = time; + } -/** - * Obtains a WorkerThread to which a task can be assigned. If an idle - * WorkerThread is present, it is removed from the idle list and returned. - * If not, and the maximum number of WorkerThreads has not been reached, - * a new WorkerThread is created. If the maximum number has been reached, - * this blocks until a WorkerThread is free. - */ -static WorkerThread -getThread() { - WorkerThread t; - synchronized (list) { - if (list.size() > 0) { - t = (WorkerThread) list.firstElement(); - list.removeElement(t); - } - else if (nactive >= max) { - while (true) { - try { - list.wait(); - } - catch (InterruptedException e) { - } - if (list.size() == 0) - continue; - t = (WorkerThread) list.firstElement(); - list.removeElement(t); - break; - } - } - else - t = new WorkerThread(); - nactive++; - } - return t; -} + /** + * Sets the maximum number of WorkerThreads that can exist at any given + * time. If this value is decreased below the current number of + * WorkerThreads, this will not take effect immediately. + */ + synchronized static void + setMaxThreads(int maxThreads) { + max = maxThreads; + } -/** - * Assigns a task to a WorkerThread - * @param task The task to be run - * @param name The name of the task - */ -public static void -assignThread(Runnable task, String name) { - while (true) { - try { - WorkerThread t = getThread(); - synchronized (t) { - t.task = task; - t.name = name; - if (!t.isAlive()) - t.start(); - else - t.notify(); - } - return; - } - catch (IllegalThreadStateException e) { - } - } -} + /** + * Obtains a WorkerThread to which a task can be assigned. If an idle + * WorkerThread is present, it is removed from the idle list and returned. + * If not, and the maximum number of WorkerThreads has not been reached, a + * new WorkerThread is created. If the maximum number has been reached, this + * blocks until a WorkerThread is free. + */ + static WorkerThread + getThread() { + WorkerThread t; + synchronized (list) { + if (list.size() > 0) { + t = (WorkerThread) list.firstElement(); + list.removeElement(t); + } else if (nactive >= max) { + while (true) { + try { + list.wait(); + } catch (InterruptedException e) { + } + if (list.size() == 0) { + continue; + } + t = (WorkerThread) list.firstElement(); + list.removeElement(t); + break; + } + } else { + t = new WorkerThread(); + } + nactive++; + } + return t; + } -/** Performs the task */ -synchronized public void -run() { - while (true) { - setName(name); - try { - task.run(); - } - catch (Throwable t) { - System.err.println(t); - } - setName("idle thread"); - synchronized (list) { - list.addElement(this); - if (nactive >= max) - list.notify(); - nactive--; - } - task = null; - try { - wait(lifetime); - } - catch (InterruptedException e) { - } - if (task == null) { - list.removeElement(this); - return; - } - } -} + /** + * Assigns a task to a WorkerThread + * + * @param task The task to be run + * @param name The name of the task + */ + public static void + assignThread(Runnable task, String name) { + while (true) { + try { + WorkerThread t = getThread(); + synchronized (t) { + t.task = task; + t.name = name; + if (!t.isAlive()) { + t.start(); + } else { + t.notify(); + } + } + return; + } catch (IllegalThreadStateException e) { + } + } + } + + /** + * Performs the task + */ + synchronized public void + run() { + while (true) { + setName(name); + try { + task.run(); + } catch (Throwable t) { + System.err.println(t); + } + setName("idle thread"); + synchronized (list) { + list.addElement(this); + if (nactive >= max) { + list.notify(); + } + nactive--; + } + task = null; + try { + wait(lifetime); + } catch (InterruptedException e) { + } + if (task == null) { + list.removeElement(this); + return; + } + } + } } diff --git a/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java b/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java index a3439c8a0..d11184d41 100644 --- a/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java +++ b/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java @@ -21,6 +21,8 @@ import com.jpexs.decompiler.flash.gui.AppFrame; import com.jpexs.decompiler.flash.gui.Main; import com.jpexs.decompiler.flash.gui.MainFrame; import com.jpexs.decompiler.flash.gui.View; +import com.jpexs.decompiler.flash.helpers.SWFDecompilerPlugin; +import com.jpexs.helpers.Helper; import com.jpexs.proxy.CatchedListener; import com.jpexs.proxy.ReplacedListener; import com.jpexs.proxy.Replacement; @@ -347,50 +349,55 @@ public class ProxyFrame extends AppFrame implements ActionListener, CatchedListe * @param contentType Content type * @param url URL of the method * @param data Data stream + * @return replacement data */ @Override - public void catched(String contentType, String url, InputStream data) { + public byte[] catched(String contentType, String url, InputStream data) { boolean swfOnly = false; if (contentType.contains(";")) { contentType = contentType.substring(0, contentType.indexOf(';')); } if ((!sniffSWFCheckBox.isSelected()) && (contentType.equals("application/x-shockwave-flash"))) { - return; + return null; } if ((!sniffJSCheckBox.isSelected()) && (contentType.equals("application/javascript") || contentType.equals("application/x-javascript") || contentType.equals("text/javascript") || contentType.equals("application/json"))) { - return; + return null; } if ((!sniffXMLCheckBox.isSelected()) && (contentType.equals("application/xml") || contentType.equals("text/xml"))) { - return; + return null; } if ((!sniffOSCheckBox.isSelected()) && (contentType.equals("application/octet-stream"))) { - return; + return null; } + + byte[] result = null; + if (!listModel.contains(url)) { try { byte[] hdr = new byte[3]; data.read(hdr); String shdr = new String(hdr); - if ((swfOnly) && ((!shdr.equals("FWS")) && (!shdr.equals("CWS")))) { - return; //NOT SWF + if (swfOnly && ((!shdr.equals("FWS")) && (!shdr.equals("CWS")) && (!shdr.equals("ZWS")))) { + return null; //NOT SWF } - File f = new File(Main.tempFile(url)); - try (FileOutputStream fos = new FileOutputStream(f)) { - fos.write(hdr); - byte[] buf = new byte[2048]; - int count; - while ((count = data.read(buf)) > 0) { - fos.write(buf, 0, count); - } + String tempFilePath = Main.tempFile(url); + data.reset(); + byte[] dataArray = Helper.readStream(data); + try (FileOutputStream fos = new FileOutputStream(new File(tempFilePath))) { + fos.write(dataArray); } - Replacement r = new Replacement(url, Main.tempFile(url)); + + result = SWFDecompilerPlugin.fireProxyFileCatched(dataArray); + + Replacement r = new Replacement(url, tempFilePath); r.lastAccess = Calendar.getInstance(); listModel.addURL(r); } catch (IOException e) { } - } + + return result; } /**