Unblock sites, use proxies on specific sites without addons with simple proxy file

Proxy Auto Configuration (PAC) is a technology that allows a browser to read a special file with instructions how and where to use proxy servers.

This is a pretty old technology, but it still works in most browsers, like Firefox, Google Chrome, Opera, both Internet Explorer and Microsoft Edge, Safari and others.

I already have an article about how to grab, verify and add to a PAC file proxies using bash scripts

In this article we will take Firefox as an example. But other browsers are not much different in this.

First decide where PAC file will be stored, it can be any user directory. Create PAC file and open it for editing in any text editor you like:

nano ~/proxy.pac

Add the following lines inside PAC file, change options with yours where i noted:

function FindProxyForURL(url, host) {                 // This is a function which allows us to use proxy servers on certain sites
var proxy = "PROXY ip:port";                                // Edit this. Determine proxy server ip address and port
var sites = new Array("google.com");                    // Edit this. Determine a domain name on which the proxy server should be used
for(var i=0; i<sites.length; i++) {                            // We can add more than one domain names to 'site' array, so all of them will be compared with a current requested site
  var domain = sites[i];
  if ( localHostOrDomainIs(host, domain) )            // Check a domain name with 'sites' array
         return proxy;                                                  // If the domain name matches, the proxy will be used
return "DIRECT";                                                   // If the domain name not matches, the proxy will not be used

The code above is written in JavaScript. It works in Firefox, but some parameters may differ in other browsers. Therefore, it is easy to fix, if necessary.

It is possible to add more proxy servers, separating them with a semicolon. Also, Firefox allows to use “SOCKS” option if you are sure that SOCKS (4 or 5) protocol is used:

var proxy = "PROXY ip:port; SOCKS ip:port; PROXY ip:port";

If the first proxy doesn’t work, the next proxy is used. The first, not worked proxy is checked after 30 minutes. If it still doesn’t work, the browser doesn’t touch it for an hour. And so on. Therefore, for reliability it is recommended to store several dozen proxies. In subsequent articles, I will show how to grab a lot of proxies in a list, check the desired site for availability with this proxy list, delete broken proxies from the list and automatically add this proxy list to PAC file in five seconds.

In modern Firefox versions, there are more options, like HTTP, HTTPS, SOCKS4, SOCKS5. Use them if you are sure which protocol a server uses:

var proxy = "HTTP ip:port; HTTPS ip:port; SOCKS4 ip:port; SOCKS5 ip:port";

Modify ‘sites’ array if you want to add more than one domain name to use with proxy:

var sites = new Array("google.com", "bing.com", "duckduckgo.com");

Now, we are done with the configuration. It remains to add our PAC file to a browser settings. Go to network settings section, use the following method to determine the path to a PAC file:

file:///C:/somedir/path/proxy.pac       # Windows example
file:///home/user/proxy.pac                # Linux example, I put PAC file in a home directory
automatic proxy configuration URL, use file:/// instead of http:// or something
PAC file configuring in Firefox

Now PAC file is loaded. If you will make some changes in PAC file, you can simply reload it with ‘Reload’ button (see the image above), or just restart the browser.

PAC tester tool

There is PAC tester which allows us to test our PAC file. To install it on Debian-based system use the following command:

sudo apt install libpacparser1

Specify the path to PAC file using -p option and site URL using -u option:

pactester -p ~/proxy.pac -u https://bing.com
HTTPS xxx.xxx.xxx.xxx:8888

If everything is correct, there will be proxy ip address and port shown and a protocol that is used.

It is possible to create a list with URL addresses that we want to use for our experiment:

cat urls

Use -f option to specify URL list:

pactester -p ~/proxy.pac -f urls
https://google.com : DIRECT
https://bing.com : HTTPS xxx.xxx.xxx.xxx:8888

I didn’t set up any proxies for google.com, therefore it is DIRECT. All things work.