|
| 1 | +--- |
| 2 | +title: "Configure Squid as an HTTPS Forward Proxy with Authentication" |
| 3 | +date: 2024-11-21T19:11:23+08:00 |
| 4 | +draft: false |
| 5 | +summary: A guide to configure Squid as an HTTPS forward proxy with authentication to resolve malformed HTTP response issue while downloading Go modules. |
| 6 | +tags: ["Squid", "HTTPS", "Proxy", "Authentication", "Go"] |
| 7 | +categories: ["English"] |
| 8 | +--- |
| 9 | + |
| 10 | +Recently, I met an issue complaining malformed HTTP response while downloading Go modules behind an HTTPS proxy on macOS. |
| 11 | + |
| 12 | +The issue occur after I set the environment variable `https_proxy` to an **HTTPS proxy** which has an **authentication**. |
| 13 | + |
| 14 | +```shell |
| 15 | +$ export https_proxy=https://user:password@proxy_server.com:443 |
| 16 | + |
| 17 | +$ go mod download -x golang.org/x/sync@latest |
| 18 | + |
| 19 | +# get https://proxy.golang.org/golang.org/x/sync/@v/list |
| 20 | +# get https://proxy.golang.org/golang.org/x/sync/@v/list: Get "https://proxy.golang.org/golang.org/x/sync/@v/list": malformed HTTP response "\x00\x00\f\x04\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00d\x00\x04\x00\x00\xff\xff" |
| 21 | +go: module golang.org/x/sync: Get "https://proxy.golang.org/golang.org/x/sync/@v/list": malformed HTTP response "\x00\x00\f\x04\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00d\x00\x04\x00\x00\xff\xff" |
| 22 | + |
| 23 | +``` |
| 24 | + |
| 25 | +Finally, I found a workaround. |
| 26 | + |
| 27 | +1. Use Squid as an intermediate proxy server. |
| 28 | +2. Set the environment variable `https_proxy` to the address of Squid **WITHOUT** authentication. |
| 29 | +3. Configure Squid to forward requests to the original HTTPS proxy which has an authentication. |
| 30 | + |
| 31 | +## Squid Configuration |
| 32 | + |
| 33 | +### Environment |
| 34 | + |
| 35 | +* **macOS** Sequoia 15.1 |
| 36 | +* **Go** 1.23.3 |
| 37 | + |
| 38 | +### Steps |
| 39 | + |
| 40 | +1. Edit the `$(brew --prefix)/etc/squid.conf` using the following content. |
| 41 | + (**NOTE:** Remember to replace `<https_proxy_server>`, `<port>`, `<user>` and `<password>` with actual values.) |
| 42 | + |
| 43 | +```squid.conf |
| 44 | +http_port 3128 |
| 45 | +
|
| 46 | +acl local src localhost |
| 47 | +http_access allow local |
| 48 | +http_access deny all |
| 49 | +
|
| 50 | +cache_peer <https_proxy_server> parent <port> 0 no-query no-digest login=<user>:<password> ssl |
| 51 | +never_direct allow all |
| 52 | +``` |
| 53 | + |
| 54 | +2. Restart Squid. |
| 55 | + |
| 56 | +```shell |
| 57 | +# Install the launchd service |
| 58 | +# brew tap homebrew/services |
| 59 | + |
| 60 | +brew services restart squid |
| 61 | +``` |
| 62 | + |
| 63 | +3. Set the environment variable `https_proxy` to the address of Squid. |
| 64 | + |
| 65 | +```shell |
| 66 | +export https_proxy=http://127.0.0.1:3128 |
| 67 | +``` |
| 68 | + |
| 69 | +4. Test again. |
| 70 | + |
| 71 | +```shell |
| 72 | +$ go mod download -x golang.org/x/sync@latest |
| 73 | + |
| 74 | +# get https://proxy.golang.org/golang.org/x/sync/@v/list |
| 75 | +# get https://proxy.golang.org/golang.org/x/sync/@v/list: 200 OK (1.187s) |
| 76 | +# get https://proxy.golang.org/golang.org/x/sync/@v/v0.9.0.info |
| 77 | +# get https://proxy.golang.org/golang.org/x/sync/@v/v0.9.0.info: 200 OK (0.157s) |
| 78 | +# get https://proxy.golang.org/golang.org/x/sync/@v/v0.9.0.mod |
| 79 | +# get https://proxy.golang.org/golang.org/x/sync/@v/v0.9.0.mod: 200 OK (0.168s) |
| 80 | +# get https://proxy.golang.org/sumdb/sum.golang.org/supported |
| 81 | +# get https://proxy.golang.org/sumdb/sum.golang.org/supported: 404 Not Found (0.161s) |
| 82 | +# get https://sum.golang.org/lookup/golang.org/x/sync@v0.9.0 |
| 83 | +# get https://sum.golang.org/lookup/golang.org/x/sync@v0.9.0: 200 OK (1.026s) |
| 84 | +# get https://sum.golang.org/tile/8/0/x123/009 |
| 85 | +# get https://sum.golang.org/tile/8/1/488.p/229 |
| 86 | +# get https://sum.golang.org/tile/8/1/480 |
| 87 | +# get https://sum.golang.org/tile/8/3/000.p/1 |
| 88 | +# get https://sum.golang.org/tile/8/2/001.p/232 |
| 89 | +# get https://sum.golang.org/tile/8/0/x125/157.p/6 |
| 90 | +# get https://sum.golang.org/tile/8/1/488.p/229: 200 OK (0.234s) |
| 91 | +# get https://sum.golang.org/tile/8/0/x125/157.p/6: 200 OK (0.234s) |
| 92 | +# get https://sum.golang.org/tile/8/2/001.p/232: 200 OK (0.235s) |
| 93 | +# get https://sum.golang.org/tile/8/1/480: 200 OK (0.306s) |
| 94 | +# get https://sum.golang.org/tile/8/3/000.p/1: 200 OK (0.310s) |
| 95 | +# get https://sum.golang.org/tile/8/0/x123/009: 200 OK (0.310s) |
| 96 | +# get https://proxy.golang.org/golang.org/x/sync/@v/v0.9.0.zip |
| 97 | +# get https://proxy.golang.org/golang.org/x/sync/@v/v0.9.0.zip: 200 OK (0.155s) |
| 98 | +``` |
| 99 | + |
| 100 | +It works! |
| 101 | + |
0 commit comments