Created
December 7, 2023 12:06
-
-
Save hnaderi/6cd5170ffa153940f06301196d4f4f8c to your computer and use it in GitHub Desktop.
Set proxy for a daemon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
CMD=$1 | |
SVC="${2:-nix-daemon}" | |
PROXY="${3:-socks5://localhost:9999}" | |
BASE_DIR=/run/systemd/system | |
SVC_DIR="$BASE_DIR/$SVC.service.d" | |
OVERRIDE_FILE="$SVC_DIR/proxy-override.conf" | |
log(){ | |
printf "Service\t: %s\n" "$SVC" | |
printf "Proxy\t: %s\n" "$PROXY" | |
} | |
on() { | |
echo Setting proxy... | |
log; | |
mkdir -p "$SVC_DIR" | |
cat << EOF >"$OVERRIDE_FILE" | |
[Service] | |
Environment="http_proxy=$PROXY" | |
Environment="https_proxy=$PROXY" | |
Environment="all_proxy=$PROXY" | |
EOF | |
reload | |
} | |
off() { | |
echo Removing proxy... | |
log; | |
rm "$OVERRIDE_FILE" | |
reload | |
} | |
reload() { | |
systemctl daemon-reload | |
systemctl restart "$SVC" | |
} | |
case $CMD in | |
"on") | |
on | |
;; | |
"off") | |
off | |
;; | |
*) | |
echo "Invalid arguments!"; | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment