From 093c46a29f79983cb69901057664d665c60d9e19 Mon Sep 17 00:00:00 2001
From: Code-Ac <76821666+Code-Ac@users.noreply.github.com>
Date: Fri, 21 Jan 2022 08:34:47 +0100
Subject: [PATCH 1/2] Revamped proxy configuration & added proxy authentication

---
 app/src/processing/app/Preferences.java | 52 ++++++++++++++++++++-----
 build/shared/lib/defaults.txt           | 18 +++++----
 2 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/app/src/processing/app/Preferences.java b/app/src/processing/app/Preferences.java
index ead30e3d39..f651ec31ee 100644
--- a/app/src/processing/app/Preferences.java
+++ b/app/src/processing/app/Preferences.java
@@ -25,6 +25,8 @@
 import java.awt.Font;
 import java.awt.SystemColor;
 import java.io.*;
+import java.net.Authenticator;
+import java.net.PasswordAuthentication;
 import java.util.*;
 
 import processing.app.ui.Toolkit;
@@ -123,9 +125,7 @@ static public void init() {
     // http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
     // Less readable version with the Oracle style sheet:
     // http://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
-    handleProxy("http", "http.proxyHost", "http.proxyPort");
-    handleProxy("https", "https.proxyHost", "https.proxyPort");
-    handleProxy("socks", "socksProxyHost", "socksProxyPort");
+    handleProxy();
   }
 
 
@@ -137,15 +137,47 @@ static public void skipInit() {
   }
 
 
-  static void handleProxy(String protocol, String hostProp, String portProp) {
-    String proxyHost = get("proxy." + protocol + ".host");
-    String proxyPort = get("proxy." + protocol + ".port");
-    if (proxyHost != null && proxyHost.length() != 0 &&
-        proxyPort != null && proxyPort.length() != 0) {
-      System.setProperty(hostProp, proxyHost);
-      System.setProperty(portProp, proxyPort);
+  static void handleProxy() {
+    String proxyType = get("proxy.type");
+    String proxyHost = get("proxy.host");
+    String proxyPort = get("proxy.port");
+    if (proxyType == null || proxyType.length() == 0 ||
+        proxyHost == null || proxyHost.length() == 0 ||
+        proxyPort == null || proxyPort.length() == 0) {
+      return;
     }
 
+    switch (proxyType.toLowerCase()){
+      case "http":
+        System.setProperty("http.proxyHost", proxyHost);
+        System.setProperty("http.proxyPort", proxyPort);
+        break;
+      case "https":
+        System.setProperty("https.proxyHost", proxyHost);
+        System.setProperty("https.proxyPort", proxyPort);
+        break;
+      case "socks":
+        System.setProperty("socksProxyHost", proxyHost);
+        System.setProperty("socksProxyPort", proxyPort);
+        break;
+      default:
+        Messages.showWarning(
+            "Proxy failed to initialize",
+            "Invalid proxy type. Can be either \"http\", \"https\" or \"socks\".");
+    }
+    String proxyUser = get("proxy.user");
+    String proxyPasswd = get("proxy.passwd");
+    if (proxyUser != null && proxyPasswd != null){
+      Authenticator.setDefault(new Authenticator() {
+        @Override
+        protected PasswordAuthentication getPasswordAuthentication() {
+          if (getRequestingHost().equalsIgnoreCase(proxyHost)) {
+            return new PasswordAuthentication(proxyUser, proxyPasswd.toCharArray());
+          }
+          return null;
+        }
+      });
+    }
   }
 
 
diff --git a/build/shared/lib/defaults.txt b/build/shared/lib/defaults.txt
index 470db235ee..ba57e6fdfa 100644
--- a/build/shared/lib/defaults.txt
+++ b/build/shared/lib/defaults.txt
@@ -313,15 +313,17 @@ run.present.stop.color = #cccccc
 # checker and the contrib manager to run properly in those environments.
 # This changed from proxy.host and proxy.port to proxy.http.host and
 # proxy.http.port in 3.0a8. In addition, https and socks were added.
-proxy.http.host=
-proxy.http.port=
-proxy.https.host=
-proxy.https.port=
-proxy.socks.host=
-proxy.socks.port=
+# This changed back to proxy.host and proxy.port in 4.0b4 because
+# you don't need multiple proxies.
+# The proxy type can be either "http", "https" or "socks".
+proxy.type=
+proxy.host=
+proxy.port=
 # Example of usage (replace 'http' with 'https' or 'socks' as needed)
-#proxy.http.host=proxy.example.com
-#proxy.http.port=8080
+#proxy.type=http
+#proxy.host=proxy.example.com
+#proxy.port=8080
+
 # Whether to use the system proxy by default
 proxy.system=true
 

From ae97a8b862c3c494ec24c34796825c2234dc26e6 Mon Sep 17 00:00:00 2001
From: Code-Ac <76821666+Code-Ac@users.noreply.github.com>
Date: Fri, 21 Jan 2022 10:10:47 +0100
Subject: [PATCH 2/2] Update defaults.txt whit auth settings

---
 build/shared/lib/defaults.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/build/shared/lib/defaults.txt b/build/shared/lib/defaults.txt
index ba57e6fdfa..6c82db3c6b 100644
--- a/build/shared/lib/defaults.txt
+++ b/build/shared/lib/defaults.txt
@@ -319,6 +319,8 @@ run.present.stop.color = #cccccc
 proxy.type=
 proxy.host=
 proxy.port=
+proxy.user=
+proxy.passwd=
 # Example of usage (replace 'http' with 'https' or 'socks' as needed)
 #proxy.type=http
 #proxy.host=proxy.example.com