From 9e6c5de7a9f777ab550a0d0264f2d68656326c78 Mon Sep 17 00:00:00 2001 From: Alberts S Date: Mon, 17 Jun 2024 23:29:50 +0300 Subject: [PATCH] feat: support compression This commit adds the ability to enable compression for websockets. The compression is disabled by default due to the fact that it is marked as experimental in gorilla/websocket and there have been issues with compression whenever Safari is enabled. This commit partially addresses #30. --- CHANGELOG.md | 1 + config.sample.yaml | 1 + internal/config/config.go | 20 ++++++++++++++++---- internal/web/server.go | 9 +++++++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4669148..f39d34f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Support for websocket compression - disabled by default (#40) ### Changed ### Fixed ### Docs diff --git a/config.sample.yaml b/config.sample.yaml index 5e26d5b..c6a9866 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -6,6 +6,7 @@ webserver: domains_only_url: "/domains-only" cert_path: "" cert_key_path: "" + compression_enabled: false prometheus: enabled: true diff --git a/internal/config/config.go b/internal/config/config.go index 91385b3..4a49a14 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,10 +27,11 @@ type ServerConfig struct { type Config struct { Webserver struct { - ServerConfig `yaml:",inline"` - FullURL string `yaml:"full_url"` - LiteURL string `yaml:"lite_url"` - DomainsOnlyURL string `yaml:"domains_only_url"` + ServerConfig `yaml:",inline"` + FullURL string `yaml:"full_url"` + LiteURL string `yaml:"lite_url"` + DomainsOnlyURL string `yaml:"domains_only_url"` + CompressionEnabled *bool `yaml:"compression_enabled,omitempty"` } Prometheus struct { ServerConfig `yaml:",inline"` @@ -134,6 +135,17 @@ func validateConfig(config Config) bool { return false } + if config.Webserver.CompressionEnabled == nil { + // On one hand the Calidog certstream server does support compression by default, however + // the gorilla/websocket library has had issues with Safari clients. + // See: https://github.com/gorilla/websocket/issues/731 + // Moreover according to gorilla/weboscket documentation, the support is experimental + // See: https://pkg.go.dev/github.com/gorilla/websocket#hdr-Compression_EXPERIMENTAL + // Thus lets default to false - i.e. disable compression by default + CompressionEnabled := false + config.Webserver.CompressionEnabled = &CompressionEnabled + } + if config.Webserver.FullURL == "" || !URLRegex.MatchString(config.Webserver.FullURL) { log.Println("Webhook full URL is not set or does not match pattern '/...'") config.Webserver.FullURL = "/full-stream" diff --git a/internal/web/server.go b/internal/web/server.go index 7b1b6c0..8468d97 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -20,7 +20,7 @@ import ( var ( ClientHandler = BroadcastManager{} - upgrader = websocket.Upgrader{} // use default options + upgrader websocket.Upgrader ) type WebServer struct { @@ -248,7 +248,8 @@ func NewMetricsServer(networkIf string, port int, certPath, keyPath string) *Web } // NewWebsocketServer starts a new webserver and initialized it with the necessary routes. -// It also starts the broadcaster in ClientHandler as a background job. +// It also starts the broadcaster in ClientHandler as a background job and takes care of +// setting up websocket.Upgrader. func NewWebsocketServer(networkIf string, port int, certPath, keyPath string) *WebServer { server := &WebServer{ networkIf: networkIf, @@ -258,6 +259,10 @@ func NewWebsocketServer(networkIf string, port int, certPath, keyPath string) *W keyPath: keyPath, } + upgrader = websocket.Upgrader{ + EnableCompression: *config.AppConfig.Webserver.CompressionEnabled, + } + if config.AppConfig.Webserver.RealIP { server.routes.Use(middleware.RealIP) }