|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.sql.core; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import javax.naming.NameNotFoundException; |
| 24 | +import javax.naming.NamingException; |
| 25 | +import javax.naming.directory.Attribute; |
| 26 | +import javax.naming.directory.InitialDirContext; |
| 27 | + |
| 28 | +/** |
| 29 | + * Implements DnsResolver using the Java JNDI built-in DNS directory. |
| 30 | + */ |
| 31 | +class JndiDnsResolver implements DnsResolver { |
| 32 | + private final String jndiPrefix; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a resolver using the system DNS settings. |
| 36 | + */ |
| 37 | + JndiDnsResolver() { |
| 38 | + this.jndiPrefix = "dns:"; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a DNS resolver that uses a specific DNS server. |
| 43 | + * @param dnsServer the DNS server hostname |
| 44 | + * @param port the DNS server port (DNS servers usually use port 53) |
| 45 | + */ |
| 46 | + JndiDnsResolver(String dnsServer, int port) { |
| 47 | + this.jndiPrefix = "dns://"+dnsServer+":"+port+"/"; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns DNS records for a domain name, sorted by priority, then target alphabetically. |
| 52 | + * @param domainName the domain name to lookup |
| 53 | + * @return the list of record |
| 54 | + * @throws javax.naming.NameNotFoundException when the domain name did not resolve. |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public Collection<DnsSrvRecord> resolveSrv(String domainName) throws javax.naming.NameNotFoundException{ |
| 58 | + try { |
| 59 | + // Notice: This is old Java 1.2 style code. It uses the ancient JNDI DNS Provider api. |
| 60 | + // See https://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html |
| 61 | + Attribute attr = |
| 62 | + new InitialDirContext() |
| 63 | + .getAttributes(jndiPrefix+domainName, new String[] {"SRV"}) |
| 64 | + .get("SRV"); |
| 65 | + // attr.getAll() returns a Vector containing strings, one for each record returned by dns. |
| 66 | + return Collections.list(attr.getAll()).stream() |
| 67 | + .map((Object v) -> new DnsSrvRecord((String) v)) |
| 68 | + .sorted(Comparator.comparing(DnsSrvRecord::getPriority)) |
| 69 | + .collect(Collectors.toList()); |
| 70 | + } catch (NameNotFoundException e) { |
| 71 | + throw e; |
| 72 | + } |
| 73 | + catch (NamingException e) { |
| 74 | + throw new RuntimeException("Unable to look up domain name "+domainName, e); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments