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

Initial cmd state #3

Merged
merged 9 commits into from
May 13, 2022
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
6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ linters-settings:
goimports:
local-prefixes: github.com/networkservicemesh
gocyclo:
min-complexity: 15
min-complexity: 20
maligned:
suggest-new: true
dupl:
threshold: 150
funlen:
Lines: 100
Statements: 50
Lines: 250
Statements: 200
goconst:
min-len: 2
min-occurrences: 2
Expand Down
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ RUN dl \

FROM go as build
WORKDIR /build
COPY go.mod go.sum ./
COPY internal ./internal
RUN go build ./internal/pkg/imports
COPY . .
RUN go build -o /bin/app .
RUN go build -o /bin/nse-istio-proxy .

FROM build as test
CMD go test -test.v ./...
Expand All @@ -20,5 +23,5 @@ FROM test as debug
CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./...

FROM alpine as runtime
COPY --from=build /bin/app /bin/app
CMD /bin/app
COPY --from=build /bin/nse-istio-proxy /bin/nse-istio-proxy
ENTRYPOINT ["/bin/nse-istio-proxy"]
17 changes: 16 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
module github.com/networkservicemesh/cmd-template
module github.com/networkservicemesh/cmd-nse-istio-proxy

go 1.16

require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/edwarnicke/grpcfd v1.1.2
github.com/kelseyhightower/envconfig v1.4.0
github.com/miekg/dns v1.1.49
github.com/networkservicemesh/api v1.3.2-0.20220512163820-8c875d61945b
github.com/networkservicemesh/sdk v0.5.1-0.20220513003022-4d9bebd00c37
github.com/networkservicemesh/sdk-kernel v0.0.0-20220513094228-e0f2b84203da
github.com/networkservicemesh/sdk-sriov v0.0.0-20220507174402-1f53cfa20170
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spiffe/go-spiffe/v2 v2.0.0
google.golang.org/grpc v1.42.0
)
480 changes: 480 additions & 0 deletions go.sum

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions internal/pkg/dns/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2022 Xored Software Inc and others.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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:
//
// http://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.

//go:build linux
// +build linux

// Package dns provides dns server with rewrite modification
package dns

import (
"context"
"errors"
"fmt"
"net"

"github.com/miekg/dns"
)

// ProxyRewriteServer - DNS server with rewrite function
type ProxyRewriteServer struct {
RewriteTO net.IP
ListenOn string
ResolveConfPath string
}

// ListenAndServe - run DNS server
func (p *ProxyRewriteServer) ListenAndServe(ctx context.Context) <-chan error {
var networks = []string{"tcp", "udp"}
var result = make(chan error, len(networks))

if p.RewriteTO == nil {
result <- errors.New("RewriteTO is not set")
}
if p.ResolveConfPath == "" {
p.ResolveConfPath = "/etc/resolv.conf"
}
for _, network := range networks {
server := &dns.Server{Addr: p.ListenOn, Net: network}
go func() {
server.Handler = p
defer func() { _ = server.Shutdown() }()
select {
case result <- server.ListenAndServe():
case <-ctx.Done():
}
}()
}

return result
}

// ServeDNS - serve DNS request
func (p *ProxyRewriteServer) ServeDNS(rw dns.ResponseWriter, m *dns.Msg) {
config, err := dns.ClientConfigFromFile(p.ResolveConfPath)
if err != nil {
dns.HandleFailed(rw, m)
return
}
var networks = []string{"tcp", "udp"}

for _, network := range networks {
var client = dns.Client{
Net: network,
}
for _, addr := range config.Servers {
var msg *dns.Msg
fmt.Println(addr)
fmt.Println(network)
if msg, _, err = client.Exchange(m, fmt.Sprintf("%v:%v", addr, config.Port)); err != nil {
fmt.Println(err.Error())
continue
}
for _, answer := range msg.Answer {
p.rewriteIP(answer)
}
if err := rw.WriteMsg(msg); err == nil {
return
}
}
}

dns.HandleFailed(rw, m)
}

func (p *ProxyRewriteServer) rewriteIP(rr dns.RR) {
switch rr.Header().Rrtype {
case dns.TypeAAAA:
if p.RewriteTO.To16() != nil {
rr.(*dns.AAAA).AAAA = p.RewriteTO.To16()
}
case dns.TypeA:
if p.RewriteTO.To4() != nil {
rr.(*dns.A).A = p.RewriteTO.To4()
}
}
}
22 changes: 22 additions & 0 deletions internal/pkg/imports/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2022 Xored Software Inc and others.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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:
//
// http://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 imports is used for generating list of imports to optimize use of docker build cache
package imports

//go:generate bash -c "rm -rf imports*.go"
//go:generate bash -c "cd $(mktemp -d) && GO111MODULE=on go install github.com/edwarnicke/imports-gen@v1.1.0"
//go:generate bash -c "GOOS=linux ${GOPATH}/bin/imports-gen"
57 changes: 57 additions & 0 deletions internal/pkg/imports/imports_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// DO NOT EDIT - generated by github.com/edwarnicke/imports-gen
package imports

import (
_ "context"
_ "errors"
_ "fmt"
_ "github.com/antonfisher/nested-logrus-formatter"
_ "github.com/edwarnicke/grpcfd"
_ "github.com/kelseyhightower/envconfig"
_ "github.com/miekg/dns"
_ "github.com/networkservicemesh/api/pkg/api/networkservice"
_ "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
_ "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/noop"
_ "github.com/networkservicemesh/api/pkg/api/networkservice/payload"
_ "github.com/networkservicemesh/api/pkg/api/registry"
_ "github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/setiptables4nattemplate"
_ "github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/setroutelocalnet"
_ "github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/token"
_ "github.com/networkservicemesh/sdk-sriov/pkg/tools/tokens"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/kernel"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/null"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/ipam/point2pointipam"
_ "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
_ "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
_ "github.com/networkservicemesh/sdk/pkg/tools/debug"
_ "github.com/networkservicemesh/sdk/pkg/tools/dnscontext"
_ "github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
_ "github.com/networkservicemesh/sdk/pkg/tools/log"
_ "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
_ "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry"
_ "github.com/networkservicemesh/sdk/pkg/tools/spiffejwt"
_ "github.com/networkservicemesh/sdk/pkg/tools/tracing"
_ "github.com/pkg/errors"
_ "github.com/sirupsen/logrus"
_ "github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
_ "github.com/spiffe/go-spiffe/v2/workloadapi"
_ "google.golang.org/grpc"
_ "google.golang.org/grpc/credentials"
_ "io/ioutil"
_ "net"
_ "net/url"
_ "os"
_ "os/signal"
_ "path/filepath"
_ "strings"
_ "syscall"
_ "time"
)
Loading