Skip to content

Instantly share code, notes, and snippets.

@bemoty
Last active December 9, 2018 15:52
Show Gist options
  • Save bemoty/c1a360678a5d6189386efa635d92e046 to your computer and use it in GitHub Desktop.
Save bemoty/c1a360678a5d6189386efa635d92e046 to your computer and use it in GitHub Desktop.
/* MIT License
Copyright (c) 2018 Bemoty
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */
package org.bemoty.altchecker;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
public class Checker {
private static File inputFile;
private static File outputFile;
private static int delay = 2000;
private static Map < String, String > altmap = new HashMap < String, String > ();
private static Map < String, String > validmap = new HashMap < String, String > ();
private static Map < String, String > ac = new HashMap < String, String > ();
enum Prefix {
FATAL("[FATAL]"), ERROR("[Error]"), INFO("[Info]"), VALID("[VALID]"), INVALID("[INVALID]");
String p;
Prefix(String p) {
this.p = p;
}
}
public static void main(final String[] args) {
if (args.length < 1 || args.length > 3) {
log(Prefix.FATAL, args.length + " arguments are not allowed. Usage: <inputfile> [outputfile] [delay]");
waitAndExit(-1);
}
if (!((inputFile = new File(args[0])).exists())) {
log(Prefix.FATAL, "\"" + args[0] + "\" is not a valid file path.");
waitAndExit(-2);
}
if (args.length >= 2) {
try {
if (!(outputFile = new File(args[1])).createNewFile()) {
log(Prefix.ERROR, "Access to output path is denied or file already exists.");
outputFile = null;
}
} catch (IOException e) {
log(Prefix.ERROR, "Access to output path is denied.");
outputFile = null;
}
}
if (args.length == 3) {
try {
int d = Integer.parseInt(args[2]);
if (d >= 1000 && d <= 60000) {
delay = d;
} else {
log(Prefix.ERROR, "Delay must be between 1000ms and 60000ms!");
}
} catch (NumberFormatException e) {
log(Prefix.ERROR, "Ignoring invalid delay.");
}
}
readInputFile();
if (altmap.isEmpty()) {
log(Prefix.FATAL, "Input file empty, nothing to check.");
waitAndExit(-5);
}
int c = 1;
for (String key: altmap.keySet()) {
connectAndCheck(key, altmap.get(key));
try {
if (c != altmap.size()) {
log(Prefix.INFO, "Sleeping for " + delay + "ms");
Thread.sleep(delay);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
c++;
}
Thread iv = new Thread(() - > {
for (String key: ac.keySet()) {
connectAndInvalidate(key, ac.get(key));
try {
Thread.sleep(200);
} catch (InterruptedException e) {}
}
});
Thread wo = new Thread(() - > {
if (!validmap.isEmpty()) {
try {
try (FileWriter fw = new FileWriter(outputFile)) {
for (String key: validmap.keySet()) {
fw.write(key + ":" + validmap.get(key) + System.lineSeparator());
}
}
} catch (IOException e) {
log(Prefix.FATAL, "Cannot write to output file.");
waitAndExit(-7);
}
} else {
log(Prefix.ERROR, "No valid accounts to write into output file.");
}
});
try {
if (!ac.isEmpty()) {
log(Prefix.INFO, "Invalidating aTokens...");
iv.start();
iv.join();
}
if (outputFile != null) {
wo.start();
wo.join();
}
} catch (InterruptedException e) {}
log(Prefix.INFO, "Successfully checked all accounts.");
waitAndExit(0);
}
private static void waitAndExit(int status) {
try (Scanner s = new Scanner(System.in)) {
s.nextLine();
} finally {
System.exit(status);
}
}
private static void log(Prefix prefix, String message) {
System.out.println(prefix.p + " " + message);
}
private static void readInputFile() {
try {
log(Prefix.INFO, "Reading file \"" + inputFile.getCanonicalPath() + "\"");
InputStream is = new BufferedInputStream(new FileInputStream(inputFile));
try (Scanner s = new Scanner(is)) {
while (s.hasNextLine()) {
String[] rla = s.nextLine().split(":");
if (rla.length != 2) {
log(Prefix.FATAL, "Invalid input format!");
waitAndExit(-5);
}
altmap.put(rla[0], rla[1]);
}
} finally {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
waitAndExit(-200);
}
}
private static void connectAndCheck(String username, String password) {
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://authserver.mojang.com/authenticate");
JSONObject json = new JSONObject();
json.put("username", username);
json.put("password", password);
JSONObject agent = new JSONObject();
agent.put("name", "Minecraft");
agent.put("version", 1);
json.put("agent", agent);
StringEntity rentity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
post.setEntity(rentity);
try {
JSONObject authResponse;
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream stream = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder o = new StringBuilder();
String cl;
while ((cl = reader.readLine()) != null) {
o.append(cl);
}
authResponse = new JSONObject(o.toString());
} finally {
stream.close();
}
} else {
log(Prefix.FATAL, "No response from server, aborting...");
waitAndExit(-6);
return; // dead code
}
int code = response.getStatusLine().getStatusCode();
if (code >= 200 && code < 300) {
log(Prefix.VALID, "\"" + username + "\" is a valid account.");
validmap.put(authResponse.getJSONArray("availableProfiles").getJSONObject(0).getString("name"), username);
ac.put(authResponse.getString("accessToken"), authResponse.getString("clientToken"));
} else {
log(Prefix.INVALID, "\"" + username + "\" is not a valid account: " + authResponse.getString("errorMessage"));
}
} catch (IOException e) {
e.printStackTrace();
waitAndExit(-200);
}
}
private static void connectAndInvalidate(String aToken, String cToken) {
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://authserver.mojang.com/invalidate");
JSONObject json = new JSONObject();
json.put("accessToken", aToken);
json.put("clienToken", cToken);
StringEntity rentity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
post.setEntity(rentity);
try {
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (!(code >= 200 && code < 300)) {
log(Prefix.ERROR, "Could not invalidate aToken \"" + aToken + "\"");
System.out.println(response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase());
}
} catch (IOException e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment