Skip to content

Commit 8ace27a

Browse files
committed
Fix
1 parent 3bec127 commit 8ace27a

File tree

17 files changed

+463
-117
lines changed

17 files changed

+463
-117
lines changed

protocol/net/authentication.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ func authurl(u, h string) string {
1818
return "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" + u + "&serverId=" + h
1919
}
2020

21-
func (c *Conn) authenticate() error {
22-
key, err := x509.MarshalPKIXPublicKey(&c.listener.privKey.PublicKey)
21+
func (conn *Conn) authenticate() error {
22+
key, err := x509.MarshalPKIXPublicKey(&conn.listener.privKey.PublicKey)
2323
if err != nil {
2424
return err
2525
}
26-
hash := c.sessionHash(key)
27-
res, err := http.Get(authurl(c.username, hash))
26+
hash := conn.sessionHash(key)
27+
res, err := http.Get(authurl(conn.username, hash))
2828
if err != nil {
2929
return err
3030
}
@@ -49,19 +49,19 @@ func (c *Conn) authenticate() error {
4949
return err
5050
}
5151

52-
c.username = response.Name
53-
c.uuid, err = uuid.Parse(response.ID)
52+
conn.username = response.Name
53+
conn.uuid, err = uuid.Parse(response.ID)
5454
if err != nil {
5555
return err
5656
}
57-
c.properties = *(*[]login.Property)(unsafe.Pointer(&response.Properties))
57+
conn.properties = *(*[]login.Property)(unsafe.Pointer(&response.Properties))
5858

5959
return nil
6060
}
6161

62-
func (c *Conn) sessionHash(publicKey []byte) string {
62+
func (conn *Conn) sessionHash(publicKey []byte) string {
6363
hash := sha1.New()
64-
hash.Write(c.sharedSecret)
64+
hash.Write(conn.sharedSecret)
6565
hash.Write(publicKey)
6666

6767
sum := hash.Sum(nil)

protocol/net/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
type Config struct {
1313
Status StatusProvider
1414

15-
IP net.IP
16-
Port int
17-
CompressionThreshold int32
18-
Encrypt bool
19-
Authenticate bool
20-
AcceptTransfers bool
15+
PacketWriteChanBuffer int
16+
IP net.IP
17+
Port int
18+
CompressionThreshold int32
19+
Encrypt bool
20+
Authenticate bool
21+
AcceptTransfers bool
2122
}
2223

2324
type StatusProvider func(*Conn) status.StatusResponseData

protocol/net/conn.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Conn struct {
5555
usesForge bool
5656
read_mu sync.Mutex
5757
write_mu sync.Mutex
58+
59+
packetWriteChan chan packet.Encodeable
5860
}
5961

6062
func (conn *Conn) UsesForge() bool {
@@ -347,6 +349,27 @@ func (conn *Conn) ReadPacket() (decoded packet.Decodeable, interceptionStopped b
347349
}
348350
}
349351

352+
// PushPacket adds a packet to the queue without waiting for it to be sent
353+
func (conn *Conn) PushPacket(pk packet.Encodeable) {
354+
conn.packetWriteChan <- pk
355+
}
356+
357+
func (conn *Conn) packetWriteChanListen() {
358+
go func() {
359+
for pk := range conn.packetWriteChan {
360+
if conn.encrypted {
361+
conn.write_mu.Lock()
362+
}
363+
364+
conn.WritePacket(pk)
365+
366+
if conn.encrypted {
367+
conn.write_mu.Unlock()
368+
}
369+
}
370+
}()
371+
}
372+
350373
func (conn *Conn) writeLegacyStatus(status status.StatusResponseData) {
351374
protocolString := fmt.Sprint(status.Version.Protocol)
352375
onlineString := fmt.Sprint(status.Players.Online)
@@ -487,7 +510,7 @@ func (conn *Conn) handleHandshake() bool {
487510
return false
488511
}
489512
if conn.listener.cfg.Authenticate {
490-
if err := conn.authenticate(); err != nil {
513+
if err := conn.authenticate(); err != nil && conn.listener.cfg.Authenticate {
491514
conn.WritePacket(&login.Disconnect{Reason: text.TextComponent{Text: "This server uses authenticated encryption mode, and you are using a cracked account."}})
492515
return false
493516
}
@@ -503,10 +526,9 @@ func (conn *Conn) handleHandshake() bool {
503526
conn.compressionSet = true
504527

505528
suc := &login.LoginSuccess{
506-
UUID: conn.uuid,
507-
Username: conn.username,
508-
Properties: conn.properties,
509-
StrictErrorHandling: true,
529+
UUID: conn.uuid,
530+
Username: conn.username,
531+
Properties: conn.properties,
510532
}
511533
if err := conn.WritePacket(suc); err != nil {
512534
return false

protocol/net/encryption.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,57 @@ import (
1111
"github.com/zeppelinmc/zeppelin/protocol/net/packet/login"
1212
)
1313

14-
func (c *Conn) encryptd(plaintext, dst []byte) {
15-
c.encrypter.XORKeyStream(dst, plaintext)
14+
func (conn *Conn) encryptd(plaintext, dst []byte) {
15+
conn.encrypter.XORKeyStream(dst, plaintext)
1616
}
1717

18-
func (c *Conn) decryptd(ciphertext, dst []byte) {
19-
c.decrypter.XORKeyStream(dst, ciphertext)
18+
func (conn *Conn) decryptd(ciphertext, dst []byte) {
19+
conn.decrypter.XORKeyStream(dst, ciphertext)
2020
}
2121

22-
func (c *Conn) encrypt() error {
23-
key, err := x509.MarshalPKIXPublicKey(&c.listener.privKey.PublicKey)
22+
func (conn *Conn) encrypt() error {
23+
key, err := x509.MarshalPKIXPublicKey(&conn.listener.privKey.PublicKey)
2424
if err != nil {
2525
return err
2626
}
2727

2828
verifyToken := make([]byte, 4)
2929
rand.Read(verifyToken)
3030

31-
c.WritePacket(&login.EncryptionRequest{
31+
conn.WritePacket(&login.EncryptionRequest{
3232
PublicKey: key,
3333
VerifyToken: verifyToken,
34-
ShouldAuthenticate: c.listener.cfg.Authenticate,
34+
ShouldAuthenticate: conn.listener.cfg.Authenticate,
3535
})
36-
p, s, err := c.ReadPacket()
36+
p, s, err := conn.ReadPacket()
3737
if err != nil || s {
3838
return err
3939
}
4040
res, ok := p.(*login.EncryptionResponse)
4141
if !ok {
4242
return fmt.Errorf("unsuccessful encryption")
4343
}
44-
c.encrypted = ok
44+
conn.encrypted = ok
4545

46-
c.sharedSecret, err = rsa.DecryptPKCS1v15(nil, c.listener.privKey, res.SharedSecret)
46+
conn.sharedSecret, err = rsa.DecryptPKCS1v15(nil, conn.listener.privKey, res.SharedSecret)
4747
if err != nil {
4848
return err
4949
}
50-
c.verifyToken, err = rsa.DecryptPKCS1v15(nil, c.listener.privKey, res.VerifyToken)
50+
conn.verifyToken, err = rsa.DecryptPKCS1v15(nil, conn.listener.privKey, res.VerifyToken)
5151
if err != nil {
5252
return err
5353
}
5454

55-
if [4]byte(c.verifyToken) != [4]byte(verifyToken) {
55+
if [4]byte(conn.verifyToken) != [4]byte(verifyToken) {
5656
return fmt.Errorf("unsuccessful encryption")
5757
}
5858

59-
block, err := aes.NewCipher(c.sharedSecret)
59+
block, err := aes.NewCipher(conn.sharedSecret)
6060
if err != nil {
6161
return err
6262
}
63-
c.encrypter = cfb8.NewCFB8(block, c.sharedSecret, false)
64-
c.decrypter = cfb8.NewCFB8(block, c.sharedSecret, true)
63+
conn.encrypter = cfb8.NewCFB8(block, conn.sharedSecret, false)
64+
conn.decrypter = cfb8.NewCFB8(block, conn.sharedSecret, true)
6565

6666
return nil
6767
}

protocol/net/io/encoding/encoding.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func VarInt(data []byte) (int32, []byte, error) {
8181
currentByte = data[0]
8282
data = data[1:]
8383

84-
value |= int32((currentByte & 127)) << position
84+
value |= int32(currentByte&127) << position
8585

8686
if (currentByte & 128) == 0 {
8787
break
@@ -99,10 +99,10 @@ func VarInt(data []byte) (int32, []byte, error) {
9999

100100
func ReadVarInt(r io.Reader) (int32, error) {
101101
var (
102-
position int32
103-
currentByte byte
104-
CONTINUE_BIT byte = 128
105-
SEGMENT_BITS byte = 127
102+
position int32
103+
currentByte byte
104+
continueBit byte = 128
105+
segmentBits byte = 127
106106

107107
value int32
108108
)
@@ -112,9 +112,9 @@ func ReadVarInt(r io.Reader) (int32, error) {
112112
return value, err
113113
}
114114

115-
value |= int32((currentByte & SEGMENT_BITS)) << position
115+
value |= int32(currentByte&segmentBits) << position
116116

117-
if (currentByte & CONTINUE_BIT) == 0 {
117+
if (currentByte & continueBit) == 0 {
118118
break
119119
}
120120

@@ -130,15 +130,15 @@ func ReadVarInt(r io.Reader) (int32, error) {
130130

131131
func AppendVarLong(data []byte, value int64) []byte {
132132
var (
133-
CONTINUE_BIT int64 = 128
134-
SEGMENT_BITS int64 = 127
133+
continueBit int64 = 128
134+
segmentBits int64 = 127
135135
)
136136
for {
137-
if (value & ^SEGMENT_BITS) == 0 {
137+
if (value & ^segmentBits) == 0 {
138138
return append(data, byte(value))
139139
}
140140

141-
data = append(data, byte((value&SEGMENT_BITS)|CONTINUE_BIT))
141+
data = append(data, byte((value&segmentBits)|continueBit))
142142

143143
value >>= 7
144144
}
@@ -180,7 +180,7 @@ func (set FixedBitSet) Get(i int) bool {
180180
}
181181

182182
func (set FixedBitSet) Set(i int) {
183-
set[i/8] |= (1 << (i % 8))
183+
set[i/8] |= 1 << (i % 8)
184184
}
185185

186186
func (set FixedBitSet) Unset(i int) {

protocol/net/listener.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package net
44
import (
55
"crypto/rsa"
66
"fmt"
7+
"github.com/zeppelinmc/zeppelin/protocol/net/packet"
78
"net"
89

910
"github.com/zeppelinmc/zeppelin/protocol/text"
@@ -63,6 +64,8 @@ func (l *Listener) newConn(c net.Conn) *Conn {
6364
conn := &Conn{
6465
Conn: c,
6566
listener: l,
67+
68+
packetWriteChan: make(chan packet.Encodeable, l.cfg.PacketWriteChanBuffer),
6669
}
6770

6871
return conn

protocol/net/metadata/metadata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const (
126126
IsInRiptideSpinAttack
127127
)
128128

129-
// base entity
129+
// base state
130130
const (
131131
// Byte (0)
132132
BaseIndex = iota
@@ -146,7 +146,7 @@ const (
146146
TicksFrozenInPowderedSnowIndex
147147
)
148148

149-
// living entity extends entity
149+
// living state extends state
150150
const (
151151
// Byte (0)
152152
LivingEntityHandstatesIndex = iota + 8
@@ -164,7 +164,7 @@ const (
164164
LivingEntitySleepingBedPositionIndex
165165
)
166166

167-
// player extends living entity
167+
// player extends living state
168168
const (
169169
// Float (3)
170170
PlayerAdditionalHeartsIndex = iota + 15

protocol/net/metadata/new.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package metadata
2+
3+
// Player returns the default player metadata object
4+
func Player(health float32) Metadata {
5+
return Metadata{
6+
// Entity
7+
BaseIndex: Byte(0),
8+
AirTicksIndex: VarInt(300),
9+
CustomNameIndex: OptionalTextComponent(nil),
10+
IsCustomNameVisibleIndex: Boolean(false),
11+
IsSilentIndex: Boolean(false),
12+
HasNoGravityIndex: Boolean(false),
13+
PoseIndex: Standing,
14+
TicksFrozenInPowderedSnowIndex: VarInt(0),
15+
// Living Entity extends Entity
16+
LivingEntityHandstatesIndex: Byte(0),
17+
LivingEntityHealthIndex: Float(health), // this one is actually stored in data (others are too but arent used yet)
18+
//LivingEntityPotionEffectColorIndex: VarInt(0),
19+
LivingEntityPotionEffectAmbientIndex: Boolean(false),
20+
LivingEntityArrowCountIndex: VarInt(0),
21+
LivingEntityBeeStingersCountIndex: VarInt(0),
22+
LivingEntitySleepingBedPositionIndex: OptionalPosition(nil),
23+
// Player extends Living Entity
24+
PlayerAdditionalHeartsIndex: Float(0),
25+
PlayerScoreIndex: VarInt(0),
26+
PlayerDisplayedSkinPartsIndex: Byte(0),
27+
PlayerMainHandIndex: Byte(1),
28+
}
29+
}

0 commit comments

Comments
 (0)