chat package will not ignore the translate message now, but return the row msg.

This commit is contained in:
Tnze
2019-08-07 15:23:25 +08:00
parent 72ba5b7345
commit 73af09e2ea
8 changed files with 54 additions and 39 deletions

View File

@ -1,7 +1,7 @@
package bot package bot
import ( import (
"github.com/Tnze/go-mc/authenticate" "github.com/Tnze/go-mc/yggdrasil"
"log" "log"
) )
@ -43,7 +43,7 @@ func ExampleClient_JoinServer_online() {
c := NewClient() c := NewClient()
//Login Mojang account to get AccessToken //Login Mojang account to get AccessToken
auth, err := authenticate.Authenticate("Your E-mail", "Your Password") auth, err := yggdrasil.Authenticate("Your E-mail", "Your Password")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -159,7 +159,7 @@ func loginAuth(AsTk, name, UUID string, shareSecret []byte, er encryptionRequest
}, },
) )
if err != nil { if err != nil {
return fmt.Errorf("create request packet to authenticate faile: %v", err) return fmt.Errorf("create request packet to yggdrasil faile: %v", err)
} }
PostRequest, err := http.NewRequest(http.MethodPost, "https://sessionserver.mojang.com/session/minecraft/join", PostRequest, err := http.NewRequest(http.MethodPost, "https://sessionserver.mojang.com/session/minecraft/join",

View File

@ -125,15 +125,19 @@ func (m Message) ClearString() string {
msg.WriteString(text) msg.WriteString(text)
//handle translate //handle translate
if m.Translate != "" && translateMap != nil { if m.Translate != "" {
args := make([]interface{}, len(m.With)) args := make([]interface{}, len(m.With))
for i, v := range m.With { for i, v := range m.With {
var arg Message var arg Message
_ = arg.UnmarshalJSON(v) //ignore error _ = arg.UnmarshalJSON(v) //ignore error
args[i] = arg.ClearString() args[i] = arg.ClearString()
} }
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
if translateMap != nil {
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
} else {
_, _ = fmt.Fprint(&msg, m.Translate, m.With)
}
} }
if m.Extra != nil { if m.Extra != nil {
@ -172,8 +176,7 @@ func (m Message) String() string {
msg.WriteString(text) msg.WriteString(text)
//handle translate //handle translate
if m.Translate != "" && translateMap != nil { if m.Translate != "" {
args := make([]interface{}, len(m.With)) args := make([]interface{}, len(m.With))
for i, v := range m.With { for i, v := range m.With {
var arg Message var arg Message
@ -181,7 +184,11 @@ func (m Message) String() string {
args[i] = arg args[i] = arg
} }
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...) if translateMap != nil {
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
} else {
_, _ = fmt.Fprint(&msg, m.Translate, m.With)
}
} }
if m.Extra != nil { if m.Extra != nil {

View File

@ -15,7 +15,7 @@ func main() {
// For online-mode, you need login your Mojang account // For online-mode, you need login your Mojang account
// and load your Name and UUID to client: // and load your Name and UUID to client:
// //
// auth, err := authenticate.Authenticate("Your E-mail", "Your Password") // auth, err := yggdrasil.Authenticate("Your E-mail", "Your Password")
// if err != nil { // if err != nil {
// panic(err) // panic(err)
// } // }

View File

@ -20,7 +20,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Tnze/go-mc/authenticate" "github.com/Tnze/go-mc/yggdrasil"
"github.com/Tnze/go-mc/bot" "github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/chat" "github.com/Tnze/go-mc/chat"
_ "github.com/Tnze/go-mc/data/lang/en-us" _ "github.com/Tnze/go-mc/data/lang/en-us"
@ -103,7 +103,7 @@ func Handle(conn net.Conn) {
case PlayerLogin: case PlayerLogin:
signal := make(chan int) signal := make(chan int)
client := bot.NewClient() client := bot.NewClient()
auth, err := authenticate.Authenticate(Email, Paswd) auth, err := yggdrasil.Authenticate(Email, Paswd)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -374,7 +374,7 @@ func (c *Client) encryptionResponse() ([]byte, []byte, error) {
return nil, nil, err return nil, nil, err
} }
if p.ID != 0x01 { if p.ID != 0x01 {
return nil, nil, fmt.Errorf("0x%02X is not Encryption Response", p.ID) return nil, nil, fmt.Errorf("0x%02X is not Encryption AuthResp", p.ID)
} }
var ( var (

View File

@ -1,6 +1,4 @@
package authenticate package yggdrasil
//Simple yggdrasil-minecraft-login method.
import ( import (
"bytes" "bytes"
@ -10,15 +8,15 @@ import (
"net/http" "net/http"
) )
// agent is a struct of auth // Agent is a struct of auth
type agent struct { type Agent struct {
Name string `json:"name"` Name string `json:"name"`
Version int `json:"version"` Version int `json:"version"`
} }
// payload is a authenticate request struct // AuthPayload is a yggdrasil request struct
type payload struct { type AuthPayload struct {
Agent agent `json:"agent"` Agent Agent `json:"agent"`
UserName string `json:"username"` UserName string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
ClientToken string `json:"clientToken"` ClientToken string `json:"clientToken"`
@ -26,18 +24,18 @@ type payload struct {
} }
// Authenticate authenticates a user using their password. // Authenticate authenticates a user using their password.
func Authenticate(user, passwd string) (respData Response, err error) { func Authenticate(user, password string) (respData AuthResp, err error) {
j, err := json.Marshal(payload{ j, err := json.Marshal(AuthPayload{
Agent: agent{ Agent: Agent{
Name: "Minecraft", Name: "Minecraft",
Version: 1, Version: 1,
}, },
UserName: user, UserName: user,
Password: passwd, Password: password,
ClientToken: "go-mc", ClientToken: "go-mc",
RequestUser: true, RequestUser: true,
}) })
// fmt.Println(string(j))
if err != nil { if err != nil {
err = fmt.Errorf("encoding json fail: %v", err) err = fmt.Errorf("encoding json fail: %v", err)
return return
@ -45,24 +43,24 @@ func Authenticate(user, passwd string) (respData Response, err error) {
//Post //Post
client := http.Client{} client := http.Client{}
PostRequest, err := http.NewRequest(http.MethodPost, "https://authserver.mojang.com/authenticate", PostRequest, err := http.NewRequest(http.MethodPost, "https://authserver.mojang.com/yggdrasil",
bytes.NewReader(j)) bytes.NewReader(j))
if err != nil { if err != nil {
err = fmt.Errorf("make request error: %v", err) err = fmt.Errorf("make request error: %v", err)
return return
} }
PostRequest.Header.Set("User-agent", "go-mc") PostRequest.Header.Set("User-Agent", "go-mc")
PostRequest.Header.Set("Connection", "keep-alive") PostRequest.Header.Set("Connection", "keep-alive")
PostRequest.Header.Set("Content-Type", "application/json") PostRequest.Header.Set("Content-Type", "application/json")
resp, err := client.Do(PostRequest) resp, err := client.Do(PostRequest)
if err != nil { if err != nil {
err = fmt.Errorf("post authenticate fail: %v", err) err = fmt.Errorf("post yggdrasil fail: %v", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
err = fmt.Errorf("read authenticate resp fail: %v", err) err = fmt.Errorf("read yggdrasil resp fail: %v", err)
return return
} }
err = json.Unmarshal(body, &respData) err = json.Unmarshal(body, &respData)
@ -71,15 +69,15 @@ func Authenticate(user, passwd string) (respData Response, err error) {
return return
} }
if respData.Error != "" { if respData.Error != "" {
err = fmt.Errorf("authenticate fail: {error: %q, errorMessage: %q, cause: %q}", err = fmt.Errorf("yggdrasil fail: {error: %q, errorMessage: %q, cause: %q}",
respData.Error, respData.ErrorMessage, respData.Cause) respData.Error, respData.ErrorMessage, respData.Cause)
return return
} }
return return
} }
// Response is the response from Mojang's auth server // AuthResp is the response from Mojang's auth server
type Response struct { type AuthResp struct {
Error string `json:"error"` Error string `json:"error"`
ErrorMessage string `json:"errorMessage"` ErrorMessage string `json:"errorMessage"`
Cause string `json:"cause"` Cause string `json:"cause"`
@ -90,14 +88,14 @@ type Response struct {
ID string `json:"ID"` // hexadecimal ID string `json:"ID"` // hexadecimal
Name string `json:"name"` Name string `json:"name"`
Legacy bool `json:"legacy"` // In practice, this field only appears in the response if true. Default to false. Legacy bool `json:"legacy"` // In practice, this field only appears in the response if true. Default to false.
} `json:"availableProfiles"` // only present if the agent field was received } `json:"availableProfiles"` // only present if the Agent field was received
SelectedProfile struct { // only present if the agent field was received SelectedProfile struct { // only present if the Agent field was received
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Legacy bool `json:"legacy"` Legacy bool `json:"legacy"`
} `json:"selectedProfile"` } `json:"selectedProfile"`
User struct { // only present if requestUser was true in the request payload User struct { // only present if requestUser was true in the request AuthPayload
ID string `json:"id"` // hexadecimal ID string `json:"id"` // hexadecimal
Properties []struct { Properties []struct {
Name string `json:"name"` Name string `json:"name"`

View File

@ -1,4 +1,4 @@
package authenticate package yggdrasil
import ( import (
"encoding/json" "encoding/json"
@ -7,8 +7,8 @@ import (
) )
func TestEncodingPayload(t *testing.T) { func TestEncodingPayload(t *testing.T) {
j, err := json.Marshal(payload{ j, err := json.Marshal(AuthPayload{
Agent: agent{ Agent: Agent{
Name: "Minecraft", Name: "Minecraft",
Version: 1, Version: 1,
}, },

10
yggdrasil/yggdrasil.go Normal file
View File

@ -0,0 +1,10 @@
// Package yggdrasil implement Yggdrasil protocol.
//
// Minecraft 1.6 introduced a new authentication scheme called Yggdrasil
// which completely replaces the previous authentication system.
// Mojang's other game, Scrolls, uses this method of authentication as well.
// Mojang has said that this authentication system should be used by everyone for custom logins,
// but credentials should never be collected from users. ----- https://wiki.vg
package yggdrasil
var Server = ""