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

105
yggdrasil/authenticate.go Normal file
View File

@ -0,0 +1,105 @@
package yggdrasil
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Agent is a struct of auth
type Agent struct {
Name string `json:"name"`
Version int `json:"version"`
}
// AuthPayload is a yggdrasil request struct
type AuthPayload struct {
Agent Agent `json:"agent"`
UserName string `json:"username"`
Password string `json:"password"`
ClientToken string `json:"clientToken"`
RequestUser bool `json:"requestUser"`
}
// Authenticate authenticates a user using their password.
func Authenticate(user, password string) (respData AuthResp, err error) {
j, err := json.Marshal(AuthPayload{
Agent: Agent{
Name: "Minecraft",
Version: 1,
},
UserName: user,
Password: password,
ClientToken: "go-mc",
RequestUser: true,
})
if err != nil {
err = fmt.Errorf("encoding json fail: %v", err)
return
}
//Post
client := http.Client{}
PostRequest, err := http.NewRequest(http.MethodPost, "https://authserver.mojang.com/yggdrasil",
bytes.NewReader(j))
if err != nil {
err = fmt.Errorf("make request error: %v", err)
return
}
PostRequest.Header.Set("User-Agent", "go-mc")
PostRequest.Header.Set("Connection", "keep-alive")
PostRequest.Header.Set("Content-Type", "application/json")
resp, err := client.Do(PostRequest)
if err != nil {
err = fmt.Errorf("post yggdrasil fail: %v", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("read yggdrasil resp fail: %v", err)
return
}
err = json.Unmarshal(body, &respData)
if err != nil {
err = fmt.Errorf("unmarshal json data fail: %v", err)
return
}
if respData.Error != "" {
err = fmt.Errorf("yggdrasil fail: {error: %q, errorMessage: %q, cause: %q}",
respData.Error, respData.ErrorMessage, respData.Cause)
return
}
return
}
// AuthResp is the response from Mojang's auth server
type AuthResp struct {
Error string `json:"error"`
ErrorMessage string `json:"errorMessage"`
Cause string `json:"cause"`
AccessToken string `json:"accessToken"`
ClientToken string `json:"clientToken"` // identical to the one received
AvailableProfiles []struct {
ID string `json:"ID"` // hexadecimal
Name string `json:"name"`
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
SelectedProfile struct { // only present if the Agent field was received
ID string `json:"id"`
Name string `json:"name"`
Legacy bool `json:"legacy"`
} `json:"selectedProfile"`
User struct { // only present if requestUser was true in the request AuthPayload
ID string `json:"id"` // hexadecimal
Properties []struct {
Name string `json:"name"`
Value string `json:"value"`
}
} `json:"user"`
}

View File

@ -0,0 +1,32 @@
package yggdrasil
import (
"encoding/json"
"fmt"
"testing"
)
func TestEncodingPayload(t *testing.T) {
j, err := json.Marshal(AuthPayload{
Agent: Agent{
Name: "Minecraft",
Version: 1,
},
UserName: "mojang account name",
Password: "mojang account password",
ClientToken: "client identifier",
RequestUser: true,
})
if err != nil {
t.Fatal(err)
}
t.Log(string(j))
}
func ExampleAuthenticate() {
resp, err := Authenticate("", "")
if err != nil {
panic(err)
}
fmt.Println(resp)
}

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 = ""