diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index f3671ae..f3bc67b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ aliyun DNS 解析的 DDNS Java 软件,可以实现自动获取 ipv4 和 ipv6 公网 然后在阿里云进行 ddns 操作! -## 启动方法 +## 操作之前的准备工作 ### 安装 aliyun 客户端 @@ -71,15 +71,17 @@ Use `aliyun --help` for more information. IP地址的位置可以写服务器当前的 IP 地址,但是也可以写错误的,DDNS 程序会自动的将您的 IP 地址进行更改!只要新增了 DNS 解析记录就可以了 +## 做为软件使用的 启动方法 + ### 启动 jar 程序 从仓库中下载好您需要的 jar 包之后,使用下面的命令启动即可! ```shell -java -jar ./LyMbl_AliyunDDNS_Java.jar [日志目录] [动态更新DNS间隔(ms) 不要太短,会导致性能浪费] [需要被动态更新的子域名(如 www)] [需要被动态更新的顶级域名(如 lingyuzhao.top)] +java -jar ./LyMbl_AliyunDDNS_Java.jar [日志目录] [动态更新DNS间隔(ms) 不要太短,会导致性能浪费] [需要被动态更新的子域名(如 www)] [需要被动态更新的顶级域名(如 baidu.com)] ``` -## 启动示例 +#### 启动示例 在这里我们展示了部分日志,且对其中的数据进行了脱敏,您启动之后看到类型下面的日志,则代表成功运行了! @@ -89,17 +91,77 @@ root@gust-desktop:/opt/app/LyMbl_DDNS_Java# java -jar ./LyMbl_AliyunDDNS_Java.ja 十一月 12, 2024 10:53:55 下午 top.lingyuzhao.lyMbl.ddns.aliyun.LyMbl_DDNS_Java main 信息: 创建日志目录成功! -十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.DDNSManager lambda$initIp$0 +十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager lambda$initIp$0 信息: RR: lsb value: *********** -十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.DDNSManager lambda$initIp$0 +十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager lambda$initIp$0 信息: 初始化 ipv6: ************ -十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.DDNSManager lambda$initIp$0 +十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager lambda$initIp$0 信息: RR: *** value: ************ -十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.DDNSManager lambda$initIp$0 +十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager lambda$initIp$0 信息: 初始化 ipv4: ************ -十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.DDNSManager start +十一月 12, 2024 10:53:56 下午 top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager start 信息: DDNS running updateTimeMS=3600000 -十一月 12, 2024 10:53:57 下午 top.lingyuzhao.lyMbl.ddns.aliyun.DDNSManager updateIp +十一月 12, 2024 10:53:57 下午 top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager updateIp 信息: DDNS update type: A ip: ************ same with last ...... ``` + +## 做为依赖包组件的使用方法 + +### 添加依赖 + +```xml + + io.github.BeardedManZhao + LyMbl_AliyunDDNS_Java + 2024.11.13 + +``` + +### maven 组件包中的 main 函数调用 + +这样的方式中,您可以在您自己的main函数中调用 LyMbl_AliyunDDNS_Java 中的 main 函数,以启动服务。 + +```java +import top.lingyuzhao.lyMbl.ddns.aliyun.LyMbl_DDNS_Java; + +/** + * @author zhao + */ +public class Main { + public static void main(String[] args) { + // "请输入参数:【logDir】【更新时间间隔(MS)】【RR参数:代表子域名字,如 www】【顶级域名:如 baidu.com】" + LyMbl_DDNS_Java.main("./logs", "3600000", "www", "baidu.com"); + } +} + +``` + +### maven 组件包中的组件开发调用 + +这样的方式中,您可以在您自己的项目代码中装载我们的 D DNS 管理器,灵活性最高,下面是一个示例 + +```java +import top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager; + +import java.util.logging.Logger; + +/** + * @author zhao + */ +public class Main { + public static void main(String[] args) { + final Logger test = Logger.getLogger("test"); + try ( + // 实例化 DDNS 管理器 + final AliYunDDnsManager aliYunDDnsManager = new AliYunDDnsManager( + // 传递日志对象 以及域名信息 + test, "baidu.com", "www" + ) + ) { + // 启动 每3600s 检查一次 DNS 信息 + aliYunDDnsManager.start(3600000); + } + } +} +``` diff --git a/out/artifacts/LyMbl_AliyunDDNS_Java_jar/LyMbl_AliyunDDNS_Java.jar b/out/artifacts/LyMbl_AliyunDDNS_Java_jar/LyMbl_AliyunDDNS_Java.jar deleted file mode 100644 index cd389f6..0000000 Binary files a/out/artifacts/LyMbl_AliyunDDNS_Java_jar/LyMbl_AliyunDDNS_Java.jar and /dev/null differ diff --git a/pom.xml b/pom.xml index 503c487..0522c2d 100644 --- a/pom.xml +++ b/pom.xml @@ -2,30 +2,18 @@ 4.0.0 - com.aliyun + io.github.BeardedManZhao LyMbl_AliyunDDNS_Java - 1.0.0 + 2024.11.13 jar - LyMbl_DDNS_Java - Alibaba Cloud SDK Code Sample for Java - - - - aliyundeveloper - Aliyun SDK - aliyunsdk@aliyun.com - - - - - sonatype-nexus-snapshots - https://s01.oss.sonatype.org/content/repositories/snapshots - - - sonatype-nexus-staging - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - + LyMbl_AliyunDDNS_Java + LyMbl_AliyunDDNS_Java-java + https://github.com/BeardedManZhao/LyMbl_AliyunDDNS_Java.git + + + 8 + 8 + @@ -39,16 +27,117 @@ 2.0.30 - - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - - + + + + + The Apache Software License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + + LingYuZhao + liming7887@qq.com + https://github.com/BeardedManZhao + + + + + + https://github.com/BeardedManZhao/LyMbl_AliyunDDNS_Java.git + scm:git:ssh://git@github.com:BeardedManZhao/LyMbl_AliyunDDNS_Java.git + https://github.com/BeardedManZhao/LyMbl_AliyunDDNS_Java + + + + + + UTF-8 + UTF-8 + + + ossrh + + true + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true + + ossrh + https://s01.oss.sonatype.org/ + true + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.0 + + + attach-sources + + jar-no-fork + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadocs + + jar + + + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + + + + + + ossrh + https://s01.oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + \ No newline at end of file diff --git a/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/DDNSManager.java b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/AliYunDDnsManager.java similarity index 88% rename from src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/DDNSManager.java rename to src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/AliYunDDnsManager.java index 6c9a541..1a75f54 100644 --- a/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/DDNSManager.java +++ b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/AliYunDDnsManager.java @@ -12,7 +12,12 @@ import java.util.concurrent.Executors; import java.util.logging.Logger; -public class DDNSManager implements AutoCloseable { +/** + * 阿里云的 DDNS 管理器 其可以实现域名解析,支持 AAAA 和 A 类型 + * + * @author lingyuzhao + */ +public class AliYunDDnsManager implements DDnsManager { private final Logger logger; private final String domain; @@ -20,7 +25,14 @@ public class DDNSManager implements AutoCloseable { private final ExecutorService executorService; private String backIpv4, backIpv6, ipv4DnsId, ipv6DnsId; - public DDNSManager(Logger logger, String domain, String RR) { + /** + * 构造函数 + * + * @param logger 日志对象 + * @param domain 域名 + * @param RR 被操作的子域名 + */ + public AliYunDDnsManager(Logger logger, String domain, String RR) { this.logger = logger; this.domain = domain; this.RR = RR; @@ -28,8 +40,15 @@ public DDNSManager(Logger logger, String domain, String RR) { initIp(); } + @Override + public String getManagerName() { + return "AliYunDDnsManager"; + } + + @Override public void start(long updateTimeMS) { logger.info("DDNS running updateTimeMS=" + updateTimeMS); + logger.info(this.getLogo()); while (true) { try { updateIPs(); @@ -44,7 +63,7 @@ public void start(long updateTimeMS) { } } - private void initIp() { + public void initIp() { try { final Process exec = Runtime.getRuntime().exec("aliyun alidns DescribeDomainRecords --DomainName " + domain); try (final InputStream inputStream = exec.getInputStream()) { @@ -74,7 +93,7 @@ private void initIp() { } } - private void updateIPs() throws Exception { + public void updateIPs() throws Exception { // 获取公网 IPV4 final String publicIpv4Address = PublicIpFetcher.getPublicIpv4Address(); updateIp(RR, "A", ipv4DnsId, publicIpv4Address); @@ -88,7 +107,7 @@ private void updateIPs() throws Exception { } } - private void updateIp(String RR, String type, String recordId, String ip) throws Exception { + public void updateIp(String RR, String type, String recordId, String ip) throws Exception { switch (type) { case "A": if (ip.equals(backIpv4)) { diff --git a/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/DDnsManager.java b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/DDnsManager.java new file mode 100644 index 0000000..81b51f4 --- /dev/null +++ b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/DDnsManager.java @@ -0,0 +1,111 @@ +package top.lingyuzhao.lyMbl.ddns.aliyun; + +/** + * DDNS 功能处理器 + * + * @author zhao + */ +public interface DDnsManager extends AutoCloseable { + + /* + + ..,,,'';::::::;'',,,.. + ..,';:::::;;::::::::::::;;:::::;',.. + .,;:::;;::::::;;;::::::::::;;;::::::;;:::;,. + ,;::;:::::;::::;;',,,,......,,,,';;::::;:::::;::;, + ,;::;::::;::;',. .,';::;::::;::;, + .;::::::;::;,. .,;::;::::::;. + ,;:;::::::'. . .'::::::;:;, + ,::::::;:;, ..,,,,,,,,,,,,,.,,,,,,,.. ,;:;::::::, + ,::::::::,. .,,,,,,',,. :,. ,,,',.,,,.. .,::::::::, + ;:::::::, .,,,,. .3,,. :,. ,','. .,,,,,. ,:::::::; + ,:::::::' .,,,,. ,:,, 7,. .:,,, .,,,,. ':::::::, + ':::::::. .,,,. ,:,, 7,. 7,,, .,,,. .:::::::' + ;::::::' .,,,. ,7,, 7,. 7,,, ,,,. '::::::; + ;;:::::, ,,,. .l,, :,. :,,. .',, ,:::::;; + ;;:::::, ',,,.........o,,............:,,..........;','.,;;;;;;''b,,. ,:::::;; + '::::::, .',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,::::::' + + ░▒▒▒▒▒▒▒▒░ ▒▒▒▒▒▒▒▒▒░ ░▒▒▒░ ░▒▒░ ░▒▓▓▓▒▒ + ░▓▓▓░░░▓▓▓▓▒ ▒▓▓▒░░▒▓▓▓▓ ░▓▓▓▓▒ ▒▓▓░ ░▓▓▓░ ░░ ░ + ░▓▓▓ ▒▓▓▓ ▒▓▓░ ░▓▓▓▒ ░▓▓▓▓▓▓░ ▒▓▓░ ░▓▓▓▓▒░ + ░▓▓▓ ░▓▓▓ ▒▓▓░ ▒▓▓▒ ░▓▓▒ ▒▓▓▒▒▓▓░ ░▒▒▓▓▓▓▒ + ░▓▓▓ ░▒▓▓▒ ▒▓▓▒ ▒▓▓▓ ░▓▓▒ ░▓▓▓▓▓░ ▒▓▓▓░ ░ + ░▓▓▓▓▓▓▓▓▒░ ▒▓▓▓▓▓▓▓▓▒ ░▓▓▒ ▒▓▓▓░ ░▓▓▒▒▓▓▓▒ + ,::::::' ,,',,,,,,,,,,,3,,,,,,,,,,,,,,':,,,,,,,,,,,,,,7,,,,,,,,,,,,,:7,. '::::::, + '::::::, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,. ,::::::' + ;;:::::, .,,'. ;,,. :,. ;,,, ;,, ,:::::;; + ;;:::::, ,,'. 7,,, :,. ,:,, ;:,. ,:::::;; + ;::::::' .,,,. :,,. 7,. ,3,, ,:,, '::::::; + ':::::::. .,,', ;,,. 7,. ,3,, .::,.. .:::::::' + ,:::::::' .,,',. :,,, 7,. ':,, .;7',. ':::::::, + ;:::::::, .,,';;,.7,,' 7,. 3',. .'7:',. ,:::::::; + ,::::::::,. ..,';',,;,....7,. ..,3,,':;,,.. .,::::::::, + ,::::::;:;, ..,,,,,'',,,'',,,,.. ,;:;::::::, + ,;:;::::::'. .'::::::;:;, + .;::::::;::;,. .,;::;::::::;. + ,;::;::::;::;',. .,';::;::::;::;, + ,;::;:::::;::::;;',,,,......,,,,';;::::;:::::;::;, + .,;:::;;::::::;;;::::::::::;;;::::::;;:::;,. + ..,';:::::;;::::::::::::;;:::::;',.. + + + */ + + /** + * @return 在启动成功之后,有些管理器会打印出 logo + */ + default String getLogo() { + return this.getManagerName() + '\n' + + " ..,,,'';::::::;'',,,..\n" + + " ..,';:::::;;::::::::::::;;:::::;',..\n" + + " .,;:::;;::::::;;;::::::::::;;;::::::;;:::;,.\n" + + " ,;::;:::::;::::;;',,,,......,,,,';;::::;:::::;::;,\n" + + " ,;::;::::;::;',. .,';::;::::;::;,\n" + + " .;::::::;::;,. .,;::;::::::;.\n" + + " ,;:;::::::'. . .'::::::;:;,\n" + + " ,::::::;:;, ..,,,,,,,,,,,,,.,,,,,,,.. ,;:;::::::,\n" + + " ,::::::::,. .,,,,,,',,. :,. ,,,',.,,,.. .,::::::::,\n" + + " ;:::::::, .,,,,. .3,,. :,. ,','. .,,,,,. ,:::::::;\n" + + " ,:::::::' .,,,,. ,:,, 7,. .:,,, .,,,,. ':::::::,\n" + + " ':::::::. .,,,. ,:,, 7,. 7,,, .,,,. .:::::::'\n" + + " ;::::::' .,,,. ,7,, 7,. 7,,, ,,,. '::::::;\n" + + " ;;:::::, ,,,. .l,, :,. :,,. .',, ,:::::;;\n" + + " ;;:::::, ',,,.........o,,............:,,..........;','.,;;;;;;''b,,. ,:::::;;\n" + + " '::::::, .',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,::::::'\n" + + " \n" + + " ░▒▒▒▒▒▒▒▒░ ▒▒▒▒▒▒▒▒▒░ ░▒▒▒░ ░▒▒░ ░▒▓▓▓▒▒ \n" + + " ░▓▓▓░░░▓▓▓▓▒ ▒▓▓▒░░▒▓▓▓▓ ░▓▓▓▓▒ ▒▓▓░ ░▓▓▓░ ░░ ░ \n" + + " ░▓▓▓ ▒▓▓▓ ▒▓▓░ ░▓▓▓▒ ░▓▓▓▓▓▓░ ▒▓▓░ ░▓▓▓▓▒░ \n" + + " ░▓▓▓ ░▓▓▓ ▒▓▓░ ▒▓▓▒ ░▓▓▒ ▒▓▓▒▒▓▓░ ░▒▒▓▓▓▓▒ \n" + + " ░▓▓▓ ░▒▓▓▒ ▒▓▓▒ ▒▓▓▓ ░▓▓▒ ░▓▓▓▓▓░ ▒▓▓▓░ ░ \n" + + " ░▓▓▓▓▓▓▓▓▒░ ▒▓▓▓▓▓▓▓▓▒ ░▓▓▒ ▒▓▓▓░ ░▓▓▒▒▓▓▓▒ \n" + + " ,::::::' ,,',,,,,,,,,,,3,,,,,,,,,,,,,,':,,,,,,,,,,,,,,7,,,,,,,,,,,,,:7,. '::::::,\n" + + " '::::::, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,. ,::::::'\n" + + " ;;:::::, .,,'. ;,,. :,. ;,,, ;,, ,:::::;;\n" + + " ;;:::::, ,,'. 7,,, :,. ,:,, ;:,. ,:::::;;\n" + + " ;::::::' .,,,. :,,. 7,. ,3,, ,:,, '::::::;\n" + + " ':::::::. .,,', ;,,. 7,. ,3,, .::,.. .:::::::'\n" + + " ,:::::::' .,,',. :,,, 7,. ':,, .;7',. ':::::::,\n" + + " ;:::::::, .,,';;,.7,,' 7,. 3',. .'7:',. ,:::::::;\n" + + " ,::::::::,. ..,';',,;,....7,. ..,3,,':;,,.. .,::::::::,\n" + + " ,::::::;:;, ..,,,,,'',,,'',,,,.. ,;:;::::::,\n" + + " ,;:;::::::'. .'::::::;:;,\n" + + " .;::::::;::;,. .,;::;::::::;.\n" + + " ,;::;::::;::;',. .,';::;::::;::;,\n" + + " ,;::;:::::;::::;;',,,,......,,,,';;::::;:::::;::;,\n" + + " .,;:::;;::::::;;;::::::::::;;;::::::;;:::;,.\n" + + " ..,';:::::;;::::::::::::;;:::::;',.."; + } + + /** + * @return D DNS Manager 名称 + */ + String getManagerName(); + + /** + * D DNS 启动函数 + * @param updateTimeMS 更新时间间隔(MS) + */ + void start(long updateTimeMS); +} diff --git a/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/LoggerFormatter.java b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/LoggerFormatter.java new file mode 100644 index 0000000..4a07346 --- /dev/null +++ b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/LoggerFormatter.java @@ -0,0 +1,30 @@ +package top.lingyuzhao.lyMbl.ddns.aliyun; + +import java.util.logging.Formatter; +import java.util.logging.LogRecord; + +/** + * 日志格式化 + * + * @author zhao + */ +public class LoggerFormatter extends Formatter { + + private static final String LEVEL_PREFIX = "["; + private static final String LEVEL_SUFFIX = "] "; + private static final String TIME_PREFIX = "["; + private static final String TIME_SUFFIX = "] "; + private static final String MESSAGE_SUFFIX = "\n"; + + @Override + public String format(LogRecord record) { + return LEVEL_PREFIX + + record.getLevel() + + LEVEL_SUFFIX + + TIME_PREFIX + + record.getMillis() + + TIME_SUFFIX + + record.getMessage() + + MESSAGE_SUFFIX; + } +} \ No newline at end of file diff --git a/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/LyMbl_DDNS_Java.java b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/LyMbl_DDNS_Java.java index cb54cb2..5fb2091 100644 --- a/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/LyMbl_DDNS_Java.java +++ b/src/main/java/top/lingyuzhao/lyMbl/ddns/aliyun/LyMbl_DDNS_Java.java @@ -14,7 +14,7 @@ public class LyMbl_DDNS_Java { logger.setLevel(Level.INFO); } - public static void main(String[] args) { + public static void main(String... args) { if (args.length < 4) { logger.warning("请输入参数:【logDir】【更新时间间隔(MS)】【RR参数:代表子域名字,如 www】【顶级域名:如 lingyuzhao.top】"); return; @@ -39,10 +39,11 @@ public static void main(String[] args) { throw new RuntimeException(e); } fileHandler.setLevel(Level.INFO); + fileHandler.setFormatter(new LoggerFormatter()); logger.addHandler(fileHandler); - try (DDNSManager ddnsManager = new DDNSManager(logger, domain, RR)) { - ddnsManager.start(updateTimeMS); + try (AliYunDDnsManager dDnsManager = new AliYunDDnsManager(logger, domain, RR)) { + dDnsManager.start(updateTimeMS); } } } \ No newline at end of file diff --git a/src/test/java/Main.java b/src/test/java/Main.java new file mode 100644 index 0000000..80da711 --- /dev/null +++ b/src/test/java/Main.java @@ -0,0 +1,22 @@ +import top.lingyuzhao.lyMbl.ddns.aliyun.AliYunDDnsManager; + +import java.util.logging.Logger; + +/** + * @author zhao + */ +public class Main { + public static void main(String[] args) { + final Logger test = Logger.getLogger("test"); + try ( + // 实例化 DDNS 管理器 + final AliYunDDnsManager aliYunDDnsManager = new AliYunDDnsManager( + // 传递日志对象 以及域名信息 + test, "baidu.com", "www" + ) + ) { + // 启动 每3600s 检查一次 DNS 信息 + aliYunDDnsManager.start(3600000); + } + } +}