Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tunneler intercept mode on linux only #2709

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tunnel/dns/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !linux

/*
Copyright NetFoundry Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package dns

// these functions are only implemented when OS=linux

func NewDnsServer(addr string) (Resolver, error) {
return nil, nil
}

func flushDnsCaches() {
// not implemented
}
97 changes: 97 additions & 0 deletions tunnel/dns/dns_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//go:build linux

/*
Copyright NetFoundry Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package dns

import (
"fmt"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"net"
"os/exec"
"sync"
"time"
)

func NewDnsServer(addr string) (Resolver, error) {
log.Infof("starting dns server...")
s := &dns.Server{
Addr: addr,
Net: "udp",
}

names := make(map[string]net.IP)
r := &resolver{
server: s,
names: names,
ips: make(map[string]string),
namesMtx: sync.Mutex{},
domains: make(map[string]*domainEntry),
domainsMtx: sync.Mutex{},
}
s.Handler = r

errChan := make(chan error)
go func() {
errChan <- s.ListenAndServe()
}()

select {
case err := <-errChan:
if err != nil {
return nil, fmt.Errorf("dns server failed to start: %w", err)
} else {
return nil, fmt.Errorf("dns server stopped prematurely")
}
case <-time.After(2 * time.Second):
log.Infof("dns server running at %s", s.Addr)
}

const resolverConfigHelp = "ziti-tunnel runs an internal DNS server which must be first in the host's\n" +
"resolver configuration. On systems that use NetManager/dhclient, this can\n" +
"be achieved by adding the following to /etc/dhcp/dhclient.conf:\n" +
"\n" +
" prepend domain-name-servers %s;\n\n"

err := r.testSystemResolver()
if err != nil {
log.Errorf("system resolver test failed: %s\n\n"+resolverConfigHelp, err, addr)
}

return r, nil
}

func flushDnsCaches() {
bin, err := exec.LookPath("systemd-resolve")
arg := "--flush-caches"
if err != nil {
bin, err = exec.LookPath("resolvectl")
if err != nil {
logrus.WithError(err).Warn("unable to find systemd-resolve or resolvectl in path, consider adding a dns flush to your restart process")
return
}
arg = "flush-caches"
}

cmd := exec.Command(bin, arg)
if err = cmd.Run(); err != nil {
logrus.WithError(err).Warn("unable to flush dns caches, consider adding a dns flush to your restart process")
} else {
logrus.Info("dns caches flushed")
}
}
70 changes: 0 additions & 70 deletions tunnel/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import (
"github.com/sirupsen/logrus"
"net"
"net/url"
"os/exec"
"strings"
"sync"
"time"
)

var log = logrus.StandardLogger()
Expand All @@ -40,26 +38,6 @@ type resolver struct {
domainsMtx sync.Mutex
}

func flushDnsCaches() {
bin, err := exec.LookPath("systemd-resolve")
arg := "--flush-caches"
if err != nil {
bin, err = exec.LookPath("resolvectl")
if err != nil {
logrus.WithError(err).Warn("unable to find systemd-resolve or resolvectl in path, consider adding a dns flush to your restart process")
return
}
arg = "flush-caches"
}

cmd := exec.Command(bin, arg)
if err = cmd.Run(); err != nil {
logrus.WithError(err).Warn("unable to flush dns caches, consider adding a dns flush to your restart process")
} else {
logrus.Info("dns caches flushed")
}
}

func NewResolver(config string) (Resolver, error) {
flushDnsCaches()
if config == "" {
Expand All @@ -85,54 +63,6 @@ func NewResolver(config string) (Resolver, error) {
return nil, fmt.Errorf("invalid resolver configuration '%s'. must be 'file://' or 'udp://' URL", config)
}

func NewDnsServer(addr string) (Resolver, error) {
log.Infof("starting dns server...")
s := &dns.Server{
Addr: addr,
Net: "udp",
}

names := make(map[string]net.IP)
r := &resolver{
server: s,
names: names,
ips: make(map[string]string),
namesMtx: sync.Mutex{},
domains: make(map[string]*domainEntry),
domainsMtx: sync.Mutex{},
}
s.Handler = r

errChan := make(chan error)
go func() {
errChan <- s.ListenAndServe()
}()

select {
case err := <-errChan:
if err != nil {
return nil, fmt.Errorf("dns server failed to start: %w", err)
} else {
return nil, fmt.Errorf("dns server stopped prematurely")
}
case <-time.After(2 * time.Second):
log.Infof("dns server running at %s", s.Addr)
}

const resolverConfigHelp = "ziti-tunnel runs an internal DNS server which must be first in the host's\n" +
"resolver configuration. On systems that use NetManager/dhclient, this can\n" +
"be achieved by adding the following to /etc/dhcp/dhclient.conf:\n" +
"\n" +
" prepend domain-name-servers %s;\n\n"

err := r.testSystemResolver()
if err != nil {
log.Errorf("system resolver test failed: %s\n\n"+resolverConfigHelp, err, addr)
}

return r, nil
}

func (r *resolver) testSystemResolver() error {
const resolverTestHostname = "ziti-tunnel.resolver.test"
resolverTestIP := net.IP{19, 65, 28, 94}
Expand Down
Loading