Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
faiqsohail committed Dec 23, 2018
1 parent 952c4a7 commit 3659da0
Show file tree
Hide file tree
Showing 22 changed files with 1,708 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: co.proxychecker.ProxyChecker.startup.ProxyChecker

Binary file added src/co/proxychecker/ProxyChecker/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 150 additions & 0 deletions src/co/proxychecker/ProxyChecker/commands/ExportCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package co.proxychecker.ProxyChecker.commands;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javafx.scene.control.Alert;
import javafx.scene.control.ListView;
import javafx.scene.control.TableView;

import co.proxychecker.ProxyChecker.gui.AlertBox;
import co.proxychecker.ProxyChecker.components.entities.Proxy;
import co.proxychecker.ProxyChecker.components.entities.ProxyStatus;
import co.proxychecker.ProxyChecker.components.entities.ProxyAnonymity;

/**
* Class contains static methods that facilitate exporting a Proxies to disk.
*/
public class ExportCommand {

/**
* Gets a user selected destination file to save all loaded proxies onto, in the format ip:port
* @param listView - collection component
*/
public static void save(ListView<String> listView) {
if (listView.getItems().size() != 0) {
File file = FileCommand.getFileToSave(false);
if (file != null) {
try {
PrintWriter printWriter = new PrintWriter(new FileWriter(file));
for (String ip_port : listView.getItems()) {
printWriter.write(ip_port + "\n");
}
printWriter.close();
AlertBox.show(Alert.AlertType.INFORMATION, "Proxies Exported",
"All proxies have been successfully exported to disk!");
} catch (IOException e) {
AlertBox.show(Alert.AlertType.ERROR, "Export Failed",
"Unable to export the data. Error: " + e.getMessage());
}
}
} else {
AlertBox.show(Alert.AlertType.INFORMATION, "No Proxies",
"There's currently no proxies loaded to export!");
}
}

/**
* Gets a user selected destination file to export the proxy table in a comma separated value file.
* @param tableView - collection component
*/
public static void save(TableView<Proxy> tableView) {
if (tableView.getItems().size() != 0) {
File file = FileCommand.getFileToSave(true);
if (file != null) {
try {
PrintWriter printWriter = new PrintWriter(new FileWriter(file));
for (Proxy proxy : tableView.getItems()) {
printWriter.write(getCSV(proxy));
}
printWriter.close();
AlertBox.show(Alert.AlertType.INFORMATION, "Table Exported",
"The table has been successfully exported to disk!");
} catch (IOException e) {
AlertBox.show(Alert.AlertType.ERROR, "Export Failed",
"Unable to export the table. Error: " + e.getMessage());
}
}
} else {
AlertBox.show(Alert.AlertType.INFORMATION, "No Data",
"There's currently no data to export!");
}
}

/**
* Gets a user selected destination file to export the proxy table based on the given ProxyStatus and
* ProxyAnonymity values, in the format ip:port
* @param tableView - collection component
* @param proxyStatus - The ProxyStatus
* @param proxyAnonymity - The ProxyAnonymity (null for all proxies belonging to ProxyStatus)
*/
public static void save(TableView<Proxy> tableView, ProxyStatus proxyStatus, ProxyAnonymity proxyAnonymity) {
if (tableView.getItems().size() != 0) {
File file = FileCommand.getFileToSave(false);
if (file != null) {
try {
PrintWriter printWriter = new PrintWriter(new FileWriter(file));
for (Proxy proxy : tableView.getItems()) {

String line = getLine(proxy, proxyStatus, proxyAnonymity);
if (line != null) { // a valid line was given
printWriter.write(line);
}
}
printWriter.close();
AlertBox.show(Alert.AlertType.INFORMATION, "Data Exported",
"The data has been successfully exported to disk!");
} catch (IOException e) {
AlertBox.show(Alert.AlertType.ERROR, "Export Failed",
"Unable to export the data. Error: " + e.getMessage());
}
}
} else {
AlertBox.show(Alert.AlertType.INFORMATION, "No Data",
"There's currently no data to export!");
}
}

/**
* Generates comma separated values (csv) for a given Proxy
* @param proxy - The Proxy object
* @return String - csv
*/
private static String getCSV(Proxy proxy) {
StringBuilder sb = new StringBuilder();
sb.append(proxy.getIp() + ",");
sb.append(proxy.getPort() + ",");
sb.append(proxy.getProxyStatus() + ",");
sb.append(proxy.getProxyAnonymity() + ",");
sb.append(proxy.getCountry() + ",");
sb.append(proxy.getResponseTime() + "\n");

return sb.toString();
}

/**
* Generates a line in the form ip:port for a given proxy confining to ProxyStatus and ProxyAnonymity
* @param proxy - The Proxy object
* @param proxyStatus - The ProxyStatus
* @param proxyAnonymity - The ProxyAnonymity (null for all proxies belonging to ProxyStatus)
* @return String - in the form ip:port,
* null if the proxy does not confine to the given proxyStatus and/or proxyAnonymity
*/
private static String getLine(Proxy proxy, ProxyStatus proxyStatus, ProxyAnonymity proxyAnonymity) {
if (proxy.getProxyStatus() == proxyStatus) {
if (proxyAnonymity != null) {
if (proxyAnonymity == proxy.getProxyAnonymity()) {
return proxy.getIp() + ":" + proxy.getPort() + "\n";
} else {
return null;
}
} else {
return proxy.getIp() + ":" + proxy.getPort() + "\n";
}
}
return null;
}
}
47 changes: 47 additions & 0 deletions src/co/proxychecker/ProxyChecker/commands/FileCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package co.proxychecker.ProxyChecker.commands;

import java.io.File;
import java.util.List;

import javafx.stage.FileChooser;

/**
* Contains static methods that allow selecting a file to save or files to open in a dialog.
*/
public class FileCommand {

private static FileChooser fileChooser;

/**
* Show a dialog and get a File to save
* @param table - Whether or not the file dialog is to save a table (csv).
* @return File - The selected File object.
*/
public static File getFileToSave(boolean table) {
fileChooser = new FileChooser();
fileChooser.setTitle("Save File");
if(table) {
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("CSV File", "*.csv")
);
} else {
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("TXT Files", "*.txt")
);
}
return fileChooser.showSaveDialog(null);
}

/**
* Shows a dialog and gets one or more files to open
* @return List - A list of files selected
*/
public static List<File> getFilesToOpen() {
fileChooser = new FileChooser();
fileChooser.setTitle("Open File(s)");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("TXT Files", "*.txt")
);
return fileChooser.showOpenMultipleDialog(null);
}
}
69 changes: 69 additions & 0 deletions src/co/proxychecker/ProxyChecker/commands/LoadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package co.proxychecker.ProxyChecker.commands;

import java.io.*;
import java.util.List;

import javafx.scene.control.Alert;
import javafx.scene.control.ListView;

import co.proxychecker.ProxyChecker.components.entities.Proxy;
import co.proxychecker.ProxyChecker.gui.AlertBox;

/**
* Loads a file or string onto a ListView in the form ip:port
*/
public class LoadCommand {

/**
* Manages the addition of one or more files onto the ListView
* @param view - Listview to add file contents onto
*/
public static void file(ListView<String> view) {
List<File> list = FileCommand.getFilesToOpen();
if (list != null) {
for (File file : list) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
addItem(line, view); // only add if its a valid line
}
} catch (FileNotFoundException e) {
AlertBox.show(Alert.AlertType.INFORMATION, "File Not Found",
"The file you selected: " + file.getName() + ", doesn't seem to exist!");
} catch (IOException e) {
AlertBox.show(Alert.AlertType.ERROR, "File Exception",
"Unable to read the file " + file.getName() + ". Error: " + e.getMessage());
}
}
}

}

/**
* Manages the addition of a single string onto the ListView
* @param string - The String to add to the ListView
* @param view - The ListView to add the string onto.
*/
public static void string(String string, ListView<String> view) {
if(view.getItems().contains(string)) {
AlertBox.show(Alert.AlertType.INFORMATION,"Already Loaded",
"The proxy you entered is already loaded and is ready to be checked!");
} else if(!addItem(string, view)) { // make sure it was added
AlertBox.show(Alert.AlertType.ERROR,"Invalid Proxy Format",
"You must enter in a valid proxy in the format ip:port !");
}
}

/**
* Adds a string onto the listview provided its in a valid format
* @param string - The String to add to the ListView
* @param view - The ListView to add the string onto.
* @return Boolean - Whether addition of item onto the listview was successful
*/
private static boolean addItem(String string, ListView<String> view) {
if( (Proxy.isValidFormat(string)) && (!view.getItems().contains(string))) {
return view.getItems().add(string);
}
return false;
}
}
108 changes: 108 additions & 0 deletions src/co/proxychecker/ProxyChecker/commands/ProxyCheckCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package co.proxychecker.ProxyChecker.commands;

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

import javafx.util.Pair;
import javafx.scene.control.*;
import javafx.application.Platform;

import co.proxychecker.ProxyChecker.components.Settings;
import co.proxychecker.ProxyChecker.components.RequestAPI;
import co.proxychecker.ProxyChecker.components.UserSettings;
import co.proxychecker.ProxyChecker.components.entities.Proxy;
import co.proxychecker.ProxyChecker.components.entities.ProxyStatus;

/**
* Takes each proxy value in the ListView, performs checks and updates TableView accordingly.
*/
public class ProxyCheckCommand {

private static int thCount_start = Thread.activeCount();
private static UserSettings settings = Settings.getConfig();

/**
* Setups the thread pool and launches asynchronous checks on the list of proxies
* @param listView - The ListView containing the proxies that will be checked
* @param tableView - The TableView that will be updated with the status of the proxies
*/
public static void check(ListView<String> listView, TableView<Proxy> tableView) {
ExecutorService executorService = Executors.newFixedThreadPool(settings.getThreads());
for (String proxy : listView.getItems()) {
executorService.submit(
new Checker(
new Proxy( // first creation of the Proxy object from the ListView
proxy,
settings.getProxyType()
),
tableView
)
);
}

executorService.shutdown();
}

/**
* @return Boolean - Whether or not if proxies are still being checked
*/
public static boolean isRunning() {
return (Thread.activeCount() - thCount_start) != 0;
}

/**
* Task that asynchronous checks each proxy and updates the TableView
*/
private static class Checker implements Runnable {

private Proxy proxy;
private TableView<Proxy> tableView;

/**
*
* @param proxy - The Proxy object to check.
* @param tableView
*/
public Checker(Proxy proxy, TableView<Proxy> tableView) {
this.proxy = proxy;
this.tableView = tableView;
}

@Override
public void run() {
RequestAPI requestAPI = new RequestAPI(settings);
java.net.Proxy proxy = new java.net.Proxy(
this.proxy.getProxyType(),
new InetSocketAddress(
this.proxy.getIp(),
this.proxy.getPort()
)
);

Pair<HttpURLConnection, Long> pair = requestAPI.connect(proxy);
if(pair != null) {
try {
this.proxy.setResponseTime(pair.getValue() + " (ms)");
this.proxy.setProxyStatus(ProxyStatus.ALIVE);
RequestAPI.Response response = requestAPI.getResponse(pair.getKey());
this.proxy.setProxyAnonymity(response.anonymity);
this.proxy.setCountry(response.country);

} catch (Exception e) {
this.proxy.setProxyStatus(ProxyStatus.DEAD);
this.proxy.setProxyAnonymity(null);
}
} else {
this.proxy.setProxyStatus(ProxyStatus.DEAD);
this.proxy.setProxyAnonymity(null);
}

// this has to be done on another thread
Platform.runLater(()-> {
tableView.getItems().add(this.proxy);
});
}
}
}
Loading

0 comments on commit 3659da0

Please sign in to comment.