|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "go.minekube.com/gate/pkg/edition/java/netmc" |
| 7 | + "go.minekube.com/gate/pkg/edition/java/proto/packet" |
| 8 | + "go.minekube.com/gate/pkg/edition/java/proxy/message" |
| 9 | +) |
| 10 | + |
| 11 | +// ForgeLoginWrapperChannel is the Forge login wrapper channel used for FML2/FML3 |
| 12 | +// mod negotiation during the LOGIN phase (Minecraft 1.13-1.20.1). |
| 13 | +const ForgeLoginWrapperChannel = "fml:loginwrapper" |
| 14 | + |
| 15 | +// modernForgeLoginRelay relays LoginPluginMessages between a backend Forge server |
| 16 | +// and a client during the LOGIN phase. This enables Forge 1.13-1.20.1 mod negotiation |
| 17 | +// through the proxy when using Velocity modern forwarding. |
| 18 | +// |
| 19 | +// The relay uses loginInboundConn to send LoginPluginMessages to the client and |
| 20 | +// receive responses via consumer callbacks. The client's read loop goroutine |
| 21 | +// processes responses via the auth session handler's HandlePacket. |
| 22 | +type modernForgeLoginRelay struct { |
| 23 | + clientLogin *loginInboundConn |
| 24 | + player *connectedPlayer |
| 25 | + |
| 26 | + // pendingLoginSuccess is the ServerLoginSuccess packet to send to the client |
| 27 | + // after the FML handshake completes. |
| 28 | + pendingLoginSuccess *packet.ServerLoginSuccess |
| 29 | + |
| 30 | + mu sync.Mutex |
| 31 | + cachedExchanges []forgeLoginExchange |
| 32 | +} |
| 33 | + |
| 34 | +// forgeLoginExchange records a single FML LoginPluginMessage exchange |
| 35 | +// between the backend and client, for replay during server switch. |
| 36 | +type forgeLoginExchange struct { |
| 37 | + channel string |
| 38 | + request []byte // data sent by backend |
| 39 | + response []byte // data from client (nil if rejected) |
| 40 | +} |
| 41 | + |
| 42 | +func newModernForgeLoginRelay( |
| 43 | + clientLogin *loginInboundConn, |
| 44 | + player *connectedPlayer, |
| 45 | + pendingLoginSuccess *packet.ServerLoginSuccess, |
| 46 | +) *modernForgeLoginRelay { |
| 47 | + return &modernForgeLoginRelay{ |
| 48 | + clientLogin: clientLogin, |
| 49 | + player: player, |
| 50 | + pendingLoginSuccess: pendingLoginSuccess, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// relayToClient forwards a backend LoginPluginMessage to the client via the |
| 55 | +// login plugin message mechanism. The consumer callback will forward the |
| 56 | +// client's response back to the backend. |
| 57 | +func (r *modernForgeLoginRelay) relayToClient( |
| 58 | + backendConn netmc.MinecraftConn, |
| 59 | + msg *packet.LoginPluginMessage, |
| 60 | +) error { |
| 61 | + identifier, err := message.ChannelIdentifierFrom(msg.Channel) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + data := msg.Data |
| 67 | + if len(data) == 0 { |
| 68 | + // SendLoginPluginMessage requires non-empty data. |
| 69 | + data = []byte{0} |
| 70 | + } |
| 71 | + |
| 72 | + consumer := &forgeRelayConsumer{ |
| 73 | + relay: r, |
| 74 | + backendConn: backendConn, |
| 75 | + backendMsgID: msg.ID, |
| 76 | + channel: msg.Channel, |
| 77 | + requestData: msg.Data, |
| 78 | + } |
| 79 | + return r.clientLogin.SendLoginPluginMessage(identifier, data, consumer) |
| 80 | +} |
| 81 | + |
| 82 | +// complete sends the pending ServerLoginSuccess to the client, |
| 83 | +// completing the delayed login. |
| 84 | +func (r *modernForgeLoginRelay) complete() error { |
| 85 | + return r.player.WritePacket(r.pendingLoginSuccess) |
| 86 | +} |
| 87 | + |
| 88 | +// exchanges returns a copy of the cached FML exchanges for server switch replay. |
| 89 | +func (r *modernForgeLoginRelay) exchanges() []forgeLoginExchange { |
| 90 | + r.mu.Lock() |
| 91 | + defer r.mu.Unlock() |
| 92 | + out := make([]forgeLoginExchange, len(r.cachedExchanges)) |
| 93 | + copy(out, r.cachedExchanges) |
| 94 | + return out |
| 95 | +} |
| 96 | + |
| 97 | +// forgeRelayConsumer forwards a client's LoginPluginResponse back to the |
| 98 | +// backend server. It also caches the exchange for future server switch replay. |
| 99 | +type forgeRelayConsumer struct { |
| 100 | + relay *modernForgeLoginRelay |
| 101 | + backendConn netmc.MinecraftConn |
| 102 | + backendMsgID int |
| 103 | + channel string |
| 104 | + requestData []byte |
| 105 | +} |
| 106 | + |
| 107 | +func (c *forgeRelayConsumer) OnMessageResponse(responseBody []byte) error { |
| 108 | + // Cache the exchange for server switch replay. |
| 109 | + c.relay.mu.Lock() |
| 110 | + c.relay.cachedExchanges = append(c.relay.cachedExchanges, forgeLoginExchange{ |
| 111 | + channel: c.channel, |
| 112 | + request: c.requestData, |
| 113 | + response: responseBody, |
| 114 | + }) |
| 115 | + c.relay.mu.Unlock() |
| 116 | + |
| 117 | + // Forward the response to the backend. |
| 118 | + return c.backendConn.WritePacket(&packet.LoginPluginResponse{ |
| 119 | + ID: c.backendMsgID, |
| 120 | + Success: responseBody != nil, |
| 121 | + Data: responseBody, |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +// modernForgeReplayRelay replays cached FML LoginPluginMessage responses |
| 126 | +// during a server switch when the client is already in PLAY state and |
| 127 | +// cannot participate in a new LOGIN-phase FML handshake. |
| 128 | +type modernForgeReplayRelay struct { |
| 129 | + cachedExchanges []forgeLoginExchange |
| 130 | + mu sync.Mutex |
| 131 | + replayIndex int |
| 132 | +} |
| 133 | + |
| 134 | +func newModernForgeReplayRelay(cached []forgeLoginExchange) *modernForgeReplayRelay { |
| 135 | + return &modernForgeReplayRelay{ |
| 136 | + cachedExchanges: cached, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// replayResponse sends the next cached response to the backend. |
| 141 | +// If no more cached responses are available, sends Success=false. |
| 142 | +func (r *modernForgeReplayRelay) replayResponse( |
| 143 | + backendMsgID int, |
| 144 | + backendConn netmc.MinecraftConn, |
| 145 | +) error { |
| 146 | + r.mu.Lock() |
| 147 | + defer r.mu.Unlock() |
| 148 | + |
| 149 | + if r.replayIndex >= len(r.cachedExchanges) { |
| 150 | + return backendConn.WritePacket(&packet.LoginPluginResponse{ |
| 151 | + ID: backendMsgID, |
| 152 | + Success: false, |
| 153 | + }) |
| 154 | + } |
| 155 | + |
| 156 | + exchange := r.cachedExchanges[r.replayIndex] |
| 157 | + r.replayIndex++ |
| 158 | + |
| 159 | + return backendConn.WritePacket(&packet.LoginPluginResponse{ |
| 160 | + ID: backendMsgID, |
| 161 | + Success: exchange.response != nil, |
| 162 | + Data: exchange.response, |
| 163 | + }) |
| 164 | +} |
0 commit comments