Skip to content

Commit

Permalink
minecraft/dial.go: Read DisplayName from Mojang chain after authentic…
Browse files Browse the repository at this point in the history
…ation and set it correctly in the client data.

This fixes a method to detect gophertunnel clients.
  • Loading branch information
Sandertv committed Jul 11, 2023
1 parent de9f238 commit 2667b2d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions minecraft/dial.go
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/elliptic"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/sandertv/go-raknet"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/sandertv/gophertunnel/minecraft/protocol/login"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"golang.org/x/oauth2"
"gopkg.in/square/go-jose.v2/jwt"
"log"
rand2 "math/rand"
"net"
Expand Down Expand Up @@ -153,6 +155,7 @@ func (d Dialer) DialContext(ctx context.Context, network, address string) (conn
if err != nil {
return nil, &net.OpError{Op: "dial", Net: "minecraft", Err: err}
}
d.IdentityData = readChainIdentityData([]byte(chainData))
}
if d.ErrorLog == nil {
d.ErrorLog = log.New(os.Stderr, "", log.LstdFlags)
Expand Down Expand Up @@ -248,6 +251,30 @@ func (d Dialer) DialContext(ctx context.Context, network, address string) (conn
}
}

// readChainIdentityData reads a login.IdentityData from the Mojang chain
// obtained through authentication.
func readChainIdentityData(chainData []byte) login.IdentityData {
chain := struct{ Chain []string }{}
if err := json.Unmarshal(chainData, &chain); err != nil {
panic("invalid chain data from authentication: " + err.Error())
}
data := chain.Chain[1]
claims := struct {
ExtraData login.IdentityData `json:"extraData"`
}{}
tok, err := jwt.ParseSigned(data)
if err != nil {
panic("invalid chain data from authentication: " + err.Error())
}
if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
panic("invalid chain data from authentication: " + err.Error())
}
if claims.ExtraData.Identity == "" {
panic("chain data contained no data")
}
return claims.ExtraData
}

// listenConn listens on the connection until it is closed on another goroutine. The channel passed will
// receive a value once the connection is logged in.
func listenConn(conn *Conn, logger *log.Logger, l, c chan struct{}) {
Expand Down

0 comments on commit 2667b2d

Please sign in to comment.