Add support for requesting ngrok remote address (#436)

This commit is contained in:
Geoff Bourne
2025-07-25 17:13:07 -06:00
committed by GitHub
parent c5c3fba846
commit 443beb2cce
4 changed files with 34 additions and 9 deletions
+12
View File
@@ -66,6 +66,8 @@ Routes Minecraft client connections to backend servers based upon the requested
any extra tags to be included with all reported metrics (env METRICS_BACKEND_CONFIG_INFLUXDB_TAGS)
-metrics-backend-config-influxdb-username string
(env METRICS_BACKEND_CONFIG_INFLUXDB_USERNAME)
-ngrok-remote-addr string
If set, the TCP address to request for this edge (env NGROK_REMOTE_ADDR)
-ngrok-token string
If set, an ngrok tunnel will be established. It is HIGHLY recommended to pass as an environment variable. (env NGROK_TOKEN)
-port port
@@ -353,6 +355,16 @@ metadata:
name: mc-forge
spec:
serviceName: mc-forge
selector:
matchLabels:
app: mc-forge
template:
metadata:
labels:
app: mc-forge
spec:
containers:
- name: mc
```
## REST API
+6 -1
View File
@@ -17,6 +17,11 @@ type RoutesConfig struct {
ConfigWatch bool `usage:"Watch for config file changes"`
}
type NgrokConfig struct {
Token string `usage:"If set, an ngrok tunnel will be established. It is HIGHLY recommended to pass as an environment variable."`
RemoteAddr string `usage:"If set, the TCP address to request for this edge"`
}
type Config struct {
Port int `default:"25565" usage:"The [port] bound to listen for Minecraft client connections"`
Default string `usage:"host:port of a default Minecraft server to use when mapping not found"`
@@ -39,7 +44,7 @@ type Config struct {
TrustedProxies []string `usage:"Comma delimited list of CIDR notation IP blocks to trust when receiving PROXY protocol"`
RecordLogins bool `default:"false" usage:"Log and generate metrics on player logins. Metrics only supported with influxdb or prometheus backend"`
Routes RoutesConfig
NgrokToken string `usage:"If set, an ngrok tunnel will be established. It is HIGHLY recommended to pass as an environment variable."`
Ngrok NgrokConfig
AutoScale AutoScale
ClientsToAllow []string `usage:"Zero or more client IP addresses or CIDRs to allow. Takes precedence over deny."`
+14 -6
View File
@@ -80,6 +80,11 @@ func NewConnector(ctx context.Context, metrics *ConnectorMetrics, sendProxyProto
}
}
type NgrokConnector struct {
token string
remoteAddr string
}
type Connector struct {
ctx context.Context
state mcproto.State
@@ -91,7 +96,7 @@ type Connector struct {
totalActiveConnections int32
activeConnections *ActiveConnections
connectionsCond *sync.Cond
ngrokToken string
ngrok NgrokConnector
clientFilter *ClientFilter
autoScaleUpAllowDenyConfig *AllowDenyConfig
connectionNotifier ConnectionNotifier
@@ -117,10 +122,12 @@ func (c *Connector) StartAcceptingConnections(listenAddress string, connRateLimi
}
func (c *Connector) createListener(listenAddress string) (net.Listener, error) {
if c.ngrokToken != "" {
if c.ngrok.token != "" {
ngrokTun, err := ngrok.Listen(c.ctx,
config.TCPEndpoint(),
ngrok.WithAuthtoken(c.ngrokToken),
config.TCPEndpoint(
config.WithRemoteAddr(c.ngrok.remoteAddr),
),
ngrok.WithAuthtoken(c.ngrok.token),
)
if err != nil {
logrus.WithError(err).Fatal("Unable to start ngrok tunnel")
@@ -608,8 +615,9 @@ func (c *Connector) pumpFrames(incoming io.Reader, outgoing io.Writer, errors ch
}
}
func (c *Connector) UseNgrok(token string) {
c.ngrokToken = token
func (c *Connector) UseNgrok(config NgrokConfig) {
c.ngrok.token = config.Token
c.ngrok.remoteAddr = config.RemoteAddr
}
func (c *Connector) UseReceiveProxyProto(trustedProxyNets []*net.IPNet) {
+2 -2
View File
@@ -99,8 +99,8 @@ func NewServer(ctx context.Context, config *Config) (*Server, error) {
NewWebhookNotifier(config.Webhook.Url, config.Webhook.RequireUser))
}
if config.NgrokToken != "" {
connector.UseNgrok(config.NgrokToken)
if config.Ngrok.Token != "" {
connector.UseNgrok(config.Ngrok)
}
if config.ReceiveProxyProtocol {