Skip to content

Commit df3fd7b

Browse files
committed
Wrap with haproxy rate limiting
1 parent 447a310 commit df3fd7b

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
FROM node:24-alpine
22

3+
RUN apk update && apk add --no-cache haproxy bash
4+
35
WORKDIR /usr/src/app
46

57
RUN mkdir /usr/src/app/cert_dir
68
ENV CERT_CACHE_DIR=/usr/src/app/cert_dir
79

810
COPY package.json package-lock.json ./
9-
1011
RUN npm ci --omit=dev
1112

1213
COPY src/ src/
1314

15+
COPY haproxy.cfg /etc/haproxy/haproxy.cfg
16+
COPY start.sh ./
17+
RUN chmod +x start.sh
18+
1419
ARG GIT_HASH
1520
ENV VERSION_HASH=${GIT_HASH}
16-
CMD ["npm", "start"]
21+
22+
CMD ["./start.sh"]

fly.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ primary_region = 'cdg'
77
image = 'httptoolkit/testserver:latest'
88

99
[env]
10-
PORTS = '8080'
10+
PORTS = '3000'
1111
CERT_CACHE_DIR = '/usr/src/app/cert_dir'
1212

1313
# If deploying a separate instance and wanting real certificates, customize these:
@@ -23,11 +23,14 @@ primary_region = 'cdg'
2323

2424
[[services.ports]]
2525
port = "80"
26+
handlers = ["proxy_proto"]
2627
[[services.ports]]
2728
port = "443"
29+
handlers = ["proxy_proto"]
2830
[[services.ports]]
2931
start_port = 8000
3032
end_port = 10000
33+
handlers = ["proxy_proto"]
3134

3235
# Continually check /echo can return a valid HTTPS response
3336
[[services.http_checks]]

haproxy.cfg

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
global
2+
log stdout format raw local0
3+
4+
defaults
5+
mode tcp
6+
log global
7+
timeout client 10m
8+
timeout server 10m
9+
timeout connect 5s
10+
11+
frontend public_shield
12+
bind *:8080 accept-proxy
13+
14+
# Track IP connection rate (100k IP storage, expires in 30s)
15+
stick-table type ip size 100k expire 30s store conn_rate(5s)
16+
tcp-request connection track-sc0 src
17+
18+
# Block if >100 connections in 5 seconds
19+
tcp-request connection reject if { sc0_conn_rate gt 100 }
20+
21+
default_backend node_app
22+
23+
backend node_app
24+
# Forward to Node on localhost
25+
server local_node 127.0.0.1:3000

start.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
npm start &
4+
NODE_PID=$!
5+
6+
haproxy -f /etc/haproxy/haproxy.cfg &
7+
HAPROXY_PID=$!
8+
9+
wait -n
10+
exit $?

0 commit comments

Comments
 (0)