PlayerList in game
This commit is contained in:
@ -75,7 +75,7 @@ func (a Ary[T]) ReadFrom(r io.Reader) (n int64, err error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Array(ary interface{}) Field {
|
func Array(ary any) Field {
|
||||||
return Ary[VarInt]{Ary: ary}
|
return Ary[VarInt]{Ary: ary}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -215,6 +216,19 @@ type Property struct {
|
|||||||
Name, Value, Signature string
|
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
|
//Texture includes player's skin and cape
|
||||||
type Texture struct {
|
type Texture struct {
|
||||||
TimeStamp int64 `json:"timestamp"`
|
TimeStamp int64 `json:"timestamp"`
|
||||||
|
69
server/auth/pubkey.go
Normal file
69
server/auth/pubkey.go
Normal 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)
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rsa"
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"github.com/Tnze/go-mc/server/auth"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"github.com/Tnze/go-mc/net"
|
"github.com/Tnze/go-mc/net"
|
||||||
@ -13,5 +13,5 @@ type GamePlay interface {
|
|||||||
//
|
//
|
||||||
// Note: the connection will be closed after this function returned.
|
// Note: the connection will be closed after this function returned.
|
||||||
// You don't need to close the connection, but to keep not returning while the player is playing.
|
// You don't need to close the connection, but to keep not returning while the player is playing.
|
||||||
AcceptPlayer(name string, id uuid.UUID, profilePubKey *rsa.PublicKey, protocol int32, conn *net.Conn)
|
AcceptPlayer(name string, id uuid.UUID, profilePubKey *auth.PublicKey, properties []auth.Property, protocol int32, conn *net.Conn)
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,20 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rsa"
|
|
||||||
"crypto/x509"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/uuid"
|
|
||||||
"io"
|
|
||||||
"time"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/Tnze/go-mc/chat"
|
"github.com/Tnze/go-mc/chat"
|
||||||
"github.com/Tnze/go-mc/data/packetid"
|
"github.com/Tnze/go-mc/data/packetid"
|
||||||
"github.com/Tnze/go-mc/net"
|
"github.com/Tnze/go-mc/net"
|
||||||
pk "github.com/Tnze/go-mc/net/packet"
|
pk "github.com/Tnze/go-mc/net/packet"
|
||||||
"github.com/Tnze/go-mc/offline"
|
"github.com/Tnze/go-mc/offline"
|
||||||
"github.com/Tnze/go-mc/server/auth"
|
"github.com/Tnze/go-mc/server/auth"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoginHandler is used to handle player login process, that is,
|
// LoginHandler is used to handle player login process, that is,
|
||||||
// from clientbound "LoginStart" packet to serverbound "LoginSuccess" packet.
|
// from clientbound "LoginStart" packet to serverbound "LoginSuccess" packet.
|
||||||
type LoginHandler interface {
|
type LoginHandler interface {
|
||||||
AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, profilePubKey *rsa.PublicKey, err error)
|
AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, profilePubKey *auth.PublicKey, properties []auth.Property, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoginChecker is the interface to check if a player is allowed to log in the server.
|
// LoginChecker is the interface to check if a player is allowed to log in the server.
|
||||||
@ -31,6 +24,9 @@ type LoginChecker interface {
|
|||||||
CheckPlayer(name string, id uuid.UUID, protocol int32) (ok bool, reason chat.Message)
|
CheckPlayer(name string, id uuid.UUID, protocol int32) (ok bool, reason chat.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure MojangLoginHandler implement LoginHandler
|
||||||
|
var _ LoginHandler = (*MojangLoginHandler)(nil)
|
||||||
|
|
||||||
// MojangLoginHandler is a standard LoginHandler that implement both online and offline login progress.
|
// MojangLoginHandler is a standard LoginHandler that implement both online and offline login progress.
|
||||||
// This implementation also support custom LoginChecker.
|
// This implementation also support custom LoginChecker.
|
||||||
// None of Custom login packet (also called LoginPluginRequest/Response) is support by this implementation.
|
// None of Custom login packet (also called LoginPluginRequest/Response) is support by this implementation.
|
||||||
@ -54,7 +50,7 @@ type MojangLoginHandler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AcceptLogin implement LoginHandler for MojangLoginHandler
|
// AcceptLogin implement LoginHandler for MojangLoginHandler
|
||||||
func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, profilePubKey *rsa.PublicKey, err error) {
|
func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name string, id uuid.UUID, profilePubKey *auth.PublicKey, properties []auth.Property, err error) {
|
||||||
//login start
|
//login start
|
||||||
var p pk.Packet
|
var p pk.Packet
|
||||||
err = conn.ReadPacket(&p)
|
err = conn.ReadPacket(&p)
|
||||||
@ -66,56 +62,41 @@ func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name s
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasSignature pk.Boolean
|
var hasPubKey pk.Boolean
|
||||||
var timestamp pk.Long
|
var pubKey auth.PublicKey
|
||||||
var profilePubKeyBytes pk.ByteArray
|
|
||||||
var signature pk.ByteArray
|
|
||||||
err = p.Scan(
|
err = p.Scan(
|
||||||
(*pk.String)(&name),
|
(*pk.String)(&name),
|
||||||
&hasSignature, pk.Opt{
|
&hasPubKey, pk.Opt{
|
||||||
Has: &hasSignature,
|
Has: &hasPubKey,
|
||||||
Field: pk.Tuple{×tamp, &profilePubKeyBytes, &signature},
|
Field: &pubKey,
|
||||||
},
|
},
|
||||||
) //decode username as pk.String
|
) //decode username as pk.String
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasSignature {
|
if hasPubKey {
|
||||||
if time.UnixMilli(int64(timestamp)).Before(time.Now()) ||
|
if !pubKey.Verify() {
|
||||||
!auth.VerifySignature(profilePubKeyBytes, signature) {
|
|
||||||
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.invalid_public_key_signature")}
|
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.invalid_public_key_signature")}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
profilePubKey = &pubKey
|
||||||
} else if d.EnforceSecureProfile {
|
} else if d.EnforceSecureProfile {
|
||||||
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.missing_public_key")}
|
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.missing_public_key")}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var properties []property
|
|
||||||
//auth
|
//auth
|
||||||
if d.OnlineMode {
|
if d.OnlineMode {
|
||||||
var resp *auth.Resp
|
var resp *auth.Resp
|
||||||
//Auth, Encrypt
|
//Auth, Encrypt
|
||||||
var key any
|
resp, err = auth.Encrypt(conn, name, profilePubKey.PubKey)
|
||||||
key, err = x509.ParsePKIXPublicKey(profilePubKeyBytes)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if rsaPubKey, ok := key.(*rsa.PublicKey); !ok {
|
|
||||||
err = errors.New("expect RSA public key")
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
profilePubKey = rsaPubKey
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err = auth.Encrypt(conn, name, profilePubKey)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
name = resp.Name
|
name = resp.Name
|
||||||
id = resp.ID
|
id = resp.ID
|
||||||
properties = *(*[]property)(unsafe.Pointer(&resp.Properties))
|
properties = resp.Properties
|
||||||
} else {
|
} else {
|
||||||
// offline-mode UUID
|
// offline-mode UUID
|
||||||
id = offline.NameToUUID(name)
|
id = offline.NameToUUID(name)
|
||||||
@ -156,21 +137,6 @@ type GameProfile struct {
|
|||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
type property auth.Property
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
type wrongPacketErr struct {
|
type wrongPacketErr struct {
|
||||||
expect, get int32
|
expect, get int32
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,6 @@ func NewPlayerList(maxPlayers int) *PlayerList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientJoin implement Component for PlayerList
|
|
||||||
func (p *PlayerList) ClientJoin(client PlayerListClient, player PlayerSample) {
|
func (p *PlayerList) ClientJoin(client PlayerListClient, player PlayerSample) {
|
||||||
p.playersLock.Lock()
|
p.playersLock.Lock()
|
||||||
defer p.playersLock.Unlock()
|
defer p.playersLock.Unlock()
|
||||||
@ -49,6 +48,7 @@ func (p *PlayerList) ClientLeft(client PlayerListClient) {
|
|||||||
delete(p.players, client)
|
delete(p.players, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckPlayer implements LoginChecker for PlayerList
|
||||||
func (p *PlayerList) CheckPlayer(string, uuid.UUID, int32) (ok bool, reason chat.Message) {
|
func (p *PlayerList) CheckPlayer(string, uuid.UUID, int32) (ok bool, reason chat.Message) {
|
||||||
p.playersLock.Lock()
|
p.playersLock.Lock()
|
||||||
defer p.playersLock.Unlock()
|
defer p.playersLock.Unlock()
|
||||||
@ -87,3 +87,17 @@ func (p *PlayerList) PlayerSamples() (sample []PlayerSample) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PlayerList) Len() int {
|
||||||
|
p.playersLock.Lock()
|
||||||
|
defer p.playersLock.Unlock()
|
||||||
|
return len(p.players)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PlayerList) Range(f func(PlayerListClient, PlayerSample)) {
|
||||||
|
p.playersLock.Lock()
|
||||||
|
defer p.playersLock.Unlock()
|
||||||
|
for client, player := range p.players {
|
||||||
|
f(client, player)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -70,7 +70,7 @@ func (s *Server) acceptConn(conn *net.Conn) {
|
|||||||
case 1: // list ping
|
case 1: // list ping
|
||||||
s.acceptListPing(conn)
|
s.acceptListPing(conn)
|
||||||
case 2: // login
|
case 2: // login
|
||||||
name, id, profilePubKey, err := s.AcceptLogin(conn, protocol)
|
name, id, profilePubKey, properties, err := s.AcceptLogin(conn, protocol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var loginErr *LoginFailErr
|
var loginErr *LoginFailErr
|
||||||
if errors.As(err, &loginErr) {
|
if errors.As(err, &loginErr) {
|
||||||
@ -84,6 +84,6 @@ func (s *Server) acceptConn(conn *net.Conn) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.AcceptPlayer(name, id, profilePubKey, protocol, conn)
|
s.AcceptPlayer(name, id, profilePubKey, properties, protocol, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user