PlayerList in game

This commit is contained in:
Tnze
2022-06-23 13:13:38 +08:00
parent eecfa6d8a8
commit d829c47731
7 changed files with 119 additions and 56 deletions

View File

@ -13,6 +13,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
@ -215,6 +216,19 @@ type Property struct {
Name, Value, Signature string
}
func (p Property) WriteTo(w io.Writer) (n int64, err error) {
hasSignature := len(p.Signature) > 0
return pk.Tuple{
pk.String(p.Name),
pk.String(p.Value),
pk.Boolean(hasSignature),
pk.Opt{
Has: hasSignature,
Field: pk.String(p.Signature),
},
}.WriteTo(w)
}
//Texture includes player's skin and cape
type Texture struct {
TimeStamp int64 `json:"timestamp"`

69
server/auth/pubkey.go Normal file
View File

@ -0,0 +1,69 @@
package auth
import (
"crypto/rsa"
"crypto/x509"
"errors"
"io"
"time"
pk "github.com/Tnze/go-mc/net/packet"
)
type PublicKey struct {
ExpiresAt time.Time
PubKey *rsa.PublicKey
Signature []byte
}
func (p *PublicKey) WriteTo(w io.Writer) (n int64, err error) {
pubKeyEncoded, err := x509.MarshalPKIXPublicKey(p.PubKey)
if err != nil {
return 0, err
}
return pk.Tuple{
pk.Long(p.ExpiresAt.UnixMilli()),
pk.ByteArray(pubKeyEncoded),
pk.ByteArray(p.Signature),
}.WriteTo(w)
}
func (p *PublicKey) ReadFrom(r io.Reader) (n int64, err error) {
var (
ExpiresAt pk.Long
PubKey pk.ByteArray
Signature pk.ByteArray
)
n, err = pk.Tuple{
&ExpiresAt,
&PubKey,
&Signature,
}.ReadFrom(r)
if err != nil {
return n, err
}
p.ExpiresAt = time.UnixMilli(int64(ExpiresAt))
pubKey, err := x509.ParsePKIXPublicKey(PubKey)
if err != nil {
return n, err
}
if key, ok := pubKey.(*rsa.PublicKey); !ok {
return n, errors.New("expect RSA public key")
} else {
p.PubKey = key
}
p.Signature = Signature
return n, nil
}
func (p *PublicKey) Verify() bool {
if p.ExpiresAt.Before(time.Now()) {
return false
}
encoded, err := x509.MarshalPKIXPublicKey(p.PubKey)
if err != nil {
return false
}
return VerifySignature(encoded, p.Signature)
}