Guest User

Updater.java

a guest
Nov 2nd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.net.URLEncoder;
  9. import java.nio.file.Files;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map.Entry;
  13. import java.util.Scanner;
  14.  
  15. public class Updater extends Thread{
  16.     public static boolean forceRefresh = false;
  17.     public static Thread mainThread;
  18.     public void run(){
  19.         Scanner scanner = new Scanner(System.in);
  20.         while(true){
  21.             String s = scanner.nextLine().toLowerCase();
  22.             if(s.contains("update") || s.contains("refresh")){
  23.                 forceRefresh = true;
  24.                 mainThread.interrupt();
  25.             }
  26.             else{
  27.                 System.out.println("Please enter \"refresh\" or \"update\" to force an update");
  28.             }
  29.         }
  30.     }
  31.     public static void main(String[] args){
  32.         mainThread = Thread.currentThread();
  33.         new Updater().start();
  34.         int refresh = 60;
  35.         String file = null;
  36.         String lastip = null;
  37.         while(true){
  38.             try{
  39.                 boolean shouldRefresh = false;
  40.                 String news = new String(Files.readAllBytes(new File("updater.cfg").toPath())).replace(" ", "");
  41.                 if(file == null || !file.equals(news)){
  42.                     file = news;
  43.                     System.out.println("File differ");
  44.                     shouldRefresh = true;
  45.                 }
  46.                 StringBuilder sb = new StringBuilder(news);
  47.                 System.out.print("Finding ip... ");
  48.                 URL getip = new URL("http://curlmyip.com/");
  49.                 BufferedReader reader = new BufferedReader(new InputStreamReader(getip.openStream()));
  50.                 String ip = reader.readLine();
  51.                 reader.close();
  52.                 System.out.println("Found: " + ip);
  53.                
  54.                 if(lastip == null || !lastip.equals(ip)){
  55.                     System.out.println("IP differ");
  56.                     lastip = ip;
  57.                     shouldRefresh = true;
  58.                 }
  59.                
  60.                 if(!shouldRefresh && !forceRefresh){
  61.                     System.out.println("No change detected");
  62.                     System.out.println();
  63.                     Thread.sleep(refresh * 1000);
  64.                     continue;
  65.                 }
  66.                
  67.                 if(forceRefresh){
  68.                     forceRefresh = false;
  69.                     System.out.println("Forcing update");
  70.                 }
  71.                 else
  72.                     System.out.println("Changes detected");
  73.                
  74.                 int begin = sb.indexOf("user=");
  75.                 int end = sb.indexOf("\n", begin);
  76.                 String email = sb.substring(begin + 5, end).trim();
  77.                 sb.delete(begin, end + 1);
  78.                
  79.                 begin = sb.indexOf("pass=");
  80.                 end = sb.indexOf("\n", begin);
  81.                 String password = sb.substring(begin + 5, end).trim();
  82.                 sb.delete(begin, end + 1);
  83.                
  84.                 begin = sb.indexOf("refresh=");
  85.                 end = sb.indexOf("\n", begin);
  86.                 refresh = Integer.parseInt(sb.substring(begin + 8, end).trim());
  87.                 sb.delete(begin, end + 1);
  88.                
  89.                 HashMap<String, String> map = new HashMap<String, String>();
  90.                
  91.                 while((begin = sb.indexOf("domain=")) != -1){
  92.                     end = sb.indexOf("{", begin);
  93.                     int last = sb.indexOf("}", end);
  94.                     String domainName = sb.substring(begin + 7, end).trim().toLowerCase();
  95.                     String body = sb.substring(end + 1, last).trim().replace("$ip$", ip);
  96.                     int position = 0;
  97.                     StringBuilder request = new StringBuilder();
  98.                     String[] lines = body.split("\n");
  99.                     for(String line : lines){
  100.                         line = line.trim();
  101.                         if(line.length() > 0){
  102.                             if(position > 0){
  103.                                 request.append('&');
  104.                             }
  105.                             int id = position / 3;
  106.                             if(position % 3 == 0){
  107.                                 request.append("hosttype_" + id + "=" + line);
  108.                             }
  109.                             if(position % 3 == 1){
  110.                                 request.append("hostname_" + id + "=" + line);
  111.                             }
  112.                             if(position % 3 == 2){
  113.                                 request.append("hostcontent_" + id + "=" + line);
  114.                             }
  115.                             ++ position;
  116.                         }
  117.                     }
  118.                     map.put(domainName, request.toString());
  119.                     sb.delete(begin, last + 1);
  120.                 }
  121.                 System.out.println("Logging in");
  122.                 String cookie = login(email, password);
  123.                
  124.                 if(cookie == null){
  125.                     Thread.sleep(refresh * 1000);
  126.                     continue;
  127.                 }
  128.                
  129.                 System.out.println("Retrieving domain information");
  130.                 HashMap<String, Integer> domainnr = getDomainnr(cookie);
  131.                
  132.                 System.out.println("Updating domain information");
  133.                 for(Entry<String, String> set : map.entrySet()){
  134.                     if(domainnr.containsKey(set.getKey())){
  135.                         update(cookie, set.getValue(), domainnr.get(set.getKey()));
  136.                     }
  137.                 }
  138.                 System.out.println("Updated");
  139.                 System.out.println();
  140.                 Thread.sleep(refresh * 1000);
  141.             }
  142.             catch(InterruptedException e){
  143.             }
  144.             catch(IOException e){
  145.                 System.err.println("Connection Error");
  146.                 e.printStackTrace();
  147.             }
  148.             catch(Exception e){
  149.                 System.err.println("An error occurred");
  150.                 e.printStackTrace();
  151.             }
  152.         }
  153.     }
  154.     public static void update(String cookie, String data, int domainnr) throws IOException{
  155.         URL url = new URL("http://my.dot.tk/cgi-bin/domainpanel-modify.taloha");
  156.         HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  157.         connection.setRequestMethod("POST");
  158.         connection.setInstanceFollowRedirects(false);
  159.         connection.setUseCaches(false);
  160.         connection.setDoInput(true);
  161.         connection.setDoOutput(true);
  162.         String postData = "action=update&domainnr=" + domainnr + "&usage_type=dns_tk_dns&forward_url=http%3A%2F%2F" + (data.length() == 0 ? "" : "&" + data);
  163.         connection.addRequestProperty("Content-Length", postData.length() + "");
  164.         connection.addRequestProperty("Referer", "http://my.dot.tk/cgi-bin/domainpanel-modify.taloha");
  165.         connection.addRequestProperty("Cookie", cookie);
  166.         connection.connect();
  167.         OutputStream o = connection.getOutputStream();
  168.         o.write(postData.getBytes());
  169.         String s;
  170.         boolean successful = false;
  171.         BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  172.         while((s = reader.readLine()) != null){
  173.             if(s.contains("Your settings are updated succesfully.")){
  174.                 successful = true;
  175.             }
  176.         }
  177.         if(!successful){
  178.             System.err.println("Unable to update domain information");
  179.         }
  180.         o.close();
  181.     }
  182.     public static HashMap<String, Integer> getDomainnr(String cookie) throws IOException{
  183.         URL url = new URL("http://my.dot.tk/cgi-bin/domainpanel.taloha");
  184.         HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  185.         connection.setRequestMethod("GET");
  186.         connection.setInstanceFollowRedirects(false);
  187.         connection.setUseCaches(false);
  188.         connection.setDoInput(true);
  189.         connection.setDoOutput(true);
  190.         connection.addRequestProperty("Referer", "http://my.dot.tk/cgi-bin/domainpanel.taloha");
  191.         connection.addRequestProperty("Cookie", cookie);
  192.         connection.connect();
  193.         BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  194.         HashMap<String, Integer> domainnrs = new HashMap<String, Integer>();
  195.         String s;
  196.         while((s = reader.readLine()) != null){
  197.             if(s.contains("class=\"overlay domainNameLink\" href=\"/cgi-bin/domainpanel-modify.taloha?domainnr=")){
  198.                 StringBuilder found = new StringBuilder((s + reader.readLine()).replace(" ", "").replace("\t", "").replace("\r", "").replace("\n", ""));
  199.                 int begin = found.indexOf("domainnr=") + 9;
  200.                 int end = found.indexOf("\">");
  201.                 int domainnr = Integer.parseInt(found.substring(begin, end));
  202.                 while(found.indexOf("<") != -1){
  203.                     begin = found.indexOf("<");
  204.                     end = found.indexOf(">");
  205.                     found.delete(begin, end + 1);
  206.                 }
  207.                 domainnrs.put(found.toString().toLowerCase(), domainnr);
  208.             }
  209.         }
  210.         return domainnrs;
  211.     }
  212.     public static String login(String email, String password) throws IOException{
  213.         URL url = new URL("http://my.dot.tk/cgi-bin/login02.taloha");
  214.         HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  215.         email = URLEncoder.encode(email, "UTF-8");
  216.         password = URLEncoder.encode(password, "UTF-8");
  217.         String postData = "fldemail=" + email + "&fldpassword=" + password;
  218.         connection.setRequestMethod("POST");
  219.         connection.setInstanceFollowRedirects(false);
  220.         connection.setUseCaches(false);
  221.         connection.setDoInput(true);
  222.         connection.setDoOutput(true);
  223.         connection.addRequestProperty("Content-Length", postData.length() + "");
  224.         connection.addRequestProperty("Referer", "http://my.dot.tk/cgi-bin/login01.taloha");
  225.         connection.connect();
  226.         OutputStream o = connection.getOutputStream();
  227.         o.write(postData.getBytes());
  228.         List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
  229.         if(cookies.size() == 1){
  230.             System.err.println("Invalid email/password");
  231.             return null;
  232.         }
  233.         String finalCookie = "";
  234.         for(int i = 0; i < cookies.size(); i ++){
  235.             finalCookie += cookies.get(i).split("; ")[0] + (i == cookies.size() - 1 ? "" : "; ");
  236.         }
  237.         o.close();
  238.         return finalCookie;
  239.     }
  240. }
Add Comment
Please, Sign In to add comment