From 68ecc423e7f25fa38ce0f5bfc19cc98db85f8008 Mon Sep 17 00:00:00 2001 From: Hashim Date: Fri, 29 Nov 2024 10:00:31 +0300 Subject: [PATCH 1/2] return error in readChainIdentityData instead of panicing --- minecraft/dial.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/minecraft/dial.go b/minecraft/dial.go index f9d58702..3a3e57b6 100644 --- a/minecraft/dial.go +++ b/minecraft/dial.go @@ -166,7 +166,11 @@ 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)) + identityData, err := readChainIdentityData([]byte(chainData)) + if err != nil { + return nil, &net.OpError{Op: "dial", Net: "minecraft", Err: err} + } + d.IdentityData = identityData } n, ok := networkByID(network, d.ErrorLog) @@ -256,10 +260,10 @@ 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 { +func readChainIdentityData(chainData []byte) (login.IdentityData, error) { chain := struct{ Chain []string }{} if err := json.Unmarshal(chainData, &chain); err != nil { - panic("invalid chain data from authentication: " + err.Error()) + return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err) } data := chain.Chain[1] claims := struct { @@ -267,15 +271,15 @@ func readChainIdentityData(chainData []byte) login.IdentityData { }{} tok, err := jwt.ParseSigned(data) if err != nil { - panic("invalid chain data from authentication: " + err.Error()) + return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err) } if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil { - panic("invalid chain data from authentication: " + err.Error()) + return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err) } if claims.ExtraData.Identity == "" { - panic("chain data contained no data") + return login.IdentityData{}, fmt.Errorf("chain data contained no data") } - return claims.ExtraData + return claims.ExtraData, nil } // listenConn listens on the connection until it is closed on another goroutine. The channel passed will From 1e48049430f1979de2841572b8cd2520e9a1c045 Mon Sep 17 00:00:00 2001 From: Hashim Date: Mon, 16 Dec 2024 12:57:47 -0500 Subject: [PATCH 2/2] change error messages --- minecraft/dial.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/minecraft/dial.go b/minecraft/dial.go index 3a3e57b6..57e4a3a7 100644 --- a/minecraft/dial.go +++ b/minecraft/dial.go @@ -263,7 +263,7 @@ func (d Dialer) DialContext(ctx context.Context, network, address string) (conn func readChainIdentityData(chainData []byte) (login.IdentityData, error) { chain := struct{ Chain []string }{} if err := json.Unmarshal(chainData, &chain); err != nil { - return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err) + return login.IdentityData{}, fmt.Errorf("read chain: read json: %w", err) } data := chain.Chain[1] claims := struct { @@ -271,13 +271,13 @@ func readChainIdentityData(chainData []byte) (login.IdentityData, error) { }{} tok, err := jwt.ParseSigned(data) if err != nil { - return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err) + return login.IdentityData{}, fmt.Errorf("read chain: parse jwt: %w", err) } if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil { - return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err) + return login.IdentityData{}, fmt.Errorf("read chain: read claims: %w", err) } if claims.ExtraData.Identity == "" { - return login.IdentityData{}, fmt.Errorf("chain data contained no data") + return login.IdentityData{}, fmt.Errorf("read chain: no extra data found") } return claims.ExtraData, nil }