This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
/
AuthenticateValidateTicketMessage.go
127 lines (107 loc) · 3.18 KB
/
AuthenticateValidateTicketMessage.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
116
117
118
119
120
121
122
123
124
125
126
127
package handlers
import (
"bytes"
"code.google.com/p/goprotobuf/proto"
"encoding/binary"
"git.cloudrack.io/aiw3/np-server/np/protocol"
"git.cloudrack.io/aiw3/np-server/np/reply"
"git.cloudrack.io/aiw3/np-server/np/storage"
"git.cloudrack.io/aiw3/np-server/np/structs"
//"git.cloudrack.io/aiw3/np-server/utils"
//"github.com/pzduniak/logger"
"net"
//"time"
)
func RPCAuthenticateValidateTicketMessage(conn net.Conn, connection_data *structs.ConnData, packet_data *structs.PacketData) error {
// Unmarshal the data
msg := new(protocol.AuthenticateValidateTicketMessage)
err := proto.Unmarshal(packet_data.Content, msg)
if err != nil {
return err
}
// Create a new buffer based on the ticket data, in order to read data from it.
buf := bytes.NewBuffer(msg.Ticket)
// Structure:
// <xxxx> <xxxxxxxx> <xxxxxxxx> <xxxx>
// version clientID serverID timeIssued
var version, issued uint32
var clientId, serverId uint64
err = binary.Read(buf, binary.LittleEndian, &version)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &clientId)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &serverId)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &issued)
if err != nil {
return err
}
// Only version 1 is valid
if version == 1 {
// Verify that the request isn't spoofed
if connection_data.Npid == serverId {
// Get client's connection
session := storage.GetClientConnection(clientId)
// Make sure the session is here
if session == nil {
return nil
}
// Make sure it's not a server
if session.IsServer {
return nil
}
// Make sure that the IDs are valid
if session.Npid != clientId {
return reply.Reply(conn, packet_data.Header.Id, &protocol.AuthenticateValidateTicketResultMessage{
Result: proto.Int32(1),
Npid: &clientId,
GroupID: proto.Int32(1),
})
}
// Make sure there's no aCI detection
if session.IsUnclean {
return reply.Reply(conn, packet_data.Header.Id, &protocol.AuthenticateValidateTicketResultMessage{
Result: proto.Int32(1),
Npid: &clientId,
GroupID: proto.Int32(1),
})
}
// Set the server ID
session.ServerId = serverId
// Heartbeat detection
/*time.AfterFunc(time.Minute*2, func() {
if session != nil && session.Valid && session.Username != "" && !session.IsUnclean {
if session.LastCI.IsZero() { //Before(time.Now().Truncate(time.Minute)) {
err = utils.BanUser(session.Username, 10000, time.Hour*24*14)
if err != nil {
logger.Warning(err)
}
err = utils.KickUser(serverId, session.Npid, 10000)
if err != nil {
logger.Warning(err)
}
session.IsUnclean = true
}
}
})*/
// Reply that everything is fine
return reply.Reply(conn, packet_data.Header.Id, &protocol.AuthenticateValidateTicketResultMessage{
Result: proto.Int32(0),
Npid: &clientId,
GroupID: proto.Int32(1),
})
}
}
// Wrong version or wrong NPID
return reply.Reply(conn, packet_data.Header.Id, &protocol.AuthenticateValidateTicketResultMessage{
Result: proto.Int32(1),
Npid: &clientId,
GroupID: proto.Int32(1),
})
}