Skip to content

Instantly share code, notes, and snippets.

@BlueskyFR
Last active June 5, 2023 12:53
Show Gist options
  • Save BlueskyFR/53fd4ec56e8f3ad7cc7193c88dedb20a to your computer and use it in GitHub Desktop.
Save BlueskyFR/53fd4ec56e8f3ad7cc7193c88dedb20a to your computer and use it in GitHub Desktop.
Linux live proxy patcher - A bash script that patches all running processes using GDB to either enable or disable proxy. Manage your proxy's state without having to logout anymore! You may have to restart your browser though :D
#!/bin/bash
# The proxy url to use when enabled
proxy=http://<proxy_ip>:<proxy_port>/
# -----------------------------------------------------
# Set proxy to "" if arg is "off"
# If arg is neither "on" or "off", exit
[[ $1 = "off" ]] && proxy= || ([[ $1 = "on" ]] || exit)
# Ensure we are root
sudo true
proxy_state=$([[ $proxy ]] && echo Enabling || echo Disabling)
echo -e "$proxy_state proxy...\n"
echo -n "Configuring snap..."
sudo snap set system proxy.http="$proxy" > /dev/null
sudo snap set system proxy.https="$proxy" > /dev/null
sudo snap set system proxy.ftp="$proxy" > /dev/null
echo -e "\033[1;32m Done!\033[0m"
pids=($(ps -Ao pid --no-headers))
echo -n "Patching all running processes (${#pids[@]} gdb instances)..."
# Loop through all running processes to patch their proxy variables
# The pid of a process can also be obtained from `pidof <program_name>`
for pid in ${pids[@]}; do
sudo gdb -p $pid \
-ex "set confirm off" -ex "set pagination off" \
-ex "call (int) putenv (\"https_proxy=$proxy\")" \
-ex "call (int) putenv (\"http_proxy=$proxy\")" \
-ex "call (int) putenv (\"ftp_proxy=$proxy\")" \
-ex "call (int) putenv (\"HTTPS_PROXY=$proxy\")" \
-ex "call (int) putenv (\"HTTP_PROXY=$proxy\")" \
-ex "call (int) putenv (\"FTP_PROXY=$proxy\")" \
-ex quit > /dev/null &> /dev/null &
done
# Wait for all gdb background instances to complete
wait
# Same as wait $(jobs -p)
echo -e "\033[1;32m Done!\033[0m"
# Restart Vivaldi
echo -n "Restarting Vivaldi..."
pkill vivaldi && i3-msg exec vivaldi-stable > /dev/null && echo -e "\033[1;32m Done!\033[0m" || echo -e "\033[1;33m Not opened :D\033[0m\n"
# Restart Slack
echo -n "Restarting Slack..."
pkill slack && i3-msg exec slack > /dev/null && echo -e "\033[1;32m Done!\033[0m" || echo -e "\033[1;33m Not opened :D\033[0m\n"
proxy_state=$([[ $proxy ]] && echo enabled || echo disabled)
echo -e "\033[1;35m\nProxy $proxy_state!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment