All those things about corpo proxy I wish I knew from the day one

Corporations with hardware products likes guarding their assortment (both hardware and people) with proxy. I got that pleasure to work for a few companies that used company proxy and spent countless hours on troubleshooting things that finally turned out to be proxy/no_proxy issues.

What is a proxy? As Wikipedia says:

Instead of connecting directly to a server that can fulfill a request for a resource, such as a file or web page, the client directs the request to the proxy server, which evaluates the request and performs the required network transactions. (…) provide additional benefits such as load balancing, privacy, or security.

And allows blocking a request when someone uploads gigabytes of confidential documents. Or blocks some nasty pages. Doesn’t sound bad, right?

Do you know that saying “it’s always DNS”? Well, when working behind proxy, and you have a connection issue, it’s in 99% cases proxy/no_proxy matter. With proxy, handling connections can be more complicated. Hopefully, after reading that article, it won’t be THAT complicated. I think understanding types of connections that can happen in the network behind proxy is a necessity.

Let’s say you just started working in a company named Example, with a company domain named example.com. Day one, you got access to the corporation’s VPN to connect to the company’s network.

Let’s iterate over a few levels of proxy interactions.

Level none

You have not connected to VPN yet, connection doesn’t need any proxy yet. Plain and simple.

Network diagram: connecting without a VPN or proxy

Level entry

You connect to that corpo VPN and you are trying to access intranet (ex. that internal news page) on URL http://intranet.example.com or your team’s test environment at http://10.1.2.3. That’s easy. Neither proxy nor no_proxy set yet, and it works out-of-the-box.

Network diagram: accessing internal resources through the corporate VPN

Level easy

Now, after you’ve read all those great news that your employer has shared, completed trainings on internal web page, it’s time to access public pages. Let’s start small, with accessing http://google.com. Typing google.com, ENTER, and… Infinite loading page. Did you type that incorrectly? Did your internet connection stop working? Nope, it’s proxy issue. Soon you will know how to fix it.

Put together the proxy string

First things first, let’s build the proxy URL. Someone mentioned proxy server available on proxy.example.com.

Do you need credentials to authorize to proxy? If so, it would be username:password@proxy.example.com.

Probably proxy over HTTP and on some arbitrary port, let’s say 8080. So final URL looks like:

http://proxy.example.com:8080

Easy, right?

Use that proxy string

Where to put it? In the browser, open settings, search for proxy, set it to custom and put it there. Don’t trust the “use system settings”, for now set your values explicitly on your own. Set the HTTP proxy to the value we just prepared. In HTTPS proxy use exactly the same value (unless your corpo has different port for HTTPS).

When setting HTTPS proxy, do NOT change http:// to https://. It has to be http:// for both.

For no proxy set:

localhost,127.0.0.1

Just for now, in case you would test something locally. Save, and we are all set. Back to the Google page. Refresh, and it SHOULD work now.

Network diagram: accessing the internet through the corporate proxy

Level easy+

You did your things there time to finish your day one and disconnect the VPN.

But beware, because once you disconnect the VPN, external connections would stop working as proxy server won’t be achievable. So you need to disable that proxy settings then.

Don’t do it manually, removing proxy entry in the browser, or switching to “no proxy” setting. To do that switching easily, I recommend using a browser extension like Proxy SwitchyOmega or similar.

Network diagram: disconnecting the VPN while proxy settings remain enabled

Level medium

Let’s start using shell with proxy. Good tools should handle both lower case and upper case proxy definitions. But for sake of saving your mental stability, better to set both:

http_proxy=http://proxy.example.com:8080
https_proxy=http://proxy.example.com:8080
no_proxy=localhost,127.0.0.1
HTTP_PROXY=${http_proxy}
HTTPS_PROXY=${https_proxy}
NO_PROXY=${no_proxy}

Now you should be able to reach the public things, like google.com.

But once you got to a point where you would need to connect hosts from your internal network, it’s “where the stairs begin” (polish saying for “here comes the hard part”). Hopefully you would be able to figure out some nice network range like 10.0.0.0/8 to get all internal IPs in one entry, and your tool would support range in no_proxy. Will get back to that later. At least, let’s start with adding to no_proxy those IPs that you want to connect immediately. If using the FQDNs (fully qualified domain names), like myserver1.lab1.example.com, you can append to no_proxy entry example.com. Once again, will get back to that later.

So, updated no_proxy can look like that:

no_proxy=localhost,127.0.0.1,10.0.0.0/8,example.com

Network diagram: internal connections bypass the proxy using no_proxy

With proxy enabled, but not correctly set no_proxy, reaching a hostname that is in the internal network, but isn’t placed in the no_proxy, will result with the connection fail or timeout. That request would get through proxy to the external world, where that internal IP won’t be known.

Network diagram: an internal request fails when it goes through the proxy

Whenever you would have a hunch that a connection isn’t working because of missing no_proxy entry, try:

curl --noproxy '*' 10.1.2.3

Please notice there is no dash between ’no’ and ‘proxy’, and there’s a single quote around star sign.

Make it permanent

Those environment proxy variables, set only in current shell session, will be gone once the session got terminated. To always have those handy, add those to ~/.bashrc (or whatever different initialization file your shell has). Don’t forget to source the .bashrc file or restart the shell once you’ve changed something there.

Nice, so are the proxy issues gone? Nope. Whenever you would run something with sudo, like sudo apt update, that command won’t have the proxy settings in that shell. If you can, worth setting those in /etc/profile too. Or wherever are global variables for your shell.

Even if you have all those things set, some applications still won’t care and won’t read those environment variables, and would need proxy set in their own config. There are different ways of doing that for different apps. Below, there are a few examples that I had to find out the hard way.

Docker proxy

File ~/.docker/config.json or /etc/docker/config.json:

{
 "proxies": {
   "default": {
     "httpProxy": "http://proxy.example.com:8080",
     "httpsProxy": "http://proxy.example.com:8080",
     "noProxy": "localhost,127.0.0.1"
   }
 }
}

Systemd service

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080"
Environment="HTTPS_PROXY=http://proxy.example.com:8080"
Environment="NO_PROXY=localhost,127.0.0.1"

Maven proxy

File ~/.m2/settings.xml:

<settings>
  <proxies>
    <proxy>
      <id>proxy-http</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.example.com</host>
      <port>8080</port>
    </proxy>
    <proxy>
      <id>proxy-https</id>
      <active>true</active>
      <protocol>https</protocol>
      <host>proxy.example.com</host>
      <port>8080</port>
    </proxy>
  </proxies>
</settings>

NPM proxy

npm config set https-proxy http://proxy.example.com:8080
npm config set http-proxy http://proxy.example.com:8080

Level hard

Hopefully you would never encounter that. With all those settings that are set as good as that can be, now try reaching a resource example.com/pkg.gz that is shared on your company domain, but achievable only from outside, not achievable on the intranet server. Dirty fix is just cleaning no_proxy.

no_proxy= NO_PROXY= curl https://example.com/pkg.gz --output pkg.gz

Network diagram: reaching a company resource available only outside the intranet

It is not an academic discussion, I had that case. It can sound easy, but getting to that conclusion, noticing where that resource is available only outside took us too much time. And having to diagnose and handle that in a longer script that is a part of infrastructure initialization, having that proxy exception, can be a real pain. Keep in mind it can happen when reaching your company’s resources.

Let’s talk about no_proxy

Unfortunately, the fact that you are able to successfully pass the proxy settings to the app that needs it, is just a half of the story. Setting correctly no_proxy is another half.

Depends on the app that would use the no_proxy environment variables (or even no proxy set directly in the app), the type of the application affects the no_proxy parsing methods. A few examples below:

Stan Hu did a good job categorizing handling of proxy/no_proxy between technologies stacks, worth checking here: https://about.gitlab.com/blog/we-need-to-talk-no-proxy/#no_proxy-format. Additionally, treating about proxy setting generally too, whole article is worth checking.

Generally, the rule of thumb should be, whenever local connection in an app doesn’t work, check no_proxy first. The fact that it works fine for one tool, doesn’t mean it would do the same for the other. Especially between different frameworks or technologies. Always keep that in mind.

Sum up

I’m glad I’ve finally written that down. There were plenty of debugging sessions caused by that kind of proxy issues. I hope that you would be able to overcome those easily. Or at least, keep proxy as one of the usual suspects. Good luck!