Skip to content

Commit 11882b0

Browse files
authored
feat(lite): Add support for hostname parameter routing in backend addresses (minekube#603)
- Introduced the ability to use wildcard parameters in backend addresses, allowing dynamic routing based on hostname segments. - Updated configuration files to include examples of the new routing feature. - Enhanced documentation to explain the usage of hostname parameters and provide real-world examples. - Implemented tests to validate the new parameter substitution functionality and ensure correct behavior in various scenarios.
1 parent f78c743 commit 11882b0

14 files changed

Lines changed: 755 additions & 162 deletions

File tree

.web/docs/guide/lite.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,89 @@ config:
4545
backend: [10.0.0.2:25566]
4646
```
4747
48+
## Hostname Parameter Routing
49+
50+
Gate Lite supports extracting parts of hostnames using wildcard patterns and using them in backend addresses via `$1`, `$2`, etc. parameters. This enables dynamic routing where parts of the hostname are used to construct the backend address.
51+
52+
:::: code-group
53+
54+
```yaml [Basic Usage]
55+
lite:
56+
routes:
57+
# Extract subdomain and use it in backend address
58+
- host: '*.domain.com'
59+
backend: '$1.servers.svc:25565'
60+
# Example: abc.domain.com → abc.servers.svc:25565
61+
```
62+
63+
```yaml [Multiple Parameters]
64+
lite:
65+
routes:
66+
# Capture multiple parts
67+
- host: '*.*.example.com'
68+
backend: '$1-$2.servers.svc:25565'
69+
# Example: abc.def.example.com → abc-def.servers.svc:25565
70+
71+
# Use parameters in different order
72+
- host: '*.subdomain.*'
73+
backend: '$2.$1.backend:25565'
74+
# Example: abc.subdomain.com → com.abc.backend:25565
75+
```
76+
77+
```yaml [Question Mark Wildcard]
78+
lite:
79+
routes:
80+
# Using ? for single character matching
81+
- host: '?.example.com'
82+
backend: 'server-$1:25565'
83+
# Example: a.example.com → server-a:25565
84+
```
85+
86+
```yaml [Real-World Example]
87+
lite:
88+
routes:
89+
# Route abc.domain.com to abc.servers.svc:25565
90+
- host: '*.domain.com'
91+
backend: '$1.servers.svc:25565'
92+
93+
# Route abc.def.domain.com to abc-def.servers.svc:25565
94+
- host: '*.*.domain.com'
95+
backend: '$1-$2.servers.svc:25565'
96+
```
97+
98+
::::
99+
100+
### Parameter Indexing
101+
102+
- `$1` refers to the first wildcard match (`*` or `?`)
103+
- `$2` refers to the second wildcard match
104+
- `$3` refers to the third wildcard match
105+
- And so on...
106+
107+
Wildcards are numbered in the order they appear in the pattern from left to right.
108+
109+
### Wildcard Types
110+
111+
- `*` matches any sequence of characters (including empty) and captures it
112+
- `?` matches any single character and captures it
113+
114+
### Edge Cases
115+
116+
- If a parameter index is out of range (e.g., `$99` when only 2 groups are captured), it remains as-is in the backend address
117+
- If no wildcards are present in the pattern, parameters in the backend address are not substituted
118+
- Empty captures (e.g., `*` matching empty string) result in empty strings in the backend address
119+
120+
::: tip Config Validation
121+
122+
Gate validates your configuration and will warn you about invalid parameter usage:
123+
124+
- **Parameters without wildcards**: If you use `$1` in a backend address but the host pattern has no wildcards, Gate will warn that parameters won't be substituted
125+
- **Out-of-range parameters**: If you use `$2` but the pattern only has one wildcard, Gate will warn that the parameter exceeds available wildcards
126+
127+
These are warnings, not errors - your configuration will still work, but parameters will remain as literal text (e.g., `$1.servers.svc:25565` instead of being substituted).
128+
129+
:::
130+
48131
## Load Balancing Strategies
49132

50133
When multiple backends are configured, Gate Lite can distribute connections using different strategies.
@@ -274,4 +357,4 @@ If you use Lite mode and your backend servers do player authentication,
274357
you do not need to worry.
275358

276359
Checkout the [Anti-DDoS](/guide/security/ddos) guide for how
277-
to protect your Minecraft servers from DDoS attacks.
360+
to protect your Minecraft servers from DDoS attacks.

config-lite.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ config:
4545
backend: 172.16.0.12:25566
4646
proxyProtocol: true # Use proxy protocol to connect to backend.
4747
tcpShieldRealIP: true # Optionally you can also use TCPShield's RealIP protocol.
48+
# Use hostname parameters in backend addresses with $1, $2, etc.
49+
# Example: abc.domain.com → abc.servers.svc:25565
50+
- host: '*.domain.com'
51+
backend: '$1.servers.svc:25565'
4852
# You can also match to multiple hosts to one or multiple backends.
4953
- host: [127.0.0.1, localhost]
5054
backend: [172.16.0.12:25566, backend.example.com:25566]

config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ config:
185185
backend: 172.16.0.12:25566
186186
proxyProtocol: true # Use proxy protocol to connect to backend.
187187
tcpShieldRealIP: true # Optionally you can also use TCPShield's RealIP protocol.
188+
# Use hostname parameters in backend addresses with $1, $2, etc.
189+
# Extract parts of the hostname and use them in the backend address.
190+
# Example: abc.domain.com → abc.servers.svc:25565
191+
- host: '*.domain.com'
192+
backend: '$1.servers.svc:25565'
188193
# You can also match to multiple hosts to one or multiple backends.
189194
- host: [127.0.0.1, localhost]
190195
backend: [172.16.0.12:25566, backend.example.com:25566]

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ require (
5252

5353
require (
5454
buf.build/gen/go/minekube/connect/protocolbuffers/go v1.36.10-20240220124425-904ce30425c9.1 // indirect
55-
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
5655
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
5756
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
5857
github.com/ebitengine/purego v0.9.0 // indirect
@@ -77,8 +76,6 @@ require (
7776
github.com/segmentio/fasthash v1.0.3 // indirect
7877
github.com/sethvargo/go-envconfig v1.3.0 // indirect
7978
github.com/shirou/gopsutil/v4 v4.25.9 // indirect
80-
github.com/shoenig/go-m1cpu v0.1.7 // indirect
81-
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
8279
github.com/spf13/afero v1.15.0 // indirect
8380
github.com/spf13/cast v1.10.0 // indirect
8481
github.com/spf13/pflag v1.0.10 // indirect

0 commit comments

Comments
 (0)