Skip to content

Commit f9d25ba

Browse files
committed
inject postgresql ALPN for tcp proxies
1 parent 1489c67 commit f9d25ba

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

internal/proxy/caddy_l4.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,35 @@ import (
1111
"github.com/mholt/caddy-l4/modules/l4tls"
1212
)
1313

14-
func mustL4Handler(name string, h any) json.RawMessage {
14+
func mustL4Handler(name string, h any, alpn []string) json.RawMessage {
1515
b, err := json.Marshal(h)
1616
if err != nil {
1717
panic(err)
1818
}
1919
var m map[string]any
2020
json.Unmarshal(b, &m)
2121
m["handler"] = name
22+
if alpn != nil {
23+
injectALPN(m, alpn)
24+
}
2225
out, _ := json.Marshal(m)
2326
return out
2427
}
2528

29+
var l4ALPN = []string{"postgresql"}
30+
31+
func injectALPN(m map[string]any, alpn []string) {
32+
policies, ok := m["connection_policies"].([]any)
33+
if !ok {
34+
return
35+
}
36+
for _, p := range policies {
37+
if pol, ok := p.(map[string]any); ok {
38+
pol["alpn"] = alpn
39+
}
40+
}
41+
}
42+
2643
func BuildL4App(routes []Route) *layer4.App {
2744
type portRoutes struct {
2845
routes []Route
@@ -84,8 +101,8 @@ func BuildL4App(routes []Route) *layer4.App {
84101
{"tls": sniJSON},
85102
},
86103
HandlersRaw: []json.RawMessage{
87-
mustL4Handler("tls", tlsHandler),
88-
mustL4Handler("proxy", proxyHandler),
104+
mustL4Handler("tls", tlsHandler, l4ALPN),
105+
mustL4Handler("proxy", proxyHandler, nil),
89106
},
90107
}
91108

@@ -101,7 +118,7 @@ func BuildL4App(routes []Route) *layer4.App {
101118
}
102119
fallbackRoute := &layer4.Route{
103120
HandlersRaw: []json.RawMessage{
104-
mustL4Handler("proxy", fallbackProxy),
121+
mustL4Handler("proxy", fallbackProxy, nil),
105122
},
106123
}
107124
l4Routes = append(l4Routes, fallbackRoute)

0 commit comments

Comments
 (0)