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

Add ip mutation to dns server #93

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/edwarnicke/grpcfd v1.1.2
github.com/kelseyhightower/envconfig v1.4.0
github.com/miekg/dns v1.1.50
github.com/networkservicemesh/api v1.6.2-0.20221205183940-84c7ff837cdd
github.com/networkservicemesh/api v1.7.2-0.20230123083145-4a6c3ec589e1
github.com/networkservicemesh/sdk v0.5.1-0.20230109230417-1492e69a650d
github.com/networkservicemesh/sdk-kernel v0.0.0-20230109230713-03b9b6155f6d
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/networkservicemesh/api v1.6.2-0.20221205183940-84c7ff837cdd h1:26HR90HrJFZHIPPP3SCLGNRpPCMFoEnSSZfhHH8MPSo=
github.com/networkservicemesh/api v1.6.2-0.20221205183940-84c7ff837cdd/go.mod h1:hOF2844BSstH1311oDMDgqqXS+kdc77htZNPRKl9mf8=
github.com/networkservicemesh/api v1.7.2-0.20230123083145-4a6c3ec589e1 h1:RxNKksXsXsnDsEo+Cfn43pdPfML024ad//QcdKfgXK4=
github.com/networkservicemesh/api v1.7.2-0.20230123083145-4a6c3ec589e1/go.mod h1:hOF2844BSstH1311oDMDgqqXS+kdc77htZNPRKl9mf8=
github.com/networkservicemesh/sdk v0.5.1-0.20230109230417-1492e69a650d h1:1EhczRkzfkutymvDj5XYinD68omLpFk2F2kJdhzYG+U=
github.com/networkservicemesh/sdk v0.5.1-0.20230109230417-1492e69a650d/go.mod h1:++MSpR2wuFlCpCYoR6lELxJ4ILKd650vDQ8d9CtGAzA=
github.com/networkservicemesh/sdk-kernel v0.0.0-20230109230713-03b9b6155f6d h1:DKXaQ1Yq7Ep6Cgo54Ip1QxgLwnqaND0MJdb4I+THhKI=
Expand Down
21 changes: 19 additions & 2 deletions internal/pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const dnsQuestionNameFilter = "cluster.local."

// ProxyRewriteServer - DNS server with rewrite function
type ProxyRewriteServer struct {
RewriteIP bool
RewriteTO net.IP
ListenOn string
ResolveConfPath string
Expand Down Expand Up @@ -95,7 +96,7 @@ func (p *ProxyRewriteServer) ServeDNS(rw dns.ResponseWriter, m *dns.Msg) {
dns.HandleFailed(rw, m)
return
}
var networks = []string{"tcp", "udp"}
var networks = []string{"udp"}

for _, network := range networks {
var client = dns.Client{
Expand All @@ -107,9 +108,16 @@ func (p *ProxyRewriteServer) ServeDNS(rw dns.ResponseWriter, m *dns.Msg) {
fmt.Println(err.Error())
continue
}
if p.RewriteIP {
for _, answer := range msg.Answer {
p.rewriteIP(answer)
}
}

for _, answer := range msg.Answer {
p.rewriteIP(answer)
p.changeFirstByte(answer)
}

if err := rw.WriteMsg(msg); err == nil {
return
}
Expand All @@ -131,3 +139,12 @@ func (p *ProxyRewriteServer) rewriteIP(rr dns.RR) {
}
}
}

func (p *ProxyRewriteServer) changeFirstByte(rr dns.RR) {
switch rr.Header().Rrtype {
case dns.TypeA:
if p.RewriteTO.To4() != nil {
rr.(*dns.A).A[0] = 199
}
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Config struct {
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint"`
RulesConfigPath string `default:"" desc:"Path to a configmap with iptables rules" split_words:"true"`
RewriteIP bool `default:"false" desc:"Rewrite ip with nse-l7-proxy ip in DNS response" split_worlds:"true"`
}

// Process prints and processes env to config
Expand Down Expand Up @@ -298,6 +299,7 @@ func main() {
log.FromContext(ctx).Infof("executing phase 7: run DNS server")
// ********************************************************************************
dnsServer := &dns.ProxyRewriteServer{
RewriteIP: config.RewriteIP,
RewriteTO: ip,
ListenOn: ":53",
}
Expand Down