3 min read

Building a Squid Proxy Container to test NPM with HTTP Proxy

Building a Squid Proxy Container to test NPM with HTTP Proxy

What's up, everyone? I'll show you how to set up your NPM to use an HTTP proxy today. A long time ago, I had a problem at a previous job where internet access was proxied and fixed to a specific endpoint.

This article is for you if you had this connection problem or if you want to use a proxy to cache some files.

To put this scenario to the test, we'll use a known caching proxy, Squid with authentication. So, make your favorite coffee ☕, and let's get started on a kernel update.

Squid Proxy

It's time to use Docker to build a squid proxy. To prepare this container, we must configure squid.conf with basic authentication; I chose this method to test a proxy with all features.

Remember to keep all assets in the same directory.
  • squid.conf
acl localnet src 0.0.0.1-0.255.255.255	# RFC 1122 "this" network (LAN)
acl localnet src 10.0.0.0/8		# RFC 1918 local private network (LAN)
acl localnet src 100.64.0.0/10		# RFC 6598 shared address space (CGN)
acl localnet src 169.254.0.0/16 	# RFC 3927 link-local (directly plugged) machines
acl localnet src 172.16.0.0/12		# RFC 1918 local private network (LAN)
acl localnet src 192.168.0.0/16		# RFC 1918 local private network (LAN)
acl localnet src fc00::/7       	# RFC 4193 local private network range
acl localnet src fe80::/10      	# RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80		# http
acl Safe_ports port 21		# ftp
acl Safe_ports port 443		# https
acl Safe_ports port 70		# gopher
acl Safe_ports port 210		# wais
acl Safe_ports port 1025-65535	# unregistered ports
acl Safe_ports port 280		# http-mgmt
acl Safe_ports port 488		# gss-http
acl Safe_ports port 591		# filemaker
acl Safe_ports port 777		# multiling http

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localhost manager
http_access deny manager

http_access allow localhost

http_port 3128

coredump_dir /var/spool/squid

refresh_pattern ^ftp:		1440	20%	10080
refresh_pattern ^gopher:	1440	0%	1440
refresh_pattern -i (/cgi-bin/|\?) 0	0%	0
refresh_pattern \/(Packages|Sources)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
refresh_pattern \/Release(|\.gpg)$ 0 0% 0 refresh-ims
refresh_pattern \/InRelease$ 0 0% 0 refresh-ims
refresh_pattern \/(Translation-.*)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
refresh_pattern .		0	20%	4320

auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwords
auth_param basic children 5
auth_param basic realm Squid Basic Authentication
auth_param basic credentialsttl 5 hours
acl authenticated proxy_auth REQUIRED

http_access allow authenticated

include /etc/squid/conf.d/*.conf
More information can be found in the official docs.
  • passwords

This password was generated using the Apache htpassword tool with the known and strong credentials user foo and password boo 🤣.

foo:$apr1$2altfem1$ubdbswuXqgJAM9fi5zNeh1
  • Dockerfile
FROM ubuntu/squid

COPY passwords /etc/squid/passwords
COPY squid.conf /etc/squid/squid.conf

RUN chown proxy:proxy /etc/squid/squid.conf /etc/squid/passwords
  • docker-compose.yml
version: "3.8"

services:
  proxy:
    build:
      context: ./
    ports:
      - "3128:3128"
    environment:
      TZ: UTC

It is now time to build our squid proxy, which will listen on port 3128.

docker-compose up -d

The curl command will be used to test the proxy, with success using credentials and failure without credentials.

curl -I --proxy http://127.0.0.1:3128 https://www.google.com 
# > HTTP/1.1 407 Proxy Authentication Required

curl -I --proxy http://foo:[email protected]:3128 https://www.google.com
# > HTTP/1.1 200 Connection established

NPM

Okay, Squid Proxy is up and running, so let's set up NPM config, with command bellow.

npm config set https-proxy http://foo:boo@localhost:3128

Next, we'll install Surge, an amazing static web publishing platform with a free tier.

npm install --global surge

As a result, your NPM package should be successfully installed.

Cleanup

If you want to clean up your proxy settings, run the script below.

npm config delete https-proxy

# removes the squid proxy container and all images used
docker-compose down --rmi all 

Finally

If you prefer, you can clone the repository

We built a Squid Proxy to test NPM with HTTP Proxy, but we can test with any tool that supports HTTP Proxy endpoint configuration. I admit that it is difficult to set up an environment when there is an HTTP Proxy, so when things do not work as expected, we must find a workaround.🔨

New packages are built and assembled on your kernel. God bless you. Please leave a comment or share to help me improve my articles. Bye! 🫶