Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.nio.file.Files;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map.Entry;
- import java.util.Scanner;
- public class Updater extends Thread{
- public static boolean forceRefresh = false;
- public static Thread mainThread;
- public void run(){
- Scanner scanner = new Scanner(System.in);
- while(true){
- String s = scanner.nextLine().toLowerCase();
- if(s.contains("update") || s.contains("refresh")){
- forceRefresh = true;
- mainThread.interrupt();
- }
- else{
- System.out.println("Please enter \"refresh\" or \"update\" to force an update");
- }
- }
- }
- public static void main(String[] args){
- mainThread = Thread.currentThread();
- new Updater().start();
- int refresh = 60;
- String file = null;
- String lastip = null;
- while(true){
- try{
- boolean shouldRefresh = false;
- String news = new String(Files.readAllBytes(new File("updater.cfg").toPath())).replace(" ", "");
- if(file == null || !file.equals(news)){
- file = news;
- System.out.println("File differ");
- shouldRefresh = true;
- }
- StringBuilder sb = new StringBuilder(news);
- System.out.print("Finding ip... ");
- URL getip = new URL("http://curlmyip.com/");
- BufferedReader reader = new BufferedReader(new InputStreamReader(getip.openStream()));
- String ip = reader.readLine();
- reader.close();
- System.out.println("Found: " + ip);
- if(lastip == null || !lastip.equals(ip)){
- System.out.println("IP differ");
- lastip = ip;
- shouldRefresh = true;
- }
- if(!shouldRefresh && !forceRefresh){
- System.out.println("No change detected");
- System.out.println();
- Thread.sleep(refresh * 1000);
- continue;
- }
- if(forceRefresh){
- forceRefresh = false;
- System.out.println("Forcing update");
- }
- else
- System.out.println("Changes detected");
- int begin = sb.indexOf("user=");
- int end = sb.indexOf("\n", begin);
- String email = sb.substring(begin + 5, end).trim();
- sb.delete(begin, end + 1);
- begin = sb.indexOf("pass=");
- end = sb.indexOf("\n", begin);
- String password = sb.substring(begin + 5, end).trim();
- sb.delete(begin, end + 1);
- begin = sb.indexOf("refresh=");
- end = sb.indexOf("\n", begin);
- refresh = Integer.parseInt(sb.substring(begin + 8, end).trim());
- sb.delete(begin, end + 1);
- HashMap<String, String> map = new HashMap<String, String>();
- while((begin = sb.indexOf("domain=")) != -1){
- end = sb.indexOf("{", begin);
- int last = sb.indexOf("}", end);
- String domainName = sb.substring(begin + 7, end).trim().toLowerCase();
- String body = sb.substring(end + 1, last).trim().replace("$ip$", ip);
- int position = 0;
- StringBuilder request = new StringBuilder();
- String[] lines = body.split("\n");
- for(String line : lines){
- line = line.trim();
- if(line.length() > 0){
- if(position > 0){
- request.append('&');
- }
- int id = position / 3;
- if(position % 3 == 0){
- request.append("hosttype_" + id + "=" + line);
- }
- if(position % 3 == 1){
- request.append("hostname_" + id + "=" + line);
- }
- if(position % 3 == 2){
- request.append("hostcontent_" + id + "=" + line);
- }
- ++ position;
- }
- }
- map.put(domainName, request.toString());
- sb.delete(begin, last + 1);
- }
- System.out.println("Logging in");
- String cookie = login(email, password);
- if(cookie == null){
- Thread.sleep(refresh * 1000);
- continue;
- }
- System.out.println("Retrieving domain information");
- HashMap<String, Integer> domainnr = getDomainnr(cookie);
- System.out.println("Updating domain information");
- for(Entry<String, String> set : map.entrySet()){
- if(domainnr.containsKey(set.getKey())){
- update(cookie, set.getValue(), domainnr.get(set.getKey()));
- }
- }
- System.out.println("Updated");
- System.out.println();
- Thread.sleep(refresh * 1000);
- }
- catch(InterruptedException e){
- }
- catch(IOException e){
- System.err.println("Connection Error");
- e.printStackTrace();
- }
- catch(Exception e){
- System.err.println("An error occurred");
- e.printStackTrace();
- }
- }
- }
- public static void update(String cookie, String data, int domainnr) throws IOException{
- URL url = new URL("http://my.dot.tk/cgi-bin/domainpanel-modify.taloha");
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- connection.setRequestMethod("POST");
- connection.setInstanceFollowRedirects(false);
- connection.setUseCaches(false);
- connection.setDoInput(true);
- connection.setDoOutput(true);
- String postData = "action=update&domainnr=" + domainnr + "&usage_type=dns_tk_dns&forward_url=http%3A%2F%2F" + (data.length() == 0 ? "" : "&" + data);
- connection.addRequestProperty("Content-Length", postData.length() + "");
- connection.addRequestProperty("Referer", "http://my.dot.tk/cgi-bin/domainpanel-modify.taloha");
- connection.addRequestProperty("Cookie", cookie);
- connection.connect();
- OutputStream o = connection.getOutputStream();
- o.write(postData.getBytes());
- String s;
- boolean successful = false;
- BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- while((s = reader.readLine()) != null){
- if(s.contains("Your settings are updated succesfully.")){
- successful = true;
- }
- }
- if(!successful){
- System.err.println("Unable to update domain information");
- }
- o.close();
- }
- public static HashMap<String, Integer> getDomainnr(String cookie) throws IOException{
- URL url = new URL("http://my.dot.tk/cgi-bin/domainpanel.taloha");
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- connection.setRequestMethod("GET");
- connection.setInstanceFollowRedirects(false);
- connection.setUseCaches(false);
- connection.setDoInput(true);
- connection.setDoOutput(true);
- connection.addRequestProperty("Referer", "http://my.dot.tk/cgi-bin/domainpanel.taloha");
- connection.addRequestProperty("Cookie", cookie);
- connection.connect();
- BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- HashMap<String, Integer> domainnrs = new HashMap<String, Integer>();
- String s;
- while((s = reader.readLine()) != null){
- if(s.contains("class=\"overlay domainNameLink\" href=\"/cgi-bin/domainpanel-modify.taloha?domainnr=")){
- StringBuilder found = new StringBuilder((s + reader.readLine()).replace(" ", "").replace("\t", "").replace("\r", "").replace("\n", ""));
- int begin = found.indexOf("domainnr=") + 9;
- int end = found.indexOf("\">");
- int domainnr = Integer.parseInt(found.substring(begin, end));
- while(found.indexOf("<") != -1){
- begin = found.indexOf("<");
- end = found.indexOf(">");
- found.delete(begin, end + 1);
- }
- domainnrs.put(found.toString().toLowerCase(), domainnr);
- }
- }
- return domainnrs;
- }
- public static String login(String email, String password) throws IOException{
- URL url = new URL("http://my.dot.tk/cgi-bin/login02.taloha");
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- email = URLEncoder.encode(email, "UTF-8");
- password = URLEncoder.encode(password, "UTF-8");
- String postData = "fldemail=" + email + "&fldpassword=" + password;
- connection.setRequestMethod("POST");
- connection.setInstanceFollowRedirects(false);
- connection.setUseCaches(false);
- connection.setDoInput(true);
- connection.setDoOutput(true);
- connection.addRequestProperty("Content-Length", postData.length() + "");
- connection.addRequestProperty("Referer", "http://my.dot.tk/cgi-bin/login01.taloha");
- connection.connect();
- OutputStream o = connection.getOutputStream();
- o.write(postData.getBytes());
- List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
- if(cookies.size() == 1){
- System.err.println("Invalid email/password");
- return null;
- }
- String finalCookie = "";
- for(int i = 0; i < cookies.size(); i ++){
- finalCookie += cookies.get(i).split("; ")[0] + (i == cookies.size() - 1 ? "" : "; ");
- }
- o.close();
- return finalCookie;
- }
- }
Add Comment
Please, Sign In to add comment