Using proxy for specific domains with safari
by: Peter Shoukry

Chrome uses too much processing and battery power on my mac. When I use it for some time my laptop just starts getting hot. It has been my browser of choice for a long time, but Safari is just the best in power management on mac. ## If you can’t beat them join them Having tried every trick to solve this issue I finally settled for using Safari even though I prefer chrome’s devtools or at least they are the ones I have been using for years. So I made the switch but one thing remained really annoying. I work with different clients and with different setups at PLUS(pluseg.com), and sometimes I need to access some domains using a proxy. Switching proxies manually wastes lots of time and setting up safari to use a dynamic configuration script isn’t really straight forward. It took me sometime and lots of troubleshooting but I finally got it working and here are the steps: 1. The proxy You can use any type of proxy but a simple proxy can be created using an ssh tunnel like this: ```bash ssh -i /path/to/your/pem/file -D 8123 -f -C -q -N user@yourdomain.com ``` 2. The proxy configuration script (.pac) A simple script to choose the proxy based on the domain is: ```bash function FindProxyForURL(url, host) { PROXY = "SOCKS 127.0.0.1:8123"if (dnsDomainIs(host,"domain_to_use_with_proxy.com") || dnsDomainIs(host, "www.domain_to_use_with_proxy.com")) { return PROXY; }return "DIRECT"; } ``` 3. local web server This is the interesting part, some solutions on the internet say that you can just use the proxy configuration in safari using the file:/// notation but this doesn’t work. Safari will never read the file because of the sandboxing mechanism and the solution is to use a simple local web server to serve the file. Your file structure should look like this: ```yaml +-- docker-compose.yml +-- html +-- proxy.pac ``` and your docker-compose.yml file should be: ```yaml version: '3' services: proxy: image: nginx volumes: - ./html:/usr/share/nginx/html ports: - "80:80" ``` ## Bringing it all together Now simply running docker-compose up proxy will start the server with the .pac file and adding http://localhost/proxy.pac as the configuration script URL in safari will make it use the proxy for your specific domain only.