Repository: gotenberg/gotenberg (go)

Issue:
Support Proxy Server for Chromium Headless
Hi

Is there any way to support a proxy server for the chromium module? I have tried setting the system proxy, but that doesn't seem to do the trick. So there's still the `--proxy-server` flag for `chromium` but I guess that's not supported, is it?

Pull request:
feat: add --chromium-proxy-server property for the Chromium module
Closes #376.

Also:

* Fixe an issue with the `--chromium-user-agent` property; it was not correctly bound internally (sigh).
* Chore: update Go dependencies.

Patch:
diff --git a/Makefile b/Makefile
index d21a286..70a8ce0 100644
--- a/Makefile
+++ b/Makefile
@@ -39,6 +39,7 @@ CHROMIUM_USER_AGENT=
 CHROMIUM_INCOGNITO=false
 CHROMIUM_IGNORE_CERTIFICATE_ERRORS=false
 CHROMIUM_ALLOW_FILE_ACCESS_FROM_FILES=false
+CHROMIUM_PROXY_SERVER=
 CHROMIUM_ALLOW_LIST=
 CHROMIUM_DENY_LIST="^file:///[^tmp].*"
 CHROMIUM_DISABLE_ROUTES=false
@@ -79,6 +80,7 @@ run: ## Start a Gotenberg container
 	--chromium-incognito=$(CHROMIUM_INCOGNITO) \
 	--chromium-ignore-certificate-errors=$(CHROMIUM_IGNORE_CERTIFICATE_ERRORS) \
 	--chromium-allow-file-access-from-files=$(CHROMIUM_ALLOW_FILE_ACCESS_FROM_FILES) \
+	--chromium-proxy-server=$(CHROMIUM_PROXY_SERVER) \
 	--chromium-allow-list=$(CHROMIUM_ALLOW_LIST) \
 	--chromium-deny-list=$(CHROMIUM_DENY_LIST) \
 	--chromium-disable-routes=$(CHROMIUM_DISABLE_ROUTES) \
diff --git a/go.mod b/go.mod
index 1e70e73..287c39e 100644
--- a/go.mod
+++ b/go.mod
@@ -20,7 +20,7 @@ require (
 	github.com/microcosm-cc/bluemonday v1.0.16
 	github.com/nwaples/rardecode v1.1.2 // indirect
 	github.com/pdfcpu/pdfcpu v0.3.12
-	github.com/pierrec/lz4/v4 v4.1.9 // indirect
+	github.com/pierrec/lz4/v4 v4.1.10 // indirect
 	github.com/prometheus/client_golang v1.11.0
 	github.com/russross/blackfriday/v2 v2.1.0
 	github.com/spf13/pflag v1.0.5
diff --git a/go.sum b/go.sum
index b60f719..86ac22f 100644
--- a/go.sum
+++ b/go.sum
@@ -231,8 +231,8 @@ github.com/orisano/pixelmatch v0.0.0-20210112091706-4fa4c7ba91d5/go.mod h1:nZgzb
 github.com/pdfcpu/pdfcpu v0.3.12 h1:B+MdKisilWNSk5OCO58Z9U6H93usH73xqk6hMOaZCls=
 github.com/pdfcpu/pdfcpu v0.3.12/go.mod h1:8XVBtVxuuIuSZL4Ez15Q4QoC+H8zeAaGnuiOEwAk8jA=
 github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
-github.com/pierrec/lz4/v4 v4.1.9 h1:xkrjwpOP5xg1k4Nn4GX4a4YFGhscyQL/3EddJ1Xxqm8=
-github.com/pierrec/lz4/v4 v4.1.9/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
+github.com/pierrec/lz4/v4 v4.1.10 h1:H0LgOg/8kTVhiN8ESXFa+gvt9udNp1hQ+mYNDdcMPTM=
+github.com/pierrec/lz4/v4 v4.1.10/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
diff --git a/pkg/modules/chromium/chromium.go b/pkg/modules/chromium/chromium.go
index 0ed0d81..5622c8b 100644
--- a/pkg/modules/chromium/chromium.go
+++ b/pkg/modules/chromium/chromium.go
@@ -52,6 +52,7 @@ type Chromium struct {
 	incognito                bool
 	ignoreCertificateErrors  bool
 	allowFileAccessFromFiles bool
+	proxyServer              string
 	allowList                *regexp.Regexp
 	denyList                 *regexp.Regexp
 	disableRoutes            bool
@@ -186,6 +187,7 @@ func (mod Chromium) Descriptor() gotenberg.ModuleDescriptor {
 			fs.Bool("chromium-incognito", false, "Start Chromium with incognito mode")
 			fs.Bool("chromium-ignore-certificate-errors", false, "Ignore the certificate errors")
 			fs.Bool("chromium-allow-file-access-from-files", false, "Allow file:// URIs to read other file:// URIs")
+			fs.String("chromium-proxy-server", "", "Set the outbound proxy server; this switch only affects HTTP and HTTPS requests")
 			fs.String("chromium-allow-list", "", "Set the allowed URLs for Chromium using a regular expression")
 			fs.String("chromium-deny-list", "^file:///[^tmp].*", "Set the denied URLs for Chromium using a regular expression")
 			fs.Bool("chromium-disable-routes", false, "Disable the routes")
@@ -199,8 +201,10 @@ func (mod Chromium) Descriptor() gotenberg.ModuleDescriptor {
 // Provision sets the module properties.
 func (mod *Chromium) Provision(ctx *gotenberg.Context) error {
 	flags := ctx.ParsedFlags()
+	mod.userAgent = flags.MustString("chromium-user-agent")
 	mod.ignoreCertificateErrors = flags.MustBool("chromium-ignore-certificate-errors")
 	mod.allowFileAccessFromFiles = flags.MustBool("chromium-allow-file-access-from-files")
+	mod.proxyServer = flags.MustString("chromium-proxy-server")
 	mod.allowList = flags.MustRegexp("chromium-allow-list")
 	mod.denyList = flags.MustRegexp("chromium-deny-list")
 	mod.disableRoutes = flags.MustBool("chromium-disable-routes")
@@ -312,6 +316,11 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath
 		args = append(args, chromedp.Flag("allow-file-access-from-files", true))
 	}
 
+	if mod.proxyServer != "" {
+		// See https://github.com/gotenberg/gotenberg/issues/376.
+		args = append(args, chromedp.ProxyServer(mod.proxyServer))
+	}
+
 	allocatorCtx, cancel := chromedp.NewExecAllocator(ctx, args...)
 	defer cancel()
 


Test patch:
diff --git a/pkg/modules/chromium/chromium_test.go b/pkg/modules/chromium/chromium_test.go
index 49521ba..12bc4fe 100644
--- a/pkg/modules/chromium/chromium_test.go
+++ b/pkg/modules/chromium/chromium_test.go
@@ -234,6 +234,7 @@ func TestChromium_PDF(t *testing.T) {
 		incognito                bool
 		ignoreCertificateErrors  bool
 		allowFileAccessFromFiles bool
+		proxyServer              string
 		allowList                *regexp.Regexp
 		denyList                 *regexp.Regexp
 		expectErr                bool
@@ -297,6 +298,7 @@ func TestChromium_PDF(t *testing.T) {
 			incognito:                true,
 			ignoreCertificateErrors:  true,
 			allowFileAccessFromFiles: true,
+			proxyServer:              "foo",
 		},
 		{
 			URL: "file:///tests/test/testdata/chromium/html/sample1/index.html",
@@ -348,6 +350,7 @@ func TestChromium_PDF(t *testing.T) {
 			mod.incognito = tc.incognito
 			mod.ignoreCertificateErrors = tc.ignoreCertificateErrors
 			mod.allowFileAccessFromFiles = tc.allowFileAccessFromFiles
+			mod.proxyServer = tc.proxyServer
 
 			if tc.allowList == nil {
 				tc.allowList = regexp.MustCompile("")