-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyset.sh
418 lines (353 loc) · 11.7 KB
/
proxyset.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/bin/bash
VERSION="1.0.0"
# Add help and version flags
show_help() {
cat << EOF
P R O X Y S E T
Usage: proxyset [command] [options]
Commands:
set Configure and enable proxy settings
rollback Remove all proxy settings
status Show current proxy configuration
Options:
-h, --help Show this help message
-v, --version Show version number
-s, --silent Run in silent mode
--no-reboot Skip reboot prompt after setting proxy
Examples:
proxyset set
proxyset rollback
proxyset status
proxyset set --no-reboot
EOF
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to set proxy for a specific configuration file
set_proxy_for_file() {
local file="$1"
local proxy_url="$2"
if [ -f "$file" ]; then
sudo sed -i '/^proxy=/d' "$file"
echo "proxy=$proxy_url" | sudo tee -a "$file" >/dev/null
fi
}
# Function to remove proxy from a specific configuration file
remove_proxy_from_file() {
local file="$1"
if [ -f "$file" ]; then
sudo sed -i '/^proxy=/d' "$file"
fi
}
# Backup important files
backup_files() {
if [ -f /etc/environment ]; then
sudo cp /etc/environment /etc/environment.bak
fi
if [ -f /etc/pacman.conf ]; then
sudo cp /etc/pacman.conf /etc/pacman.conf.bak
fi
if [ -f /etc/iptables/iptables.rules ]; then
sudo cp /etc/iptables/iptables.rules /etc/iptables/iptables.rules.bak
fi
}
# Add status function
show_proxy_status() {
echo "Current Proxy Configuration:"
echo "-------------------------"
echo "HTTP Proxy: ${http_proxy:-Not set}"
echo "HTTPS Proxy: ${https_proxy:-Not set}"
echo "FTP Proxy: ${ftp_proxy:-Not set}"
echo "No Proxy: ${no_proxy:-Not set}"
echo -e "\nSystem Configurations:"
echo "-------------------------"
if [ -f "/etc/environment" ]; then
echo "Environment file proxy settings:"
grep -i "proxy" /etc/environment || echo "No proxy settings in environment file"
fi
if command_exists git; then
echo -e "\nGit proxy settings:"
git config --global --get http.proxy || echo "No Git HTTP proxy set"
git config --global --get https.proxy || echo "No Git HTTPS proxy set"
fi
}
# Main proxy setup function
setup_proxy() {
local PROXY_SERVER PROXY_PORT AUTH_REQUIRED PROXY_USER PROXY_PASS PROXY_URL
read -p "Enter proxy server (e.g., proxy.example.com or 192.168.1.100): " PROXY_SERVER
read -p "Enter proxy port: " PROXY_PORT
read -p "Does your proxy require authentication? (y/n): " AUTH_REQUIRED
if [[ $AUTH_REQUIRED =~ ^[Yy]$ ]]; then
read -p "Enter username: " PROXY_USER
read -s -p "Enter password: " PROXY_PASS
echo # Add a newline after the password input
PROXY_URL="http://${PROXY_USER}:${PROXY_PASS}@${PROXY_SERVER}:${PROXY_PORT}"
else
PROXY_URL="http://${PROXY_SERVER}:${PROXY_PORT}"
fi
# Backup important files
backup_files
# Set environment variables
export http_proxy="$PROXY_URL"
export https_proxy="$PROXY_URL"
export ftp_proxy="$PROXY_URL"
export no_proxy="localhost,127.0.0.1,::1"
# Add to /etc/environment
sudo tee -a /etc/environment << EOF
http_proxy=$PROXY_URL
https_proxy=$PROXY_URL
ftp_proxy=$PROXY_URL
no_proxy=localhost,127.0.0.1,::1
EOF
# Configure Pacman if it exists
if [ -f /etc/pacman.conf ]; then
sudo sed -i 's/^#XferCommand/XferCommand/' /etc/pacman.conf
sudo sed -i '/^XferCommand/d' /etc/pacman.conf
echo "XferCommand = /usr/bin/curl -x $PROXY_URL -L %u -o %o" | sudo tee -a /etc/pacman.conf
fi
# Configure Flatpak
if command_exists flatpak; then
flatpak --system config --set system.proxy "$PROXY_URL"
fi
# Configure Git
if command_exists git; then
git config --global http.proxy "$PROXY_URL"
git config --global https.proxy "$PROXY_URL"
fi
# Configure Wget
set_proxy_for_file "$HOME/.wgetrc" "$PROXY_URL"
# Configure cURL
set_proxy_for_file "$HOME/.curlrc" "$PROXY_URL"
# Configure NPM
if command_exists npm; then
npm config set proxy "$PROXY_URL"
npm config set https-proxy "$PROXY_URL"
fi
# Configure Yarn
if command_exists yarn; then
yarn config set proxy "$PROXY_URL"
yarn config set https-proxy "$PROXY_URL"
fi
# Configure system-wide GNOME proxy settings
if command_exists gsettings; then
gsettings set org.gnome.system.proxy mode 'manual'
gsettings set org.gnome.system.proxy.http host "$PROXY_SERVER"
gsettings set org.gnome.system.proxy.http port "$PROXY_PORT"
gsettings set org.gnome.system.proxy.https host "$PROXY_SERVER"
gsettings set org.gnome.system.proxy.https port "$PROXY_PORT"
gsettings set org.gnome.system.proxy.ftp host "$PROXY_SERVER"
gsettings set org.gnome.system.proxy.ftp port "$PROXY_PORT"
if [[ $AUTH_REQUIRED =~ ^[Yy]$ ]]; then
gsettings set org.gnome.system.proxy.http authentication-user "$PROXY_USER"
gsettings set org.gnome.system.proxy.http authentication-password "$PROXY_PASS"
fi
fi
# Set up iptables rules if iptables exists
if command_exists iptables; then
sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination "${PROXY_SERVER}:${PROXY_PORT}"
sudo iptables -t nat -A OUTPUT -p tcp --dport 443 -j DNAT --to-destination "${PROXY_SERVER}:${PROXY_PORT}"
# Save iptables rules if directory exists
if [ -d /etc/iptables ]; then
sudo iptables-save | sudo tee /etc/iptables/iptables.rules
fi
# Enable iptables service if systemd exists
if command_exists systemctl; then
sudo systemctl enable iptables.service
fi
fi
# Set up transparent proxy using Python script
setup_transparent_proxy
# Reboot prompt
if [ "${SKIP_REBOOT:-0}" -eq 0 ]; then
read -p "Proxy setup complete. Would you like to reboot now? (y/n): " answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
sudo reboot
fi
fi
}
# Function to set up transparent proxy using Python
setup_transparent_proxy() {
if ! command_exists python3; then
echo "Python3 is not installed. Skipping transparent proxy setup."
return 1
fi
cat << 'EOF' > /tmp/transparent_proxy.py
import socket
import threading
import socketserver
import urllib.request
import os
class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
allow_reuse_address = True
class ProxyHandler(socketserver.StreamRequestHandler):
def handle(self):
try:
req = self.request.recv(1024).decode('utf-8')
first_line = req.split('\n')[0]
url = first_line.split(' ')[1]
print(f"Requesting: {url}")
proxy_url = os.environ.get('PROXY_URL')
if not proxy_url:
raise ValueError("Proxy URL not set in environment")
proxy_handler = urllib.request.ProxyHandler({
'http': proxy_url,
'https': proxy_url
})
opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
self.request.sendall(response.read())
except Exception as e:
print(f"Error: {e}")
self.request.sendall(b"HTTP/1.1 500 Internal Server Error\r\n\r\n")
if __name__ == "__main__":
HOST, PORT = "localhost", 8080
with ThreadingTCPServer((HOST, PORT), ProxyHandler) as server:
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
print(f"Transparent proxy server running on {HOST}:{PORT}")
try:
server_thread.join()
except KeyboardInterrupt:
server.shutdown()
EOF
# Set up systemd service for the transparent proxy
if command_exists systemctl; then
sudo tee /etc/systemd/system/transparent-proxy.service << EOF
[Unit]
Description=Transparent Proxy Service
After=network.target
[Service]
Environment="PROXY_URL=$PROXY_URL"
ExecStart=/usr/bin/python3 /tmp/transparent_proxy.py
Restart=always
User=nobody
Group=nobody
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable transparent-proxy.service
sudo systemctl start transparent-proxy.service
fi
}
# Function to remove proxy settings
rollback_proxy() {
echo "Rolling back proxy settings..."
# Remove from /etc/environment
if [ -f /etc/environment ]; then
sudo sed -i '/http_proxy/d; /https_proxy/d; /ftp_proxy/d; /no_proxy/d' /etc/environment
fi
# Remove environment variables
unset http_proxy https_proxy ftp_proxy no_proxy
# Restore Pacman configuration
if [ -f /etc/pacman.conf ]; then
sudo sed -i 's/^XferCommand/#XferCommand/' /etc/pacman.conf
remove_proxy_from_file "/etc/pacman.conf"
fi
# Remove Flatpak proxy
if command_exists flatpak; then
flatpak --system config --unset system.proxy
fi
# Remove Git proxy settings
if command_exists git; then
git config --global --unset http.proxy
git config --global --unset https.proxy
fi
# Remove Wget proxy settings
remove_proxy_from_file "$HOME/.wgetrc"
if [ -f /etc/wgetrc ]; then
sudo rm -f /etc/wgetrc
fi
# Remove cURL proxy settings
remove_proxy_from_file "$HOME/.curlrc"
if [ -f /etc/curlrc ]; then
sudo rm -f /etc/curlrc
fi
# Remove NPM proxy settings
if command_exists npm; then
npm config delete proxy
npm config delete https-proxy
fi
# Remove Yarn proxy settings
if command_exists yarn; then
yarn config delete proxy
yarn config delete https-proxy
fi
# Remove GNOME proxy settings
if command_exists gsettings; then
gsettings set org.gnome.system.proxy mode 'none'
fi
# Clear iptables rules if iptables exists
if command_exists iptables; then
sudo iptables -t nat -F OUTPUT
if [ -d /etc/iptables ]; then
sudo iptables-save | sudo tee /etc/iptables/iptables.rules
fi
fi
# Stop and remove transparent proxy service
if command_exists systemctl; then
sudo systemctl stop transparent-proxy.service
sudo systemctl disable transparent-proxy.service
sudo rm -f /etc/systemd/system/transparent-proxy.service
fi
sudo rm -f /tmp/transparent_proxy.py
echo "Proxy settings rolled back successfully."
}
# Add argument parsing
parse_args() {
case "$1" in
-h|--help)
show_help
exit 0
;;
-v|--version)
echo "Proxyset v${VERSION}"
exit 0
;;
set)
shift
SKIP_REBOOT=0
SILENT_MODE=0
while [[ $# -gt 0 ]]; do
case "$1" in
--no-reboot)
SKIP_REBOOT=1
;;
-s|--silent)
SILENT_MODE=1
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
shift
done
setup_proxy
;;
rollback)
rollback_proxy
;;
status)
show_proxy_status
;;
*)
show_help
exit 1
;;
esac
}
# Main script entry point
main() {
if [ $# -eq 0 ]; then
show_help
exit 1
fi
parse_args "$@"
}
# Call main with all arguments
main "$@"