Advertisement
Doddy

[Java] Class DH Tools 0.2

Aug 29th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.68 KB | None | 0 0
  1. // Class : DH Tools
  2. // Version : 0.2
  3. // (C) Doddy Hackman 2015
  4. // Functions :
  5. //
  6. //public String toma(String link)
  7. //public String tomar(String pagina, String data)
  8. //public void savefile(String ruta, String texto)
  9. //public String read_file(String ruta)
  10. //public String console(String command)
  11. //public String httpfinger(String target)
  12. //public Integer response_code(String page)
  13. //public ArrayList repes(ArrayList array)
  14. //public ArrayList cortar(ArrayList array)
  15. //public String regex(String code, String deaca, String hastaaca)
  16. //public Boolean download(String url, File savefile)
  17. //public String extract_file_by_url(String url)
  18. //public String uri_split(String link, String opcion)
  19. //public String md5_encode(String text)
  20. //public String md5_file(String file)
  21. //public String get_ip(String hostname)
  22. //
  23. package dhtools;
  24.  
  25. import java.io.*;
  26. import java.net.*;
  27. import java.nio.channels.Channels;
  28. import java.nio.channels.ReadableByteChannel;
  29. import java.util.ArrayList;
  30. import java.util.Scanner;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. import java.security.*;
  34.  
  35. public class DH_Tools {
  36.  
  37.     public String toma(String link) {
  38.         String re;
  39.         StringBuffer conte = new StringBuffer(40);
  40.         try {
  41.             URL url = new URL(link);
  42.             URLConnection nave = url.openConnection();
  43.             nave.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
  44.             BufferedReader leyendo = new BufferedReader(
  45.                     new InputStreamReader(nave.getInputStream()));
  46.             while ((re = leyendo.readLine()) != null) {
  47.                 conte.append(re);
  48.             }
  49.             leyendo.close();
  50.         } catch (Exception e) {
  51.             //
  52.         }
  53.         return conte.toString();
  54.     }
  55.  
  56.     public String tomar(String pagina, String data) {
  57.         // Credits : Function based in http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
  58.         String respuesta = "";
  59.  
  60.         try {
  61.             URL url_now = new URL(pagina);
  62.             HttpURLConnection nave = (HttpURLConnection) url_now.openConnection();
  63.  
  64.             nave.setRequestMethod("POST");
  65.             nave.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
  66.  
  67.             nave.setDoOutput(true);
  68.             DataOutputStream send = new DataOutputStream(nave.getOutputStream());
  69.             send.writeBytes(data);
  70.             send.flush();
  71.             send.close();
  72.  
  73.             BufferedReader leyendo = new BufferedReader(new InputStreamReader(nave.getInputStream()));
  74.             StringBuffer code = new StringBuffer();
  75.             String linea;
  76.  
  77.             while ((linea = leyendo.readLine()) != null) {
  78.                 code.append(linea);
  79.             }
  80.             leyendo.close();
  81.             respuesta = code.toString();
  82.         } catch (Exception e) {
  83.             //
  84.         }
  85.         return respuesta;
  86.     }
  87.  
  88.     public void savefile(String ruta, String texto) {
  89.  
  90.         FileWriter escribir = null;
  91.         File archivo = null;
  92.  
  93.         try {
  94.  
  95.             archivo = new File(ruta);
  96.  
  97.             if (!archivo.exists()) {
  98.                 archivo.createNewFile();
  99.             }
  100.  
  101.             escribir = new FileWriter(archivo, true);
  102.             escribir.write(texto);
  103.             escribir.flush();
  104.             escribir.close();
  105.  
  106.         } catch (Exception e) {
  107.             //
  108.         }
  109.     }
  110.  
  111.     public String read_file(String ruta) {
  112.         String contenido = null;
  113.         try {
  114.             Scanner leyendo = new Scanner(new FileReader(ruta));
  115.             contenido = leyendo.next();
  116.         } catch (Exception e) {
  117.             //
  118.         }
  119.         return contenido;
  120.     }
  121.  
  122.     public String console(String command) {
  123.         String contenido = null;
  124.         try {
  125.             Process proceso = Runtime.getRuntime().exec("cmd /c " + command);
  126.             proceso.waitFor();
  127.             BufferedReader leyendo = new BufferedReader(
  128.                     new InputStreamReader(proceso.getInputStream()));
  129.             String linea;
  130.             StringBuffer code = new StringBuffer();
  131.             while ((linea = leyendo.readLine()) != null) {
  132.                 code.append(linea);
  133.             }
  134.             contenido = code.toString();
  135.         } catch (Exception e) {
  136.             //
  137.         }
  138.         return contenido;
  139.     }
  140.  
  141.     public String httpfinger(String target) {
  142.  
  143.         String resultado = "";
  144.  
  145.         //http://www.mkyong.com/java/how-to-get-http-response-header-in-java/
  146.         try {
  147.  
  148.             URL page = new URL(target);
  149.             URLConnection nave = page.openConnection();
  150.  
  151.             String server = nave.getHeaderField("Server");
  152.             String etag = nave.getHeaderField("ETag");
  153.             String content_length = nave.getHeaderField("Content-Length");
  154.             String expires = nave.getHeaderField("Expires");
  155.             String last_modified = nave.getHeaderField("Last-Modified");
  156.             String connection = nave.getHeaderField("Connection");
  157.             String powered = nave.getHeaderField("X-Powered-By");
  158.             String pragma = nave.getHeaderField("Pragma");
  159.             String cache_control = nave.getHeaderField("Cache-Control");
  160.             String date = nave.getHeaderField("Date");
  161.             String vary = nave.getHeaderField("Vary");
  162.             String content_type = nave.getHeaderField("Content-Type");
  163.             String accept_ranges = nave.getHeaderField("Accept-Ranges");
  164.  
  165.             if (server != null) {
  166.                 resultado += "[+] Server : " + server + "\n";
  167.             }
  168.             if (etag != null) {
  169.                 resultado += "[+] E-tag : " + etag + "\n";
  170.             }
  171.             if (content_length != null) {
  172.                 resultado += "[+] Content-Length : " + content_length + "\n";
  173.             }
  174.  
  175.             if (expires != null) {
  176.                 resultado += "[+] Expires : " + expires + "\n";
  177.             }
  178.  
  179.             if (last_modified != null) {
  180.                 resultado += "[+] Last Modified : " + last_modified + "\n";
  181.             }
  182.  
  183.             if (connection != null) {
  184.                 resultado += "[+] Connection : " + connection + "\n";
  185.             }
  186.  
  187.             if (powered != null) {
  188.                 resultado += "[+] Powered : " + powered + "\n";
  189.             }
  190.  
  191.             if (pragma != null) {
  192.                 resultado += "[+] Pragma : " + pragma + "\n";
  193.             }
  194.  
  195.             if (cache_control != null) {
  196.                 resultado += "[+] Cache control : " + cache_control + "\n";
  197.             }
  198.  
  199.             if (date != null) {
  200.                 resultado += "[+] Date : " + date + "\n";
  201.             }
  202.             if (vary != null) {
  203.                 resultado += "[+] Vary : " + vary + "\n";
  204.             }
  205.             if (content_type != null) {
  206.                 resultado += "[+] Content-Type : " + content_type + "\n";
  207.             }
  208.             if (accept_ranges != null) {
  209.                 resultado += "[+] Accept Ranges : " + accept_ranges + "\n";
  210.             }
  211.  
  212.         } catch (Exception e) {
  213.             //
  214.         }
  215.  
  216.         return resultado;
  217.  
  218.     }
  219.  
  220.     public Integer response_code(String page) {
  221.         Integer response = 0;
  222.         try {
  223.             URL url = new URL(page);
  224.             URLConnection nave1 = url.openConnection();
  225.             HttpURLConnection nave2 = (HttpURLConnection) nave1;
  226.             nave2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
  227.             response = nave2.getResponseCode();
  228.         } catch (Exception e) {
  229.             response = 404;
  230.         }
  231.         return response;
  232.     }
  233.  
  234.     public ArrayList repes(ArrayList array) {
  235.         Object[] listando = array.toArray();
  236.         for (Object item : listando) {
  237.             if (array.indexOf(item) != array.lastIndexOf(item)) {
  238.                 array.remove(array.lastIndexOf(item));
  239.             }
  240.         }
  241.         return array;
  242.     }
  243.  
  244.     public ArrayList cortar(ArrayList array) {
  245.         ArrayList array2 = new ArrayList();
  246.         for (int i = 0; i < array.size(); i++) {
  247.             String code = (String) array.get(i);
  248.             Pattern regex1 = null;
  249.             Matcher regex2 = null;
  250.             regex1 = Pattern.compile("(.*?)=(.*?)");
  251.             regex2 = regex1.matcher(code);
  252.             if (regex2.find()) {
  253.                 array2.add(regex2.group(1) + "=");
  254.             }
  255.         }
  256.         return array2;
  257.     }
  258.  
  259.     public String regex(String code, String deaca, String hastaaca) {
  260.         String resultado = "";
  261.         Pattern regex1 = null;
  262.         Matcher regex2 = null;
  263.         regex1 = Pattern.compile(deaca + "(.*?)" + hastaaca);
  264.         regex2 = regex1.matcher(code);
  265.         if (regex2.find()) {
  266.             resultado = regex2.group(1);
  267.         }
  268.         return resultado;
  269.     }
  270.  
  271.     public Boolean download(String url, File savefile) {
  272.         // Credits : Based on http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
  273.         // Thanks to Brian Risk
  274.         try {
  275.             URL download_page = new URL(url);
  276.             ReadableByteChannel down1 = Channels.newChannel(download_page.openStream());
  277.             FileOutputStream down2 = new FileOutputStream(savefile);
  278.             down2.getChannel().transferFrom(down1, 0, Long.MAX_VALUE);
  279.             down1.close();
  280.             down2.close();
  281.             return true;
  282.         } catch (IOException e) {
  283.             return false;
  284.         }
  285.     }
  286.  
  287.     public String extract_file_by_url(String url) {
  288.         return url.substring(url.lastIndexOf('/') + 1);
  289.     }
  290.  
  291.     public String uri_split(String link, String opcion) {
  292.         String resultado = "";
  293.         try {
  294.             URL url = new URL(link);
  295.             if (opcion == "protocol") {
  296.                 resultado = url.getProtocol();
  297.             } else if (opcion == "authority") {
  298.                 resultado = url.getAuthority();
  299.             } else if (opcion == "host") {
  300.                 resultado = url.getHost();
  301.             } else if (opcion == "port") {
  302.                 resultado = String.valueOf(url.getPort());
  303.             } else if (opcion == "path") {
  304.                 resultado = url.getPath();
  305.             } else if (opcion == "query") {
  306.                 resultado = url.getQuery();
  307.             } else if (opcion == "filename") {
  308.                 resultado = url.getFile();
  309.             } else if (opcion == "ref") {
  310.                 resultado = url.getRef();
  311.             } else {
  312.                 resultado = "Error";
  313.             }
  314.  
  315.         } catch (Exception e) {
  316.             //
  317.         }
  318.         return resultado;
  319.     }
  320.  
  321.     public String md5_encode(String text) {
  322.         // Credits : Based on http://www.avajava.com/tutorials/lessons/how-do-i-generate-an-md5-digest-for-a-string.html
  323.         StringBuffer string_now = null;
  324.         try {
  325.             MessageDigest generate = MessageDigest.getInstance("MD5");
  326.             generate.update(text.getBytes());
  327.             byte[] result = generate.digest();
  328.             string_now = new StringBuffer();
  329.             for (byte line : result) {
  330.                 string_now.append(String.format("%02x", line & 0xff));
  331.             }
  332.         } catch (Exception e) {
  333.             //
  334.         }
  335.         return string_now.toString();
  336.     }
  337.  
  338.     public String md5_file(String file) {
  339.         //Credits : Based on http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java
  340.         // Thanks to
  341.         String resultado = "";
  342.         try {
  343.             MessageDigest convert = MessageDigest.getInstance("MD5");
  344.             FileInputStream file_now = new FileInputStream(file);
  345.  
  346.             byte[] bytes_now = new byte[1024];
  347.  
  348.             int now_now = 0;
  349.             while ((now_now = file_now.read(bytes_now)) != -1) {
  350.                 convert.update(bytes_now, 0, now_now);
  351.             };
  352.             byte[] converting = convert.digest();
  353.             StringBuffer result = new StringBuffer();
  354.             for (int i = 0; i < converting.length; i++) {
  355.                 result.append(Integer.toString((converting[i] & 0xff) + 0x100, 16).substring(1));
  356.             }
  357.             resultado = result.toString();
  358.         } catch (Exception e) {
  359.             //
  360.         }
  361.         return resultado;
  362.     }
  363.  
  364.     public String get_ip(String hostname) {
  365.         String resultado = "";
  366.         try {
  367.             InetAddress getting_ip = InetAddress.getByName(hostname);
  368.             resultado = getting_ip.getHostAddress();
  369.         } catch (Exception e) {
  370.             //
  371.         }
  372.         return resultado;
  373.     }
  374. }
  375.  
  376. // The End ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement