Skip to content

Commit

Permalink
use dns library to check that we have commented resolv config
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik committed Sep 15, 2022
1 parent b2cc990 commit 1d73fd8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
50 changes: 30 additions & 20 deletions pkg/networkservice/connectioncontext/dnscontext/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ package dnscontext

import (
"context"
"errors"
"os"
"strings"

"github.com/golang/protobuf/ptypes/empty"
"github.com/miekg/dns"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"google.golang.org/grpc"

Expand All @@ -33,10 +35,6 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

const (
modifiedResolvConfKey = "# modified"
)

type dnsContextClient struct {
chainContext context.Context
resolveConfigPath string
Expand Down Expand Up @@ -98,25 +96,40 @@ func (c *dnsContextClient) Close(ctx context.Context, conn *networkservice.Conne
return next.Client(ctx).Close(ctx, conn, opts...)
}

func (c *dnsContextClient) restoreResolvConf() {
func (c *dnsContextClient) getResolvConfigComments() (string, error) {
bytes, err := os.ReadFile(c.resolveConfigPath)
modifiedResolvConf := string(bytes)
if err != nil || modifiedResolvConf == "" {
return
resolvConf := string(bytes)
if err != nil {
return "", err
}

if !strings.HasPrefix(modifiedResolvConf, modifiedResolvConfKey) {
return
if resolvConf == "" {
return "", errors.New("resolv.conf is empty")
}

originalResolvConf := make([]string, 0)
for _, line := range strings.Split(modifiedResolvConf, "\n")[1:] {
resolvConfComments := make([]string, 0)
for _, line := range strings.Split(resolvConf, "\n") {
if strings.HasPrefix(line, "#") {
originalResolvConf = append(originalResolvConf, line[1:])
resolvConfComments = append(resolvConfComments, line[1:])
}
}

_ = os.WriteFile(c.resolveConfigPath, []byte(strings.Join(originalResolvConf, "\n")), os.ModePerm)
return strings.Join(resolvConfComments, "\n"), nil
}

func (c *dnsContextClient) restoreResolvConf() {
commentedPart, err := c.getResolvConfigComments()
if err != nil {
log.FromContext(c.chainContext).Error(err.Error())
return
}
resolvConf, _ := dns.ClientConfigFromReader(strings.NewReader(commentedPart))

if len(resolvConf.Servers) == 0 {
return
}

_ = os.WriteFile(c.resolveConfigPath, []byte(commentedPart), os.ModePerm)
}

func (c *dnsContextClient) storeOriginalResolvConf() {
Expand All @@ -127,14 +140,11 @@ func (c *dnsContextClient) storeOriginalResolvConf() {
}

lines := strings.Split(originalResolvConfig, "\n")
modifiedResolvConf := make([]string, len(lines)+1)
modifiedResolvConf[0] = modifiedResolvConfKey

for i, line := range lines {
modifiedResolvConf[i+1] = "#" + line
for i := range lines {
lines[i] = "#" + lines[i]
}

_ = os.WriteFile(c.resolveConfigPath, []byte(strings.Join(modifiedResolvConf, "\n")+"\n\n"), os.ModePerm)
_ = os.WriteFile(c.resolveConfigPath, []byte(strings.Join(lines, "\n")+"\n\n"), os.ModePerm)
}

func (c *dnsContextClient) appendResolvConf(resolvConf string) error {
Expand Down
8 changes: 5 additions & 3 deletions pkg/networkservice/connectioncontext/dnscontext/resolvconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ func (r *resolveConfig) readProperties() error {
return err
}
for _, l := range strings.Split(string(b), "\n") {
words := strings.Split(l, " ")
if len(words) > 1 {
r.properties[words[0]] = words[1:]
if !strings.HasPrefix(l, "#") {
words := strings.Split(l, " ")
if len(words) > 1 {
r.properties[words[0]] = words[1:]
}
}
}
return nil
Expand Down

0 comments on commit 1d73fd8

Please sign in to comment.