-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
377 lines (304 loc) · 14 KB
/
mainwindow.cpp
File metadata and controls
377 lines (304 loc) · 14 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QSpinBox>
#include <QLineEdit>
#include <QTextEdit>
#include <QLabel>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QDateTime>
#include <QScrollBar>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QMessageBox>
#include <QCoreApplication> // Added for processEvents
#include <QUrlQuery>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(nullptr), totalGenerated(0), checkedCount(0),
availableCount(0), usernameLength(3), isRunning(false)
{
auto *central = new QWidget(this);
auto *mainLayout = new QVBoxLayout(central);
// Title
auto *titleLabel = new QLabel("Minecraft Username Checker");
titleLabel->setStyleSheet("font-size: 18px; font-weight: bold; color: #2E86C1; padding: 10px;");
titleLabel->setAlignment(Qt::AlignCenter);
mainLayout->addWidget(titleLabel);
// Settings Group
auto *settingsGroup = new QGroupBox("Settings");
auto *settingsLayout = new QVBoxLayout(settingsGroup);
auto *lenLayout = new QHBoxLayout();
auto *lenLabel = new QLabel("Username length (3-16):");
auto *lenSpin = new QSpinBox();
lenSpin->setRange(3, 16);
lenSpin->setValue(3);
lenLayout->addWidget(lenLabel);
lenLayout->addWidget(lenSpin);
lenLayout->addStretch();
auto *charLayout = new QHBoxLayout();
auto *charLabel = new QLabel("Allowed characters:");
auto *charEdit = new QLineEdit("abcdefghijklmnopqrstuvwxyz0123456789_");
charEdit->setMaximumWidth(300);
charLayout->addWidget(charLabel);
charLayout->addWidget(charEdit);
charLayout->addStretch();
settingsLayout->addLayout(lenLayout);
settingsLayout->addLayout(charLayout);
mainLayout->addWidget(settingsGroup);
// Status Group
auto *statusGroup = new QGroupBox("Status");
auto *statusGroupLayout = new QVBoxLayout(statusGroup);
statusLabel = new QLabel("Ready to start");
statusLabel->setStyleSheet("font-weight: bold; font-size: 14px; color: #27AE60;");
progressLabel = new QLabel("Progress: 0/0 (0 available)");
progressLabel->setStyleSheet("font-size: 13px;");
statusGroupLayout->addWidget(statusLabel);
statusGroupLayout->addWidget(progressLabel);
mainLayout->addWidget(statusGroup);
// Controls
auto *controlsLayout = new QHBoxLayout();
auto *startBtn = new QPushButton("▶ Start Checking");
startBtn->setStyleSheet("QPushButton { background-color: #27AE60; color: white; font-weight: bold; padding: 10px; border-radius: 5px; }"
"QPushButton:hover { background-color: #229954; }");
startBtn->setFixedHeight(40);
auto *stopBtn = new QPushButton("⏹ Stop");
stopBtn->setStyleSheet("QPushButton { background-color: #E74C3C; color: white; padding: 10px; border-radius: 5px; }"
"QPushButton:hover { background-color: #C0392B; }"
"QPushButton:disabled { background-color: #95A5A6; }");
stopBtn->setFixedHeight(40);
stopBtn->setEnabled(false);
auto *clearBtn = new QPushButton("Clear Output");
clearBtn->setStyleSheet("QPushButton { background-color: #3498DB; color: white; padding: 10px; border-radius: 5px; }"
"QPushButton:hover { background-color: #2980B9; }");
clearBtn->setFixedHeight(40);
controlsLayout->addWidget(startBtn);
controlsLayout->addWidget(stopBtn);
controlsLayout->addWidget(clearBtn);
controlsLayout->addStretch();
mainLayout->addLayout(controlsLayout);
// Output Group
auto *outputGroup = new QGroupBox("Output");
auto *outputLayout = new QVBoxLayout(outputGroup);
auto *availableLabel = new QLabel("Available usernames will appear here:");
availableLabel->setStyleSheet("font-weight: bold; color: #2C3E50;");
outputTextEdit = new QTextEdit();
outputTextEdit->setReadOnly(true);
outputTextEdit->setStyleSheet("font-family: 'Courier New', monospace; font-size: 12px; background-color: #1C2833; color: #EAECEE;");
outputTextEdit->setMinimumHeight(300);
outputLayout->addWidget(availableLabel);
outputLayout->addWidget(outputTextEdit);
mainLayout->addWidget(outputGroup);
// Footer
auto *footerLabel = new QLabel("Rate: 50 requests/minute • Checks Mojang API for available usernames");
footerLabel->setStyleSheet("color: #7F8C8D; font-size: 11px; padding: 5px;");
footerLabel->setAlignment(Qt::AlignCenter);
mainLayout->addWidget(footerLabel);
setCentralWidget(central);
// Initialize network manager
network = new QNetworkAccessManager(this);
// Connect signals
connect(startBtn, &QPushButton::clicked, this, [=]() {
if (charEdit->text().isEmpty()) {
QMessageBox::warning(this, "Input Error", "Please enter allowed characters!");
return;
}
usernameLength = lenSpin->value();
allowedChars = charEdit->text().toLower();
// Reset counters
totalGenerated = 0;
checkedCount = 0;
availableCount = 0;
isRunning = true;
// Clear queue and output
while (!usernameQueue.empty()) usernameQueue.pop();
outputTextEdit->clear();
// Generate usernames
outputTextEdit->append(QString("[%1] ⚙ Generating usernames of length %2...")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(usernameLength));
outputTextEdit->append(QString("[%1] 📝 Using characters: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(allowedChars));
QCoreApplication::processEvents(); // Changed from QApplication::processEvents()
generateUsernames("", usernameLength);
outputTextEdit->append(QString("[%1] ✅ Generated %2 usernames")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(totalGenerated));
outputTextEdit->append(QString("[%1] 🚀 Starting check (50 requests/minute)...")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
statusLabel->setText("Checking usernames...");
statusLabel->setStyleSheet("font-weight: bold; font-size: 14px; color: #F39C12;");
startBtn->setEnabled(false);
stopBtn->setEnabled(true);
lenSpin->setEnabled(false);
charEdit->setEnabled(false);
// Start timer for rate limiting (1200ms = 50 requests per minute)
rateTimer.start(1200);
});
connect(stopBtn, &QPushButton::clicked, this, [=]() {
rateTimer.stop();
isRunning = false;
outputTextEdit->append(QString("[%1] ⏹ Stopped by user")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
outputTextEdit->append(QString("[%1] 📊 Results: Checked %2/%3 usernames, %4 available")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(checkedCount).arg(totalGenerated).arg(availableCount));
statusLabel->setText("Stopped");
statusLabel->setStyleSheet("font-weight: bold; font-size: 14px; color: #E74C3C;");
startBtn->setEnabled(true);
stopBtn->setEnabled(false);
lenSpin->setEnabled(true);
charEdit->setEnabled(true);
});
connect(clearBtn, &QPushButton::clicked, this, [=]() {
outputTextEdit->clear();
});
connect(&rateTimer, &QTimer::timeout, this, &MainWindow::processQueue);
connect(network, &QNetworkAccessManager::finished, this, &MainWindow::handleReply);
setWindowTitle("Minecraft Username Checker v1.0");
resize(800, 700);
}
MainWindow::~MainWindow() {}
void MainWindow::startChecking() {
// Implemented via lambda above
}
void MainWindow::generateUsernames(QString current, int length) {
if (length == 0) {
usernameQueue.push(current);
totalGenerated++;
// Update progress every 1000 generated usernames
if (totalGenerated % 1000 == 0) {
progressLabel->setText(QString("Generating: %1 usernames...").arg(totalGenerated));
QCoreApplication::processEvents(); // Changed from QApplication::processEvents()
}
return;
}
for (QChar c : allowedChars) {
generateUsernames(current + c, length - 1);
}
}
void MainWindow::processQueue() {
if (!isRunning) return;
if (usernameQueue.empty()) {
rateTimer.stop();
isRunning = false;
outputTextEdit->append("\n" + QString("[%1] 🎉 Finished checking all usernames!")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
outputTextEdit->append(QString("[%1] 📈 Final results: %2 available out of %3 checked")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(availableCount).arg(checkedCount));
statusLabel->setText("Finished!");
statusLabel->setStyleSheet("font-weight: bold; font-size: 14px; color: #27AE60;");
// Re-enable controls
QList<QPushButton*> buttons = findChildren<QPushButton*>();
for (auto *btn : buttons) {
if (btn->text().contains("Start")) btn->setEnabled(true);
}
QList<QSpinBox*> spins = findChildren<QSpinBox*>();
for (auto *spin : spins) spin->setEnabled(true);
QList<QLineEdit*> edits = findChildren<QLineEdit*>();
for (auto *edit : edits) edit->setEnabled(true);
return;
}
// Send one request per tick (50 requests per minute)
if (!usernameQueue.empty()) {
QString username = usernameQueue.front();
usernameQueue.pop();
checkUsername(username);
}
}
void MainWindow::checkUsername(QString username) {
QNetworkRequest req(
QUrl("https://api.ashcon.app/mojang/v2/user/" + username)
);
// Set headers
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
req.setRawHeader("User-Agent", "MinecraftUsernameChecker/1.0");
// Use QVariant to store username
req.setAttribute(QNetworkRequest::User, username);
network->get(req);
}
void MainWindow::handleReply(QNetworkReply *reply) {
QString username;
// Get username from request attribute
QVariant usernameVar = reply->request().attribute(QNetworkRequest::User);
if (usernameVar.isValid()) {
username = usernameVar.toString();
} else {
// Fallback: extract from URL
QString url = reply->url().toString();
username = url.split('/').last();
}
if (reply->error() != QNetworkReply::NoError) {
// Check for 404 (username available)
if (reply->error() == QNetworkReply::ContentNotFoundError ||
reply->error() == QNetworkReply::HostNotFoundError ||
reply->errorString().contains("404")) {
availableCount++;
// Format and display available username
QString message = QString("[%1] ✅ <span style='color:#27AE60; font-weight:bold;'>AVAILABLE:</span> %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(username);
outputTextEdit->append(message);
// Also log to console for easy copying
qDebug() << "AVAILABLE:" << username;
} else {
// Other errors
QString errorMsg = QString("[%1] ❌ Error checking %2: %3")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(username)
.arg(reply->errorString());
outputTextEdit->append(errorMsg);
}
} else {
// Username is taken (200 OK)
QByteArray data = reply->readAll();
QJsonParseError parseError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &parseError);
if (parseError.error == QJsonParseError::NoError && jsonDoc.isObject()) {
QJsonObject jsonObj = jsonDoc.object();
if (jsonObj.contains("username") || jsonObj.contains("name")) {
QString takenName = jsonObj.contains("username") ?
jsonObj["username"].toString() : jsonObj["name"].toString();
QString message = QString("[%1] ❌ <span style='color:#E74C3C;'>TAKEN:</span> %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(takenName);
outputTextEdit->append(message);
}
} else {
// Couldn't parse JSON, but username is taken
QString message = QString("[%1] ❌ <span style='color:#E74C3C;'>TAKEN:</span> %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(username);
outputTextEdit->append(message);
}
}
checkedCount++;
updateProgress();
// Auto-scroll to bottom
QScrollBar *scrollbar = outputTextEdit->verticalScrollBar();
scrollbar->setValue(scrollbar->maximum());
reply->deleteLater();
}
void MainWindow::updateProgress() {
if (checkedCount % 10 == 0 || checkedCount == totalGenerated) {
double percentage = totalGenerated > 0 ? (checkedCount * 100.0 / totalGenerated) : 0;
progressLabel->setText(
QString("Progress: %1/%2 (%3%) • Available: %4 • Rate: 50/min")
.arg(checkedCount)
.arg(totalGenerated)
.arg(QString::number(percentage, 'f', 1))
.arg(availableCount)
);
if (isRunning) {
statusLabel->setText(
QString("Checking... %1% complete")
.arg(QString::number(percentage, 'f', 1))
);
}
}
}