From: Niki Date: Wed, 10 Apr 2019 11:48:20 +0000 (+0200) Subject: Proxy: allow single string usage X-Git-Tag: nikiroo-utils-4.6.0~3 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=4409b5bbc7c4e55f0fb238f4daab913c5dd13d2b;p=nikiroo-utils.git Proxy: allow single string usage --- 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. */