From a0daf5e26202260c08eff03d0108bcd01341e01e Mon Sep 17 00:00:00 2001 From: Laurent Clouet Date: Mon, 6 Jul 2015 18:41:28 +0100 Subject: [PATCH] Add proxy info to conf file and swing ui --- src/com/sheepit/client/Configuration.java | 10 +++ src/com/sheepit/client/SettingsLoader.java | 17 ++++- src/com/sheepit/client/network/Proxy.java | 71 +++++++++++++++++++ src/com/sheepit/client/standalone/Worker.java | 24 +------ .../standalone/swing/activity/Settings.java | 41 +++++++++-- 5 files changed, 135 insertions(+), 28 deletions(-) create mode 100644 src/com/sheepit/client/network/Proxy.java diff --git a/src/com/sheepit/client/Configuration.java b/src/com/sheepit/client/Configuration.java index 56be5d6..ffd8be1 100644 --- a/src/com/sheepit/client/Configuration.java +++ b/src/com/sheepit/client/Configuration.java @@ -44,6 +44,7 @@ public class Configuration { public String static_exeDirName; private String login; private String password; + private String proxy; private int maxUploadingJob; private int nbCores; private ComputeType computeMethod; @@ -57,6 +58,7 @@ public class Configuration { public Configuration(File cache_dir_, String login_, String password_) { this.login = login_; this.password = password_; + this.proxy = null; this.static_exeDirName = "exe"; this.maxUploadingJob = 1; this.nbCores = -1; // ie not set @@ -93,6 +95,14 @@ public class Configuration { this.password = password_; } + public String getProxy() { + return this.proxy; + } + + public void setProxy(String url) { + this.proxy = url; + } + public int maxUploadingJob() { return this.maxUploadingJob; } diff --git a/src/com/sheepit/client/SettingsLoader.java b/src/com/sheepit/client/SettingsLoader.java index dc268b2..9d82f59 100644 --- a/src/com/sheepit/client/SettingsLoader.java +++ b/src/com/sheepit/client/SettingsLoader.java @@ -23,6 +23,7 @@ public class SettingsLoader { private String login; private String password; + private String proxy; private String computeMethod; private String gpu; private String cores; @@ -38,10 +39,11 @@ public class SettingsLoader { path = path_; } - public SettingsLoader(String login_, String password_, ComputeType computeMethod_, GPUDevice gpu_, int cores_, String cacheDir_, boolean autoSignIn_, String ui_) { + public SettingsLoader(String login_, String password_, String proxy_, ComputeType computeMethod_, GPUDevice gpu_, int cores_, String cacheDir_, boolean autoSignIn_, String ui_) { path = getDefaultFilePath(); login = login_; password = password_; + proxy = proxy_; cacheDir = cacheDir_; autoSignIn = String.valueOf(autoSignIn_); ui = ui_; @@ -100,6 +102,10 @@ public class SettingsLoader { prop.setProperty("password", password); } + if (proxy != null) { + prop.setProperty("proxy", proxy); + } + if (autoSignIn != null) { prop.setProperty("auto-signin", autoSignIn); } @@ -143,6 +149,7 @@ public class SettingsLoader { public void loadFile() { this.login = null; this.password = null; + this.proxy = null; this.computeMethod = null; this.gpu = null; this.cacheDir = null; @@ -183,6 +190,10 @@ public class SettingsLoader { this.password = prop.getProperty("password"); } + if (prop.containsKey("proxy")) { + this.proxy = prop.getProperty("proxy"); + } + if (prop.containsKey("auto-signin")) { this.autoSignIn = prop.getProperty("auto-signin"); } @@ -224,6 +235,10 @@ public class SettingsLoader { config.setPassword(password); } + if ((config.getProxy() == null || config.getProxy().isEmpty()) && proxy != null) { + config.setProxy(proxy); + } + try { if ((config.getComputeMethod() == null && computeMethod != null) || (computeMethod != null && config.getComputeMethod() != ComputeType.valueOf(computeMethod))) { config.setComputeMethod(ComputeType.valueOf(computeMethod)); diff --git a/src/com/sheepit/client/network/Proxy.java b/src/com/sheepit/client/network/Proxy.java new file mode 100644 index 0000000..770a68c --- /dev/null +++ b/src/com/sheepit/client/network/Proxy.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Laurent CLOUET + * Author Laurent CLOUET + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package com.sheepit.client.network; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.Authenticator; +import com.sheepit.client.network.Proxy; +import com.sheepit.client.network.ProxyAuthenticator; + +public class Proxy { + + public static void set(String url_) throws MalformedURLException { + URL url = new URL(url_); + String userinfo = url.getUserInfo(); + if (userinfo != null) { + String[] elements = userinfo.split(":"); + if (elements.length == 2) { + String proxy_user = elements[0]; + String proxy_password = elements[1]; + + if (proxy_user != null && proxy_password != null) { + Authenticator.setDefault(new ProxyAuthenticator(proxy_user, proxy_password)); + } + } + } + + int port = url.getPort(); + if (port == -1) { + port = 8080; + } + + System.setProperty("http.proxyHost", url.getHost()); + System.setProperty("http.proxyPort", Integer.toString(port)); + + System.setProperty("https.proxyHost", url.getHost()); + System.setProperty("https.proxyPort", Integer.toString(port)); + } + + public static boolean isValidURL(String url_) { + if (url_ == null || url_.isEmpty()) { + return true; + } + try { + new URL(url_); + + return true; + } + catch (MalformedURLException e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/src/com/sheepit/client/standalone/Worker.java b/src/com/sheepit/client/standalone/Worker.java index d2ce58b..3d690eb 100644 --- a/src/com/sheepit/client/standalone/Worker.java +++ b/src/com/sheepit/client/standalone/Worker.java @@ -25,9 +25,7 @@ import static org.kohsuke.args4j.ExampleMode.REQUIRED; import org.kohsuke.args4j.Option; import java.io.File; -import java.net.Authenticator; import java.net.MalformedURLException; -import java.net.URL; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -42,7 +40,7 @@ import com.sheepit.client.SettingsLoader; import com.sheepit.client.ShutdownHook; import com.sheepit.client.hardware.gpu.GPU; import com.sheepit.client.hardware.gpu.GPUDevice; -import com.sheepit.client.network.ProxyAuthenticator; +import com.sheepit.client.network.Proxy; public class Worker { @Option(name = "-server", usage = "Render-farm server, default https://www.sheepit-renderfarm.com", metaVar = "URL", required = false) @@ -209,25 +207,7 @@ public class Worker { if (proxy != null) { try { - URL url = new URL(proxy); - String userinfo = url.getUserInfo(); - if (userinfo != null) { - String[] elements = userinfo.split(":"); - if (elements.length == 2) { - String proxy_user = elements[0]; - String proxy_password = elements[1]; - - if (proxy_user != null && proxy_password != null) { - Authenticator.setDefault(new ProxyAuthenticator(proxy_user, proxy_password)); - } - } - } - - System.setProperty("http.proxyHost", url.getHost()); - System.setProperty("http.proxyPort", Integer.toString(url.getPort())); - - System.setProperty("https.proxyHost", url.getHost()); - System.setProperty("https.proxyPort", Integer.toString(url.getPort())); + Proxy.set(proxy); } catch (MalformedURLException e) { System.err.println("Error: wrong url for proxy"); diff --git a/src/com/sheepit/client/standalone/swing/activity/Settings.java b/src/com/sheepit/client/standalone/swing/activity/Settings.java index fffb46c..62955fd 100644 --- a/src/com/sheepit/client/standalone/swing/activity/Settings.java +++ b/src/com/sheepit/client/standalone/swing/activity/Settings.java @@ -5,6 +5,7 @@ import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; +import java.net.MalformedURLException; import java.util.LinkedList; import java.util.List; @@ -23,6 +24,7 @@ import com.sheepit.client.SettingsLoader; import com.sheepit.client.hardware.cpu.CPU; import com.sheepit.client.hardware.gpu.GPU; import com.sheepit.client.hardware.gpu.GPUDevice; +import com.sheepit.client.network.Proxy; import com.sheepit.client.standalone.GuiSwing; public class Settings implements Activity { @@ -38,6 +40,7 @@ public class Settings implements Activity { private JCheckBox useCPU; private List useGPUs; private JSlider cpuCores; + private JTextField proxy; private JCheckBox saveFile; private JCheckBox autoSignIn; @@ -61,8 +64,8 @@ public class Settings implements Activity { int start_label_left = 109; int start_label_right = 265; int end_label_right = 490; - int n = 10; - int sep = 45; + int n = 5; + int sep = 40; ImageIcon image = new ImageIcon(getClass().getResource("/title.png")); JLabel labelImage = new JLabel(image); @@ -70,7 +73,7 @@ public class Settings implements Activity { n = labelImage.getHeight(); parent.getContentPane().add(labelImage); - n += 40; + n += sep; JLabel loginLabel = new JLabel("Login:"); loginLabel.setBounds(start_label_left, n, 170, size_height_label); @@ -98,6 +101,21 @@ public class Settings implements Activity { n += sep; + JLabel proxyLabel = new JLabel("Proxy:"); + proxyLabel.setBounds(start_label_left, n, 170, size_height_label); + proxyLabel.setToolTipText("http://login:password@host:port"); + parent.getContentPane().add(proxyLabel); + + proxy = new JTextField(); + proxy.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label); + proxy.setToolTipText("http://login:password@host:port"); + System.out.println("parent.getConfiguration().getProxy() " + parent.getConfiguration().getProxy()); + proxy.setText(parent.getConfiguration().getProxy()); + proxy.addKeyListener(new CheckCanStart()); + parent.getContentPane().add(proxy); + + n += sep; + JLabel cacheLabel = new JLabel("Working directory:"); cacheLabel.setBounds(start_label_left, n, 240, size_height_label); parent.getContentPane().add(cacheLabel); @@ -215,7 +233,7 @@ public class Settings implements Activity { selected = true; } } - if (login.getText().isEmpty() || password.getPassword().length == 0) { + if (login.getText().isEmpty() || password.getPassword().length == 0 || Proxy.isValidURL(proxy.getText()) == false) { selected = false; } saveButton.setEnabled(selected); @@ -322,6 +340,19 @@ public class Settings implements Activity { config.setUseNbCores(cpu_cores); } + String proxyText = null; + if (proxy != null) { + try { + Proxy.set(proxy.getText()); + proxyText = proxy.getText(); + } + catch (MalformedURLException e1) { + System.err.println("Error: wrong url for proxy"); + System.err.println(e); + System.exit(2); + } + } + parent.setCredentials(login.getText(), new String(password.getPassword())); String cachePath = null; @@ -330,7 +361,7 @@ public class Settings implements Activity { } if (saveFile.isSelected()) { - new SettingsLoader(login.getText(), new String(password.getPassword()), method, selected_gpu, cpu_cores, cachePath, autoSignIn.isSelected(), GuiSwing.type).saveFile(); + new SettingsLoader(login.getText(), new String(password.getPassword()), proxyText, method, selected_gpu, cpu_cores, cachePath, autoSignIn.isSelected(), GuiSwing.type).saveFile(); } else { try {