|
| 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