-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblaze.java
More file actions
102 lines (81 loc) · 2.93 KB
/
blaze.java
File metadata and controls
102 lines (81 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import com.fizzed.blaze.Config;
import com.fizzed.blaze.Contexts;
import com.fizzed.cloudns.CloudnsClient;
import com.fizzed.cloudns.Record;
import org.slf4j.Logger;
import java.net.InetAddress;
import static com.fizzed.blaze.Https.httpGet;
public class blaze {
private final Logger log = Contexts.logger();
private final Config config = Contexts.config();
private String getHost() throws Exception {
return InetAddress.getLocalHost().getHostName();
}
private String getIPV4() throws Exception {
final String ipv4 = httpGet("https://api4.ipify.org")
.runCaptureOutput()
.toString();
return ipv4;
}
private String getIPV6() throws Exception {
final String ipv6 = httpGet("https://api6.ipify.org")
.runCaptureOutput()
.toString();
return ipv6;
}
public void my_host() throws Exception {
final String host = this.getHost();
log.info("host: {}", host);
}
public void my_ipv4() throws Exception {
final String ipv4 = this.getIPV4();
log.info("ipv4: {}", ipv4);
}
public void my_ipv6() throws Exception {
final String ipv6 = this.getIPV6();
log.info("ipv6: {}", ipv6);
}
public void register() throws Exception {
final Integer apiAuthId = this.config.value("cloudns.id", Integer.class).get();
final String apiAuthPassword = this.config.value("cloudns.pw").get();
final CloudnsClient client = new CloudnsClient(apiAuthId, apiAuthPassword);
final boolean ipv4Enabled = this.config.value("ipv4", Boolean.class).orElse(false);
final boolean ipv6Enabled = this.config.value("ipv6", Boolean.class).orElse(false);
String host = this.config.value("host").orNull();
if (host == null) {
host = this.getHost();
}
final String domain = this.config.value("domain").get();
String ipv4 = null;
if (ipv4Enabled) {
ipv4 = this.getIPV4();
}
String ipv6 = null;
if (ipv6Enabled) {
ipv6 = this.getIPV6();
}
log.info("Will register:");
log.info("host: {}", host);
log.info("domain: {}", domain);
log.info("ipv4: {}", ipv4);
log.info("ipv6: {}", ipv6);
// A record
if (ipv4Enabled) {
log.info("Registering A record for {}.{} -> {}", host, domain, ipv4);
client.createOrUpdateRecord(domain, new Record()
.setType("A")
.setHost(host)
.setTtl(300)
.setRecord(ipv4));
}
// AAAA record
if (ipv6Enabled) {
log.info("Registering AAAA record for {}.{} -> {}", host, domain, ipv6);
client.createOrUpdateRecord(domain, new Record()
.setType("AAAA")
.setHost(host)
.setTtl(300)
.setRecord(ipv6));
}
}
}