Skip to content

Commit 429ce78

Browse files
committed
add linux script
1 parent f539135 commit 429ce78

1 file changed

Lines changed: 216 additions & 0 deletions

File tree

scripts/proxy_set_linux_sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash
2+
3+
trim() {
4+
local -n ref=$1
5+
ref="${ref#"${ref%%[![:space:]]*}"}"
6+
ref="${ref%"${ref##*[![:space:]]}"}"
7+
}
8+
9+
build_gsettings_array() {
10+
[[ -z "$1" ]] && echo "[]" && return
11+
local host joined hosts=()
12+
IFS=',' read -ra parts <<< "$1"
13+
for host in "${parts[@]}"; do
14+
trim host
15+
[[ -n "$host" ]] && hosts+=("$host")
16+
done
17+
[[ ${#hosts[@]} -eq 0 ]] && echo "[]" && return
18+
printf -v joined "'%s'," "${hosts[@]}"
19+
echo "[${joined%,}]"
20+
}
21+
22+
# Function to set proxy for GNOME
23+
set_gnome_proxy() {
24+
local MODE=$1
25+
local PROXY_IP=$2
26+
local PROXY_PORT=$3
27+
local IGNORE_HOSTS=$4
28+
29+
# Set the proxy mode
30+
gsettings set org.gnome.system.proxy mode "$MODE"
31+
32+
if [ "$MODE" == "manual" ]; then
33+
# List of protocols
34+
local PROTOCOLS=("http" "https" "ftp" "socks")
35+
36+
# Loop through protocols to set the proxy
37+
for PROTOCOL in "${PROTOCOLS[@]}"; do
38+
gsettings set org.gnome.system.proxy.$PROTOCOL host "$PROXY_IP"
39+
gsettings set org.gnome.system.proxy.$PROTOCOL port "$PROXY_PORT"
40+
done
41+
42+
# Set ignored hosts
43+
gsettings set org.gnome.system.proxy ignore-hosts "$(build_gsettings_array "$IGNORE_HOSTS")"
44+
45+
echo "GNOME: Manual proxy settings applied."
46+
echo "Proxy IP: $PROXY_IP"
47+
echo "Proxy Port: $PROXY_PORT"
48+
echo "Ignored Hosts: $IGNORE_HOSTS"
49+
elif [ "$MODE" == "none" ]; then
50+
echo "GNOME: Proxy disabled."
51+
fi
52+
}
53+
54+
# Function to set proxy for KDE
55+
set_kde_proxy() {
56+
local MODE=$1
57+
local PROXY_IP=$2
58+
local PROXY_PORT=$3
59+
local IGNORE_HOSTS=$4
60+
61+
# Determine the correct kwriteconfig command based on KDE_SESSION_VERSION
62+
if [ "$KDE_SESSION_VERSION" == "6" ]; then
63+
KWRITECONFIG="kwriteconfig6"
64+
else
65+
KWRITECONFIG="kwriteconfig5"
66+
fi
67+
68+
# KDE uses kwriteconfig to modify proxy settings
69+
if [ "$MODE" == "manual" ]; then
70+
# Set proxy for all protocols
71+
$KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key ProxyType 1
72+
$KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key httpProxy "http://$PROXY_IP:$PROXY_PORT"
73+
$KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key httpsProxy "http://$PROXY_IP:$PROXY_PORT"
74+
$KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key ftpProxy "http://$PROXY_IP:$PROXY_PORT"
75+
$KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key socksProxy "http://$PROXY_IP:$PROXY_PORT"
76+
77+
# Set ignored hosts
78+
$KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key NoProxyFor "$IGNORE_HOSTS"
79+
80+
echo "KDE: Manual proxy settings applied."
81+
echo "Proxy IP: $PROXY_IP"
82+
echo "Proxy Port: $PROXY_PORT"
83+
echo "Ignored Hosts: $IGNORE_HOSTS"
84+
elif [ "$MODE" == "none" ]; then
85+
# Disable proxy
86+
$KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key ProxyType 0
87+
echo "KDE: Proxy disabled."
88+
fi
89+
90+
# Apply changes by restarting KDE's network settings
91+
dbus-send --type=signal /KIO/Scheduler org.kde.KIO.Scheduler.reparseSlaveConfiguration string:""
92+
}
93+
94+
# Detect the current desktop environment
95+
detect_desktop_environment() {
96+
if [[ "$XDG_CURRENT_DESKTOP" == *"GNOME"* ]] || [[ "$XDG_SESSION_DESKTOP" == *"GNOME"* ]]; then
97+
echo "gnome"
98+
return
99+
fi
100+
101+
if [[ "$XDG_CURRENT_DESKTOP" == *"XFCE"* ]] || [[ "$XDG_SESSION_DESKTOP" == *"XFCE"* ]]; then
102+
echo "gnome"
103+
return
104+
fi
105+
106+
if [[ "$XDG_CURRENT_DESKTOP" == *"X-Cinnamon"* ]] || [[ "$XDG_SESSION_DESKTOP" == *"cinnamon"* ]]; then
107+
echo "gnome"
108+
return
109+
fi
110+
111+
if [[ "$XDG_CURRENT_DESKTOP" == *"UKUI"* ]] || [[ "$XDG_SESSION_DESKTOP" == *"ukui"* ]]; then
112+
echo "gnome"
113+
return
114+
fi
115+
116+
if [[ "$XDG_CURRENT_DESKTOP" == *"DDE"* ]] || [[ "$XDG_SESSION_DESKTOP" == *"dde"* ]]; then
117+
echo "gnome"
118+
return
119+
fi
120+
121+
if [[ "$XDG_CURRENT_DESKTOP" == *"MATE"* ]] || [[ "$XDG_SESSION_DESKTOP" == *"mate"* ]]; then
122+
echo "gnome"
123+
return
124+
fi
125+
126+
local KDE_ENVIRONMENTS=("KDE" "plasma")
127+
for ENV in "${KDE_ENVIRONMENTS[@]}"; do
128+
if [ "$XDG_CURRENT_DESKTOP" == "$ENV" ] || [ "$XDG_SESSION_DESKTOP" == "$ENV" ]; then
129+
echo "kde"
130+
return
131+
fi
132+
done
133+
134+
# Fallback to GNOME method if CLI utility is available. This solves the
135+
# proxy configuration issues on minimal installation systems, like setups
136+
# with only window managers, that borrow some parts from big DEs.
137+
if command -v gsettings >/dev/null 2>&1; then
138+
echo "gnome"
139+
return
140+
fi
141+
142+
echo "unsupported"
143+
}
144+
145+
# Function to check if proxy is currently enabled for GNOME
146+
is_proxy_enabled() {
147+
local MODE=$(gsettings get org.gnome.system.proxy mode | tr -d "'")
148+
if [ "$MODE" == "none" ]; then
149+
echo "Proxy is currently disabled."
150+
else
151+
echo "Proxy is currently enabled."
152+
echo "Proxy Mode: $MODE"
153+
if [ "$MODE" == "manual" ]; then
154+
local HTTP_PROXY=$(gsettings get org.gnome.system.proxy.http host | tr -d "'")
155+
local HTTP_PORT=$(gsettings get org.gnome.system.proxy.http port)
156+
local IGNORE_HOSTS=$(gsettings get org.gnome.system.proxy ignore-hosts)
157+
echo "HTTP Proxy: $HTTP_PROXY:$HTTP_PORT"
158+
echo "Ignored Hosts: $IGNORE_HOSTS"
159+
fi
160+
fi
161+
}
162+
163+
# Fuction to check if proxy is currently enabled for KDE
164+
is_kde_proxy_enabled() {
165+
166+
# Determine the correct kwriteconfig command based on KDE_SESSION_VERSION
167+
if [ "$KDE_SESSION_VERSION" == "6" ]; then
168+
KWRITECONFIG="kwriteconfig6"
169+
else
170+
KWRITECONFIG="kwriteconfig5"
171+
fi
172+
173+
local PROXY_TYPE=$($KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key ProxyType)
174+
if [ "$PROXY_TYPE" -eq 0 ]; then
175+
echo "Proxy is currently disabled."
176+
else
177+
echo "Proxy is currently enabled."
178+
local HTTP_PROXY=$($KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key httpProxy)
179+
local IGNORE_HOSTS=$($KWRITECONFIG --file kioslaverc --group "Proxy Settings" --key NoProxyFor)
180+
echo "HTTP Proxy: $HTTP_PROXY"
181+
echo "Ignored Hosts: $IGNORE_HOSTS"
182+
fi
183+
}
184+
185+
# Main script logic
186+
if [ "$#" -lt 1 ]; then
187+
echo "Usage: $0 <mode> [proxy_ip proxy_port ignore_hosts]"
188+
echo " mode: 'none' or 'manual'"
189+
echo " If mode is 'manual', provide proxy IP, port, and ignore hosts."
190+
exit 1
191+
fi
192+
193+
# Get the mode
194+
MODE=$1
195+
PROXY_IP=$2
196+
PROXY_PORT=$3
197+
IGNORE_HOSTS=$4
198+
199+
if ! [[ "$MODE" =~ ^(manual|none)$ ]]; then
200+
echo "Invalid mode. Use 'none' or 'manual'." >&2
201+
exit 1
202+
fi
203+
204+
# Detect desktop environment
205+
DE=$(detect_desktop_environment)
206+
207+
# Apply settings based on the desktop environment
208+
if [ "$DE" == "gnome" ]; then
209+
set_gnome_proxy "$MODE" "$PROXY_IP" "$PROXY_PORT" "$IGNORE_HOSTS"
210+
elif [ "$DE" == "kde" ]; then
211+
set_gnome_proxy "$MODE" "$PROXY_IP" "$PROXY_PORT" "$IGNORE_HOSTS"
212+
set_kde_proxy "$MODE" "$PROXY_IP" "$PROXY_PORT" "$IGNORE_HOSTS"
213+
else
214+
echo "Unsupported desktop environment: $DE" >&2
215+
exit 1
216+
fi

0 commit comments

Comments
 (0)