Skip to content

Commit

Permalink
Add client brand
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Dec 18, 2023
1 parent 56beea2 commit 3ddf9db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pkg/edition/java/proto/packet/plugin/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func RewriteMinecraftBrand(message *Message, protocol proto.Protocol) *Message {
return message
}

currentBrand := readBrandMessage(message.Data)
currentBrand := ReadBrandMessage(message.Data)
rewrittenBrand := fmt.Sprintf("%s (Gate by Minekube)", currentBrand)

rewrittenBuf := new(bytes.Buffer)
Expand All @@ -133,11 +133,14 @@ func RewriteMinecraftBrand(message *Message, protocol proto.Protocol) *Message {
}
}

// ReadBrandMessage reads the brand message from the given packet data.
// The returned string will be empty if the data is invalid.
//
// Some clients (mostly poorly-implemented bots) do not send validly-formed brand messages.
// In order to accommodate their broken behavior, we'll first try to read in the 1.8 format, and
// if that fails, treat it as a 1.7-format message (which has no prefixed length).
// (The message the proxy sends will be in the correct format depending on the protocol.)
func readBrandMessage(data []byte) string {
func ReadBrandMessage(data []byte) string {
s, err := util.ReadString(bytes.NewReader(data))
if err != nil {
s, _ = util.ReadStringWithoutLen(bytes.NewReader(data))
Expand Down
15 changes: 15 additions & 0 deletions pkg/edition/java/proxy/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Player interface {
// SendActionBar sends an action bar to the player.
SendActionBar(msg component.Component) error
TabList() tablist.TabList // Returns the player's tab list.
ClientBrand() string // Returns the player's client brand. Empty if unspecified.

// Looking for title or bossbar methods? See the title and bossbar packages.
}
Expand Down Expand Up @@ -107,6 +108,7 @@ type connectedPlayer struct {
previousResourceResponse *bool
pendingResourcePack *ResourcePackInfo
appliedResourcePack *ResourcePackInfo
clientBrand string // may be empty

serversToTry []string // names of servers to try if we got disconnected from previous
tryIndex int
Expand Down Expand Up @@ -699,3 +701,16 @@ func (p *connectedPlayer) switchToConfigState(pkt *cfgpacket.StartUpdate) {
}
p.SetState(state.Config)
}

func (p *connectedPlayer) ClientBrand() string {
p.mu.RLock()
defer p.mu.RUnlock()
return p.clientBrand
}

// setClientBrand sets the client brand of the player.
func (p *connectedPlayer) setClientBrand(brand string) {
p.mu.Lock()
p.clientBrand = brand
p.mu.Unlock()
}
3 changes: 2 additions & 1 deletion pkg/edition/java/proxy/session_client_play.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ func (c *clientPlaySessionHandler) handlePluginMessage(packet *plugin.Message) {
} else if plugin.IsUnregister(packet) {
_ = backendConn.WritePacket(packet)
} else if plugin.McBrand(packet) {
// TODO read brand message & fire PlayerClientBrandEvent & cache client brand
// TODO fire PlayerClientBrandEvent & cache client brand
c.player.setClientBrand(plugin.ReadBrandMessage(packet.Data))
_ = backendConn.WritePacket(plugin.RewriteMinecraftBrand(packet, c.player.Protocol()))
} else {
serverConnPhase := serverConn.phase()
Expand Down

0 comments on commit 3ddf9db

Please sign in to comment.