From ab8129facde6e948451b80d5e4145fda665a730d Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 26 Mar 2016 16:29:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=20=D0=B2=D0=B8=D0=B4=D0=B5?= =?UTF-8?q?=D0=BE=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20proxy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kinosearch/core/warez/Onlinelife.java | 24 ++++++- .../webapp/servlets/ProxyServlet.java | 64 +++++++++++++++++++ webapp/WEB-INF/player.html | 4 +- webapp/WEB-INF/web.xml | 10 +++ 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/main/java/kinosearch/webapp/servlets/ProxyServlet.java diff --git a/src/main/java/kinosearch/core/warez/Onlinelife.java b/src/main/java/kinosearch/core/warez/Onlinelife.java index 410432a..0bc6893 100644 --- a/src/main/java/kinosearch/core/warez/Onlinelife.java +++ b/src/main/java/kinosearch/core/warez/Onlinelife.java @@ -1,6 +1,8 @@ package kinosearch.core.warez; import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import kinosearch.core.browser.Browser; import kinosearch.core.Kino; @@ -68,12 +70,30 @@ public class Onlinelife implements KinoWarez { JsonObject jsonObj = new Gson().fromJson(json, JsonObject.class); if (jsonObj.has("file")) { - return "{\"file\":\"" + jsonObj.get("file").getAsString() + "\"}"; + String fileMp4 = jsonObj.get("file").getAsString().substring("http://".length()); + return "{\"file\":\"/proxy/onlinelife/" + fileMp4 + "\"}"; } else if (jsonObj.has("pl")) { browser.setEncoding("utf-8"); - return browser.get(jsonObj.get("pl").getAsString()); + return replaceToProxy(browser.get(jsonObj.get("pl").getAsString())); } else { return "{}"; } } + + private String replaceToProxy(String json) { + JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class); + JsonArray jsonArray = jsonObject.get("playlist").getAsJsonArray(); + + //jsonArray.get(0).getAsJsonObject().get("playlist").getAsJsonArray().get(0).getAsJsonObject().get("file").getAsString(); + + for (JsonElement elm1 : jsonArray) { + JsonArray arr1 = elm1.getAsJsonObject().get("playlist").getAsJsonArray(); + for (JsonElement elm2 : arr1) { + JsonObject obj1 = elm2.getAsJsonObject(); + obj1.addProperty("file", obj1.get("file").getAsString().replace("http://", "/proxy/onlinelife/")); + } + } + + return new Gson().toJson(jsonObject); + } } \ No newline at end of file diff --git a/src/main/java/kinosearch/webapp/servlets/ProxyServlet.java b/src/main/java/kinosearch/webapp/servlets/ProxyServlet.java new file mode 100644 index 0000000..8b6b493 --- /dev/null +++ b/src/main/java/kinosearch/webapp/servlets/ProxyServlet.java @@ -0,0 +1,64 @@ +package kinosearch.webapp.servlets; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.Part; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; + +/** + * Created by DmitriyMX + * 2016 + */ +public class ProxyServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String path = request.getPathInfo().substring("/onlinelife/".length()); + + URL url = new URL("http://" + path); + HttpURLConnection con =(HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.setDoOutput(true); + con.setDoInput(true); + con.setUseCaches(true); + + for (Enumeration names = request.getHeaderNames(); names.hasMoreElements();) { + String headerName = names.nextElement().toString(); + if (headerName.equalsIgnoreCase("referer")) continue; + con.setRequestProperty(headerName, request.getHeader(headerName)); + } + + con.connect(); + + int statusCode = con.getResponseCode(); + response.setStatus(statusCode); + + for (Map.Entry> stringListEntry : con.getHeaderFields().entrySet()) { + Map.Entry mapEntry = (Map.Entry) stringListEntry; + if (mapEntry.getKey() != null) { + response.setHeader(mapEntry.getKey().toString(), ((List) mapEntry.getValue()).get(0).toString()); + } + } + + BufferedInputStream webToProxyBuf = new BufferedInputStream(con.getInputStream()); + BufferedOutputStream proxyToClientBuf = new BufferedOutputStream(response.getOutputStream()); + + int oneByte; + while ((oneByte = webToProxyBuf.read()) != -1) { + proxyToClientBuf.write(oneByte); + } + + proxyToClientBuf.flush(); + proxyToClientBuf.close(); + webToProxyBuf.close(); + con.disconnect(); + } +} diff --git a/webapp/WEB-INF/player.html b/webapp/WEB-INF/player.html index 536b3cc..ce854de 100644 --- a/webapp/WEB-INF/player.html +++ b/webapp/WEB-INF/player.html @@ -4,7 +4,7 @@ $(function(){ if (typeof(video_data.file) !== 'undefined') { - $('#player').attr('src', video_data.file); + $('#player').attr('src', '${basedir}'+video_data.file); } else if (typeof(video_data.playlist) !== 'undefined') { $('#pl-season').removeClass('hide'); menu = $('#pl-season-menu'); @@ -28,7 +28,7 @@ smenu.find('a').bind('click', function(){ $('#dropdownSerial').html(this.text + ' '); - $('#player').attr('src', this.attributes['data-file'].value); + $('#player').attr('src', '${basedir}'+this.attributes['data-file'].value); }); $('#pl-serial').removeClass('hide'); diff --git a/webapp/WEB-INF/web.xml b/webapp/WEB-INF/web.xml index 12aede8..d619e21 100644 --- a/webapp/WEB-INF/web.xml +++ b/webapp/WEB-INF/web.xml @@ -22,6 +22,11 @@ kinosearch.webapp.servlets.PlayerServlet + + proxy + kinosearch.webapp.servlets.ProxyServlet + + index / @@ -38,6 +43,11 @@ /onlinelife/* + + proxy + /proxy/* + + default /js/*