Skip to content

Commit 40eded4

Browse files
committed
refactor: bundle config for connector
1 parent 44e5619 commit 40eded4

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

cmd/mc-router/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,18 @@ func main() {
155155
trustedIpNets = append(trustedIpNets, ipNet)
156156
}
157157

158-
connector := server.NewConnector(metricsBuilder.BuildConnectorMetrics(), config.UseProxyProtocol, config.ReceiveProxyProtocol, trustedIpNets, config.RecordLogins, autoScaleUpAllowDenyConfig, config.FakeOnline, config.FakeOnlineMOTD)
158+
connectorConfig := server.ConnectorConfig{
159+
SendProxyProto: config.UseProxyProtocol,
160+
ReceiveProxyProto: config.ReceiveProxyProtocol,
161+
TrustedProxyNets: trustedIpNets,
162+
RecordLogins: config.RecordLogins,
163+
AutoScaleUpAllowDenyConfig: autoScaleUpAllowDenyConfig,
164+
AutoScaleUp: config.AutoScaleUp,
165+
FakeOnline: config.FakeOnline,
166+
FakeOnlineMOTD: config.FakeOnlineMOTD,
167+
}
168+
169+
connector := server.NewConnector(metricsBuilder.BuildConnectorMetrics(), connectorConfig)
159170

160171
clientFilter, err := server.NewClientFilter(config.ClientsToAllow, config.ClientsToDeny)
161172
if err != nil {

server/connector.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,35 @@ func (p *PlayerInfo) String() string {
7171
return fmt.Sprintf("%s/%s", p.Name, p.Uuid)
7272
}
7373

74-
func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool, receiveProxyProto bool, trustedProxyNets []*net.IPNet, recordLogins bool, autoScaleUpAllowDenyConfig *AllowDenyConfig, fakeOnline bool, fakeOnlineMOTD string) *Connector {
74+
func NewConnector(metrics *ConnectorMetrics, cfg ConnectorConfig) *Connector {
7575
return &Connector{
7676
metrics: metrics,
77-
sendProxyProto: sendProxyProto,
7877
connectionsCond: sync.NewCond(&sync.Mutex{}),
79-
receiveProxyProto: receiveProxyProto,
80-
trustedProxyNets: trustedProxyNets,
81-
recordLogins: recordLogins,
82-
autoScaleUpAllowDenyConfig: autoScaleUpAllowDenyConfig,
83-
fakeOnline: fakeOnline,
84-
fakeOnlineMOTD: fakeOnlineMOTD,
78+
config: cfg,
8579
}
8680
}
8781

82+
type ConnectorConfig struct {
83+
SendProxyProto bool
84+
ReceiveProxyProto bool
85+
TrustedProxyNets []*net.IPNet
86+
RecordLogins bool
87+
AutoScaleUpAllowDenyConfig *AllowDenyConfig
88+
AutoScaleUp bool
89+
FakeOnline bool
90+
FakeOnlineMOTD string
91+
}
92+
8893
type Connector struct {
8994
state mcproto.State
9095
metrics *ConnectorMetrics
91-
sendProxyProto bool
92-
receiveProxyProto bool
93-
recordLogins bool
94-
trustedProxyNets []*net.IPNet
9596

9697
activeConnections int32
9798
connectionsCond *sync.Cond
9899
ngrokToken string
99100
clientFilter *ClientFilter
100-
autoScaleUpAllowDenyConfig *AllowDenyConfig
101101

102-
fakeOnline bool
103-
fakeOnlineMOTD string
102+
config ConnectorConfig
104103

105104
connectionNotifier ConnectionNotifier
106105
}
@@ -145,7 +144,7 @@ func (c *Connector) createListener(ctx context.Context, listenAddress string) (n
145144
}
146145
logrus.WithField("listenAddress", listenAddress).Info("Listening for Minecraft client connections")
147146

148-
if c.receiveProxyProto {
147+
if c.config.ReceiveProxyProto {
149148
proxyListener := &proxyproto.Listener{
150149
Listener: listener,
151150
Policy: c.createProxyProtoPolicy(),
@@ -159,7 +158,7 @@ func (c *Connector) createListener(ctx context.Context, listenAddress string) (n
159158

160159
func (c *Connector) createProxyProtoPolicy() func(upstream net.Addr) (proxyproto.Policy, error) {
161160
return func(upstream net.Addr) (proxyproto.Policy, error) {
162-
trustedIpNets := c.trustedProxyNets
161+
trustedIpNets := c.config.TrustedProxyNets
163162

164163
if len(trustedIpNets) == 0 {
165164
logrus.Debug("No trusted proxy networks configured, using the PROXY header by default")
@@ -361,8 +360,8 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
361360
clientAddr net.Addr, preReadContent io.Reader, serverAddress string, playerInfo *PlayerInfo, nextState mcproto.State) {
362361

363362
backendHostPort, resolvedHost, waker := Routes.FindBackendForServerAddress(ctx, serverAddress)
364-
if waker != nil && nextState > mcproto.StateStatus {
365-
serverAllowsPlayer := c.autoScaleUpAllowDenyConfig.ServerAllowsPlayer(serverAddress, playerInfo)
363+
if c.config.AutoScaleUp && waker != nil && nextState > mcproto.StateStatus {
364+
serverAllowsPlayer := c.config.AutoScaleUpAllowDenyConfig.ServerAllowsPlayer(serverAddress, playerInfo)
366365
logrus.
367366
WithField("client", clientAddr).
368367
WithField("server", serverAddress).
@@ -414,7 +413,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
414413
case mcproto.StateLogin:
415414
backendTry = retry.NewConstant(backendRetryInterval)
416415
// Connect request: if autoscaler is enabled, try to connect until backendTimeout is reached
417-
if waker != nil {
416+
if c.config.AutoScaleUp {
418417
// Autoscaler enabled: retry until backendTimeout is reached
419418
backendTry = retry.WithMaxDuration(backendTimeout, backendTry)
420419
} else {
@@ -456,11 +455,11 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
456455
}
457456
}
458457

459-
if nextState == mcproto.StateStatus && c.fakeOnline && waker != nil {
458+
if nextState == mcproto.StateStatus && c.config.FakeOnline && c.config.AutoScaleUp {
460459
logrus.Info("Server is offline, sending fakeOnlineMOTD for status request")
461460
writeStatusErr := mcproto.WriteStatusResponse(
462461
frontendConn,
463-
c.fakeOnlineMOTD,
462+
c.config.FakeOnlineMOTD,
464463
)
465464

466465
if writeStatusErr != nil {
@@ -488,7 +487,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
488487

489488
c.metrics.ActiveConnections.Set(float64(
490489
atomic.AddInt32(&c.activeConnections, 1)))
491-
if c.recordLogins && playerInfo != nil {
490+
if c.config.RecordLogins && playerInfo != nil {
492491
logrus.
493492
WithField("client", clientAddr).
494493
WithField("player", playerInfo).
@@ -517,7 +516,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
517516
}
518517
c.metrics.ActiveConnections.Set(float64(
519518
atomic.AddInt32(&c.activeConnections, -1)))
520-
if c.recordLogins && playerInfo != nil {
519+
if c.config.RecordLogins && playerInfo != nil {
521520
c.metrics.ServerActivePlayer.
522521
With("player_name", playerInfo.Name).
523522
With("player_uuid", playerInfo.Uuid.String()).
@@ -528,7 +527,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
528527
}()
529528

530529
// PROXY protocol implementation
531-
if c.sendProxyProto {
530+
if c.config.SendProxyProto {
532531

533532
// Determine transport protocol for the PROXY header by "analyzing" the frontend connection's address
534533
transportProtocol := proxyproto.TCPv4

0 commit comments

Comments
 (0)