Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
keeptpa committed Nov 23, 2021
0 parents commit 57b8e2f
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Project exclude paths
/out/
/target/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>net.keeptpa</groupId>
<artifactId>AliDDNS</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alidns20150109</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<!-- jsoup HTML parser library @ https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Entry

40 changes: 40 additions & 0 deletions src/main/net.keeptpa/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import org.jsoup.Jsoup;

import java.io.IOException;

public class AppConfig {
public AppConfig() throws IOException {

this.path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

if(System.getProperty("os.name").contains("dows"))
{
this.path = this.path.substring(1,this.path.length());
}
if(path.contains(".jar"))
{
this.path = this.path.substring(0,this.path.lastIndexOf("."));
this.path = this.path.substring(0,this.path.lastIndexOf("/"));
}
this.path = this.path.replace("target/classes/", "");
//System.out.println("App config created in " + this.path);

externalIP = Jsoup.connect("http://ipv4.icanhazip.com/").get().outerHtml();;

externalIP = externalIP.replaceAll("head","");
externalIP = externalIP.replaceAll("html","");
externalIP = externalIP.replaceAll("body","");
externalIP = externalIP.replaceAll("<","");
externalIP = externalIP.replaceAll(">","");
externalIP = externalIP.replaceAll("\n","");
externalIP = externalIP.replaceAll("/","");
externalIP = externalIP.replaceAll(" ","");

}

public String path;
protected String key;
protected String sec;
public String domain;
public String externalIP;
}
9 changes: 9 additions & 0 deletions src/main/net.keeptpa/ClientConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class ClientConfig {
String recordID;
String status;
String type;
String RR;
String currentRemoteValue;
String expectedValue = "";
boolean isControlling;
}
16 changes: 16 additions & 0 deletions src/main/net.keeptpa/ClientCreate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import com.aliyun.tea.*;
import com.aliyun.alidns20150109.*;
import com.aliyun.alidns20150109.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;

public class ClientCreate {
public static com.aliyun.alidns20150109.Client createClient(String key,String sec) throws Exception {
Config config = new Config()
.setAccessKeyId(key)
.setAccessKeySecret(sec);
config.endpoint = "alidns.cn-beijing.aliyuncs.com";

return new com.aliyun.alidns20150109.Client(config);
}
}
134 changes: 134 additions & 0 deletions src/main/net.keeptpa/Entry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import com.aliyun.alidns20150109.models.DescribeDomainRecordsRequest;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponse;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody;
import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.jsoup.Jsoup;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Entry {
public static AppConfig aconfig;
public static void main(String[] args) throws Exception {

//Set config and config directory
aconfig = new AppConfig();
File configDirectory = new File(aconfig.path + "\\config\\");
if(!configDirectory.exists()){
configDirectory.mkdir();
}
File appConfig = new File(aconfig.path + "\\config\\appConfig.json");

//App run once
Logger.Log("<==============================>");
Logger.Log("App running...");
System.out.println("App path: " + aconfig.path);
Gson gson = new GsonBuilder().setPrettyPrinting().create();

if(appConfig.exists()){
Logger.Log("App config found, importing..");
String aconfigLocalJson = FileIO.Read(aconfig.path + "\\config\\appConfig.json");
AppConfig currentLocalConfig = gson.fromJson(aconfigLocalJson,AppConfig.class);

aconfig.key = currentLocalConfig.key;
aconfig.sec = currentLocalConfig.sec;
aconfig.domain = currentLocalConfig.domain;

}else {
Logger.Log("App config not found, create one");
Scanner sc = new Scanner(System.in);
//ASK
System.out.println("Input accessKey:");
aconfig.key = sc.nextLine();
System.out.println("Input accessSecret:");
aconfig.sec = sc.nextLine();
System.out.println("Input domain");
aconfig.domain = sc.nextLine();

//Save
appConfig.createNewFile();
FileIO.Write(aconfig.path + "\\config\\appConfig.json", gson.toJson(aconfig),false);

}
//====>Create client
com.aliyun.alidns20150109.Client client = ClientCreate.createClient(aconfig.key,aconfig.sec);

//Query
DescribeDomainRecordsRequest describeDomainRecordsRequest = new DescribeDomainRecordsRequest()
.setDomainName(aconfig.domain);

DescribeDomainRecordsResponse resp = client.describeDomainRecords(describeDomainRecordsRequest);

ArrayList<ClientConfig> thisQuery = new ArrayList<ClientConfig>();
for (DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord temp: resp.body.domainRecords.record
) {
ClientConfig cc = new ClientConfig();
cc.currentRemoteValue = temp.value;
cc.status = temp.status;
cc.RR = temp.RR;
cc.type = temp.type;
cc.recordID = temp.recordId;

thisQuery.add(cc);
}

//Check if a client config exist
File clientConfig = new File(aconfig.path + "\\config\\clientConfig.json");
if(!clientConfig.exists()){
clientConfig.createNewFile();

FileIO.Write(aconfig.path + "\\config\\clientConfig.json",gson.toJson(thisQuery),false);
}

//Then load the client config;
Logger.Log("Loading client config..");
ArrayList<ClientConfig> localClientConfig = new ArrayList<ClientConfig>();
localClientConfig = gson.fromJson(FileIO.Read(aconfig.path + "\\config\\clientConfig.json"),new TypeToken<ArrayList<ClientConfig>>(){}.getType());
//And check their controlling status
Boolean needUpdate = false;
for (ClientConfig local:localClientConfig
) {
for (ClientConfig remote:thisQuery
) {
if(local.recordID.equals(remote.recordID)){
Logger.Log("Checking " + local.recordID + " ...");
remote.isControlling = local.isControlling;
//If it's controlling, and ip not match, set expectedValue to current external ip
if(remote.isControlling){
if(!remote.currentRemoteValue.equals(aconfig.externalIP)){
Logger.Log("Found RecordID: " +local.recordID +" needs update.");
remote.expectedValue = aconfig.externalIP;
needUpdate = true;
}
}
}
}
}

//Cover it.
FileIO.Write(aconfig.path + "\\config\\clientConfig.json",gson.toJson(thisQuery),false);

//Start update
if(needUpdate){
for (ClientConfig remote:thisQuery
) {
if(remote.isControlling && !remote.currentRemoteValue.equals(aconfig.externalIP)){
UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest()
.setRecordId(remote.recordID)
.setRR(remote.RR)
.setType(remote.type)
.setValue(remote.expectedValue);
client.updateDomainRecord(updateDomainRecordRequest);
Logger.Log(remote.recordID + " is updated from " + remote.currentRemoteValue + " to " + remote.expectedValue);
}
}
}


}
}
27 changes: 27 additions & 0 deletions src/main/net.keeptpa/FileIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import com.sun.org.apache.xpath.internal.operations.Bool;

import java.io.*;

public class FileIO {
public static String Read(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));

String line = "";
String res = "";

while((line = br.readLine()) != null){
res += line;
}
br.close();
return res;
}

public static void Write(String path,String str, Boolean append ) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(path,append));

bw.write(str);
bw.close();
}


}
18 changes: 18 additions & 0 deletions src/main/net.keeptpa/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Logger {
static File log = new File(Entry.aconfig.path + "\\log.txt");
static Date date = new Date();
static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public static void Log(String str) throws IOException {
if (!log.exists()) {
log.createNewFile();
}
str = "[" + formatter.format(date) + "]: " + str;
FileIO.Write(log.getPath(),str,true);
FileIO.Write(log.getPath(),"\n",true);
}
}

0 comments on commit 57b8e2f

Please sign in to comment.