Skip to content

Commit

Permalink
Add referer header spoofing for domain restricted sites
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Sep 20, 2019
1 parent fa280ee commit fb46b77
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
20 changes: 14 additions & 6 deletions src/main/java/me/puyodead1/wistiaembeddownloader/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/*
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand All @@ -67,7 +69,6 @@ public class WistiaEmbedDownloader extends Shell {
private HashMap<String, String> assets = new HashMap<String, String>();

public JsonParser parser = new JsonParser();
private Label lblNoteThatVideo;

public static Label getProgressLbl() {
return lblProgress;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit fb46b77

Please sign in to comment.