From 4409b5bbc7c4e55f0fb238f4daab913c5dd13d2b Mon Sep 17 00:00:00 2001 From: Niki Date: Wed, 10 Apr 2019 13:48:20 +0200 Subject: [PATCH] Proxy: allow single string usage --- src/be/nikiroo/utils/Proxy.java | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/be/nikiroo/utils/Proxy.java b/src/be/nikiroo/utils/Proxy.java index f6ce11b..750b3ee 100755 --- a/src/be/nikiroo/utils/Proxy.java +++ b/src/be/nikiroo/utils/Proxy.java @@ -9,6 +9,64 @@ import java.net.PasswordAuthentication; * @author niki */ public class Proxy { + /** + * Use the proxy described by this string: + * + * Some examples: + * + * + * @param proxy + * the proxy + */ + static public void use(String proxy) { + if (proxy != null && !proxy.isEmpty()) { + String user = null; + String password = null; + int port = 8080; + + if (proxy.contains("@")) { + int pos = proxy.indexOf("@"); + user = proxy.substring(0, pos); + proxy = proxy.substring(pos + 1); + if (user.contains(":")) { + pos = user.indexOf(":"); + password = user.substring(pos + 1); + user = user.substring(0, pos); + } + } + + if (proxy.equals(":")) { + proxy = null; + } else if (proxy.contains(":")) { + int pos = proxy.indexOf(":"); + try { + port = Integer.parseInt(proxy.substring(0, pos)); + proxy = proxy.substring(pos + 1); + } catch (Exception e) { + } + } + + if (proxy == null) { + Proxy.useSystemProxy(user, password); + } else { + Proxy.useProxy(proxy, port, user, password); + } + } + } + /** * Use the system proxy. */ -- 2.27.0