mirror of
https://github.com/google/nomulus.git
synced 2025-04-30 12:07:51 +02:00
This change renames directories in preparation for the great package rename. The repository is now in a broken state because the code itself hasn't been updated. However this should ensure that git correctly preserves history for each file.
158 lines
5.7 KiB
Java
158 lines
5.7 KiB
Java
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.domain.registry.tools;
|
|
|
|
import static com.google.common.io.BaseEncoding.base16;
|
|
import static com.google.domain.registry.model.ofy.ObjectifyService.ofy;
|
|
import static com.google.domain.registry.model.registry.Registries.assertTldExists;
|
|
import static com.google.domain.registry.util.DateTimeUtils.isBeforeOrAt;
|
|
import static java.nio.charset.StandardCharsets.US_ASCII;
|
|
|
|
import com.google.common.base.Function;
|
|
import com.google.common.collect.FluentIterable;
|
|
import com.google.common.collect.ImmutableList;
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.common.collect.Ordering;
|
|
import com.google.domain.registry.model.domain.DomainResource;
|
|
import com.google.domain.registry.model.domain.secdns.DelegationSignerData;
|
|
import com.google.domain.registry.model.host.HostResource;
|
|
import com.google.domain.registry.tools.Command.GtechCommand;
|
|
import com.google.domain.registry.tools.Command.RemoteApiCommand;
|
|
import com.google.domain.registry.tools.params.PathParameter;
|
|
import com.google.domain.registry.util.Clock;
|
|
|
|
import com.beust.jcommander.Parameter;
|
|
import com.beust.jcommander.Parameters;
|
|
|
|
import org.joda.time.DateTime;
|
|
import org.json.simple.JSONValue;
|
|
|
|
import java.net.InetAddress;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.Map;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
/** Command to generate a report of all DNS data. */
|
|
@Parameters(separators = " =", commandDescription = "Generate report of all DNS data in a TLD.")
|
|
final class GenerateDnsReportCommand implements RemoteApiCommand, GtechCommand {
|
|
|
|
@Parameter(
|
|
names = {"-t", "--tld"},
|
|
description = "Target TLD.",
|
|
required = true)
|
|
private String tld;
|
|
|
|
@Parameter(
|
|
names = {"-o", "--output"},
|
|
description = "Output file.",
|
|
validateWith = PathParameter.OutputFile.class)
|
|
private Path output = Paths.get("/dev/stdout");
|
|
|
|
@Inject
|
|
Clock clock;
|
|
|
|
@Override
|
|
public void run() throws Exception {
|
|
assertTldExists(tld);
|
|
Files.write(output, new Generator().generate().getBytes(US_ASCII));
|
|
}
|
|
|
|
private class Generator {
|
|
private final DateTime now = clock.nowUtc();
|
|
private final StringBuilder result = new StringBuilder();
|
|
private boolean first = true;
|
|
|
|
String generate() {
|
|
result.append("[\n");
|
|
|
|
Iterable<DomainResource> domains = ofy().load().type(DomainResource.class).filter("tld", tld);
|
|
for (DomainResource domain : domains) {
|
|
// Skip deleted domains and domains that don't get published to DNS.
|
|
if (isBeforeOrAt(domain.getDeletionTime(), now) || !domain.shouldPublishToDns()) {
|
|
continue;
|
|
}
|
|
write(domain);
|
|
}
|
|
|
|
Iterable<HostResource> nameservers = ofy().load().type(HostResource.class);
|
|
for (HostResource nameserver : nameservers) {
|
|
// Skip deleted hosts and external hosts.
|
|
if (isBeforeOrAt(nameserver.getDeletionTime(), now)
|
|
|| nameserver.getInetAddresses().isEmpty()) {
|
|
continue;
|
|
}
|
|
write(nameserver);
|
|
}
|
|
|
|
return result.append("\n]\n").toString();
|
|
}
|
|
|
|
private void write(DomainResource domain) {
|
|
ImmutableList<String> nameservers = FluentIterable.from(domain.loadNameservers())
|
|
.transform(new Function<HostResource, String>() {
|
|
@Override
|
|
public String apply(HostResource host) {
|
|
return host.getForeignKey();
|
|
}})
|
|
.toSortedList(Ordering.natural());
|
|
ImmutableList<Map<String, ?>> dsData = FluentIterable.from(domain.getDsData())
|
|
.transform(new Function<DelegationSignerData, Map<String, ?>>() {
|
|
@Override
|
|
public Map<String, ?> apply(DelegationSignerData dsData) {
|
|
return ImmutableMap.of(
|
|
"keyTag", dsData.getKeyTag(),
|
|
"algorithm", dsData.getAlgorithm(),
|
|
"digestType", dsData.getDigestType(),
|
|
"digest", base16().encode(dsData.getDigest()));
|
|
}})
|
|
.toList();
|
|
ImmutableMap.Builder<String, Object> mapBuilder = new ImmutableMap.Builder<>();
|
|
mapBuilder.put("domain", domain.getFullyQualifiedDomainName());
|
|
if (!nameservers.isEmpty()) {
|
|
mapBuilder.put("nameservers", nameservers);
|
|
}
|
|
if (!dsData.isEmpty()) {
|
|
mapBuilder.put("dsData", dsData);
|
|
}
|
|
writeJson(mapBuilder.build());
|
|
}
|
|
|
|
private void write(HostResource nameserver) {
|
|
ImmutableList<String> ipAddresses = FluentIterable.from(nameserver.getInetAddresses())
|
|
.transform(new Function<InetAddress, String>() {
|
|
@Override
|
|
public String apply(InetAddress inetAddress) {
|
|
return inetAddress.getHostAddress();
|
|
}})
|
|
.toSortedList(Ordering.natural());
|
|
ImmutableMap<String, ?> map = ImmutableMap.of(
|
|
"host", nameserver.getFullyQualifiedHostName(),
|
|
"ips", ipAddresses);
|
|
writeJson(map);
|
|
}
|
|
|
|
private void writeJson(Map<String, ?> map) {
|
|
if (first) {
|
|
first = false;
|
|
} else {
|
|
result.append(",\n");
|
|
}
|
|
result.append(JSONValue.toJSONString(map));
|
|
}
|
|
}
|
|
}
|