mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
macOS dns support format like service.namespace:port
This commit is contained in:
@@ -9,12 +9,14 @@ import (
|
||||
"github.com/fsnotify/fsnotify"
|
||||
miekgdns "github.com/miekg/dns"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -22,7 +24,12 @@ import (
|
||||
var cancel context.CancelFunc
|
||||
var resolv = "/etc/resolv.conf"
|
||||
|
||||
// sw_vers to using different strategy on different
|
||||
// SetupDNS support like
|
||||
// service:port
|
||||
// service.namespace:port
|
||||
// service.namespace.svc:port
|
||||
// service.namespace.svc.cluster:port
|
||||
// service.namespace.svc.cluster.local:port
|
||||
func SetupDNS(ip string, namespace string) error {
|
||||
usingResolver(ip, namespace)
|
||||
_ = exec.Command("killall", "mDNSResponderHelper").Run()
|
||||
@@ -37,17 +44,34 @@ func usingResolver(ip string, namespace string) {
|
||||
if err = os.MkdirAll(filepath.Join("/", "etc", "resolver"), fs.ModePerm); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
builder := strings.Builder{}
|
||||
builder.WriteString(fmt.Sprintf("nameserver %s\n", ip))
|
||||
builder.WriteString(fmt.Sprintf("search %s\n", strings.Join([]string{namespace + ".svc.cluster.local", "svc.cluster.local", "cluster.local"}, " ")))
|
||||
builder.WriteString(fmt.Sprintf("options ndots:5\n"))
|
||||
builder.WriteString(fmt.Sprintf("options timeout:1\n"))
|
||||
|
||||
config := miekgdns.ClientConfig{
|
||||
Servers: []string{ip},
|
||||
Search: []string{namespace + ".svc.cluster.local", "svc.cluster.local", "cluster.local"},
|
||||
Ndots: 5,
|
||||
Timeout: 1,
|
||||
}
|
||||
// for support like: service:port, service.namespace.svc.cluster.local:port
|
||||
filename := filepath.Join("/", "etc", "resolver", "local")
|
||||
_ = ioutil.WriteFile(filename, []byte(builder.String()), 0644)
|
||||
_ = ioutil.WriteFile(filename, []byte(toString(config)), 0644)
|
||||
|
||||
filename = filepath.Join("/", "etc", "resolver", namespace)
|
||||
_ = ioutil.WriteFile(filename, []byte(builder.String()), 0644)
|
||||
// for support like: service.namespace:port, service.namespace.svc:port, service.namespace.svc.cluster:port
|
||||
port := util.GetAvailableUDPPortOrDie()
|
||||
go func(port int, ip, namespace string) {
|
||||
if err = NewDNSServer("udp", "127.0.0.1:"+strconv.Itoa(port), ip+":53", namespace); err != nil {
|
||||
log.Warnln(err)
|
||||
}
|
||||
}(port, ip, namespace)
|
||||
config = miekgdns.ClientConfig{
|
||||
Servers: []string{"127.0.0.1"},
|
||||
Search: []string{namespace + ".svc.cluster.local", "svc.cluster.local", "cluster.local"},
|
||||
Port: strconv.Itoa(port),
|
||||
Ndots: 5,
|
||||
Timeout: 1,
|
||||
}
|
||||
for _, s := range []string{namespace, "svc", "cluster"} {
|
||||
filename = filepath.Join("/", "etc", "resolver", s)
|
||||
_ = ioutil.WriteFile(filename, []byte(toString(config)), 0644)
|
||||
}
|
||||
}
|
||||
|
||||
func usingNetworkSetup(ip string, namespace string) {
|
||||
@@ -92,28 +116,31 @@ func usingNetworkSetup(ip string, namespace string) {
|
||||
|
||||
func toString(config miekgdns.ClientConfig) string {
|
||||
var builder strings.Builder
|
||||
builder.WriteString(`#
|
||||
# macOS Notice
|
||||
#
|
||||
# This file is not consulted for DNS hostname resolution, address
|
||||
# resolution, or the DNS query routing mechanism used by most
|
||||
# processes on this system.
|
||||
#
|
||||
# To view the DNS configuration used by this system, use:
|
||||
# scutil --dns
|
||||
#
|
||||
# SEE ALSO
|
||||
# dns-sd(1), scutil(8)
|
||||
#
|
||||
# This file is automatically generated.
|
||||
#`)
|
||||
builder.WriteString("\n")
|
||||
// builder.WriteString(`#
|
||||
//# macOS Notice
|
||||
//#
|
||||
//# This file is not consulted for DNS hostname resolution, address
|
||||
//# resolution, or the DNS query routing mechanism used by most
|
||||
//# processes on this system.
|
||||
//#
|
||||
//# To view the DNS configuration used by this system, use:
|
||||
//# scutil --dns
|
||||
//#
|
||||
//# SEE ALSO
|
||||
//# dns-sd(1), scutil(8)
|
||||
//#
|
||||
//# This file is automatically generated.
|
||||
//#`)
|
||||
// builder.WriteString("\n")
|
||||
if len(config.Search) > 0 {
|
||||
builder.WriteString(fmt.Sprintf("search %s\n", strings.Join(config.Search, " ")))
|
||||
}
|
||||
for i := range config.Servers {
|
||||
builder.WriteString(fmt.Sprintf("nameserver %s\n", config.Servers[i]))
|
||||
}
|
||||
if len(config.Port) != 0 {
|
||||
builder.WriteString(fmt.Sprintf("port %s\n", config.Port))
|
||||
}
|
||||
builder.WriteString(fmt.Sprintf("options ndots:%d\n", config.Ndots))
|
||||
builder.WriteString(fmt.Sprintf("options timeout:%d\n", config.Timeout))
|
||||
//builder.WriteString(fmt.Sprintf("options attempts:%d\n", config.Attempts))
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
//port := util.GetAvailableUDPPortOrDie()
|
||||
port := 58477
|
||||
port := util.GetAvailableUDPPortOrDie()
|
||||
fmt.Println(port)
|
||||
err := NewDNSServer("udp", "127.0.0.1:"+strconv.Itoa(port), "172.20.135.131:53", "test")
|
||||
if err != nil {
|
||||
log.Warnln(err)
|
||||
|
||||
Reference in New Issue
Block a user