Skip to content

Commit 1887a70

Browse files
committed
proxy protocol support
1 parent a1abe7e commit 1887a70

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

apps/browser-proxy/fly.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ primary_region = 'iad'
66
internal_port = 5432
77
protocol = "tcp"
88
[[services.ports]]
9+
handlers = ["proxy_proto"]
910
port = 5432
1011

1112
[[services]]

apps/browser-proxy/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dependencies": {
99
"@aws-sdk/client-s3": "^3.645.0",
1010
"debug": "^4.3.7",
11+
"findhit-proxywrap": "^0.3.13",
1112
"pg-gateway": "^0.3.0-alpha.7",
1213
"ws": "^8.18.0"
1314
},

apps/browser-proxy/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as net from 'node:net'
1+
import * as nodeNet from 'node:net'
22
import * as https from 'node:https'
33
import { PostgresConnection } from 'pg-gateway'
44
import { WebSocketServer, type WebSocket } from 'ws'
@@ -11,7 +11,7 @@ import { extractIP } from './extract-ip.ts'
1111

1212
const debug = makeDebug('browser-proxy')
1313

14-
const tcpConnections = new Map<string, net.Socket>()
14+
const tcpConnections = new Map<string, nodeNet.Socket>()
1515
const websocketConnections = new Map<string, WebSocket>()
1616

1717
let tlsOptions = await getTls()
@@ -79,6 +79,11 @@ websocketServer.on('connection', (socket, request) => {
7979
})
8080
})
8181

82+
// we need to use proxywrap to make our tcp server to enable the PROXY protocol support
83+
const net = (
84+
process.env.FLY_APP_NAME ? (await import('findhit-proxywrap')).default.proxy(nodeNet) : nodeNet
85+
) as typeof nodeNet
86+
8287
const tcpServer = net.createServer()
8388

8489
tcpServer.on('connection', (socket) => {
@@ -144,7 +149,7 @@ tcpServer.on('connection', (socket) => {
144149

145150
const clientIpMessage = createParameterStatusMessage(
146151
'client_ip',
147-
extractIP(connection.socket.remoteAddress!)
152+
extractIP(socket.remoteAddress!)
148153
)
149154
websocket.send(clientIpMessage)
150155
},

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)