Say I have a public server with a service (email, web server, etc) that’s accessible through https://myservice.example.com, and I would like to restrict that service with a VPN. How do I do that?

I know how to setup a VPN. I know how to use some of the services through that VPN. But see, if I want to use that VPN, I connect my client to that VPN, then I get the subnet of that VPN, say 10.10.100.0, through which I can access the devices by address.

But I see some services offer things like https://myservice.example.com, and they only work when that VPN is connected. How does that work? Is it just some DNS setting at the domain level or there’s more to it?

  • SheeEttin@lemmy.world
    link
    fedilink
    English
    arrow-up
    17
    ·
    1 year ago

    Just… stop making it accessible from the Internet? Whatever you did to allow it through your firewall, just remove that rule.

    • TheQuantumPhysicist@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      6
      arrow-down
      2
      ·
      1 year ago

      I’ve been managing servers for over 10 years, and I never have felt stupider, and I still don’t understand how to do this. Everyone is making a comment that I don’t understand.

      Let’s talk internet 101, and please tell me where I’m wrong.

      You make a request to https://myservice.example.com. The DNS responds to a query giving you an IP address, say 1.2.3.4. Now the client software makes another request to 1.2.3.4:433 (say if we’re attempting to access an https server, binding the SNI address to the SSL/TLS header). The request will be sent to that server, and the server will respond. In what part of all this process can the VPN can do anything?

      Normally if you want to access a device through VPN, you make a request to a WHOLE other ip address in another subnet on another (virtual) device locally. It has absolutely nothing to do with 1.2.3.4. It’s something like 10.10.100.X… or similar. How will my domain, myservice.example.com, route to that address, 10.10.100.X? Is it as dumb and simple as routing there? Or is there more to it? It doesn’t sound right to make the DNS server record point to 10.10.100.X.

      • Radium@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        11
        ·
        1 year ago

        It is that simple. Make the dns entry point to your vpn subnet 10.10.100.X. The way it works is anyone not on your vpn won’t be able to resolve the ip address and will get an error. Anyone on the vpn will be able to resolve the ip address and connect via the vpn connection.

        The part people are talking about that is likely confusing you is that if your service is already available via your actual ip address 1.2.3.4 then you have a security concern since anyone can access 1.2.3.4 even without your domain name pointing there. They are encouraging you to make sure your 1.2.3.4 network doesn’t allow access but updating your firewall settings to make sure it blocks connections that are not made via your vpn subnet of 10.10.100.X

      • SheeEttin@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        The request will be sent to that server, and the server will respond. In what part of all this process can the VPN can do anything?

        If there is a firewall between the client and the server (which there should be), then no, the server will not respond.

        The VPN jumps the firewall. When you set up the VPN service, you created firewall rules to allow the VPN traffic. Then when you have the VPN connected, all traffic to the destination network is tunneled through that one connection.

      • InvertedParallax@lemm.ee
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        Same as the public one, just with an internal address.

        Have this on my domain, public domain with a subdomain server behind VPN and 1 host that points to an internal address.

        Anyone tries to reach from outside just times out or something.

        DNS is just a lookup of names to numbers, that’s all it is, the numbers can be anything, I can point my domain to Google if I want.

        • TheQuantumPhysicist@lemmy.worldOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          edit-2
          1 year ago

          Please elaborate a little more. So assuming the server where the service lies has IP address 1.2.3.4, and some VPN that I can connect to with 1.2.3.4:1194. If my DNS server points to 1.2.3.4, and say there’s an http server there that’s normally accessible with 1.2.3.4:80, how will we enforce that working only through VPN?

          • InvertedParallax@lemm.ee
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 year ago

            There’s an internal ip address for the VPN server, say 4.3.2.1, you point the http dns record to that address.

            The VPN server has 2 addresses by definition, an internal address and an external, public one that you connect the VPN to. Make sure the webserver only exposes itself on the private address, either by configuration (nginx/apache listen address) or by firewall (iptables -A input -j DROP)

          • pe1uca@lemmy.pe1uca.dev
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 year ago

            About the DNS, you can use an internal DNS and find some way for your VPN to configure it in all the machines connected to it.
            DNS usually has a fall back if the name is not found, so you can always have your custom DNS on and it’ll first check its own records then check for some level up (I’m basing this off of my experience with with pihole https://docs.pi-hole.net/ftldns/ )

            About your ports question: you just need to change the ip to the VPN one.
            For example, I have a VPS which has a public IP and I have tailscale installed.
            If I were to make my service listen to all interfaces I could use 1.2.3.4:1194 or 100.100.100.100:1194 (this being the tailscale ip)
            But I usually only configure them to listen to tailscale0, so I can no longer reach them with 1.2.3.4:1194, only with the tailscale ip.
            In your DNS you need to configure this new IP to be served.

            I’m guessing you can also do some configuration with a firewall.
            Probably ufw add allow from 10.0.0.0/8 could work if this was the IP range of your VPN, then any one can still use your public IP and only your VPN will be able to connect (But don’t quote me on this, I haven’t done it).
            (Just be sure to check the configuration of your service, docker can bypass ufw :/ )

            • TheQuantumPhysicist@lemmy.worldOP
              link
              fedilink
              English
              arrow-up
              2
              ·
              1 year ago

              Thank you, but my question was specifically about DNS. Another person pointed out that setting the DNS record to the VPN destination is the right answer. I appreciate the details you wrote and I’ll look into them.

          • El Barto@lemmy.world
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 year ago

            Whatever you do with IPs, you can do with ports too.

            “Allow this port, but not that one”.

            • TheQuantumPhysicist@lemmy.worldOP
              link
              fedilink
              English
              arrow-up
              2
              arrow-down
              1
              ·
              edit-2
              1 year ago

              Please see this comment to understand my frustrations with the answers in this thread (copy/pasted from another comment):

              I’ve been managing servers for over 10 years, and I never have felt stupider, and I still don’t understand how to do this. Everyone is making a comment that I don’t understand.

              Let’s talk internet 101, and please tell me where I’m wrong.

              You make a request to https://myservice.example.com. The DNS responds to a query giving you an IP address, say 1.2.3.4. Now the client software makes another request to 1.2.3.4:433 (say if we’re attempting to access an https server, binding the SNI address to the SSL/TLS header). The request will be sent to that server, and the server will respond. In what part of all this process can the VPN can do anything?

              Normally if you want to access a device through VPN, you make a request to a WHOLE other ip address in another subnet on another (virtual) device locally. It has absolutely nothing to do with 1.2.3.4. It’s something like 10.10.100.X… or similar. How will my domain, myservice.example.com, route to that address, 10.10.100.X? Is it as dumb and simple as routing there? Or is there more to it? It doesn’t sound right to make the DNS server record point to 10.10.100.X.

              • El Barto@lemmy.world
                link
                fedilink
                English
                arrow-up
                1
                ·
                1 year ago

                I’m sorry. I don’t know enough about the subject. I’m sure there are firewall daemons, or IPTABLES configuration that can do what you want, but that’s beyond my area of expertise. Good luck.

  • Black616Angel@feddit.de
    link
    fedilink
    English
    arrow-up
    4
    ·
    1 year ago

    Okay, the others are a little short for what you need.

    Your VPN needs it’s own DNS. With that DNS you can route the traffic internally from the VPN subnet to the server. Then you can set the server up to only take requests from that subnet either via firewall or the reverse proxy.

    • Snowplow8861@lemmus.org
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      100%.Or set host file entries on each endpoint to resolve the mail.domain.com to your internal ip that’s available only over vpn. Not going to be easy on mobiles.

      There is an assumption though that the mail server has an internal IP address wherever you are hosting. That might not be true. I would always put the public IP on the firewall and then NAT with specific port 25 in to the private IP of the server, but who knows what this particular OP has done.

  • sloppy_diffuser@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    Set myservice.example.com to an internal address like 10.10.100.x. Those addresses are not routable on the public internet. They can only be reached from a local network which the VPN tunnels you into.

    If your VPN also supplies DNS to clients, can also add a private zone if its supported so nobody can resolve that internal address except those clients.

  • Decronym@lemmy.decronym.xyzB
    link
    fedilink
    English
    arrow-up
    2
    arrow-down
    1
    ·
    edit-2
    1 year ago

    Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I’ve seen in this thread:

    Fewer Letters More Letters
    DNS Domain Name Service/System
    HTTP Hypertext Transfer Protocol, the Web
    IP Internet Protocol
    NAT Network Address Translation
    SSL Secure Sockets Layer, for transparent encryption
    TLS Transport Layer Security, supersedes SSL
    VPN Virtual Private Network
    VPS Virtual Private Server (opposed to shared hosting)
    nginx Popular HTTP server

    8 acronyms in this thread; the most compressed thread commented on today has 19 acronyms.

    [Thread #37 for this sub, first seen 13th Aug 2023, 16:25] [FAQ] [Full list] [Contact] [Source code]