This commit is contained in:
Tnze
2022-12-06 22:39:45 +08:00
12 changed files with 249 additions and 85 deletions

View File

@ -17,7 +17,7 @@ type PublicKey struct {
Signature []byte
}
func (p *PublicKey) WriteTo(w io.Writer) (n int64, err error) {
func (p PublicKey) WriteTo(w io.Writer) (n int64, err error) {
pubKeyEncoded, err := x509.MarshalPKIXPublicKey(p.PubKey)
if err != nil {
return 0, err

View File

@ -4,6 +4,7 @@ import (
"container/list"
"strconv"
"sync"
"sync/atomic"
pk "github.com/Tnze/go-mc/net/packet"
)
@ -28,21 +29,27 @@ func (s WritePacketError) Unwrap() error {
return s.Err
}
type PacketQueue struct {
queue *list.List
closed bool
cond sync.Cond
type PacketQueue interface {
Push(packet pk.Packet)
Pull() (packet pk.Packet, ok bool)
Close()
}
func NewPacketQueue() (p *PacketQueue) {
p = &PacketQueue{
func NewPacketQueue() (p PacketQueue) {
p = &LinkedListPacketQueue{
queue: list.New(),
cond: sync.Cond{L: new(sync.Mutex)},
}
return p
}
func (p *PacketQueue) Push(packet pk.Packet) {
type LinkedListPacketQueue struct {
queue *list.List
closed bool
cond sync.Cond
}
func (p *LinkedListPacketQueue) Push(packet pk.Packet) {
p.cond.L.Lock()
if !p.closed {
p.queue.PushBack(packet)
@ -51,7 +58,7 @@ func (p *PacketQueue) Push(packet pk.Packet) {
p.cond.L.Unlock()
}
func (p *PacketQueue) Pull() (packet pk.Packet, ok bool) {
func (p *LinkedListPacketQueue) Pull() (packet pk.Packet, ok bool) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
for p.queue.Front() == nil && !p.closed {
@ -65,9 +72,36 @@ func (p *PacketQueue) Pull() (packet pk.Packet, ok bool) {
return
}
func (p *PacketQueue) Close() {
func (p *LinkedListPacketQueue) Close() {
p.cond.L.Lock()
p.closed = true
p.cond.Broadcast()
p.cond.L.Unlock()
}
type ChannelPacketQueue struct {
c chan pk.Packet
closed atomic.Bool
}
func (c ChannelPacketQueue) Push(packet pk.Packet) {
if c.closed.Load() {
return
}
select {
case c.c <- packet:
default:
c.closed.Store(true)
}
}
func (c ChannelPacketQueue) Pull() (packet pk.Packet, ok bool) {
if !c.closed.Load() {
packet, ok = <-c.c
}
return
}
func (c ChannelPacketQueue) Close() {
c.closed.Store(true)
}

View File

@ -118,7 +118,6 @@ func (k *KeepAlive) tickPlayer(c KeepAliveClient) {
elem, ok := k.listIndex[c]
if !ok {
panic(errors.New("keepalive: fail to tick player: client not found"))
return
}
if elem.Prev() == nil {
if !k.waitTimer.Stop() {
@ -156,7 +155,6 @@ func keepAliveSetTimer(l *list.List, timer *time.Timer, interval time.Duration)
}
}
timer.Reset(interval)
return
}
type keepAliveItem struct {

View File

@ -64,21 +64,13 @@ func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name s
}
var (
hasPubKey pk.Boolean
pubKey auth.PublicKey
hasUUID pk.Boolean
profileUUID pk.UUID // ignored
pubKey pk.Option[auth.PublicKey, *auth.PublicKey]
profileUUID pk.Option[pk.UUID, *pk.UUID] // ignored
)
err = p.Scan(
(*pk.String)(&name), // decode username as pk.String
&hasPubKey, pk.Opt{
Has: &hasPubKey,
Field: &pubKey,
},
&hasUUID, pk.Opt{
Has: &hasUUID,
Field: &profileUUID,
},
&pubKey,
&profileUUID,
)
if err != nil {
return
@ -86,12 +78,12 @@ func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name s
// auth
if d.OnlineMode {
if hasPubKey {
if !pubKey.Verify() {
if pubKey.Has {
if !pubKey.Val.Verify() {
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.invalid_public_key_signature")}
return
}
profilePubKey = &pubKey
profilePubKey = &pubKey.Val
} else if d.EnforceSecureProfile {
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.missing_public_key")}
return
@ -99,7 +91,7 @@ func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name s
var resp *auth.Resp
// Auth, Encrypt
resp, err = auth.Encrypt(conn, name, pubKey.PubKey)
resp, err = auth.Encrypt(conn, name, pubKey.Val.PubKey)
if err != nil {
return
}