Skip to content

Commit 2c0b066

Browse files
committed
Added more DDNS services, renamed to DynamicDNS
Afraid.org, Dynu, No-IP and a Custom URL are now supported.
1 parent 77ac3d2 commit 2c0b066

File tree

6 files changed

+276
-107
lines changed

6 files changed

+276
-107
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
# DuckDNS Spigot
2-
An unofficial DuckDNS plugin for spigot
1+
# DynamicDNS for Spigot
2+
A dynamic DNS plugin for Spigot
33
### Commands:
4-
* /UpdateIP \[IP\] - Force update the IP to DuckDNS, the IP argument can also be used to overide DuckDNS's IP detection (best to leave it blank)
4+
* /UpdateIP \[IP\] - Force updates the IP on your DDNS Services, the IP argument can also be used to overide the IP detection on the DDNS services (best to leave it blank)
55
### Permissions:
6-
* /UpdateIP: DuckDNS.update
7-
* /UpdateIP \[IP\]: DuckDNS.update.ip
6+
* /UpdateIP: DynamicDNS.update
7+
* /UpdateIP \[IP\]: DynamicDNS.update.ip
88
### Config:
99
```yaml
10-
# The Configuration File for Steve's DuckDNS Plugin
10+
# The Configuration File for Steve's DynamicDNS Plugin
1111
period: 3600 # The period between updating IPs in seconds (3600 = 1 hour)
12-
domain: exampledomain # Your subdomain on DuckDNS.org
13-
token: a7c4d0ad-114e-40ef-ba1d-d217904a50f2 # Your token on DuckDNS.org
12+
13+
afraid:
14+
enabled: false
15+
token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # The token taken from the URL on the Dynamic DNS page (eg. http://freedns.afraid.org/dynamic/update.php?xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
16+
17+
duckdns:
18+
enabled: false
19+
domain: exampledomain # Your subdomain on DuckDNS.org
20+
token: a7c4d0ad-114e-40ef-ba1d-d217904a50f2 # Your token on DuckDNS.org
21+
22+
dynu:
23+
enabled: false
24+
hostname: example.ddnsfree.com # Your domain on Dynu
25+
password: password as plaintext, md5 or sha256 # Your password for Dynu (preferably hashed as md5 or sha256 so you're not storing your password in this file)
26+
27+
noip:
28+
enabled: false
29+
hostname: example.ddnsfree.com # Your domain on No-IP
30+
username: username # The username to your account
31+
password: password # The password to your account (sadly they don't support hashed passwords)
32+
33+
custom:
34+
enabled: false
35+
url: https://example.com/?ip=%ip% # The URL to your custom DDNS service, %ip% will be replaced with an ip given as an argument to /updateip [IP]
1436
```

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>me.steve8playz</groupId>
8-
<artifactId>DuckDNS</artifactId>
8+
<artifactId>DynamicDNS</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010

11+
1112
<repositories>
1213
<repository>
1314
<id>spigotmc-repo</id>
@@ -22,12 +23,11 @@
2223
<version>1.15.2-R0.1-SNAPSHOT</version>
2324
<scope>provided</scope>
2425
</dependency>
25-
26-
<dependency>
27-
<groupId>com.konghq</groupId>
28-
<artifactId>unirest-java</artifactId>
29-
<version>2.3.08</version>
30-
<classifier>standalone</classifier>
31-
</dependency>
3226
</dependencies>
27+
28+
<properties>
29+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30+
<maven.compiler.source>1.8</maven.compiler.source>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
</properties>
3333
</project>

src/main/java/me/steve8playz/DuckDNS.java

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package me.steve8playz;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.ChatColor;
5+
import org.bukkit.command.Command;
6+
import org.bukkit.command.CommandSender;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.event.Listener;
9+
import org.bukkit.plugin.java.JavaPlugin;
10+
11+
import java.io.BufferedReader;
12+
import java.io.InputStreamReader;
13+
import java.net.URL;
14+
import java.net.URLConnection;
15+
import java.nio.charset.StandardCharsets;
16+
import java.util.Base64;
17+
18+
public class DynamicDNS extends JavaPlugin implements Listener {
19+
20+
private final String messagePrefix = ChatColor.GOLD + "[DynamicDNS] " + ChatColor.RESET;
21+
22+
@Override
23+
public void onEnable() {
24+
getLogger().info("DynamicDNS " + this.getDescription().getVersion() + " has been Enabled");
25+
getConfig().options().copyDefaults(true);
26+
saveConfig();
27+
if (!(getConfig().getBoolean("afraid.enabled") ||
28+
getConfig().getBoolean("duckdns.enabled") ||
29+
getConfig().getBoolean("dynu.enabled") ||
30+
getConfig().getBoolean("noip.enabled") ||
31+
getConfig().getBoolean("custom.enabled"))) {
32+
getLogger().warning("No DDNS services are enabled, Disabling Plugin");
33+
this.getPluginLoader().disablePlugin(this);
34+
} else updateIPTimer();
35+
}
36+
37+
@Override
38+
public void onDisable() {
39+
saveConfig();
40+
getLogger().info("DynamicDNS " + this.getDescription().getVersion() + " has been Disabled");
41+
}
42+
43+
public boolean updateAfraid(String token, String ip) {
44+
try {
45+
URL url = new URL("https://freedns.afraid.org/dynamic/update.php?" + token + "&address=" + ip);
46+
URLConnection conn = url.openConnection();
47+
conn.connect();
48+
String data = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
49+
if (data.startsWith("Updated")) {
50+
getLogger().info("Updated IP on Afraid.org");
51+
return true;
52+
} else if (data.endsWith("has not changed.")) {
53+
getLogger().info("IP had not changed on Afraid.org");
54+
return true;
55+
} else if (data.startsWith("ERROR: ")) {
56+
getLogger().warning("Error updating IP on Afraid.org: " + data.replace("ERROR: ", ""));
57+
} else {
58+
getLogger().warning("Error updating IP on Afraid.org");
59+
}
60+
61+
} catch (Exception e) {
62+
getLogger().severe(e.getMessage());
63+
}
64+
return false;
65+
}
66+
67+
public boolean updateDuckDNS(String domain, String token, String ip) {
68+
try {
69+
URL url = new URL("https://www.duckdns.org/update?domains=" + domain + "&token=" + token + "&ip=" + ip);
70+
URLConnection conn = url.openConnection();
71+
conn.connect();
72+
String data = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
73+
if (data.equals("OK")) {
74+
getLogger().info("Updated IP on DuckDNS");
75+
return true;
76+
} else if (data.equals("KO")) {
77+
getLogger().warning("Error updating IP on DuckDNS: Check your Domain and Token are correct in the config");
78+
} else {
79+
getLogger().warning("Error updating IP on DuckDNS");
80+
}
81+
82+
} catch (Exception e) {
83+
getLogger().severe(e.getMessage());
84+
}
85+
return false;
86+
}
87+
88+
public boolean updateDynu(String hostname, String password, String ip) {
89+
try {
90+
URL url = new URL("https://api.dynu.com/nic/update?hostname=" + hostname + "&myip=" + ip + "&password=" + password);
91+
URLConnection conn = url.openConnection();
92+
conn.connect();
93+
String data = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
94+
if (data.startsWith("good")) {
95+
getLogger().info("Updated IP on Dynu");
96+
return true;
97+
} else if (data.equals("nochg")) {
98+
getLogger().info("IP had not changed on Dynu");
99+
return true;
100+
} else if (data.equals("badauth")) {
101+
getLogger().warning("Error updating IP on Dynu: Check your Hostname and Password are correct in the config");
102+
} else {
103+
getLogger().warning("Error updating IP on Dynu");
104+
}
105+
106+
} catch (Exception e) {
107+
getLogger().severe(e.getMessage());
108+
}
109+
return false;
110+
}
111+
112+
public boolean updateNoIP(String hostname, String username, String password, String ip) {
113+
try {
114+
URL url = new URL("https://dynupdate.no-ip.com/nic/update?hostname=" + hostname + "&myip=" + ip);
115+
URLConnection conn = url.openConnection();
116+
conn.setRequestProperty("Authorization", "Basic " + new String(Base64.getEncoder().encode((username+":"+password).getBytes(StandardCharsets.UTF_8))));
117+
conn.connect();
118+
String data = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
119+
if (data.startsWith("good")) {
120+
getLogger().info("Updated IP on No-IP");
121+
return true;
122+
} else if (data.startsWith("nochg")) {
123+
getLogger().info("IP had not changed on No-IP");
124+
return true;
125+
} else {
126+
getLogger().warning("Error updating IP on No-IP");
127+
}
128+
129+
} catch (Exception e) {
130+
getLogger().severe(e.getMessage());
131+
}
132+
return false;
133+
}
134+
public boolean updateCustom(String updateURL, String ip) {
135+
try {
136+
URL url = new URL(updateURL.replaceAll("(?i)%ip%", ip));
137+
URLConnection conn = url.openConnection();
138+
conn.connect();
139+
String data = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
140+
141+
getLogger().info("Attempted to update IP on Custom DDNS: " + data);
142+
return true;
143+
144+
} catch (Exception e) {
145+
getLogger().severe(e.getMessage());
146+
}
147+
return false;
148+
}
149+
150+
public boolean updateIP(String ip) {
151+
boolean success = true;
152+
if (getConfig().getBoolean("afraid.enabled")) {
153+
if (!updateAfraid(getConfig().getString("afraid.token"), ip)) {
154+
success = false;
155+
}
156+
}
157+
if (getConfig().getBoolean("duckdns.enabled")) {
158+
if (!updateDuckDNS(getConfig().getString("duckdns.domain"), getConfig().getString("duckdns.token"), ip)) {
159+
success = false;
160+
}
161+
}
162+
if (getConfig().getBoolean("dynu.enabled")) {
163+
if (!updateDynu(getConfig().getString("dynu.hostname"), getConfig().getString("dynu.password"), ip)) {
164+
success = false;
165+
}
166+
}
167+
if (getConfig().getBoolean("noip.enabled")) {
168+
if (!updateNoIP(getConfig().getString("noip.hostname"), getConfig().getString("noip.username"), getConfig().getString("noip.password"), ip)) {
169+
success = false;
170+
}
171+
}
172+
if (getConfig().getBoolean("custom.enabled")) {
173+
if (!updateCustom(getConfig().getString("custom.url"), ip)) {
174+
success = false;
175+
}
176+
}
177+
return success;
178+
}
179+
180+
private void updateIPTimer() {
181+
Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(this, () -> updateIP(""), (0), (getConfig().getInt("period") * 20L));
182+
}
183+
184+
@Override
185+
public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
186+
if ((cmd.getName().equalsIgnoreCase("updateip")) && (sender.hasPermission("duckdns.update"))) {
187+
if ((args.length == 1) && (sender.hasPermission("duckdns.update.ip"))) {
188+
Bukkit.getServer().getScheduler().runTaskAsynchronously(this, () -> {
189+
if (updateIP(args[0]) && sender instanceof Player) {
190+
sender.sendMessage(messagePrefix + "Updated IP.");
191+
} else if (sender instanceof Player) {
192+
sender.sendMessage(messagePrefix + "Error updating IP, check the log for details.");
193+
}
194+
});
195+
} else {
196+
Bukkit.getServer().getScheduler().runTaskAsynchronously(this, () -> {
197+
if (updateIP("") && sender instanceof Player) {
198+
sender.sendMessage(messagePrefix + "Updated IP.");
199+
} else if (sender instanceof Player) {
200+
sender.sendMessage(messagePrefix + "Error updating IP, check the log for details.");
201+
}
202+
});
203+
}
204+
}
205+
return true;
206+
}
207+
}

0 commit comments

Comments
 (0)