GithubHelp home page GithubHelp logo

earthstar-project / dns_sd Goto Github PK

View Code? Open in Web Editor NEW
39.0 3.0 1.0 143 KB

DNS-SD (aka Zeroconf, Bonjour, Avahi) in TypeScript for Deno and Node.

License: GNU Lesser General Public License v3.0

TypeScript 100.00%
avahi bonjour discovery dns-sd multicast-dns rfc-6762 rfc-6763 zeroconf

dns_sd's Introduction

dns-sd

A module of utilities for DNS-SD (aka Zeroconf, Bonjour, Avahi), featuring:

  • Advertise and browse DNS-SD services over multicast DNS (compliant with RFC 6763)
  • A generic mDNS continuous querier (compliant with RFC 6762)
  • A generic mDNS responder (compliant with RFC 6762)
  • Utilities for encoding and decoding DNS messages to and from Uint8Array.
  • Works with any JS runtime (e.g. Deno, Node)
  • Implemented completely in TypeScript

Motivation

This module was made so that Earthstar can have automatic peer discovery on a local network. Earthstar is written in TypeScript, and can run in browsers, Deno, and Node.

I made this because there weren't any DNS-SD libraries written in TypeScript and which had service browsing and service advertising and which could run on alternative JS runtimes like Deno (theoretically this module can run in the browser, which is cool โ€” even if it doesn't make any sense).

I also wanted something that used standard Uint8Array instead of Node's Buffer for message encoding and decoding.

Usage

This module works for both Deno and Node. The multicast drivers for each can be found at the @earthstar/dns-sd/deno and @earthstar/dns-sd/node entrypoints, respectively.

NB: Deno's multicasting APIs have not yet stabilised, so the --unstable-net flag is required to use this module.

API

browse

Searches for given DNS-SD services on the local network. Returns an async iterator of discovered services.

console.log("Browsing for local HTTP services...");

for await (
  const service of browse({
    multicastInterface: new MulticastInterface(),
    service: {
      protocol: "tcp",
      type: "http",
    },
  })
) {
  if (service.isActive) {
    console.log(`๐Ÿ“ก ${service.name} - ${service.host}:${service.port}`);
  }
}

advertise

Advertise a service over multicast DNS.

Returns a promise which will reject if fifteen failed attempts to claim a name are made within a ten second interval.

If the service has to be renamed due to a conflict, a warning with the new name will be sent to the console.

await advertise({
  service: {
    host: "10.0.0.7",
    name: "My Web Server",
    port: 8080,
    protocol: "tcp",
    type: "http",
    txt: {
      psswd: "abc123",
    },
  },
  multicastInterface: new MulticastInterface(),
});

Advanced API

These are lower-level APIs for directly working with DNS records. browse and advertise are implemented using these.

respond

Runs a multicast DNS responder for the given resource records.

Returns a promise that will reject when:

  • Probing for proposed records fails
  • Another responder starts responding with records our responder previously lay claim to.
await respond({
  proposedRecords: [myAAAARecord],
  multicastInterface: new MulticastInterface(),
});

Query

A continuous multicast DNS query.

Reports additions, flushes, and expirations of resource records answering the given query via an asynchronous iterator:

const query = new Query(
  [{ name: "_http._tcp.local", recordType: 255 }],
  multicastInterface: new MulticastInterface()
);

for await (const event of query) {
  console.log(event);
}

Also has an answers method which returns all records given as answers to this query, as well as an additional method which returns all records found in the additional section of DNS messages containing answers to our queries.

decodeMessage

Decode a DNS message from Uint8Array.

Resource Records of the following types will have their RDATA decoded: A, PTR, TXT, AAAA, SRV, NSEC. Other types of resource records will have their RDATA left as Uint8Array.

encodeMessage

Encode a DNS message as Uint8Array.

Compresses domain names, so re-encoded messages may come out smaller.

Will never use the TC flag in the header.

MulticastInterface

This represents a network interface which has joined a multicast group. When combined with a MulticastDriver it can be made to work with different runtimes such as Deno and Node. If no driver is provided when this class is instantiated, it automatically selects a driver appropriate to the current runtime.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.