@Override public List<ServiceCallServer> getUpdatedListOfServers(String name) { final Lookup lookup = lookupFactory.apply(name); final Record[] records = lookup.run(); List<ServiceCallServer> servers; if (Objects.nonNull(records) && lookup.getResult() == Lookup.SUCCESSFUL) { servers = Arrays.stream(records) .filter(SRVRecord.class::isInstance) .map(SRVRecord.class::cast) .sorted(DnsServiceCallServer.COMPARATOR) .map(DnsServiceCallServer::new) .collect(Collectors.toList()); } else { servers = Collections.emptyList(); } return servers; }
private String resolveSrvByName(Resolver resolver, String name) { try { Lookup lookup = new Lookup(name, SRV); if (resolver != null) { lookup.setResolver(resolver); } Record[] records = lookup.run(); if (records == null) { return null; } return of(records) .filter(it -> it instanceof SRVRecord) .map(srv -> resolveHostByName(resolver, ((SRVRecord) srv).getTarget()) + ":" + ((SRVRecord) srv).getPort()) .distinct() .collect(joining(",")); } catch (TextParseException e) { log.warn("unable to resolve using SRV record " + name, e); return null; } }
public List<DNSServiceRecord> getMultiSRV(String key) throws ConfigException { String qkey = fullyQualify(key); List<DNSServiceRecord> result = new ArrayList<DNSServiceRecord>(); List<Record> list = m_records.get(makeHostKey(qkey)); if (list == null) { throw new NotFoundException("No such record: " + makeHostKey(qkey)); } for (Record r : list) { if (r.getType() != Type.SRV) { continue; } result.add(new DNSServiceRecord(((SRVRecord) r).rdataToString())); } return result; }
/** * Static builder. It wraps out a {@link SRVRecord} by extracting relevant data. * * @param srvRecord A {@link SRVRecord} instance to be worked out * @return An instance of <code>ServiceRecord</code> */ public static ServiceRecord build(SRVRecord srvRecord) { String proto = "N/A"; String owner = srvRecord.getName().toString(); if(owner.contains(Constants.TCP)) proto = Constants.TCP.replace("_", "").toUpperCase(); else if(owner.contains(Constants.UDP)) proto = Constants.UDP.replace("_", "").toUpperCase(); return new ServiceRecord(srvRecord.getName().toString(), srvRecord.getTarget().toString(), proto, srvRecord.getPort(), srvRecord.getPriority(), srvRecord.getWeight(), srvRecord.getTTL()); }
private static List<LookupResult> toLookupResults(Record[] queryResult) { ImmutableList.Builder<LookupResult> builder = ImmutableList.builder(); if (queryResult != null) { for (Record record: queryResult) { if (record instanceof SRVRecord) { SRVRecord srvRecord = (SRVRecord) record; builder.add(LookupResult.create(srvRecord.getTarget().toString(), srvRecord.getPort(), srvRecord.getPriority(), srvRecord.getWeight(), srvRecord.getTTL())); } } } return builder.build(); }
public DNSServiceRecord getSRV(String key) throws ConfigException { String qkey = fullyQualify(key); List<Record> list = m_records.get(makeHostKey(qkey)); if (list == null || list.get(0).getType() != Type.SRV) { throw new NotFoundException("No such record: " + makeHostKey(qkey)); } SRVRecord srec = (SRVRecord) list.get(0); return new DNSServiceRecord(srec.rdataToString()); }
/** * Scrapes the Discovery Service Records according to their nature. * * @param records An array of <code>Record</code> retrieve upon a lookup * @param set A <code>ResourcesContainer</code> * @param pht A Resource Record Type holder */ private void parseRecords(Record[] records, final RecordsContainer set, RrHolderType pht) { if (records != null) { for (Record record : records) { if ((record instanceof PTRRecord && pht == RrHolderType.ZONES) || (record instanceof PTRRecord && pht == RrHolderType.NAMES)) { String zone = PointerRecord.build((PTRRecord) record).getRData(); if (zone != null) { set.getLabels().add(zone); } } else if (record instanceof PTRRecord && pht == RrHolderType.TYPES) { set.getLabels().add(PointerRecord.build((PTRRecord) record).getServiceType()); } else if (record instanceof SRVRecord) { ServiceRecord svcRecord = ServiceRecord.build((SRVRecord) record); if (svcRecord != null) { set.getRecords().add(svcRecord); } } else if (record instanceof TXTRecord) { set.getTexts().add(TextRecord.build((TXTRecord) record)); } else { errorsTrace.get().put( ExceptionsUtil.traceKey(record.toString(), "", "Parsing-Service-Records"), StatusCode.RESOURCE_UNEXPECTED); } } } }
private InetAddress[] getAllAddresses(SRVRecord srv) throws UnknownHostException { try { return org.xbill.DNS.Address.getAllByName(srv.getTarget().canonicalize().toString(true)); } catch (UnknownHostException e) { logger.severe("Parsing DNS records failed", e); throw e; } }
private Message messageWithNodes(String query, Iterable<String> names) throws TextParseException { Name queryName = Name.fromString(query); Record question = Record.newRecord(queryName, Type.SRV, DClass.IN); Message queryMessage = Message.newQuery(question); Message result = new Message(); result.setHeader(queryMessage.getHeader()); result.addRecord(question, Section.QUESTION); for (String name1 : names){ result.addRecord(new SRVRecord(queryName, DClass.IN, 1, 1, 1, 8080, Name.fromString(name1)), Section.ANSWER); } return result; }
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException { String result; try { Record[] records = new Lookup(query, Type.SRV).run(); if (records != null && records.length > 0) { SRVRecord srv = (SRVRecord) records[0]; result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort(); } else { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved"); } } catch (TextParseException e) { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e); } return result; }
public DnsServiceCallServer(SRVRecord record) { super(record.getTarget().toString(true), record.getPort()); }
public static Comparator<SRVRecord> comparator() { Comparator<SRVRecord> byPriority = (e1, e2) -> Integer.compare(e2.getPriority(), e1.getPriority()); Comparator<SRVRecord> byWeight = (e1, e2) -> Integer.compare(e2.getWeight(), e1.getWeight()); return byPriority.thenComparing(byWeight); }
public static SRVRecord createSrvRecord ( String host, int port, int priority, int weight, long ttl ) throws TextParseException { return new SRVRecord( Name.fromString( "example.com." ), DClass.IN, ttl, priority, weight, port, Name.fromString( host ) ); }
@Before public void setup() throws Exception { PowerMockito.whenNew(DefaultKubernetesClient.class).withAnyArguments().thenReturn(client); PowerMockito.whenNew(SRVRecord.class).withAnyArguments().thenReturn(srvRecord); when(srvRecord.getTarget()).thenReturn(Name.fromString("127.0.0.1")); }
private synchronized void startAddressFetch() { if( addressFetchThread != null ) return; addressFetchThread = new Thread() { @Override public void run() { Record[] stunRecords = null; Record[] turnRecords = null; for( int i=0; i<hosts.length; ++i ) { String stunQuery = "_stun._udp." + hosts[i] ; String turnQuery = "_turn._udp." + hosts[i] ; try { if( stunRecords == null ) stunRecords = lookupSrv( stunQuery ); if( turnRecords == null ) turnRecords = lookupSrv( turnQuery ); if( stunRecords != null && turnRecords != null ) break; } catch( TextParseException tpe ) { throw new RuntimeException( tpe ); } } if( stunRecords == null ) { stunAddresses = null ; } else { stunAddresses = new TransportAddress[stunRecords.length]; for( int i=0; i<stunRecords.length; ++i ) { SRVRecord srv = (SRVRecord) stunRecords[i] ; stunAddresses[i] = new TransportAddress(srv.getTarget().toString().replaceFirst("\\.$", ""), srv.getPort(), Transport.UDP); } } if( turnRecords == null ) { turnAddresses = null ; } else { turnAddresses = new TransportAddress[stunRecords.length]; for( int i=0; i<turnAddresses.length; ++i ) { SRVRecord srv = (SRVRecord) turnRecords[i] ; turnAddresses[i] = new TransportAddress(srv.getTarget().toString().replaceFirst("\\.$", ""), srv.getPort(), Transport.UDP); } } } private Record[] lookupSrv(String query) throws TextParseException { while( true ) { try { // Bug 6427854 causes this to sometimes throw an NPE return new Lookup( query, Type.SRV ).run(); } catch( NullPointerException npe ) { Thread.yield(); } } } }; addressFetchThread.start(); }