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

Optimize service add #1727

Merged
merged 17 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/emirpasic/gods v1.18.1
github.com/fatih/color v1.16.0
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa
github.com/gaissmai/extnetip v0.4.0
github.com/go-acme/lego/v4 v4.15.0
github.com/go-openapi/errors v0.21.0
github.com/go-openapi/loads v0.21.5
Expand Down
18 changes: 3 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa h1:RDBNVkRviHZtvDvId8XSGPu3rmpmSe+wKRcEWNgsfWU=
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
github.com/gaissmai/extnetip v0.4.0 h1:9pNd/Z6QSlkda35bug/IYuPYaPMTYRuqcxPce5Z9TTQ=
github.com/gaissmai/extnetip v0.4.0/go.mod h1:M3NWlyFKaVosQXWXKKeIPK+5VM4U85DahdIqNYX4TK4=
github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-acme/lego/v4 v4.14.2 h1:/D/jqRgLi8Cbk33sLGtu2pX2jEg3bGJWHyV8kFuUHGM=
github.com/go-acme/lego/v4 v4.14.2/go.mod h1:kBXxbeTg0x9AgaOYjPSwIeJy3Y33zTz+tMD16O4MO6c=
github.com/go-acme/lego/v4 v4.15.0 h1:A7MHEU3b+TDFqhC/HmzMJnzPbyeaYvMZQBbqgvbThhU=
github.com/go-acme/lego/v4 v4.15.0/go.mod h1:eeGhjW4zWT7Ccqa3sY7ayEqFLCAICx+mXgkMHKIkLxg=
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
Expand Down Expand Up @@ -236,8 +236,6 @@ github.com/go-openapi/strfmt v0.22.0 h1:Ew9PnEYc246TwrEspvBdDHS4BVKXy/AOVsfqGDgA
github.com/go-openapi/strfmt v0.22.0/go.mod h1:HzJ9kokGIju3/K6ap8jL+OlGAbjpSv27135Yr9OivU4=
github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE=
github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE=
github.com/go-openapi/validate v0.22.6 h1:+NhuwcEYpWdO5Nm4bmvhGLW0rt1Fcc532Mu3wpypXfo=
github.com/go-openapi/validate v0.22.6/go.mod h1:eaddXSqKeTg5XpSmj1dYyFTK/95n/XHwcOY+BMxKMyM=
github.com/go-openapi/validate v0.23.0 h1:2l7PJLzCis4YUGEoW6eoQw3WhyM65WSIcjX6SQnlfDw=
github.com/go-openapi/validate v0.23.0/go.mod h1:EeiAZ5bmpSIOJV1WLfyYF9qp/B1ZgSaEpHTJHtN5cbE=
github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8=
Expand Down Expand Up @@ -404,8 +402,6 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb-client-go/v2 v2.2.2/go.mod h1:fa/d1lAdUHxuc1jedx30ZfNG573oQTQmUni3N6pcW+0=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
github.com/jedib0t/go-pretty/v6 v6.5.3 h1:GIXn6Er/anHTkVUoufs7ptEvxdD6KIhR7Axa2wYCPF0=
github.com/jedib0t/go-pretty/v6 v6.5.3/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg=
github.com/jedib0t/go-pretty/v6 v6.5.4 h1:gOGo0613MoqUcf0xCj+h/V3sHDaZasfv152G6/5l91s=
github.com/jedib0t/go-pretty/v6 v6.5.4/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
Expand Down Expand Up @@ -518,8 +514,6 @@ github.com/michaelquigley/pfxlog v0.6.10 h1:IbC/H3MmSDcPlQHF1UZPQU13Dkrs0+ycWRyQ
github.com/michaelquigley/pfxlog v0.6.10/go.mod h1:gEiNTfKEX6cJHSwRpOuqBpc8oYrlhMiDK/xMk/gV7D0=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU=
Expand Down Expand Up @@ -753,9 +747,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down Expand Up @@ -872,8 +866,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -975,8 +967,6 @@ golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -1182,8 +1172,6 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
20 changes: 10 additions & 10 deletions router/xgress_edge_tunnel/tunneler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func (self *tunneler) Start(notifyClose <-chan struct{}) error {
log := pfxlog.Logger()
log.WithField("mode", self.listenOptions.mode).Info("creating interceptor")

resolver, err := dns.NewResolver(self.listenOptions.resolver)
if err != nil {
pfxlog.Logger().WithError(err).Error("failed to start DNS resolver")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an issue related to this code: #1513
Do you think you could pop in a quick fix for that here.. just assign a default resolver if the selected resolver initialization fails?

}

if err = intercept.SetDnsInterceptIpRange(self.listenOptions.dnsSvcIpRange); err != nil {
pfxlog.Logger().Errorf("invalid dns service IP range %s: %v", self.listenOptions.dnsSvcIpRange, err)
return err
}

if strings.HasPrefix(self.listenOptions.mode, "tproxy") {
tproxyConfig := tproxy.Config{
LanIf: self.listenOptions.lanIf,
Expand All @@ -96,16 +106,6 @@ func (self *tunneler) Start(notifyClose <-chan struct{}) error {
return errors.Errorf("unsupported tunnel mode '%v'", self.listenOptions.mode)
}

resolver, err := dns.NewResolver(self.listenOptions.resolver)
if err != nil {
pfxlog.Logger().WithError(err).Error("failed to start DNS resolver")
}

if err = intercept.SetDnsInterceptIpRange(self.listenOptions.dnsSvcIpRange); err != nil {
pfxlog.Logger().Errorf("invalid dns service IP range %s: %v", self.listenOptions.dnsSvcIpRange, err)
return err
}

self.servicePoller.serviceListener = intercept.NewServiceListener(self.interceptor, resolver)
self.servicePoller.serviceListener.HandleProviderReady(self.fabricProvider)

Expand Down
4 changes: 3 additions & 1 deletion tunnel/dns/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func (h *hostFile) AddDomain(name string, _ func(string) (net.IP, error)) error
return fmt.Errorf("cannot add wildcard domain[%s] to hostfile resolver", name)
}

func (h *hostFile) RemoveDomain(string) {}

func (h *hostFile) Lookup(_ net.IP) (string, error) {
return "", fmt.Errorf("not implemented")
}
Expand Down Expand Up @@ -81,7 +83,7 @@ func (h *hostFile) AddHostname(hostname string, ip net.IP) error {
return nil
}

func (h *hostFile) RemoveHostname(_ string) error {
func (h *hostFile) RemoveHostname(_ string) net.IP {
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion tunnel/dns/refcount.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (self *RefCountingResolver) AddDomain(name string, cb func(string) (net.IP,
return self.wrapped.AddDomain(name, cb)
}

func (self *RefCountingResolver) RemoveDomain(name string) {
self.wrapped.RemoveDomain(name)
}

func (self *RefCountingResolver) AddHostname(s string, ip net.IP) error {
err := self.wrapped.AddHostname(s, ip)
if err != nil {
Expand All @@ -38,7 +42,7 @@ func (self *RefCountingResolver) AddHostname(s string, ip net.IP) error {
return err
}

func (self *RefCountingResolver) RemoveHostname(s string) error {
func (self *RefCountingResolver) RemoveHostname(s string) net.IP {
val := self.names.Upsert(s, 1, func(exist bool, valueInMap int, newValue int) int {
if exist {
return valueInMap - 1
Expand Down
3 changes: 2 additions & 1 deletion tunnel/dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type Resolver interface {
AddHostname(string, net.IP) error
AddDomain(string, func(string) (net.IP, error)) error
Lookup(net.IP) (string, error)
RemoveHostname(string) error
RemoveHostname(string) net.IP
RemoveDomain(string)
Cleanup() error
}

Expand Down
23 changes: 19 additions & 4 deletions tunnel/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ func (r *resolver) getAddress(name string) (net.IP, error) {
de, ok := r.domains[canonical]

if ok {
name = name[:len(name)-1]
ip, err := de.getIP(name)
if err != nil {
return nil, err
}
log.Debugf("assigned %v => %v", name, ip)
_ = r.AddHostname(name[:len(name)-1], ip) // this resolver impl never returns an error
_ = r.AddHostname(name, ip) // this resolver impl never returns an error
return ip, err
}
}
Expand Down Expand Up @@ -250,6 +251,18 @@ func (r *resolver) AddDomain(name string, ipCB func(string) (net.IP, error)) err
return nil
}

func (r *resolver) RemoveDomain(name string) {
if name[0] != '*' {
log.Warnf("invalid wildcard domain '%s'", name)
return
}
domainSfx := name[1:] + "."
r.domainsMtx.Lock()
defer r.domainsMtx.Unlock()
log.Infof("removing domain %s from resolver", domainSfx)
delete(r.domains, domainSfx)
}

func (r *resolver) AddHostname(hostname string, ip net.IP) error {
r.namesMtx.Lock()
defer r.namesMtx.Unlock()
Expand Down Expand Up @@ -277,18 +290,20 @@ func (r *resolver) Lookup(ip net.IP) (string, error) {
return "", errors.New("not found")
}

func (r *resolver) RemoveHostname(hostname string) error {
func (r *resolver) RemoveHostname(hostname string) net.IP {
r.namesMtx.Lock()
defer r.namesMtx.Unlock()

key := strings.ToLower(hostname) + "."
if ip, ok := r.names[key]; ok {
var ip net.IP
var ok bool
if ip, ok = r.names[key]; ok {
log.Infof("removing %s from resolver", hostname)
delete(r.ips, ip.String())
delete(r.names, key)
}

return nil
return ip
}

func (r *resolver) Cleanup() error {
Expand Down
4 changes: 2 additions & 2 deletions tunnel/entities/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func makeAllowedAddress(addr string) (allowedAddress, error) {
return &domainAddress{domain: strings.ToLower(addr)}, nil
}

if _, cidr, err := utils.GetDialIP(addr); err == nil {
if cidr, err := utils.GetCidr(addr); err == nil {
return &cidrAddress{cidr: *cidr}, nil
}

Expand Down Expand Up @@ -291,7 +291,7 @@ func (self *HostV1Config) GetAllowedSourceAddressRoutes() ([]*net.IPNet, error)
var routes []*net.IPNet
for _, addr := range self.AllowedSourceAddresses {
// need to get CIDR from address - iputils.getInterceptIp?
_, ipNet, err := utils.GetDialIP(addr)
ipNet, err := utils.GetCidr(addr)
if err != nil {
return nil, errors.Errorf("failed to parse allowed source address '%s': %v", addr, err)
}
Expand Down
2 changes: 1 addition & 1 deletion tunnel/intercept/hosting.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (self *hostingContext) SetCloseCallback(f func()) {
func (self *hostingContext) OnClose() {
log := pfxlog.Logger().WithField("service", self.service.Name)
for _, addr := range self.config.AllowedSourceAddresses {
_, ipNet, err := utils.GetDialIP(addr)
ipNet, err := utils.GetCidr(addr)
if err != nil {
log.WithError(err).Error("failed to get dial IP")
} else if self.addrTracker.RemoveAddress(ipNet.String()) {
Expand Down
28 changes: 17 additions & 11 deletions tunnel/intercept/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ type Interceptor interface {
// - service name - when a service is removed (e.g. from an appwan)

type InterceptAddress struct {
cidr *net.IPNet
lowPort uint16
highPort uint16
protocol string
TproxySpec []string
AcceptSpec []string
cidr *net.IPNet
routeRequired bool
lowPort uint16
highPort uint16
protocol string
TproxySpec []string
AcceptSpec []string
}

func (addr *InterceptAddress) Proto() string {
Expand All @@ -59,6 +60,10 @@ func (addr *InterceptAddress) IpNet() *net.IPNet {
return addr.cidr
}

func (addr *InterceptAddress) RouteRequired() bool {
return addr.routeRequired
}

func (addr *InterceptAddress) LowPort() uint16 {
return addr.lowPort
}
Expand All @@ -82,14 +87,15 @@ type InterceptAddrCB interface {

func GetInterceptAddresses(service *entities.Service, protocols []string, resolver dns.Resolver, addressCB InterceptAddrCB) error {
for _, addr := range service.InterceptV1Config.Addresses {
err := getInterceptIP(service, addr, resolver, func(ip net.IP, ipNet *net.IPNet) {
err := getInterceptIP(service, addr, resolver, func(ipNet *net.IPNet, routeRequired bool) {
for _, protocol := range protocols {
for _, portRange := range service.InterceptV1Config.PortRanges {
addr := &InterceptAddress{
cidr: ipNet,
lowPort: portRange.Low,
highPort: portRange.High,
protocol: protocol}
cidr: ipNet,
routeRequired: routeRequired,
lowPort: portRange.Low,
highPort: portRange.High,
protocol: protocol}
addressCB.Apply(addr)
}
}
Expand Down
Loading
Loading