forked from seishun/SteamPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steam++.cpp
197 lines (160 loc) · 6.59 KB
/
steam++.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cryptopp/modes.h>
#include "cmclient.h"
SteamID::SteamID(std::uint64_t steamID64) :
steamID64(steamID64) {}
SteamID::operator std::uint64_t() const {
return steamID64;
}
SteamClient::SteamClient(
std::function<void(std::size_t length, std::function<void(unsigned char* buffer)> fill)> write,
std::function<void(std::function<void()> callback, int timeout)> set_interval
) : cmClient(new CMClient(std::move(write))), setInterval(std::move(set_interval)) {}
SteamClient::~SteamClient() {
delete cmClient;
}
void SteamClient::LogOn(const char* username, const char* password, const unsigned char hash[20], const char* code, SteamID steamID) {
if (steamID)
cmClient->steamID = steamID;
CMsgClientLogon logon;
logon.set_account_name(username);
logon.set_password(password);
logon.set_protocol_version(65575);
if (hash) {
logon.set_sha_sentryfile(hash, 20);
}
if (code) {
logon.set_auth_code(code);
}
cmClient->WriteMessage(EMsg::ClientLogon, logon);
}
void SteamClient::SetPersonaState(EPersonaState state) {
CMsgClientChangeStatus change_status;
change_status.set_persona_state(static_cast<google::protobuf::uint32>(state));
cmClient->WriteMessage(EMsg::ClientChangeStatus, change_status);
}
void SteamClient::JoinChat(SteamID chat) {
if (chat.type == static_cast<unsigned>(EAccountType::Clan)) {
// this is a ClanID - convert to its respective ChatID
chat.instance = static_cast<unsigned>(0x100000 >> 1); // TODO: this should be defined somewhere else
chat.type = static_cast<unsigned>(EAccountType::Chat);
}
cmClient->WriteMessage(EMsg::ClientJoinChat, sizeof(MsgClientJoinChat), [&chat](unsigned char* buffer) {
auto join_chat = new (buffer) MsgClientJoinChat;
join_chat->steamIdChat = chat;
});
}
void SteamClient::LeaveChat(SteamID chat) {
// TODO: move this somwehre else
if (chat.type == static_cast<unsigned>(EAccountType::Clan)) {
// this is a ClanID - convert to its respective ChatID
chat.instance = static_cast<unsigned>(0x100000 >> 1); // TODO: this should be defined somewhere else
chat.type = static_cast<unsigned>(EAccountType::Chat);
}
cmClient->WriteMessage(EMsg::ClientChatMemberInfo, sizeof(MsgClientChatMemberInfo) + 20, [&](unsigned char* buffer) {
auto leave_chat = new (buffer) MsgClientChatMemberInfo;
leave_chat->steamIdChat = chat;
leave_chat->type = static_cast<unsigned>(EChatInfoType::StateChange);
auto payload = buffer + sizeof(MsgClientChatMemberInfo);
*reinterpret_cast<std::uint64_t*>(payload) = cmClient->steamID; // chatter_acted_on
*reinterpret_cast<EChatMemberStateChange*>(payload + 8) = EChatMemberStateChange::Left; // state_change
*reinterpret_cast<std::uint64_t*>(payload + 8 + 4) = cmClient->steamID; // chatter_acted_by
});
}
void SteamClient::SendChatMessage(SteamID chat, const char* message) {
// TODO: move this somwehre else
if (chat.type == static_cast<unsigned>(EAccountType::Clan)) {
// this is a ClanID - convert to its respective ChatID
chat.instance = static_cast<unsigned>(0x100000 >> 1); // TODO: this should be defined somewhere else
chat.type = static_cast<unsigned>(EAccountType::Chat);
}
cmClient->WriteMessage(EMsg::ClientChatMsg, sizeof(MsgClientChatMsg) + std::strlen(message) + 1, [&](unsigned char* buffer) {
auto send_msg = new (buffer) MsgClientChatMsg;
send_msg->chatMsgType = static_cast<std::uint32_t>(EChatEntryType::ChatMsg);
send_msg->steamIdChatRoom = chat;
send_msg->steamIdChatter = cmClient->steamID;
std::strcpy(reinterpret_cast<char*>(buffer + sizeof(MsgClientChatMsg)), message);
});
}
void SteamClient::SendPrivateMessage(SteamID user, const char* message) {
CMsgClientFriendMsg msg;
msg.set_steamid(user);
msg.set_message(message);
msg.set_chat_entry_type(static_cast<google::protobuf::uint32>(EChatEntryType::ChatMsg));
cmClient->WriteMessage(EMsg::ClientFriendMsg, msg);
}
void SteamClient::SendTyping(SteamID user) {
CMsgClientFriendMsg msg;
msg.set_steamid(user);
msg.set_chat_entry_type(static_cast<google::protobuf::uint32>(EChatEntryType::Typing));
cmClient->WriteMessage(EMsg::ClientFriendMsg, msg);
}
void SteamClient::RequestUserInfo(std::size_t count, SteamID users[]) {
CMsgClientRequestFriendData request;
while (count--)
request.add_friends(users[count]);
// TODO: allow custom flags
request.set_persona_state_requested(282);
cmClient->WriteMessage(EMsg::ClientRequestFriendData, request);
}
std::size_t SteamClient::connected() {
packetLength = 0;
cmClient->steamID.ID = 0;
cmClient->sessionID = 0;
cmClient->encrypted = false;
return 8;
}
std::size_t SteamClient::readable(const unsigned char* input) {
if (!packetLength) {
packetLength = *reinterpret_cast<const std::uint32_t*>(input);
assert(std::equal(MAGIC, MAGIC + 4, input + 4));
return packetLength;
}
if (cmClient->encrypted) {
byte iv[16];
ECB_Mode<AES>::Decryption(cmClient->sessionKey, sizeof(cmClient->sessionKey)).ProcessData(iv, input, 16);
auto crypted_data = input + 16;
CBC_Mode<AES>::Decryption d(cmClient->sessionKey, sizeof(cmClient->sessionKey), iv);
// I don't see any way to get the decrypted size other than to use a string
std::string output;
ArraySource(
crypted_data,
packetLength - 16,
true,
new StreamTransformationFilter(d, new StringSink(output))
);
ReadMessage(reinterpret_cast<const unsigned char*>(output.data()), output.length());
} else {
ReadMessage(input, packetLength);
}
packetLength = 0;
return 8;
}
void SteamClient::ReadMessage(const unsigned char* data, std::size_t length) {
auto raw_emsg = *reinterpret_cast<const std::uint32_t*>(data);
auto emsg = static_cast<EMsg>(raw_emsg & ~PROTO_MASK);
// first figure out the header type
if (emsg == EMsg::ChannelEncryptRequest || emsg == EMsg::ChannelEncryptResult) {
auto header = reinterpret_cast<const MsgHdr*>(data);
HandleMessage(emsg, data + sizeof(MsgHdr), length - sizeof(MsgHdr), header->sourceJobID);
} else if (raw_emsg & PROTO_MASK) {
auto header = reinterpret_cast<const MsgHdrProtoBuf*>(data);
CMsgProtoBufHeader proto;
proto.ParseFromArray(header->proto, header->headerLength);
if (!cmClient->sessionID && header->headerLength > 0) {
cmClient->sessionID = proto.client_sessionid();
cmClient->steamID = proto.steamid();
}
HandleMessage(
emsg,
data + sizeof(MsgHdrProtoBuf) + header->headerLength,
length - sizeof(MsgHdrProtoBuf) - header->headerLength,
proto.jobid_source()
);
} else {
auto header = reinterpret_cast<const ExtendedClientMsgHdr*>(data);
HandleMessage(emsg, data + sizeof(ExtendedClientMsgHdr), length - sizeof(ExtendedClientMsgHdr), header->sourceJobID);
}
}