Last active
November 11, 2020 11:08
-
-
Save igilham/f2fb41190258f385eebe48a6c46421e5 to your computer and use it in GitHub Desktop.
Enable/Disable company proxy settings on MacOS
This file contains 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 python3 | |
from argparse import ArgumentParser | |
import os | |
import subprocess | |
proxy_host = os.getenv('PROXY_HOST', default='http-gw.mycompany.com') | |
proxy_port = os.getenv('PROXY_PORT', default='80') | |
base_java_opts = os.getenv("BASE_JAVA_OPTS", "") | |
on_network = "MyCompany On Network" | |
off_network = "MyCompany Off Network" | |
no_proxy_hosts = [ | |
"localhost", | |
"127.0.0.1", | |
"*.local", | |
"national.core.mycompany.com", | |
"sandbox", | |
"*.sandbox.dev.mycompany.com", | |
] | |
proxy_env_keys = [ | |
"http_proxy", | |
"HTTP_PROXY", | |
"https_proxy", | |
"HTTPS_PROXY", | |
] | |
def which(program): | |
is_exe = lambda fpath: os.path.isfile(fpath) and os.access(fpath, os.X_OK) | |
fpath, fname = os.path.split(program) | |
if fpath: | |
if is_exe(program): | |
return program | |
else: | |
for path in os.environ["PATH"].split(os.pathsep): | |
exe_file = os.path.join(path, program) | |
if is_exe(exe_file): | |
return exe_file | |
return None | |
def print_env(key, value): | |
print("export {}=\"{}\"".format(key, value)) | |
def sh(args): | |
print(" ".join(args), end=" &>/dev/null\n") | |
def configure_common(): | |
platform_java_opts = os.getenv("PLATFORM_JAVA_OPTS") | |
print_env("no_proxy", ",".join(no_proxy_hosts)) | |
print_env("MAVEN_OPTS", "-Xms256m -Xmx512m {}".format(platform_java_opts)) | |
def configure_proxy_on(): | |
"""configure settings for enabled proxy""" | |
host_port = "{}:{}".format(proxy_host, proxy_port) | |
http_proxy = "http://{}".format(host_port) | |
for key in proxy_env_keys: | |
print_env(key, http_proxy) | |
print_env("FTP_PROXY", ftp_proxy_host) | |
print_env("PLATFORM_JAVA_OPTS", "{} -Dhttp.proxyHost={} -Dhttp.proxyPort={} -Dhttps.proxyHost={} -Dhttps.proxyPort={} -Dhttp.nonProxyHosts='{}'".format(base_java_opts, proxy_host, proxy_port, proxy_host, proxy_port, "|".join(no_proxy_hosts))) | |
if which("git"): | |
sh(["git", "config", "--global", "http.proxy", host_port]) | |
sh(["git", "config", "--global", "https.proxy", host_port]) | |
if which("npm"): | |
sh(["npm", "config", "-g", "set", "proxy", http_proxy]) | |
sh(["npm", "config", "-g", "set", "https-proxy", http_proxy]) | |
configure_common() | |
def configure_proxy_off(): | |
"""configure settings for disabled proxy""" | |
for key in proxy_env_keys: | |
print_env(key, "") | |
print_env("FTP_PROXY", "") | |
print_env("PLATFORM_JAVA_OPTS", base_java_opts) | |
if which("git"): | |
sh(["git", "config", "--global", "--remove-section", "http"]) | |
sh(["git", "config", "--global", "--remove-section", "https"]) | |
if which("npm"): | |
sh(["npm", "config", "-g", "delete", "proxy"]) | |
sh(["npm", "config", "-g", "delete", "https-proxy"]) | |
configure_common() | |
def proxy_on(): | |
"""Enable proxy settings""" | |
subprocess.run( | |
args=["networksetup", "-switchtolocation",on_network], | |
capture_output=True, | |
) | |
configure_proxy_on() | |
def proxy_off(): | |
"""Disable proxy settings""" | |
subprocess.run( | |
args=["networksetup", "-switchtolocation", off_network], | |
capture_output=True, | |
) | |
configure_proxy_off() | |
def proxy_status(): | |
"""Check current network location""" | |
location = get_location() | |
print("Network location: {}".format(location)) | |
def proxy_reset(): | |
location = get_location() | |
if location == on_network: | |
configure_proxy_on() | |
else: | |
configure_proxy_off() | |
if __name__ == '__main__': | |
parser = ArgumentParser(description=""" | |
Configure proxy settings on MacOS. | |
Use with `eval` in the shell to make changes to your configuration, e.g. | |
`eval $(proxy.py on)` | |
""") | |
parser.add_argument('command', metavar='COMMAND', choices=['on', 'off', 'status', 'reset'], help="on|off|status|reset") | |
args = parser.parse_args() | |
if args.command == 'on': | |
proxy_on() | |
elif args.command == 'off': | |
proxy_off() | |
elif args.command == 'status': | |
proxy_status() | |
elif args.command == 'reset': | |
proxy_reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment