Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
BeardedManZhao committed Nov 13, 2024
1 parent 09d2f9b commit 63fb583
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 51 deletions.
7 changes: 7 additions & 0 deletions .idea/encodings.xml

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

80 changes: 71 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

aliyun DNS 解析的 DDNS Java 软件,可以实现自动获取 ipv4 和 ipv6 公网 然后在阿里云进行 ddns 操作!

## 启动方法
## 操作之前的准备工作

### 安装 aliyun 客户端

Expand Down Expand Up @@ -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)]
```

## 启动示例
#### 启动示例

在这里我们展示了部分日志,且对其中的数据进行了脱敏,您启动之后看到类型下面的日志,则代表成功运行了!

Expand All @@ -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
<dependency>
<groupId>io.github.BeardedManZhao</groupId>
<artifactId>LyMbl_AliyunDDNS_Java</artifactId>
<version>2024.11.13</version>
</dependency>
```

### 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);
}
}
}
```
Binary file not shown.
157 changes: 123 additions & 34 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,18 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>com.aliyun</groupId>
<groupId>io.github.BeardedManZhao</groupId>
<artifactId>LyMbl_AliyunDDNS_Java</artifactId>
<version>1.0.0</version>
<version>2024.11.13</version>
<packaging>jar</packaging>
<name>LyMbl_DDNS_Java</name>
<description>Alibaba Cloud SDK Code Sample for Java
</description>
<developers>
<developer>
<id>aliyundeveloper</id>
<name>Aliyun SDK</name>
<email>[email protected]</email>
</developer>
</developers>
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<name>LyMbl_AliyunDDNS_Java</name>
<description>LyMbl_AliyunDDNS_Java-java</description>
<url>https://github.com/BeardedManZhao/LyMbl_AliyunDDNS_Java.git</url>

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

<dependencies>
<dependency>
Expand All @@ -39,16 +27,117 @@
<version>2.0.30</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<!-- 添加开源协议-->
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<!-- 添加作者信息-->
<developers>
<developer>
<name>LingYuZhao</name>
<email>[email protected]</email>
<organization>https://github.com/BeardedManZhao</organization>
</developer>
</developers>

<!-- 添加项目信息-->
<scm>
<connection>https://github.com/BeardedManZhao/LyMbl_AliyunDDNS_Java.git</connection>
<developerConnection>scm:git:ssh://[email protected]:BeardedManZhao/LyMbl_AliyunDDNS_Java.git</developerConnection>
<url>https://github.com/BeardedManZhao/LyMbl_AliyunDDNS_Java</url>
</scm>

<profiles>
<profile>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<!--发布到中央SNAPSHOT仓库插件-->
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

<!--生成源码插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

<!--生成API文档插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!--gpg插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,43 @@
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;
private final String RR;
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;
this.executorService = Executors.newFixedThreadPool(2);
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();
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
Loading

0 comments on commit 63fb583

Please sign in to comment.