This repository has been archived by the owner on Jun 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
initialize.go
115 lines (95 loc) · 3.19 KB
/
initialize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package mcproto
import (
"errors"
"fmt"
"net"
"github.com/BRA1L0R/go-mcproto/packets"
"github.com/BRA1L0R/go-mcproto/packets/models"
)
// LoginDisconnectError rapresents the disconnection of the client during the login state,
// during the initialization process
type LoginDisconnectError struct {
// Reason is the json encoded Reason for which the client has been
// disconnected from the server
Reason string
}
func (disconnectError *LoginDisconnectError) Error() string {
return fmt.Sprintf("mcproto: server disconnected the client during initialization with message: %s", disconnectError.Reason)
}
// Connect initializes the connection to the server.
//
// host must have the format of "host:port" as a port has to be specified in order
// to open a connection. 25565 is not taken for granted
func (mc *Client) Connect(host string) error {
conn, err := net.Dial("tcp", host)
if err != nil {
return err
}
mc.connection = conn.(*net.TCPConn)
return nil
}
// Initializes the connection to the server by sending
// the handshake packet and the login packet
//
// host is the server fqdn or ip address of the server, port is the uint16 port where the server is listening on
//
// username is the in-game username the client will send to the server during handshaking. Might differ from the actual
// in-game username as the server sends a confirmation of it after the login state.
func (mc *Client) Initialize(host string, port uint16, protocolVersion int32, username string) error {
if err := mc.Connect(fmt.Sprintf("%s:%v", host, port)); err != nil {
return err
}
// Create handshake packet with the latest protocol version
// and state information
// NOTE: serveraddress and serverport are not used by the
// notchian server, but it's best practice to fill them
hp := models.HandshakePacket{
MinecraftPacket: packets.MinecraftPacket{PacketID: 0x00},
ProtocolVersion: protocolVersion,
ServerAddress: host,
ServerPort: port,
NextState: 2,
}
err := mc.WritePacket(&hp)
if err != nil {
return err
}
loginPacket := models.LoginStartPacket{
MinecraftPacket: packets.MinecraftPacket{PacketID: 0x00},
Name: username,
}
err = mc.WritePacket(&loginPacket)
if err != nil {
return err
}
for {
p, err := mc.ReceivePacket()
if err != nil {
return err
}
switch p.PacketID {
case 0x00: // disconnected
disconnectPacket := new(models.DisconnectPacket)
if err := p.DeserializeData(disconnectPacket); err != nil {
return err
}
return &LoginDisconnectError{Reason: disconnectPacket.Reason}
case 0x01: // encryption request
return errors.New("mcproto: received an encryption request which means the server is in online mode. Online mode not currently supported")
case 0x03: // set compression
setCompression := new(models.SetCompressionPacket)
err := p.DeserializeData(setCompression)
if err != nil {
return err
}
if setCompression.Treshold < 0 {
return errors.New("mcproto: server sent a set compression packet with a negative treshold")
}
mc.CompressionTreshold = setCompression.Treshold
case 0x02: // login success
loginSuccess := new(models.LoginSuccessPacket)
err := p.DeserializeData(loginSuccess)
return err
}
}
}