functionproxy{param([string]$Action="toggle",[string]$IP="127.0.0.1",[string]$Port="7897")$ProxyServer="http://$IP`:$Port"# Colors for better UX$Green="Green"$Red="Red"$Yellow="Yellow"$Cyan="Cyan"# Check current status$CurrentProxy=$env:HTTP_PROXY??$env:http_proxy$IsEnabled=$CurrentProxy-eq$ProxyServerswitch($Action.ToLower()){"on"{if($IsEnabled){Write-Host"Proxy already enabled: $ProxyServer"-ForegroundColor$Greenreturn}$env:HTTP_PROXY=$ProxyServer$env:HTTPS_PROXY=$ProxyServer$env:http_proxy=$ProxyServer$env:https_proxy=$ProxyServerWrite-Host"Proxy enabled: $ProxyServer"-ForegroundColor$Green}"off"{if(-not$IsEnabled){Write-Host"Proxy already disabled"-ForegroundColor$Greenreturn}Remove-ItemEnv:HTTP_PROXY-ErrorActionSilentlyContinueRemove-ItemEnv:HTTPS_PROXY-ErrorActionSilentlyContinueRemove-ItemEnv:http_proxy-ErrorActionSilentlyContinueRemove-ItemEnv:https_proxy-ErrorActionSilentlyContinueWrite-Host"Proxy disabled"-ForegroundColor$Red}"status"{Write-Host"Proxy Status:"-ForegroundColor$CyanWrite-Host" HTTP_PROXY: $($env:HTTP_PROXY??'Not set')"Write-Host" HTTPS_PROXY: $($env:HTTPS_PROXY??'Not set')"# Test connectivity if proxy is enabledif($IsEnabled){Write-Host" Testing connection to Google..."-ForegroundColor$Yellowtry{$test=Invoke-WebRequest-Uri"https://www.google.com"-TimeoutSec5-UseBasicParsingWrite-Host" Connection test: SUCCESS"-ForegroundColor$Green}catch{Write-Host" Connection test: FAILED"-ForegroundColor$Red}}else{Write-Host" Status: DISABLED"-ForegroundColor$Red}}"toggle"{if($IsEnabled){proxy-Actionoff}else{proxy-Actionon-IP$IP-Port$Port}}default{Write-Host"Unknown action: $Action"-ForegroundColor$RedWrite-Host"Valid actions: on, off, status, toggle"-ForegroundColor$Yellow}}}# Create alias for convenienceSet-Alias-Namepxy-Valueproxy
Comments
konosubakonoakua · 2025-12-15T04:32:19Z
proxy(){localaction="${1:-status}"localip="${2:-127.0.0.1}"localport="${3:-7897}"localproxy_url="http://$ip:$port"case"$action"inon|enable)echo"Enabling proxy: $proxy_url"exporthttp_proxy="$proxy_url"exporthttps_proxy="$proxy_url"exportHTTP_PROXY="$proxy_url"exportHTTPS_PROXY="$proxy_url"echo"✅ Proxy enabled: $proxy_url";;off|disable)echo"Disabling proxy..."unsethttp_proxyhttps_proxyHTTP_PROXYHTTPS_PROXY
echo"✅ Proxy disabled";;toggle)if[-n"$http_proxy"];thenproxyoff
elseproxyon"$ip""$port"fi;;status)echo"=== Proxy Environment Variables ==="if[-n"$http_proxy"];thenecho"Current Proxy: $http_proxy"elseecho"Current Proxy: Not set"fiecho-e"\n=== Testing Google Connectivity ==="localtimeout=2echo-n"Testing https://www.google.com ... "ifcurl-s--connect-timeout"$timeout""https://www.google.com">/dev/null2>&1;thenecho"✅ SUCCESS"echo"Network connectivity is working correctly!"elseecho"❌ FAILED"if[-n"$http_proxy"];thenecho"Proxy may not be working properly."elseecho"No proxy is set or there's a connection issue."fifi;;*)echo"Usage: proxy {on|off|toggle|status} [ip] [port]"echo"Examples:"echo" proxy on # Enable with default 127.0.0.1:7897"echo" proxy on 192.168.138.254 7897 # Enable with custom IP:port"echo" proxy off # Disable proxy"echo" proxy toggle # Toggle proxy state"echo" proxy status # Show status and test connectivity"return1;;esac}