Skip to content

Instantly share code, notes, and snippets.

@mendhak
Last active August 27, 2024 20:58
Show Gist options
  • Save mendhak/bdbed5dea83fede0b2160a75f8a0883f to your computer and use it in GitHub Desktop.
Save mendhak/bdbed5dea83fede0b2160a75f8a0883f to your computer and use it in GitHub Desktop.
Playwright, using a proxy, with a domain being transparently mapped to another domain

With Playwright in a container, I want to have it call 'example.com', and when it does, reroute that request to some other domain, such as httpbin.org. In my case this would be a load balancer.

How it works - Playwright uses its proxy feature and calls Squid.
The Squid container has been configured to think that example.com is on the nginx container's IP address.
Nginx just uses TCP forwarding and sends the request on to httpbin.org.
Nginx doesn't use HTTP proxying because then it would need to host a certificate.

Bring up the nginx and squid

docker compose up nginx squid

Set Playwright to use Squid proxy

    proxy: {
      server: 'http://127.0.0.1:3128',
    }

And have it go to /anything on the domain which translates to httpbin.org/anything

await page.goto('https://example.com/anything');

When you run npx playwright test --headed, and it goes to https://example.com/anything, the request goes to Squid which resolves the DNS to nginx, which takes the request and sends it on to https://httpbin.org/anything. The resopnse from httpbin.org/anything shows 'example.com' as the Host header.

server {
listen 443;
#TCP traffic will be forwarded to the specified server
proxy_pass $TARGET_SERVER:$TARGET_PORT;
}
services:
squid:
image: ubuntu/squid
volumes:
- ${PWD}/squid.conf:/etc/squid/squid.conf
ports:
- "3128:3128"
extra_hosts:
- "example.com:10.1.0.100"
networks:
development:
ipv4_address: 10.1.0.31
nginx:
image: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./default.conf.template:/etc/nginx/templates/default.conf.template
ports:
- "80:80"
- "443:443"
environment:
- TARGET_SERVER=httpbin.org
- TARGET_PORT=443
networks:
development:
ipv4_address: 10.1.0.100
networks:
development:
ipam:
driver: default
config:
- subnet: 10.1.0.0/24
events {}
stream {
include /etc/nginx/conf.d/*.conf;
}
debug_options ALL,2
http_port 3128
acl http_ports port 443 80
acl ftp_ports port 21 21000 1025-65353
acl sftp_ports port 22 2222
http_access deny !http_ports !ftp_ports !sftp_ports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment