Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

A Java application to find the missing file for realistic 2 - Java Code Bank


A Java application to find the missing file for realistic 2
This Java application attempts to locate the missing backup file for the Realistic 2 mission. It attempts to open each possible backup file until one is found and notifies you with the first page that it finds.
                package realistic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Find the hidden file for the HellBound Hackers Realistic 2 mission.
 * 
 * http://www.hellboundhackers.org/challenges/real2/
 * 
 * "My Friend heard that HellBound Hackers Backup Generator's 
 * Admin that the had made a backup and they were stored in a directory 
 * called bacups/ or backups/ and that the backup file is named in this 
 * order year, month, day, hour, .sql with no minutes and he also heard 
 * that the backup was made on September of 2004. It looks like 
 * backup_2004-09-01_1000.sql"
 * 
 * @author furtypajohn
 */
public class Mission2 {
	/** The prefix for the backup folder for realistic 2. */
	private static String HBH = "http://www.hellboundhackers.org/challenges/real2/backups/";
	/** The backup file prefix. */
	private static String BACKUP = "backup_2004-09-";
	/** The extension for the backup file.*/
	private static String EXT = ".sql";
	
	/**
	 * Determine if the given URL leads to an existing page.
	 * 
	 * @param page The URL to attempt to open
	 * @return True if the page exists, false otherwise
	 */
	public static boolean pageExists(String page)
	{
		URL url;
		URLConnection con;
		
		// Try to create the URL from the String
		try { url = new URL(page); } 
		catch (MalformedURLException e) { return false; }
		
		// Try to open a connection to the created URL
		try { con = url.openConnection(); } 
		catch (IOException e) {	return false; }
		
		// Try to read in the webpage
		try { 
			BufferedReader in = new BufferedReader(new 
					InputStreamReader(con.getInputStream()));
			
			// If the reader exists, so does the page
			return in != null;
		} catch (IOException e) {
			return false;
		}
	}
	
	/**
	 * Create the URL using the day and the hour.
	 * 
	 * @param day The day
	 * @param hour The hour
	 * @return The URL
	 */
	public static String getURL(int day, int hour)
	{
		// Add the zeroes before low numbers
		String strDay = (day <= 9 ? "0" + day : "" + day); 
		String strHour = (hour <= 900 ? "0" + hour : "" + hour);
		
		// Create the appropriate URL string
		return (HBH + BACKUP + strDay + "_" + strHour + EXT);
	}
	
	/**
	 * Run the Mission2 application.
	 * 
	 * @param args The command line arguments
	 */
	public static void main(String args[])
	{		
		// Traverse through all the days in September
		// NOTE: September only has 30 days.
		for (int day = 1; day <= 30; day++)
		{
			System.out.println("Testing day [" + day + "]...");
			
			// Traverse through all the hours: 1:00 to 24:00
			for (int hour = 100; hour <= 1200; hour += 100)
			{
				System.out.println("    Testing:");
				
				// Get the URL for the morning and evening files
				String morning = getURL(day, hour);
				String evening = getURL(day, hour + 1200);
				
				// Check the morning hour
				System.out.println("\t[" + morning + "]");
				if (Mission2.pageExists(morning))
				{
					System.out.println("\nANSWER: [" + morning + "]");
					System.exit(0);
				}

				// Check the evening hour
				System.out.println("\t[" + evening + "]");
				if (Mission2.pageExists(evening))
				{
					System.out.println("\nANSWER: [" + evening + "]");
					System.exit(0);
				}
			}
		}
		
		System.out.println("Unable to find an answer....");
	}
}

            
Comments
Sorry but there are no comments to display