-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathProgram.cs
259 lines (220 loc) · 9.61 KB
/
Program.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
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Google.Protobuf;
using Newtonsoft.Json;
using NLog;
using NLog.Config;
using POGOLib.Official.LoginProviders;
using POGOLib.Official.Net;
using POGOLib.Official.Net.Authentication;
using POGOLib.Official.Net.Authentication.Data;
using POGOLib.Official.Util.Hash;
using POGOProtos.Networking.Requests;
using POGOProtos.Networking.Requests.Messages;
using POGOProtos.Networking.Responses;
using LogLevel = POGOLib.Official.Logging.LogLevel;
using POGOLib.Official.Extensions;
using POGOLib.Official.Net.Captcha;
namespace POGOLib.Official.Demo.ConsoleApp
{
public class Program
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// This is just a demo application to test out the library / show a bit how it works.
/// </summary>
/// <param name="args">The command line arguments.</param>
public static void Main(string[] args)
{
Run(args).GetAwaiter().GetResult();
}
private static async Task Run(string[] args)
{
// Configure Logger
LogManager.Configuration = new XmlLoggingConfiguration(Path.Combine(Directory.GetCurrentDirectory(), "nlog.config"));
Logging.Logger.RegisterLogOutput((level, message) =>
{
switch (level)
{
case LogLevel.Debug:
Logger.Debug(message);
break;
case LogLevel.Info:
Logger.Info(message);
break;
case LogLevel.Notice:
case LogLevel.Warn:
Logger.Warn(message);
break;
case LogLevel.Error:
Logger.Error(message);
break;
default:
throw new ArgumentOutOfRangeException(nameof(level), level, null);
}
});
// Initiate console
Logger.Info("Booting up.");
Logger.Info("Type 'q', 'quit' or 'exit' to exit.");
Console.Title = "POGO Demo";
// Configure hasher - DO THIS BEFORE ANYTHING ELSE!!
//
// If you want to use the latest POGO version, you have
// to use the PokeHashHasher. For more information:
// https://talk.pogodev.org/d/51-api-hashing-service-by-pokefarmer
//
// You may also not use the PokeHashHasher, it will then use
// the built-in hasher which was made for POGO 0.45.0.
// Don't forget to use "Configuration.IgnoreHashVersion = true;" too.
//
// Expect some captchas in that case..
var pokeHashAuthKey = Environment.GetEnvironmentVariable("POKEHASH_AUTHKEY") ?? "";
Configuration.Hasher = new PokeHashHasher(pokeHashAuthKey);
// Configuration.IgnoreHashVersion = true;
// Settings
var loginProviderStr = "ptc";
var usernameStr = Environment.GetEnvironmentVariable("PTC_USERNAME") ?? ""; // Your PTC username
var passwordStr = Environment.GetEnvironmentVariable("PTC_PASSWORD") ?? ""; // Your PTC password
// Login
ILoginProvider loginProvider;
switch (loginProviderStr)
{
case "google":
loginProvider = new GoogleLoginProvider(usernameStr, passwordStr);
break;
case "ptc":
loginProvider = new PtcLoginProvider(usernameStr, passwordStr);
break;
default:
throw new ArgumentException("Login provider must be either \"google\" or \"ptc\".");
}
var locRandom = new Random();
var latitude = 51.507352 + locRandom.NextDouble(-0.000030, 0.000030); // Somewhere in London
var longitude = -0.127758 + locRandom.NextDouble(-0.000030, 0.000030);
var session = await GetSession(loginProvider, latitude, longitude, true);
SaveAccessToken(session.AccessToken);
session.AccessTokenUpdated += SessionOnAccessTokenUpdated;
session.InventoryUpdate += InventoryOnUpdate;
session.MapUpdate += MapOnUpdate;
session.CaptchaReceived += SessionOnCaptchaReceived;
// Send initial requests and start HeartbeatDispatcher.
// This makes sure that the initial heartbeat request finishes and the "session.Map.Cells" contains stuff.
if (!await session.StartupAsync())
{
throw new Exception("Session couldn't start up.");
}
// Retrieve the closest fort to your current player coordinates.
var closestFort = session.Map.GetFortsSortedByDistance().FirstOrDefault();
if (closestFort != null)
{
var fortDetailsBytes = await session.RpcClient.SendRemoteProcedureCallAsync(new Request
{
RequestType = RequestType.FortDetails,
RequestMessage = new FortDetailsMessage
{
FortId = closestFort.Id,
Latitude = closestFort.Latitude,
Longitude = closestFort.Longitude
}.ToByteString()
});
if (fortDetailsBytes != null)
{
var fortDetailsResponse = FortDetailsResponse.Parser.ParseFrom(fortDetailsBytes);
Console.WriteLine(JsonConvert.SerializeObject(fortDetailsResponse, Formatting.Indented));
}
else
{
Console.WriteLine("Couldn't print fort info, we probably had a captcha.");
}
}
else
{
Logger.Info("No fort found nearby.");
}
// Handle quit commands.
HandleCommands();
}
private static void SessionOnCaptchaReceived(object sender, CaptchaEventArgs e)
{
var session = (Session)sender;
Logger.Warn("Captcha received: " + e.CaptchaUrl);
// Solve
// var verifyChallengeResponse = await session.RpcClient.SendRemoteProcedureCallAsync(new Request
// {
// RequestType = RequestType.VerifyChallenge,
// RequestMessage = new VerifyChallengeMessage
// {
// Token = "token"
// }.ToByteString()
// }, false);
//
// var verifyChallenge = VerifyChallengeResponse.Parser.ParseFrom(verifyChallengeResponse);
//
// Console.WriteLine(JsonConvert.SerializeObject(verifyChallenge, Formatting.Indented));
}
private static void SessionOnAccessTokenUpdated(object sender, EventArgs e)
{
var session = (Session)sender;
SaveAccessToken(session.AccessToken);
Logger.Info("Saved access token to file.");
}
private static void InventoryOnUpdate(object sender, EventArgs e)
{
Logger.Info("Inventory was updated.");
}
private static void MapOnUpdate(object sender, EventArgs e)
{
Logger.Info("Map was updated.");
}
private static void SaveAccessToken(AccessToken accessToken)
{
var fileName = Path.Combine(Directory.GetCurrentDirectory(), "Cache", $"{accessToken.Uid}.json");
File.WriteAllText(fileName, JsonConvert.SerializeObject(accessToken, Formatting.Indented));
}
private static void HandleCommands()
{
var keepRunning = true;
while (keepRunning)
{
var command = Console.ReadLine();
switch (command)
{
case "q":
case "quit":
case "exit":
keepRunning = false;
break;
}
}
}
/// <summary>
/// Login to PokémonGo and return an authenticated <see cref="Session" />.
/// </summary>
/// <param name="loginProvider">Provider must be PTC or Google.</param>
/// <param name="initLat">The initial latitude.</param>
/// <param name="initLong">The initial longitude.</param>
/// <param name="mayCache">Can we cache the <see cref="AccessToken" /> to a local file?</param>
private static async Task<Session> GetSession(ILoginProvider loginProvider, double initLat, double initLong, bool mayCache = false)
{
var cacheDir = Path.Combine(Directory.GetCurrentDirectory(), "Cache");
var fileName = Path.Combine(cacheDir, $"{loginProvider.UserId}-{loginProvider.ProviderId}.json");
if (mayCache)
{
if (!Directory.Exists(cacheDir))
Directory.CreateDirectory(cacheDir);
if (File.Exists(fileName))
{
var accessToken = JsonConvert.DeserializeObject<AccessToken>(File.ReadAllText(fileName));
if (!accessToken.IsExpired)
return Login.GetSession(loginProvider, accessToken, initLat, initLong);
}
}
var session = await Login.GetSession(loginProvider, initLat, initLong);
if (mayCache)
SaveAccessToken(session.AccessToken);
return session;
}
}
}