Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(lark): add proxy support for WebSocket connections (#4166)

Willow Lopezaf34b8f83a2b
diff --git a/.env.example b/.env.example
index 583f5a2f7a..296898eec6 100644
--- a/.env.example
+++ b/.env.example
@@ -255,6 +255,10 @@ MULTICA_LARK_SECRET_KEY=
# clear these afterwards. See docs/lark-bot-integration.
MULTICA_LARK_HTTP_BASE_URL=
MULTICA_LARK_CALLBACK_BASE_URL=
+# Optional fixed HTTP CONNECT proxy URL for Lark/Feishu WebSocket long-conn
+# handshakes. Leave empty to use standard HTTP_PROXY / HTTPS_PROXY / NO_PROXY
+# environment handling.
+MULTICA_LARK_WS_PROXY_URL=
# Frontend
# Leave empty — auto-derived from page origin in browser, set by Makefile for local dev.
diff --git a/server/cmd/server/router.go b/server/cmd/server/router.go
index 1bddb1ae76..5ce9e43ce4 100644
--- a/server/cmd/server/router.go
+++ b/server/cmd/server/router.go
@@ -1041,6 +1041,9 @@ func buildLarkConnectorFactory(installSvc *lark.InstallationService, apiClient l
}
decoder := lark.NewLarkJSONFrameDecoder()
dialer := lark.NewGorillaDialer()
+ if proxyURL := strings.TrimSpace(os.Getenv("MULTICA_LARK_WS_PROXY_URL")); proxyURL != "" {
+ dialer.ProxyURL = proxyURL
+ }
credsProvider := lark.CredentialsProviderFunc(func(ctx context.Context, inst lark.Installation) (lark.InstallationCredentials, error) {
secret, err := installSvc.DecryptAppSecret(inst)
if err != nil {
diff --git a/server/internal/integrations/lark/ws_connector.go b/server/internal/integrations/lark/ws_connector.go
index e2b3e54962..52367f2e8f 100644
--- a/server/internal/integrations/lark/ws_connector.go
+++ b/server/internal/integrations/lark/ws_connector.go
@@ -541,6 +541,10 @@ type GorillaDialer struct {
// (e.g. for custom proxy auth or a fixed proxy URL). To disable proxy
// entirely, pass a func that returns (nil, nil).
Proxy func(*http.Request) (*url.URL, error)
+
+ // ProxyURL is an optional fixed HTTP CONNECT proxy URL for the
+ // WebSocket handshake. When set, it takes precedence over Proxy.
+ ProxyURL string
}
func NewGorillaDialer() *GorillaDialer {
@@ -558,7 +562,13 @@ func (g *GorillaDialer) DialContext(ctx context.Context, urlStr string, requestH
}
// Shallow copy so we don't mutate the shared dialer's Proxy field.
dd := *d
- if g.Proxy != nil {
+ if g.ProxyURL != "" {
+ proxyURL, err := url.Parse(g.ProxyURL)
+ if err != nil {
+ return nil, nil, fmt.Errorf("lark ws dialer: parse proxy url %q: %w", g.ProxyURL, err)
+ }
+ dd.Proxy = http.ProxyURL(proxyURL)
+ } else if g.Proxy != nil {
dd.Proxy = g.Proxy
}
if dd.Proxy == nil {
diff --git a/server/internal/integrations/lark/ws_connector_test.go b/server/internal/integrations/lark/ws_connector_test.go
index f323b005d1..f8f65fe32d 100644
--- a/server/internal/integrations/lark/ws_connector_test.go
+++ b/server/internal/integrations/lark/ws_connector_test.go
@@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strconv"
+ "strings"
"sync"
"sync/atomic"
"testing"
@@ -747,3 +748,58 @@ func TestWSConnectorCredentialsErrorIsReturned(t *testing.T) {
t.Fatalf("expected wrapped credentials error, got %v", err)
}
}
+
+func TestGorillaDialerInvalidProxyURL(t *testing.T) {
+ t.Parallel()
+ d := &GorillaDialer{
+ Dialer: websocket.DefaultDialer,
+ ProxyURL: "://invalid-url",
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
+ defer cancel()
+ _, _, err := d.DialContext(ctx, "wss://example.com/ws", nil)
+ if err == nil {
+ t.Fatal("expected error for invalid proxy URL")
+ }
+ if !strings.Contains(err.Error(), "parse proxy url") {
+ t.Fatalf("expected parse proxy url error, got: %v", err)
+ }
+}
+
+func TestGorillaDialerProxyURLApplied(t *testing.T) {
+ t.Parallel()
+ // Use a valid proxy URL that points to a non-listening address.
+ // The dial should fail with a connection error, not a parse error,
+ // proving the proxy was configured.
+ d := &GorillaDialer{
+ Dialer: websocket.DefaultDialer,
+ ProxyURL: "http://127.0.0.1:1",
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
+ defer cancel()
+ _, _, err := d.DialContext(ctx, "wss://example.com/ws", nil)
+ if err == nil {
+ t.Fatal("expected connection error when proxy is unreachable")
+ }
+ if strings.Contains(err.Error(), "parse proxy url") {
+ t.Fatalf("valid proxy URL should not produce parse error, got: %v", err)
+ }
+ // The error should be about the proxy connection (refused / timeout),
+ // not about the URL parsing.
+}
+
+func TestGorillaDialerEmptyProxyURLDefaultsToEnv(t *testing.T) {
+ t.Parallel()
+ d := NewGorillaDialer()
+ if d.ProxyURL != "" {
+ t.Fatalf("NewGorillaDialer ProxyURL should be empty, got %q", d.ProxyURL)
+ }
+ // DialContext with empty ProxyURL should not panic or error on the
+ // proxy path — the underlying gorilla dialer uses ProxyFromEnvironment.
+ ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
+ defer cancel()
+ _, _, _ = d.DialContext(ctx, "wss://127.0.0.1:1/ws", nil)
+ // We don't assert on the error (it'll be a connection error), we just
+ // verify that DialContext with empty ProxyURL doesn't panic or return
+ // a parse error.
+}