|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Araf Karsh Hamid |
| 3 | + * <p> |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * <p> |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 17 | + * THE SOFTWARE. |
| 18 | + * <p> |
| 19 | + * This program and the accompanying materials are dual-licensed under |
| 20 | + * either the terms of the Eclipse Public License v1.0 as published by |
| 21 | + * the Eclipse Foundation |
| 22 | + * <p> |
| 23 | + * or (per the licensee's choosing) |
| 24 | + * <p> |
| 25 | + * under the terms of the Apache 2 License version 2.0 |
| 26 | + * as published by the Apache Software Foundation. |
| 27 | + */ |
| 28 | +package java18; |
| 29 | + |
| 30 | +import javax.naming.NamingException; |
| 31 | +import javax.naming.directory.Attribute; |
| 32 | +import javax.naming.directory.Attributes; |
| 33 | +import javax.naming.directory.DirContext; |
| 34 | +import javax.naming.directory.InitialDirContext; |
| 35 | +import java.net.InetAddress; |
| 36 | +import java.net.UnknownHostException; |
| 37 | +import java.net.spi.InetAddressResolver; |
| 38 | +import java.net.spi.InetAddressResolverProvider; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.List; |
| 41 | +import java.util.stream.Stream; |
| 42 | + |
| 43 | +/** |
| 44 | + * JavaExamples / CustomDNSResolverProvider |
| 45 | + * |
| 46 | + * Place the file "java.net.spi.InetAddressResolverProvider" in the |
| 47 | + * resources/META-INF/services folder. |
| 48 | + * Content of the file "java18.CustomDNSResolverProvider" |
| 49 | + * |
| 50 | + project-root/ |
| 51 | + ├── src/ |
| 52 | + │ └── main/ |
| 53 | + │ └── java/ |
| 54 | + │ └── java18/ |
| 55 | + │ └── CustomInetAddressResolverProvider.java |
| 56 | + ├── resources/ |
| 57 | + └── META-INF/ |
| 58 | + └── services/ |
| 59 | + └── java.net.spi.InetAddressResolverProvider |
| 60 | + OR |
| 61 | + src/ |
| 62 | + ├── main/ |
| 63 | + │ ├── java/ |
| 64 | + │ │ └── your/package/name/ |
| 65 | + │ │ └── CustomInetAddressResolverProvider.java |
| 66 | + │ └── resources/ |
| 67 | + │ └── META-INF/ |
| 68 | + │ └── services/ |
| 69 | + │ └── java.net.spi.InetAddressResolverProvider |
| 70 | + * |
| 71 | + * If the JVM doesn't pick up the file then right-click on the "resources" folder and click the |
| 72 | + * option "Mark Director as" Resource Root. |
| 73 | + * |
| 74 | + * @author: Araf Karsh Hamid |
| 75 | + * @version: 0.1 |
| 76 | + * @date: 2024-09-30T07:54 |
| 77 | + */ |
| 78 | +public class CustomDNSResolverProvider extends InetAddressResolverProvider { |
| 79 | + @Override |
| 80 | + public InetAddressResolver get(Configuration configuration) { |
| 81 | + return new CustomInetAddressResolver(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * {@return the name of this provider, or {@code null} if unnamed} |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public String name() { |
| 89 | + return ""; |
| 90 | + } |
| 91 | + |
| 92 | + static class CustomInetAddressResolver implements InetAddressResolver { |
| 93 | + |
| 94 | + @Override |
| 95 | + public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy) throws UnknownHostException { |
| 96 | + try { |
| 97 | + // System.out.println("Inside CustomDNSResolverProvider..... "); |
| 98 | + // Custom rule: return a fixed IP address for any domain ending in ".fusion" |
| 99 | + if (host.endsWith(".fusion")) { |
| 100 | + return Stream.of(InetAddress.getByName("10.18.36.54")); |
| 101 | + } else { |
| 102 | + // Fallback to the default resolution mechanism |
| 103 | + // Bypass recursion for default resolution mechanism |
| 104 | + return lookupUsingJNDI(host); |
| 105 | + } |
| 106 | + } catch (Exception e) { |
| 107 | + System.out.println("Host Not Found "+host+" ERROR: "+e.getMessage()); |
| 108 | + // e.printStackTrace(); |
| 109 | + return Stream.empty(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * 1. Proper Attribute Handling: |
| 115 | + * The Attributes object from the JNDI context is properly parsed. |
| 116 | + * The “A” record is for IPv4 addresses. |
| 117 | + * The “AAAA” record is for IPv6 addresses. |
| 118 | + * The code checks if these attributes exist and iterates over their values. |
| 119 | + * 2. Trim Strings: |
| 120 | + * The IP address strings retrieved are trimmed using .trim() before passing them to |
| 121 | + * InetAddress.getByName() to ensure there are no extra spaces that can cause parsing |
| 122 | + * issues. |
| 123 | + * 3. Improved Error Handling: |
| 124 | + * Each potential DNS record (IPv4 or IPv6) is individually checked and converted to |
| 125 | + * an InetAddress. Any parsing issues will be caught, but won’t stop the entire lookup |
| 126 | + * process. |
| 127 | + * |
| 128 | + * @param host |
| 129 | + * @return |
| 130 | + */ |
| 131 | + private Stream<InetAddress> lookupUsingJNDI(String host) { |
| 132 | + List<InetAddress> addressList = new ArrayList<>(); |
| 133 | + try { |
| 134 | + DirContext ictx = new InitialDirContext(); |
| 135 | + Attributes attrs = ictx.getAttributes("dns:/" + host); |
| 136 | + |
| 137 | + // Attempt to retrieve both IPv4 ("A") and IPv6 ("AAAA") records |
| 138 | + Attribute aRecord = attrs.get("A"); |
| 139 | + Attribute aaaaRecord = attrs.get("AAAA"); |
| 140 | + |
| 141 | + // Process A records (IPv4) |
| 142 | + if (aRecord != null) { |
| 143 | + for (int i = 0; i < aRecord.size(); i++) { |
| 144 | + String ip = (String) aRecord.get(i); |
| 145 | + try { |
| 146 | + InetAddress address = InetAddress.getByName(ip.trim()); |
| 147 | + addressList.add(address); |
| 148 | + } catch (UnknownHostException e) { |
| 149 | + e.printStackTrace(); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + // Process AAAA records (IPv6) |
| 154 | + if (aaaaRecord != null) { |
| 155 | + for (int i = 0; i < aaaaRecord.size(); i++) { |
| 156 | + String ip = (String) aaaaRecord.get(i); |
| 157 | + try { |
| 158 | + InetAddress address = InetAddress.getByName(ip.trim()); |
| 159 | + addressList.add(address); |
| 160 | + } catch (UnknownHostException e) { |
| 161 | + e.printStackTrace(); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + } catch (NamingException e) { |
| 167 | + e.printStackTrace(); |
| 168 | + } |
| 169 | + return addressList.stream(); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public String lookupByAddress(byte[] addr) throws UnknownHostException { |
| 174 | + try { |
| 175 | + // Fallback to default reverse lookup mechanism |
| 176 | + InetAddress address = InetAddress.getByAddress(addr); |
| 177 | + return address.getHostName(); |
| 178 | + } catch (Exception e) { |
| 179 | + e.printStackTrace(); |
| 180 | + return null; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments