Add proxy settings
Add titled pane to settings tab to configure a proxy. Assign the proxy settings on start to the according System properties (see https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies), so that they get picked up by okhttp, HttpUrlConnection etc.
This commit is contained in:
parent
5e2e9667f4
commit
e09d0c35aa
|
@ -7,6 +7,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import ctbrec.Settings.ProxyType;
|
||||||
import ctbrec.ui.CookieJarImpl;
|
import ctbrec.ui.CookieJarImpl;
|
||||||
import ctbrec.ui.HtmlParser;
|
import ctbrec.ui.HtmlParser;
|
||||||
import ctbrec.ui.Launcher;
|
import ctbrec.ui.Launcher;
|
||||||
|
@ -29,6 +30,7 @@ public class HttpClient {
|
||||||
private String token;
|
private String token;
|
||||||
|
|
||||||
private HttpClient() {
|
private HttpClient() {
|
||||||
|
loadProxySettings();
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.cookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.connectTimeout(Config.getInstance().getSettings().httpTimeout, TimeUnit.SECONDS)
|
.connectTimeout(Config.getInstance().getSettings().httpTimeout, TimeUnit.SECONDS)
|
||||||
|
@ -38,6 +40,44 @@ public class HttpClient {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadProxySettings() {
|
||||||
|
ProxyType proxyType = Config.getInstance().getSettings().proxyType;
|
||||||
|
switch (proxyType) {
|
||||||
|
case HTTP:
|
||||||
|
System.setProperty("http.proxyHost", Config.getInstance().getSettings().proxyHost);
|
||||||
|
System.setProperty("http.proxyPort", Config.getInstance().getSettings().proxyPort);
|
||||||
|
System.setProperty("https.proxyHost", Config.getInstance().getSettings().proxyHost);
|
||||||
|
System.setProperty("https.proxyPort", Config.getInstance().getSettings().proxyPort);
|
||||||
|
break;
|
||||||
|
case SOCKS4:
|
||||||
|
System.setProperty("socksProxyVersion", "4");
|
||||||
|
System.setProperty("socksProxyHost", Config.getInstance().getSettings().proxyHost);
|
||||||
|
System.setProperty("socksProxyPort", Config.getInstance().getSettings().proxyPort);
|
||||||
|
break;
|
||||||
|
case SOCKS5:
|
||||||
|
System.setProperty("socksProxyVersion", "5");
|
||||||
|
System.setProperty("socksProxyHost", Config.getInstance().getSettings().proxyHost);
|
||||||
|
System.setProperty("socksProxyPort", Config.getInstance().getSettings().proxyPort);
|
||||||
|
if(Config.getInstance().getSettings().proxyUser != null && !Config.getInstance().getSettings().proxyUser.isEmpty()) {
|
||||||
|
System.setProperty("java.net.socks.username", Config.getInstance().getSettings().proxyUser);
|
||||||
|
System.setProperty("java.net.socks.password", Config.getInstance().getSettings().proxyPassword);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DIRECT:
|
||||||
|
default:
|
||||||
|
System.clearProperty("http.proxyHost");
|
||||||
|
System.clearProperty("http.proxyPort");
|
||||||
|
System.clearProperty("https.proxyHost");
|
||||||
|
System.clearProperty("https.proxyPort");
|
||||||
|
System.clearProperty("socksProxyVersion");
|
||||||
|
System.clearProperty("socksProxyHost");
|
||||||
|
System.clearProperty("socksProxyPort");
|
||||||
|
System.clearProperty("java.net.socks.username");
|
||||||
|
System.clearProperty("java.net.socks.password");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static HttpClient getInstance() {
|
public static HttpClient getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
@ -113,6 +153,10 @@ public class HttpClient {
|
||||||
return loggedIn;
|
return loggedIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void reconfigure() {
|
||||||
|
instance = new HttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
public String getToken() throws IOException {
|
public String getToken() throws IOException {
|
||||||
if(token == null) {
|
if(token == null) {
|
||||||
login();
|
login();
|
||||||
|
|
|
@ -5,6 +5,14 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Settings {
|
public class Settings {
|
||||||
|
|
||||||
|
public enum ProxyType {
|
||||||
|
DIRECT,
|
||||||
|
HTTP,
|
||||||
|
SOCKS4,
|
||||||
|
SOCKS5
|
||||||
|
}
|
||||||
|
|
||||||
public boolean singlePlayer = true;
|
public boolean singlePlayer = true;
|
||||||
public boolean localRecording = true;
|
public boolean localRecording = true;
|
||||||
public int httpPort = 8080;
|
public int httpPort = 8080;
|
||||||
|
@ -24,4 +32,9 @@ public class Settings {
|
||||||
public boolean chooseStreamQuality = false;
|
public boolean chooseStreamQuality = false;
|
||||||
public boolean recordFollowed = false;
|
public boolean recordFollowed = false;
|
||||||
public byte[] key = null;
|
public byte[] key = null;
|
||||||
|
public ProxyType proxyType = ProxyType.DIRECT;
|
||||||
|
public String proxyHost;
|
||||||
|
public String proxyPort;
|
||||||
|
public String proxyUser;
|
||||||
|
public String proxyPassword;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ public class Launcher extends Application {
|
||||||
private Recorder recorder;
|
private Recorder recorder;
|
||||||
private HttpClient client;
|
private HttpClient client;
|
||||||
private static HostServices hostServices;
|
private static HostServices hostServices;
|
||||||
|
private SettingsTab settingsTab;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) throws Exception {
|
public void start(Stage primaryStage) throws Exception {
|
||||||
|
@ -97,7 +98,8 @@ public class Launcher extends Application {
|
||||||
root.getTabs().add(modelsTab);
|
root.getTabs().add(modelsTab);
|
||||||
RecordingsTab recordingsTab = new RecordingsTab("Recordings", recorder, config);
|
RecordingsTab recordingsTab = new RecordingsTab("Recordings", recorder, config);
|
||||||
root.getTabs().add(recordingsTab);
|
root.getTabs().add(recordingsTab);
|
||||||
root.getTabs().add(new SettingsTab());
|
settingsTab = new SettingsTab();
|
||||||
|
root.getTabs().add(settingsTab);
|
||||||
root.getTabs().add(new DonateTabFx());
|
root.getTabs().add(new DonateTabFx());
|
||||||
|
|
||||||
primaryStage.setScene(new Scene(root, 1340, 800));
|
primaryStage.setScene(new Scene(root, 1340, 800));
|
||||||
|
@ -112,6 +114,7 @@ public class Launcher extends Application {
|
||||||
new Thread() {
|
new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
settingsTab.saveConfig();
|
||||||
recorder.shutdown();
|
recorder.shutdown();
|
||||||
client.shutdown();
|
client.shutdown();
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -0,0 +1,113 @@
|
||||||
|
package ctbrec.ui;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.sun.javafx.collections.ObservableListWrapper;
|
||||||
|
|
||||||
|
import ctbrec.Config;
|
||||||
|
import ctbrec.Settings.ProxyType;
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
|
import javafx.event.EventHandler;
|
||||||
|
import javafx.scene.control.ComboBox;
|
||||||
|
import javafx.scene.control.Label;
|
||||||
|
import javafx.scene.control.PasswordField;
|
||||||
|
import javafx.scene.control.TextField;
|
||||||
|
import javafx.scene.control.TitledPane;
|
||||||
|
import javafx.scene.layout.GridPane;
|
||||||
|
|
||||||
|
public class ProxySettingsPane extends TitledPane implements EventHandler<ActionEvent> {
|
||||||
|
|
||||||
|
private ComboBox<ProxyType> proxyType;
|
||||||
|
private TextField proxyHost = new TextField();
|
||||||
|
private TextField proxyPort = new TextField();
|
||||||
|
private TextField proxyUser = new TextField();
|
||||||
|
private PasswordField proxyPassword = new PasswordField();
|
||||||
|
|
||||||
|
public ProxySettingsPane() {
|
||||||
|
createGui();
|
||||||
|
loadConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createGui() {
|
||||||
|
setText("Proxy");
|
||||||
|
setCollapsible(false);
|
||||||
|
GridPane layout = SettingsTab.createGridLayout();
|
||||||
|
setContent(layout);
|
||||||
|
|
||||||
|
Label l = new Label("Type");
|
||||||
|
layout.add(l, 0, 0);
|
||||||
|
List<ProxyType> proxyTypes = new ArrayList<>();
|
||||||
|
proxyTypes.add(ProxyType.DIRECT);
|
||||||
|
proxyTypes.add(ProxyType.HTTP);
|
||||||
|
proxyTypes.add(ProxyType.SOCKS4);
|
||||||
|
proxyTypes.add(ProxyType.SOCKS5);
|
||||||
|
proxyType = new ComboBox<>(new ObservableListWrapper<>(proxyTypes));
|
||||||
|
proxyType.setOnAction(this);
|
||||||
|
layout.add(proxyType, 1, 0);
|
||||||
|
|
||||||
|
l = new Label("Host");
|
||||||
|
layout.add(l, 0, 1);
|
||||||
|
layout.add(proxyHost, 1, 1);
|
||||||
|
|
||||||
|
l = new Label("Port");
|
||||||
|
layout.add(l, 0, 2);
|
||||||
|
layout.add(proxyPort, 1, 2);
|
||||||
|
|
||||||
|
l = new Label("Username");
|
||||||
|
layout.add(l, 0, 3);
|
||||||
|
layout.add(proxyUser, 1, 3);
|
||||||
|
|
||||||
|
l = new Label("Password");
|
||||||
|
layout.add(l, 0, 4);
|
||||||
|
layout.add(proxyPassword, 1, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadConfig() {
|
||||||
|
proxyType.valueProperty().set(Config.getInstance().getSettings().proxyType);
|
||||||
|
proxyHost.setText(Config.getInstance().getSettings().proxyHost);
|
||||||
|
proxyPort.setText(Config.getInstance().getSettings().proxyPort);
|
||||||
|
proxyUser.setText(Config.getInstance().getSettings().proxyUser);
|
||||||
|
proxyPassword.setText(Config.getInstance().getSettings().proxyPassword);
|
||||||
|
setComponentDisableState();
|
||||||
|
}
|
||||||
|
|
||||||
|
void saveConfig() {
|
||||||
|
System.out.println("Saving proxy settings");
|
||||||
|
Config.getInstance().getSettings().proxyType = proxyType.getValue();
|
||||||
|
Config.getInstance().getSettings().proxyHost = proxyHost.getText();
|
||||||
|
Config.getInstance().getSettings().proxyPort = proxyPort.getText();
|
||||||
|
Config.getInstance().getSettings().proxyUser = proxyUser.getText();
|
||||||
|
Config.getInstance().getSettings().proxyPassword = proxyPassword.getText();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(ActionEvent event) {
|
||||||
|
setComponentDisableState();
|
||||||
|
SettingsTab.showRestartRequired();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setComponentDisableState() {
|
||||||
|
switch (proxyType.getValue()) {
|
||||||
|
case DIRECT:
|
||||||
|
proxyHost.setDisable(true);
|
||||||
|
proxyPort.setDisable(true);
|
||||||
|
proxyUser.setDisable(true);
|
||||||
|
proxyPassword.setDisable(true);
|
||||||
|
break;
|
||||||
|
case HTTP:
|
||||||
|
case SOCKS4:
|
||||||
|
proxyHost.setDisable(false);
|
||||||
|
proxyPort.setDisable(false);
|
||||||
|
proxyUser.setDisable(true);
|
||||||
|
proxyPassword.setDisable(true);
|
||||||
|
break;
|
||||||
|
case SOCKS5:
|
||||||
|
proxyHost.setDisable(false);
|
||||||
|
proxyPort.setDisable(false);
|
||||||
|
proxyUser.setDisable(false);
|
||||||
|
proxyPassword.setDisable(false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ import ctbrec.Hmac;
|
||||||
import javafx.beans.value.ChangeListener;
|
import javafx.beans.value.ChangeListener;
|
||||||
import javafx.beans.value.ObservableValue;
|
import javafx.beans.value.ObservableValue;
|
||||||
import javafx.geometry.Insets;
|
import javafx.geometry.Insets;
|
||||||
|
import javafx.geometry.VPos;
|
||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
import javafx.scene.control.Alert;
|
import javafx.scene.control.Alert;
|
||||||
import javafx.scene.control.Alert.AlertType;
|
import javafx.scene.control.Alert.AlertType;
|
||||||
|
@ -37,7 +38,7 @@ import javafx.scene.paint.Color;
|
||||||
import javafx.stage.DirectoryChooser;
|
import javafx.stage.DirectoryChooser;
|
||||||
import javafx.stage.FileChooser;;
|
import javafx.stage.FileChooser;;
|
||||||
|
|
||||||
public class SettingsTab extends Tab {
|
public class SettingsTab extends Tab implements TabSelectionListener {
|
||||||
|
|
||||||
private static final transient Logger LOG = LoggerFactory.getLogger(SettingsTab.class);
|
private static final transient Logger LOG = LoggerFactory.getLogger(SettingsTab.class);
|
||||||
|
|
||||||
|
@ -60,6 +61,7 @@ public class SettingsTab extends Tab {
|
||||||
private RadioButton recordLocal;
|
private RadioButton recordLocal;
|
||||||
private RadioButton recordRemote;
|
private RadioButton recordRemote;
|
||||||
private ToggleGroup recordLocation;
|
private ToggleGroup recordLocation;
|
||||||
|
private ProxySettingsPane proxySettingsPane;
|
||||||
|
|
||||||
private TitledPane ctb;
|
private TitledPane ctb;
|
||||||
private TitledPane mergePane;
|
private TitledPane mergePane;
|
||||||
|
@ -112,6 +114,13 @@ public class SettingsTab extends Tab {
|
||||||
locations.setCollapsible(false);
|
locations.setCollapsible(false);
|
||||||
mainLayout.add(locations, 0, 0);
|
mainLayout.add(locations, 0, 0);
|
||||||
|
|
||||||
|
proxySettingsPane = new ProxySettingsPane();
|
||||||
|
mainLayout.add(proxySettingsPane, 1, 0);
|
||||||
|
GridPane.setRowSpan(proxySettingsPane, 2);
|
||||||
|
GridPane.setFillWidth(proxySettingsPane, true);
|
||||||
|
GridPane.setHgrow(proxySettingsPane, Priority.ALWAYS);
|
||||||
|
GridPane.setValignment(proxySettingsPane, VPos.TOP);
|
||||||
|
|
||||||
layout = createGridLayout();
|
layout = createGridLayout();
|
||||||
layout.add(new Label("Chaturbate User"), 0, 0);
|
layout.add(new Label("Chaturbate User"), 0, 0);
|
||||||
username = new TextField(Config.getInstance().getSettings().username);
|
username = new TextField(Config.getInstance().getSettings().username);
|
||||||
|
@ -299,7 +308,7 @@ public class SettingsTab extends Tab {
|
||||||
setRecordingMode(recordLocal.isSelected());
|
setRecordingMode(recordLocal.isSelected());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showRestartRequired() {
|
static void showRestartRequired() {
|
||||||
Alert restart = new AutosizeAlert(AlertType.INFORMATION);
|
Alert restart = new AutosizeAlert(AlertType.INFORMATION);
|
||||||
restart.setTitle("Restart required");
|
restart.setTitle("Restart required");
|
||||||
restart.setHeaderText("Restart required");
|
restart.setHeaderText("Restart required");
|
||||||
|
@ -307,7 +316,7 @@ public class SettingsTab extends Tab {
|
||||||
restart.show();
|
restart.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private GridPane createGridLayout() {
|
static GridPane createGridLayout() {
|
||||||
GridPane layout = new GridPane();
|
GridPane layout = new GridPane();
|
||||||
layout.setPadding(new Insets(10));
|
layout.setPadding(new Insets(10));
|
||||||
layout.setHgap(5);
|
layout.setHgap(5);
|
||||||
|
@ -502,4 +511,17 @@ public class SettingsTab extends Tab {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void selected() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deselected() {
|
||||||
|
saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveConfig() {
|
||||||
|
proxySettingsPane.saveConfig();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue