-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoGUI.cs
229 lines (205 loc) · 8.71 KB
/
NoGUI.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
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Xml;
namespace SimuCoins
{
public class NoGUI : HttpClient, IDisposable
{
// Create an HttpClient to handle HTTP requests and responses
private readonly HttpClient httpClient = new(new HttpClientHandler { CookieContainer = new() });
private static bool noShowEcho = false;
private static bool isClaimed = false;
public NoGUI()
{
httpClient.Timeout = TimeSpan.FromSeconds(30);
}
private static List<(string, string)> LoadXML()
{
var users = new List<(string, string)>();
string pluginPath = PluginInfo.Coin?.get_Variable("PluginPath") ?? string.Empty;
if (!string.IsNullOrEmpty(pluginPath))
{
string xmlPath = Path.Combine(pluginPath, "SimuCoins.xml");
if (File.Exists(xmlPath))
{
var xmlDocument = new XmlDocument();
xmlDocument.Load(xmlPath);
var userNodes = xmlDocument.SelectNodes("//user");
if (userNodes != null)
{
foreach (XmlNode userNode in userNodes)
{
string? username = userNode.Attributes?["username"]?.Value;
string? password = userNode.Attributes?["password"]?.Value;
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
users.Add((username, password));
}
}
}
}
}
return users;
}
internal async Task DoAll()
{
noShowEcho = true;
var users = LoadXML();
PluginInfo.Coin?.EchoText("\nChecking Account(s)...");
foreach (var (username, password) in users)
{
await Login(username, EncryptDecrypt.Decrypt(password));
}
PluginInfo.Coin?.EchoText("\nAccount(s) Checked...\n");
}
internal void NoGUILogin(string username, string password)
{
noShowEcho = false;
isClaimed = false;
Task.Run(async () => await Login(username, password));
}
private async Task<bool> Login(string username, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
return false;
}
try
{
string url = PluginInfo.LoginUrl; // URL for the login page
var response = await httpClient.GetAsync(url); // Send GET request to the login page
string token = PluginInfo.Token; // Extract the verification token from the page content
var content = new FormUrlEncodedContent(new Dictionary<string, string> // Create the content object to send with the POST request
{
{ "__RequestVerificationToken", token },
{ "UserName", username },
{ "Password", password },
{ "RememberMe", "true" }
});
response = await httpClient.PostAsync(url, content); // Send POST request to the login page
_ = await response.Content.ReadAsStringAsync(); // Read the response content as a string
if (response.RequestMessage?.RequestUri?.ToString() == PluginInfo.StoreUrl) // Check if the login was successful
{
await DisplayBalance();
}
else
{
PluginInfo.Coin?.EchoText("\nIncorrect Username and/or Password\n");
}
}
catch (HttpRequestException)
{
PluginInfo.Coin?.EchoText("No Connection available.");
}
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
// Handle timeout exception
PluginInfo.Coin?.EchoText("The request timed out.");
}
catch (SocketException ex) when (ex.ErrorCode == 995)
{
// Handle the specific "SocketException (995)" error
PluginInfo.Coin?.EchoText("SocketException (995): The I/O operation was aborted.");
}
catch (Exception ex)
{
// Handle other exceptions
PluginInfo.Coin?.EchoText($"An error occurred with store.play.net: {ex.Message}");
}
return true;
}
private async Task DisplayBalance()
{
try
{
var response = await httpClient.GetAsync(PluginInfo.BalanceUrl);
var pageContent = await response.Content.ReadAsStringAsync();
Match match = Regex.Match(pageContent, PluginInfo.NamePattern);
if (!noShowEcho)
PluginInfo.Coin?.EchoText("\nAccount: " + match.Groups[1].Value);
var claimAmount = GetClaimAmount(pageContent);
if (!string.IsNullOrEmpty(claimAmount))
{
if (noShowEcho)
PluginInfo.Coin?.EchoText("\nAccount: " + match.Groups[1].Value);
UpdateBalance(pageContent);
await ClaimReward();
}
else
{
UpdateTime(pageContent);
UpdateBalance(pageContent);
}
}
catch (Exception ex)
{
PluginInfo.Coin?.EchoText($"DisplayBalance: {ex.Message}");
}
await PluginInfo.SignOut();
}
private static void UpdateTime(string pageContent)
{
var time = Regex.Match(pageContent, PluginInfo.TimePattern).Groups[1].Value;
if (!noShowEcho)
PluginInfo.Coin?.EchoText(time);
}
private static void UpdateBalance(string pageContent)
{
var balance = Regex.Match(pageContent, PluginInfo.BalancePattern).Groups[1].Value;
if (!noShowEcho)
{
if (isClaimed)
PluginInfo.Coin?.EchoText($"You Now Have {balance} SimuCoins!\n");
else
PluginInfo.Coin?.EchoText($"You Have {balance} SimuCoins!");
}
}
private static string? GetClaimAmount(string pageContent)
{
var match = Regex.Match(pageContent, PluginInfo.ClaimPattern);
return match.Success ? match.Groups[1].Value : null;
}
private async Task<bool> ClaimReward()
{
try
{
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("game", "DR"),
new KeyValuePair<string, string>("filter", ""),
new KeyValuePair<string, string>("itemSearch", "")
});
var response = await httpClient.PostAsync(PluginInfo.ClaimUrl, formContent);
if (response.IsSuccessStatusCode)
{
var claimPageContent = await response.Content.ReadAsStringAsync();
var match = Regex.Match(claimPageContent, @"<h1 class=""RewardMessage centered sans_serif"">Claimed (\d+) SimuCoin reward!</h1>");
if (match.Success)
{
var claimAmount = match.Groups[1].Value;
PluginInfo.Coin?.EchoText($"Claimed {claimAmount} SimuCoins");
isClaimed = true;
UpdateBalance(claimPageContent);
return true;
}
else
{
PluginInfo.Coin?.EchoText("Claim Failed");
return false;
}
}
else
{
// handle error response
PluginInfo.Coin?.EchoText("Request failed: " + response.StatusCode);
return false;
}
}
catch (Exception ex)
{
PluginInfo.Coin?.EchoText($"ClaimReward: {ex.Message}");
return false;
}
}
}
}