From fb46b77c6bce98ff1af4e0502924143c61299bc0 Mon Sep 17 00:00:00 2001 From: Puyodead1 <23562356riley@gmail.com> Date: Fri, 20 Sep 2019 14:44:09 -0400 Subject: [PATCH] Add referer header spoofing for domain restricted sites --- .../wistiaembeddownloader/Downloader.java | 20 +++++-- .../FileExistsDialog.java | 14 +++-- .../WistiaEmbedDownloader.java | 60 +++++++++++++++---- 3 files changed, 71 insertions(+), 23 deletions(-) diff --git a/src/main/java/me/puyodead1/wistiaembeddownloader/Downloader.java b/src/main/java/me/puyodead1/wistiaembeddownloader/Downloader.java index bf7bcef..513b9dd 100644 --- a/src/main/java/me/puyodead1/wistiaembeddownloader/Downloader.java +++ b/src/main/java/me/puyodead1/wistiaembeddownloader/Downloader.java @@ -26,13 +26,14 @@ public class Downloader implements Runnable { private static int size; private static int downloaded; private static int status = INVALID; - private String output; + private String output, referer; private static Thread thread; - public Downloader(URL url, String output) { + public Downloader(URL url, String output, String referer) { this.url = url; this.output = output; + this.referer = referer; size = -1; downloaded = 0; status = DOWNLOADING; @@ -82,21 +83,26 @@ public void run() { // Specify what portion of file to download. connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); + if(referer != null) { + connection.setRequestProperty("Referer", referer); + } // Connect to server. connection.connect(); // Make sure response code is in the 200 range. if (connection.getResponseCode() / 100 != 2) { - error(); + System.out.println("invalid response code"); WistiaEmbedDownloader.error("Invalid response code"); + error(); } // Check for valid content length. int contentLength = connection.getContentLength(); if (contentLength < 1) { - error(); + System.out.println("invalid content length"); WistiaEmbedDownloader.error("Invalid content length"); + error(); } /* @@ -143,10 +149,11 @@ public void run() { stateChanged(); } } catch (Exception e) { - error(); + System.out.println("[ERROR]: 153"); + e.printStackTrace(); StringWriter writer = new StringWriter(); - e.printStackTrace(new PrintWriter(writer)); WistiaEmbedDownloader.error(writer.toString()); + error(); } finally { // Close file. if (file != null) { @@ -174,6 +181,7 @@ public void run() { private void stateChanged() { if (status == ERROR) { + System.out.println("error downloading"); WistiaEmbedDownloader.error("Error downloading."); } else if (status == DOWNLOADING) { // update progess bar diff --git a/src/main/java/me/puyodead1/wistiaembeddownloader/FileExistsDialog.java b/src/main/java/me/puyodead1/wistiaembeddownloader/FileExistsDialog.java index 7dd909f..4cb3e18 100644 --- a/src/main/java/me/puyodead1/wistiaembeddownloader/FileExistsDialog.java +++ b/src/main/java/me/puyodead1/wistiaembeddownloader/FileExistsDialog.java @@ -15,11 +15,10 @@ public class FileExistsDialog extends Dialog { protected Object result; protected Shell shlFileExists; - private Text txtFileName; - private Text txtPath; + private Text txtFileName, txtPath; private static Button btnOverwrite; - private String fileName, outputPath; + private String fileName, outputPath, referer; public static Button getOverwriteBtn() { return btnOverwrite; @@ -32,11 +31,12 @@ public static Button getOverwriteBtn() { * @param string2 * @param string */ - public FileExistsDialog(Shell parent, int style, String fileName, String outputPath) { + public FileExistsDialog(Shell parent, int style, String fileName, String outputPath, String referer) { super(parent, style); setText("SWT Dialog"); this.fileName = fileName; this.outputPath = outputPath; + this.referer = referer; } /** @@ -93,7 +93,11 @@ private void createContents() { @Override public void mouseDown(MouseEvent e) { shlFileExists.dispose(); - new Downloader(WistiaEmbedDownloader.getAssetDirectURL(), WistiaEmbedDownloader.getFileSaveName()); + if(referer != null) { + new Downloader(WistiaEmbedDownloader.getAssetDirectURL(), WistiaEmbedDownloader.getFileSaveName(), referer); + } else { + new Downloader(WistiaEmbedDownloader.getAssetDirectURL(), WistiaEmbedDownloader.getFileSaveName(), null); + } WistiaEmbedDownloader.getBtnDownload().setText("Abort"); } }); diff --git a/src/main/java/me/puyodead1/wistiaembeddownloader/WistiaEmbedDownloader.java b/src/main/java/me/puyodead1/wistiaembeddownloader/WistiaEmbedDownloader.java index d9dc8b5..08c290c 100644 --- a/src/main/java/me/puyodead1/wistiaembeddownloader/WistiaEmbedDownloader.java +++ b/src/main/java/me/puyodead1/wistiaembeddownloader/WistiaEmbedDownloader.java @@ -32,6 +32,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; public class WistiaEmbedDownloader extends Shell { @@ -43,10 +45,10 @@ public class WistiaEmbedDownloader extends Shell { * URL input box Invalid URL - URL does not start with valid http:// or https:// */ - private Text txtURL, txtOutputLocation; + private Text txtURL, txtOutputLocation, txtReferer; private static StyledText txtConsole; - private Label lblEnterUrl, lblOutputLocation; - private Button btnBrowse; + private Label lblEnterUrl, lblOutputLocation, lblNoteThatVideo, lblRefererDomain; + private Button btnBrowse, btnSpoofReferer; private static Button btnGetAvailableQualities; private static Button btnDownload; private static Combo comboQualities; @@ -67,7 +69,6 @@ public class WistiaEmbedDownloader extends Shell { private HashMap assets = new HashMap(); public JsonParser parser = new JsonParser(); - private Label lblNoteThatVideo; public static Label getProgressLbl() { return lblProgress; @@ -78,7 +79,7 @@ public static ProgressBar getProgressBar() { } public static void log(String text) { - final int start = txtConsole.getText().length(); + int start = txtConsole.getText().length(); txtConsole.append(text + "\n"); StyleRange range = new StyleRange(); range.start = start; @@ -87,7 +88,7 @@ public static void log(String text) { } public static void error(String text) { - final int start = txtConsole.getText().length(); + int start = txtConsole.getText().length(); txtConsole.append(text + "\n"); StyleRange range = new StyleRange(); range.start = start; @@ -203,6 +204,9 @@ public void mouseDown(MouseEvent e) { } URLConnection connection = assetListURL.openConnection(); + if(txtReferer.isEnabled()) { + connection.addRequestProperty("Referer", txtReferer.getText()); + } connection.connect(); JsonElement root = parser @@ -357,12 +361,12 @@ public void mouseDown(MouseEvent e) { System.out.println(getAssetDirectURL()); String[] a = txtURL.getText().trim().contains("?wvideo=") - ? txtURL.getText().trim().split("\\?wvideo=")[0].split("/lessons/") + ? txtURL.getText().trim().split("\\?wvideo=")[0].split("(/lesson/)|(/lessons/)") : txtURL.getText().trim().split(";wvideoid=")[0].split("/lessons/"); String b = a[a.length - 1]; String videoTitle = txtURL.getText().trim().split("\\|")[0].length() == 10 - ? txtURL.getText().trim().split("\\|")[1] - : txtURL.getText().trim().contains("?wvideo=") ? a[a.length - 1].split("/")[0] + ? txtURL.getText().trim().split("|")[1] + : txtURL.getText().trim().contains("\\?wvideo=") ? a[a.length - 1].split("/")[0] : a[a.length - 1].split("/")[0]; System.out.println(videoTitle); @@ -373,12 +377,22 @@ public void mouseDown(MouseEvent e) { if (getAssetDirectURL() != null || getFileSaveName() != null) { File file = new File(getFileSaveName()); if (!file.exists()) { - new Downloader(getAssetDirectURL(), getFileSaveName()); + if(txtReferer.isEnabled()) { + new Downloader(getAssetDirectURL(), getFileSaveName(), txtReferer.getText()); + } else { + new Downloader(getAssetDirectURL(), getFileSaveName(), null); + } getBtnDownload().setText("Abort"); } else { // TODO: Show overwrite dialog - final FileExistsDialog dialog = new FileExistsDialog(getShell(), getStyle(), - videoTitle + ".mp4", txtOutputLocation.getText().trim()); + final FileExistsDialog dialog; + if(txtReferer.isEnabled()) { + dialog = new FileExistsDialog(getShell(), getStyle(), + videoTitle + ".mp4", txtOutputLocation.getText().trim(), txtReferer.getText()); + } else { + dialog = new FileExistsDialog(getShell(), getStyle(), + videoTitle + ".mp4", txtOutputLocation.getText().trim(), null); + } dialog.open(); } } else { @@ -409,6 +423,28 @@ public void mouseDown(MouseEvent e) { lblNoteThatVideo = new Label(this, SWT.WRAP); lblNoteThatVideo.setBounds(10, 74, 125, 37); lblNoteThatVideo.setText("Note: Video IDs are 10 characters long."); + + btnSpoofReferer = new Button(this, SWT.CHECK); + btnSpoofReferer.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + lblRefererDomain.setEnabled(true); + txtReferer.setEnabled(true); + txtReferer.forceFocus(); + } + }); + btnSpoofReferer.setBounds(10, 110, 115, 16); + btnSpoofReferer.setText("Spoof Referer?"); + + txtReferer = new Text(this, SWT.BORDER); + txtReferer.setEnabled(false); + txtReferer.setBounds(10, 153, 94, 21); + + lblRefererDomain = new Label(this, SWT.NONE); + lblRefererDomain.setEnabled(false); + lblRefererDomain.setAlignment(SWT.CENTER); + lblRefererDomain.setBounds(10, 132, 94, 15); + lblRefererDomain.setText("Referer"); createContents(); }