/
Bot.cs
277 lines (225 loc) · 9.73 KB
/
Bot.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Text;
using System.Net;
using System.Threading;
using SteamKit2;
namespace SteamBot
{
public class Bot
{
public bool IsLoggedIn = false;
public string DisplayName { get; private set; }
public string ChatResponse;
public ulong[] Admins;
public SteamFriends SteamFriends;
public SteamClient SteamClient;
public SteamTrading SteamTrade;
public SteamUser SteamUser;
public Trade CurrentTrade;
public Trade.TradeListener TradeListener;
public bool IsDebugMode = false;
string Username;
string Password;
string AuthCode;
string apiKey;
string sessionId;
string token;
public Bot(Configuration.BotInfo config, string apiKey, bool debug = false)
{
Username = config.Username;
Password = config.Password;
DisplayName = config.DisplayName;
ChatResponse = config.ChatResponse;
Admins = config.Admins;
this.apiKey = apiKey;
AuthCode = null;
TradeListener = new TradeEnterTradeListener(this);
// Hacking around https
ServicePointManager.ServerCertificateValidationCallback += SteamWeb.ValidateRemoteCertificate;
SteamClient = new SteamClient();
SteamTrade = SteamClient.GetHandler<SteamTrading>();
SteamUser = SteamClient.GetHandler<SteamUser>();
SteamFriends = SteamClient.GetHandler<SteamFriends>();
SteamClient.Connect();
Thread CallbackThread = new Thread(() => // Callback Handling
{
while (true) {
CallbackMsg msg = SteamClient.WaitForCallback (true);
HandleSteamMessage (msg);
}
});
new Thread(() => // Trade Polling if needed
{
while (true) {
Thread.Sleep (800);
if (CurrentTrade != null) {
try {
CurrentTrade.Poll ();
} catch (Exception e) {
Console.Write ("Error polling the trade: ");
Console.WriteLine (e);
}
}
}
}).Start ();
CallbackThread.Start();
CallbackThread.Join();
}
void HandleSteamMessage (CallbackMsg msg)
{
#region Login
msg.Handle<SteamClient.ConnectedCallback> (callback =>
{
PrintConsole ("Connection Callback: " + callback.Result, ConsoleColor.Magenta);
if (callback.Result == EResult.OK) {
SteamUser.LogOn (new SteamUser.LogOnDetails
{
Username = Username,
Password = Password,
AuthCode = AuthCode
});
} else {
PrintConsole ("Failed to Connect to the steam community!\n", ConsoleColor.Red);
SteamClient.Connect ();
}
});
msg.Handle<SteamUser.LoggedOnCallback> (callback =>
{
PrintConsole ("Logged on callback: " + callback.Result, ConsoleColor.Magenta);
if (callback.Result != EResult.OK)
{
PrintConsole("Login Failure: " + callback.Result, ConsoleColor.Red);
}
if (callback.Result == EResult.AccountLogonDenied) {
PrintConsole("This account is protected by Steam Guard. Enter the authentication code sent to the associated email address", ConsoleColor.DarkYellow);
AuthCode = Console.ReadLine();
}
});
msg.Handle<SteamUser.LoginKeyCallback> (callback =>
{
while (true) {
if (Authenticate (callback))
{
PrintConsole ("Authenticated.");
break;
}
else
{
PrintConsole ("Retrying auth...", ConsoleColor.Red);
Thread.Sleep (2000);
}
}
PrintConsole ("Downloading schema...", ConsoleColor.Magenta);
Trade.CurrentSchema = Schema.FetchSchema (apiKey);
PrintConsole ("All Done!", ConsoleColor.Magenta);
SteamFriends.SetPersonaName ("[SteamBot] "+DisplayName);
SteamFriends.SetPersonaState (EPersonaState.LookingToTrade);
PrintConsole ("Successfully Logged In!\nWelcome " + SteamUser.SteamID + "\n\n", ConsoleColor.Magenta);
IsLoggedIn = true;
});
#endregion
#region Friends
msg.Handle<SteamFriends.PersonaStateCallback> (callback =>
{
SteamFriends.AddFriend (callback.FriendID);
});
msg.Handle<SteamFriends.FriendMsgCallback> (callback =>
{
//Type (emote or chat)
EChatEntryType type = callback.EntryType;
if (type == EChatEntryType.ChatMsg)
{
PrintConsole ("[Chat] " + SteamFriends.GetFriendPersonaName (callback.Sender) + ": " + callback.Message, ConsoleColor.Magenta);
//string message = callback.Message;
string response = ChatResponse;
SteamFriends.SendChatMessage (callback.Sender, EChatEntryType.ChatMsg, response);
}
});
#endregion
#region Trading
msg.Handle<SteamTrading.TradeStartSessionCallback> (call =>
{
CurrentTrade = new Trade (SteamUser.SteamID, call.Other, sessionId, token, apiKey, TradeListener);
});
msg.Handle<SteamTrading.TradeProposedCallback> (thing =>
{
SteamTrade.RequestTrade (thing.Other);
});
msg.Handle<SteamTrading.TradeRequestCallback> (thing =>
{
PrintConsole ("Trade Status: " + thing.Status, ConsoleColor.Magenta);
if (thing.Status == ETradeStatus.Accepted) {
PrintConsole ("Trade accepted!", ConsoleColor.Magenta);
}
});
#endregion
#region Disconnect
msg.Handle<SteamUser.LoggedOffCallback> (callback =>
{
PrintConsole ("[SteamRE] Logged Off: " + callback.Result, ConsoleColor.Magenta);
});
msg.Handle<SteamClient.DisconnectedCallback> (callback =>
{
IsLoggedIn = false;
if (CurrentTrade != null)
{
CurrentTrade = null;
}
PrintConsole ("[SteamRE] Disconnected from Steam Network!", ConsoleColor.Magenta);
SteamClient.Connect ();
});
#endregion
}
// Authenticate. This does the same as SteamWeb.DoLogin(),
// but without contacting the Steam Website.
// Should this one doesnt work anymore, use SteamWeb.DoLogin().
bool Authenticate (SteamUser.LoginKeyCallback callback)
{
sessionId = WebHelpers.EncodeBase64 (callback.UniqueID.ToString ());
PrintConsole ("Got login key, performing web auth...");
using (dynamic userAuth = WebAPI.GetInterface("ISteamUserAuth"))
{
// generate an AES session key
var sessionKey = CryptoHelper.GenerateRandomBlock (32);
// rsa encrypt it with the public key for the universe we're on
byte[] cryptedSessionKey = null;
using (var rsa = new RSACrypto(KeyDictionary.GetPublicKey(SteamClient.ConnectedUniverse))) {
cryptedSessionKey = rsa.Encrypt (sessionKey);
}
var loginKey = new byte[20];
Array.Copy (Encoding.ASCII.GetBytes (callback.LoginKey), loginKey, callback.LoginKey.Length);
// aes encrypt the loginkey with our session key
byte[] cryptedLoginKey = CryptoHelper.SymmetricEncrypt (loginKey, sessionKey);
KeyValue authResult;
try
{
authResult = userAuth.AuthenticateUser (
steamid: SteamClient.SteamID.ConvertToUInt64 (),
sessionkey: WebHelpers.UrlEncode (cryptedSessionKey),
encrypted_loginkey: WebHelpers.UrlEncode (cryptedLoginKey),
method: "POST"
);
}
catch (Exception)
{
return false;
}
token = authResult ["token"].AsString ();
return true;
}
}
protected void PrintConsole(String line, ConsoleColor color = ConsoleColor.White, bool isDebug = false)
{
Console.ForegroundColor = color;
if (isDebug && IsDebugMode)
{
Console.WriteLine(line);
}
else
{
Console.WriteLine(line);
}
Console.ForegroundColor = ConsoleColor.White;
}
}
}