Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/JCurl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static void main(String[] args) throws Exception {
final OptionSpec<String> headerSpec;
@NonNull
final OptionSpec<String> engineOptionSpec;
@NonNull
final OptionSpec<String> hostnamesSpec;

public JCurl() {
parser = new OptionParser();
Expand All @@ -67,6 +69,9 @@ public JCurl() {
)
.withRequiredArg()
.ofType(String.class);
hostnamesSpec = parser.acceptsAll(asList("hostnames", "n"), "hostnames /etc/hosts")
.withRequiredArg()
.ofType(String.class);
}

public ResponseEntity<String> execute(String... args) throws Exception {
Expand Down Expand Up @@ -108,6 +113,12 @@ public ResponseEntity<String> execute(String... args) throws Exception {
}
}
}

if (optionSet.has("hostnames")) {
String hostnames = optionSet.valueOf(hostnamesSpec);
JCurlNameService.setHostNames(hostnames);
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,mine");
}

return engineType.getEngine().submit(options);
}
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/JCurlNameService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import java.io.File;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import io.netty.resolver.HostsFileEntries;
import io.netty.resolver.HostsFileParser;
import sun.net.spi.nameservice.NameService;

public class JCurlNameService implements NameService {
private static String hostNames;
private HostsFileEntries systemEntries;
private HostsFileEntries customEntries;
private Map<Inet4Address, String> s4;
private Map<Inet6Address, String> s6;
private Map<Inet4Address, String> c4;
private Map<Inet6Address, String> c6;

public static void setHostNames(String hostNames) {
JCurlNameService.hostNames = hostNames;
}

public JCurlNameService() throws Exception {
HostsFileEntries hfe;
if (hostNames == null) {
hfe = new HostsFileEntries(new HashMap<String, Inet4Address>(), new HashMap<String, Inet6Address>());
} else {
hfe = HostsFileParser.parse(new File(hostNames));
}
this.init(hfe);
}

public void init(HostsFileEntries customEntries) {
systemEntries = HostsFileParser.parseSilently();
this.customEntries = customEntries;
s4 = invert4(systemEntries.inet4Entries());
s6 = invert6(systemEntries.inet6Entries());
c4 = invert4(customEntries.inet4Entries());
c6 = invert6(customEntries.inet6Entries());
}

// Fixme: Handle duplicate Inet4Addresses?
private Map<Inet4Address, String> invert4(Map<String, Inet4Address> m) {
Map<Inet4Address, String> n = new HashMap<>();
for (Entry<String, Inet4Address> e: m.entrySet()) {
n.put(e.getValue(), e.getKey());
}
return n;
}

// Fixme: Handle duplicate Inet6Addresses?
private Map<Inet6Address, String> invert6(Map<String, Inet6Address> m) {
Map<Inet6Address, String> n = new HashMap<>();
for (Entry<String, Inet6Address> e: m.entrySet()) {
n.put(e.getValue(), e.getKey());
}
return n;
}

private InetAddress lookup(String host, HostsFileEntries he) {
InetAddress ia = this.customEntries.inet4Entries().get(host);
if (ia == null) {
ia = this.customEntries.inet6Entries().get(host);
}
return ia;
}
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
InetAddress ia = lookup(host, customEntries);
if (ia == null) {
ia = lookup(host, systemEntries);
}
if (ia == null) {
throw new UnknownHostException(host);
}
return new InetAddress[]{ia};
}

public String getHostByAddr(byte[] addr) throws UnknownHostException {
InetAddress ia = InetAddress.getByAddress(addr);
String host = c4.get(ia);
if (host == null) {
host = c6.get(ia);
}
if (host == null) {
host = s4.get(ia);
}
if (host == null) {
host = s6.get(ia);
}
if (host == null) {
host = ia.getHostName();
}
if (host == null) {
throw new UnknownHostException(Arrays.toString(addr));
}
return host;
}
}
18 changes: 18 additions & 0 deletions src/main/java/JCurlNameServiceDescriptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
*
*/
import sun.net.spi.nameservice.*;

public final class JCurlNameServiceDescriptor implements NameServiceDescriptor {
public NameService createNameService() throws Exception {
return new JCurlNameService();
}

public String getProviderName() {
return "mine";
}

public String getType() {
return "dns";
}
}
63 changes: 63 additions & 0 deletions src/main/java/io/netty/resolver/HostsFileEntries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2017 The Netty Project
*
* The Netty Project licenses this file to you 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 io.netty.resolver;

//import io.netty.util.internal.UnstableApi;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* A container of hosts file entries
*/
//@UnstableApi
public final class HostsFileEntries {

/**
* Empty entries
*/
static final HostsFileEntries EMPTY =
new HostsFileEntries(
Collections.<String, Inet4Address>emptyMap(),
Collections.<String, Inet6Address>emptyMap());

private final Map<String, Inet4Address> inet4Entries;
private final Map<String, Inet6Address> inet6Entries;

public HostsFileEntries(Map<String, Inet4Address> inet4Entries, Map<String, Inet6Address> inet6Entries) {
this.inet4Entries = Collections.unmodifiableMap(new HashMap<String, Inet4Address>(inet4Entries));
this.inet6Entries = Collections.unmodifiableMap(new HashMap<String, Inet6Address>(inet6Entries));
}

/**
* The IPv4 entries
* @return the IPv4 entries
*/
public Map<String, Inet4Address> inet4Entries() {
return inet4Entries;
}

/**
* The IPv6 entries
* @return the IPv6 entries
*/
public Map<String, Inet6Address> inet6Entries() {
return inet6Entries;
}
}
Loading