From a958ede7b906ee104df6f1bac1b00709abf4170f Mon Sep 17 00:00:00 2001
From: nborisenkov <n.borisenkov@gmail.com>
Date: Tue, 6 Aug 2024 14:26:22 +0300
Subject: [PATCH] Setting up a system-wide proxy (linux)

---
 util/os.go | 95 +++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 62 insertions(+), 33 deletions(-)

diff --git a/util/os.go b/util/os.go
index 9c402dc..8bb70c1 100644
--- a/util/os.go
+++ b/util/os.go
@@ -8,48 +8,77 @@ import (
 )
 
 func SetOsProxy(port int) error {
-	if runtime.GOOS != "darwin" {
+	if runtime.GOOS == "darwin" {
+		network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output()
+
+		if err != nil {
+			return err
+		}
+
+		_, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
+		if err != nil {
+			return err
+		}
+
+		_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
+		if err != nil {
+			return err
+		}
+	} else if runtime.GOOS == "linux" {
+		// dconf write /system/proxy/http/port "8080"
+		// dconf write /system/proxy/http/host "'127.0.0.1'"
+		// dconf write /system/proxy/mode "'manual'"
+		var err error
+
+		_, err = exec.Command("sh", "-c", "dconf write /system/proxy/http/port \"" + fmt.Sprint(port) + "\"").Output()
+		if err != nil {
+			return err
+		}
+
+		_, err = exec.Command("sh", "-c", "dconf write /system/proxy/http/host \"'127.0.0.1'\"").Output()
+		if err != nil {
+			return err
+		}
+
+		_, err = exec.Command("sh", "-c", "dconf write /system/proxy/mode \"'manual'\"").Output()
+		if err != nil {
+			return err
+		}
+	} else {
+		// TO-DO: Output a message to the INFO log that the system-wide proxy could not be set because the OS is not supported
 		return nil
 	}
 
-	network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output()
-
-	if err != nil {
-		return err
-	}
-
-	_, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
-	if err != nil {
-		return err
-	}
-
-	_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
-	if err != nil {
-		return err
-	}
-
 	return nil
 }
 
 func UnsetOsProxy() error {
-	if runtime.GOOS != "darwin" {
+	if runtime.GOOS == "darwin" {
+		network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output()
+		if err != nil {
+			return err
+		}
+
+		_, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
+		if err != nil {
+			return err
+		}
+
+		_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
+		if err != nil {
+			return err
+		}
+	} else if runtime.GOOS == "linux" {
+		var err error
+
+		_, err = exec.Command("sh", "-c", "dconf write /system/proxy/mode \"'none'\"").Output()
+		if err != nil {
+			return err
+		}
+	} else {
+		// TO-DO: Output a message to the INFO log that the system-wide proxy could not be unset because the OS is not supported
 		return nil
 	}
 
-	network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output()
-	if err != nil {
-		return err
-	}
-
-	_, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
-	if err != nil {
-		return err
-	}
-
-	_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
-	if err != nil {
-		return err
-	}
-
 	return nil
 }