#!/usr/bin/env bash # ╭──────────────╮ # │ proxy config │ # ╰──────────────╯ # ─────────────────────────────────────< unset proxy >─────────────────────────────────── noproxy() { unset {HTTP_PROXY,HTTPS_PROXY,http_proxy,https_proxy} } # ──────────────────< proxy function to set or show the proxy variables >──────────────── proxy() { local ip="172.22.11.69" local port="8080" if [ -z "$1" ]; then echo "Usage: proxy [--show|--set]" echo " --show Show current proxy settings" echo " --set Set proxy to $ip:$port" return 0 fi case "$1" in --show) echo "These are your proxy configurations:" if [ -n "${http_proxy}" ] || [ -n "${HTTP_PROXY}" ] || [ -n "${https_proxy}" ] || [ -n "${HTTPS_PROXY}" ]; then echo "http: ${http_proxy}| HTTP: ${HTTP_PROXY}" echo "https: ${https_proxy}| HTTPS: ${HTTPS_PROXY}" else echo "No proxy configured." fi ;; --set) # ─< Check if the IP and port are reachable >───────────────────────────────────────────── if nc -z -w5 $ip $port; then # ─< If reachable, set the proxy environment variables >────────────────────────────────── local proxy="http://${ip}:${port}" for e in http_proxy HTTP_PROXY https_proxy HTTPS_PROXY; do export "${e}=${proxy}" done echo "Proxy set to: $proxy" else echo "IP $ip with port $port is not reachable." fi ;; *) echo "Invalid option! Use --show or --set." ;; esac } # ────────────< check network availability and set proxy if in correct network >──────────── noproxy if ping -c 1 -W 2 swu.dom &>/dev/null; then proxy --set else noproxy fi