Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

feat(proxy): add WHATSAPP_PROXY for whatsmeow outbound proxy (#664)

FlowMind36aaafaf5e22
diff --git a/readme.md b/readme.md
index 1c95c40a..6ebbe215 100644
--- a/readme.md
+++ b/readme.md
@@ -232,6 +232,7 @@ To use environment variables:
| `WHATSAPP_WEBHOOK_IGNORE_JIDS` | JIDs/wildcards to skip when forwarding (comma-separated) | - | `[email protected]` |
| `WHATSAPP_ACCOUNT_VALIDATION` | Enable account validation | `true` | `WHATSAPP_ACCOUNT_VALIDATION=false` |
| `WHATSAPP_PRESENCE_ON_CONNECT` | Presence on connect: `available`, `unavailable`, or `none` | `unavailable` | `WHATSAPP_PRESENCE_ON_CONNECT=unavailable` |
+| `WHATSAPP_PROXY` | Outbound proxy for the WhatsApp WebSocket (socks5/http/https) | - | `WHATSAPP_PROXY=socks5://user:pass@host:1080` |
| `WHATSAPP_PRESENCE_PULSE_ENABLED` | Enable daily available/unavailable presence pulse | `true` | `WHATSAPP_PRESENCE_PULSE_ENABLED=false` |
| `WHATSAPP_PRESENCE_PULSE_INTERVAL` | Interval between presence pulses | `24h` | `WHATSAPP_PRESENCE_PULSE_INTERVAL=24h` |
| `WHATSAPP_PRESENCE_PULSE_DURATION` | Duration to stay available during each pulse | `5m` | `WHATSAPP_PRESENCE_PULSE_DURATION=5m` |
diff --git a/src/.env.example b/src/.env.example
index b5208de0..ffbb4930 100644
--- a/src/.env.example
+++ b/src/.env.example
@@ -33,6 +33,12 @@ WHATSAPP_PRESENCE_PULSE_ENABLED=true
WHATSAPP_PRESENCE_PULSE_INTERVAL=24h
WHATSAPP_PRESENCE_PULSE_DURATION=5m
WHATSAPP_CHAT_STORAGE=true
+# Outbound proxy applied to whatsmeow's WebSocket dialer via SetProxyAddress.
+# Standard HTTP_PROXY env does not apply to the underlying ws connection.
+# Useful for self-hosted deploys behind DPI / network egress restrictions.
+# Supports SOCKS5/HTTP/HTTPS schemes. Empty = direct connection.
+# Example: WHATSAPP_PROXY=socks5://user:[email protected]:1080
+WHATSAPP_PROXY=
# Chatwoot Integration
CHATWOOT_ENABLED=false
diff --git a/src/cmd/root.go b/src/cmd/root.go
index 91f132c0..23c528d3 100644
--- a/src/cmd/root.go
+++ b/src/cmd/root.go
@@ -163,6 +163,12 @@ func initEnvConfig() {
if envPresenceOnConnect := viper.GetString("whatsapp_presence_on_connect"); envPresenceOnConnect != "" {
config.WhatsappPresenceOnConnect = envPresenceOnConnect
}
+ // Outbound proxy for whatsmeow WebSocket. Standard HTTP_PROXY env does not
+ // apply to the underlying ws dialer; this binding plumbs the address into
+ // (*whatsmeow.Client).SetProxyAddress before Connect. See issue #581.
+ if envProxy := viper.GetString("whatsapp_proxy"); envProxy != "" {
+ config.WhatsappProxy = envProxy
+ }
if viper.IsSet("whatsapp_presence_pulse_enabled") {
config.WhatsappPresencePulseEnabled = viper.GetBool("whatsapp_presence_pulse_enabled")
}
@@ -392,6 +398,12 @@ func initFlags() {
config.WhatsappPresenceOnConnect,
`presence to send on connect: "available", "unavailable", or "none" --presence-on-connect <string> | example: --presence-on-connect="unavailable"`,
)
+ rootCmd.PersistentFlags().StringVarP(
+ &config.WhatsappProxy,
+ "whatsapp-proxy", "",
+ config.WhatsappProxy,
+ `outbound proxy for the WhatsApp WebSocket dialer --whatsapp-proxy <string> | example: --whatsapp-proxy="socks5://user:pass@host:1080"`,
+ )
rootCmd.PersistentFlags().BoolVarP(
&config.WhatsappPresencePulseEnabled,
"presence-pulse-enabled", "",
diff --git a/src/config/settings.go b/src/config/settings.go
index a464d3b2..e5ecad59 100644
--- a/src/config/settings.go
+++ b/src/config/settings.go
@@ -52,6 +52,14 @@ var (
WhatsappPresencePulseInterval = 24 * time.Hour
WhatsappPresencePulseDuration = 5 * time.Minute
+ // WhatsappProxy is forwarded to whatsmeow's *Client.SetProxyAddress before
+ // Connect. Accepts SOCKS5/HTTP/HTTPS schemes, e.g.
+ // "socks5://user:pass@host:1080" or "http://host:8080". Empty = direct
+ // (no proxy). Useful for self-hosted deployments behind DPI / network
+ // egress restrictions where standard HTTP_PROXY env vars do not apply
+ // to the WhatsApp WebSocket dialer.
+ WhatsappProxy = ""
+
ChatStorageURI = "file:storages/chatstorage.db"
ChatStorageEnableForeignKeys = true
ChatStorageEnableWAL = true
diff --git a/src/infrastructure/whatsapp/device_manager.go b/src/infrastructure/whatsapp/device_manager.go
index 47a995d2..e2cd3f1e 100644
--- a/src/infrastructure/whatsapp/device_manager.go
+++ b/src/infrastructure/whatsapp/device_manager.go
@@ -615,6 +615,13 @@ func (m *DeviceManager) EnsureClient(ctx context.Context, deviceID string) (*Dev
baseLogger := waLog.Stdout(fmt.Sprintf("Client-%s", deviceID), config.WhatsappLogLevel, true)
client := whatsmeow.NewClient(storeDevice, newFilteredLogger(baseLogger))
+ if proxyURL := config.WhatsappProxy; proxyURL != "" {
+ if err := client.SetProxyAddress(proxyURL); err != nil {
+ baseLogger.Errorf("failed to apply WHATSAPP_PROXY=%q for device %s: %v", redactProxyURL(proxyURL), deviceID, err)
+ } else {
+ baseLogger.Infof("applied outbound proxy from WHATSAPP_PROXY for device %s", deviceID)
+ }
+ }
client.EnableAutoReconnect = true
client.AutoTrustIdentity = true
diff --git a/src/infrastructure/whatsapp/init.go b/src/infrastructure/whatsapp/init.go
index 0a3b5826..c6257ec5 100644
--- a/src/infrastructure/whatsapp/init.go
+++ b/src/infrastructure/whatsapp/init.go
@@ -103,6 +103,13 @@ func InitWaCLI(ctx context.Context, storeContainer, keysStoreContainer *sqlstore
// Create and configure the client with filtered logging to avoid noisy reconnection EOF errors
baseLogger := waLog.Stdout("Client", config.WhatsappLogLevel, true)
client := whatsmeow.NewClient(device, newFilteredLogger(baseLogger))
+ if proxyURL := config.WhatsappProxy; proxyURL != "" {
+ if err := client.SetProxyAddress(proxyURL); err != nil {
+ baseLogger.Errorf("failed to apply WHATSAPP_PROXY=%q: %v", redactProxyURL(proxyURL), err)
+ } else {
+ baseLogger.Infof("applied outbound proxy from WHATSAPP_PROXY")
+ }
+ }
client.EnableAutoReconnect = true
client.AutoTrustIdentity = true
diff --git a/src/infrastructure/whatsapp/proxy.go b/src/infrastructure/whatsapp/proxy.go
new file mode 100644
index 00000000..40a46d78
--- /dev/null
+++ b/src/infrastructure/whatsapp/proxy.go
@@ -0,0 +1,14 @@
+package whatsapp
+
+import "net/url"
+
+// redactProxyURL returns a form of the proxy URL safe for logging: the
+// password (and any unparseable URL entirely) is masked so credentials from
+// WHATSAPP_PROXY never reach the logs.
+func redactProxyURL(raw string) string {
+ u, err := url.Parse(raw)
+ if err != nil {
+ return "<unparseable-proxy-url>"
+ }
+ return u.Redacted()
+}