NetOperator Wibby
NetOperator Wibby

Reputation: 1404

Making .local resolve to IP address AND port (mdns)

I'm using the multicast-dns node module to attempt making this work.

Looking up custom.local in the browser gives me the console message I setup, but I'm unable to see my actual server running (which is doing so at localhost:12345, where 12345 is a dynamic number). I want to be able to see my local server when visiting custom.local. Is this possible?

Here's some code:

mdns.on("query", query => {
  if (query.questions[0] && query.questions[0].name === "custom.local") {
    console.log(query);

    mdns.respond({
      answers: [
        {
          name: "custom.local",
          type: "SRV",
          data: {
            port: n.get("p"), // dynamic port
            weight: 0,
            priority: 10,
            target: ip // local IP
          }
        }, {
          name: "custom.local",
          type: "A",
          data: ip,
          ttl: 300
        }
      ]
    });
  }
});

EDIT: I can connect to my local server just fine, that wasn't an issue.

Upvotes: 0

Views: 3546

Answers (1)

NetOperator Wibby
NetOperator Wibby

Reputation: 1404

Quoting cfreak:

You can't put port numbers in DNS. DNS is only for looking up an IP by name. For your browser to see it by the name alone you need a proxy program in front of your service or you need to run the service itself on port 80. Port numbers really shouldn't be dynamic. You should specify it in the setup of your service.

That answers my question and offers next steps. Thanks!

UPDATE: Figured out what I was trying to do. Here's some code!


FOUND A SOLUTION, WOOP WOOP!

I'm using this module, but tweaked the source a bit (only because I have dynamic ports, because I feel like it).

/* jshint undef: true, unused: true, esversion: 6, node: true */



"use strict";



//
//  G E T
//  P A C K A G E S

import express from "express";
import http from "http";
import local from "./server/local";

const n = express();



n.get("/", (req, res) => {
  res.send("Welcome home");
});



//
//  L A U N C H

const server = http.createServer(n);

server.listen(0, () => {
  const port = server.address().port;
  local.add(port, "custom.local");
});

Hope this helps you as well, future Internet searcher! :D Don't let negative folks on other SE sites bring you down. :virtual fist bump:

Upvotes: 1

Related Questions