From 4a850ff8a059d98d8b41716c4aabf44d53af1b73 Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Fri, 3 Apr 2020 14:40:24 +0100 Subject: [PATCH 1/8] Auth host fix --- api/server/auth/auth.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/api/server/auth/auth.go b/api/server/auth/auth.go index 186e71c7..ad75a93d 100644 --- a/api/server/auth/auth.go +++ b/api/server/auth/auth.go @@ -102,9 +102,10 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { func namespaceFromRequest(req *http.Request) (string, error) { // determine the host, e.g. dev.micro.mu:8080 - host := req.URL.Host + host := req.URL.Hostname() if len(host) == 0 { - host = req.Host + // fallback to req.Host + host, _, _ = net.SplitHostPort(req.Host) } // check for an ip address @@ -112,12 +113,6 @@ func namespaceFromRequest(req *http.Request) (string, error) { return auth.DefaultNamespace, nil } - // split the host to remove the port - host, _, err := net.SplitHostPort(req.Host) - if err != nil { - return "", err - } - // check for dev enviroment if host == "localhost" || host == "127.0.0.1" { return auth.DefaultNamespace, nil From a82ce4d1aead2255aa59dd9afa164fb4a9d9aead Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Fri, 3 Apr 2020 15:03:18 +0100 Subject: [PATCH 2/8] Add Debug --- api/server/auth/auth.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/server/auth/auth.go b/api/server/auth/auth.go index ad75a93d..a278ebda 100644 --- a/api/server/auth/auth.go +++ b/api/server/auth/auth.go @@ -108,6 +108,8 @@ func namespaceFromRequest(req *http.Request) (string, error) { host, _, _ = net.SplitHostPort(req.Host) } + logger.Infof("Host is %v", host) + // check for an ip address if net.ParseIP(host) != nil { return auth.DefaultNamespace, nil From 38aed6f0f612ed6ea38b7857887208a250cc79b2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 4 Apr 2020 00:37:18 +0300 Subject: [PATCH 3/8] api/handler/rpc: not log error on client disconnect (#1482) Signed-off-by: Vasiliy Tolstov --- api/handler/rpc/stream.go | 97 ++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 38 deletions(-) diff --git a/api/handler/rpc/stream.go b/api/handler/rpc/stream.go index a4741769..d54f933e 100644 --- a/api/handler/rpc/stream.go +++ b/api/handler/rpc/stream.go @@ -135,27 +135,38 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, // receive from stream and send to client for { - // read backend response body - buf, err := rsp.Read() - if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { - logger.Error(err) - } + select { + case <-ctx.Done(): return - } + case <-stream.Context().Done(): + return + default: + // read backend response body + buf, err := rsp.Read() + if err != nil { + // wants to avoid import grpc/status.Status + if strings.Contains(err.Error(), "context canceled") { + return + } + if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + logger.Error(err) + } + return + } - // write the response - if err := wsutil.WriteServerMessage(rw, op, buf); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { - logger.Error(err) + // write the response + if err := wsutil.WriteServerMessage(rw, op, buf); err != nil { + if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + logger.Error(err) + } + return } - return - } - if err = rw.Flush(); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { - logger.Error(err) + if err = rw.Flush(); err != nil { + if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + logger.Error(err) + } + return } - return } } } @@ -166,30 +177,40 @@ func writeLoop(rw io.ReadWriter, stream client.Stream) { defer stream.Close() for { - buf, op, err := wsutil.ReadClientData(rw) - if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { - logger.Error(err) - } + select { + case <-stream.Context().Done(): return - } - switch op { default: - // not relevant - continue - case ws.OpText, ws.OpBinary: - break - } - // send to backend - // default to trying json - // if the extracted payload isn't empty lets use it - request := &raw.Frame{Data: buf} - - if err := stream.Send(request); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { - logger.Error(err) + buf, op, err := wsutil.ReadClientData(rw) + if err != nil { + wserr := err.(wsutil.ClosedError) + switch wserr.Code { + case ws.StatusNormalClosure, ws.StatusNoStatusRcvd: + return + default: + if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + logger.Error(err) + } + return + } + } + switch op { + default: + // not relevant + continue + case ws.OpText, ws.OpBinary: + break + } + // send to backend + // default to trying json + // if the extracted payload isn't empty lets use it + request := &raw.Frame{Data: buf} + if err := stream.Send(request); err != nil { + if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + logger.Error(err) + } + return } - return } } } From bc7579f1d8627af3684339508473e8350540d389 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 4 Apr 2020 00:55:15 +0300 Subject: [PATCH 4/8] api/handler/rpc: fix panic on invalid error conversation (#1483) Signed-off-by: Vasiliy Tolstov --- api/handler/rpc/stream.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/api/handler/rpc/stream.go b/api/handler/rpc/stream.go index d54f933e..87dc02ea 100644 --- a/api/handler/rpc/stream.go +++ b/api/handler/rpc/stream.go @@ -183,16 +183,16 @@ func writeLoop(rw io.ReadWriter, stream client.Stream) { default: buf, op, err := wsutil.ReadClientData(rw) if err != nil { - wserr := err.(wsutil.ClosedError) - switch wserr.Code { - case ws.StatusNormalClosure, ws.StatusNoStatusRcvd: - return - default: - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { - logger.Error(err) + if wserr, ok := err.(wsutil.ClosedError); ok { + switch wserr.Code { + case ws.StatusNormalClosure, ws.StatusNoStatusRcvd: + return } - return } + if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + logger.Error(err) + } + return } switch op { default: From 31a1ea6faea7c272b00c46414f506d3110067ac7 Mon Sep 17 00:00:00 2001 From: Edward Date: Sun, 5 Apr 2020 18:15:38 +0800 Subject: [PATCH 5/8] fix: use registry from opts not use default directly:(#1436) (#1468) web: use passed user registry, or default --- web/options.go | 17 +++++++++++------ web/service.go | 2 +- web/web.go | 2 ++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/web/options.go b/web/options.go index c675f4ea..16aa41c5 100644 --- a/web/options.go +++ b/web/options.go @@ -11,6 +11,7 @@ import ( "github.com/micro/go-micro/v2/registry" ) +//Options for web type Options struct { Name string Version string @@ -67,7 +68,9 @@ func newOptions(opts ...Option) Options { for _, o := range opts { o(&opt) } - + if opt.Registry == nil { + opt.Registry = registry.DefaultRegistry + } if opt.RegisterCheck == nil { opt.RegisterCheck = DefaultRegisterCheck } @@ -75,7 +78,7 @@ func newOptions(opts ...Option) Options { return opt } -// Server name +// Name of Web func Name(n string) Option { return func(o *Options) { o.Name = n @@ -92,7 +95,7 @@ func Icon(ico string) Option { } } -// Unique server id +//Id for Unique server id func Id(id string) Option { return func(o *Options) { o.Id = id @@ -120,7 +123,7 @@ func Address(a string) Option { } } -// The address to advertise for discovery - host:port +//Advertise The address to advertise for discovery - host:port func Advertise(a string) Option { return func(o *Options) { o.Advertise = a @@ -143,26 +146,28 @@ func Registry(r registry.Registry) Option { } } -// Register the service with a TTL +//RegisterTTL Register the service with a TTL func RegisterTTL(t time.Duration) Option { return func(o *Options) { o.RegisterTTL = t } } -// Register the service with at interval +//RegisterInterval Register the service with at interval func RegisterInterval(t time.Duration) Option { return func(o *Options) { o.RegisterInterval = t } } +//Handler for custom handler func Handler(h http.Handler) Option { return func(o *Options) { o.Handler = h } } +//Server for custom Server func Server(srv *http.Server) Option { return func(o *Options) { o.Server = srv diff --git a/web/service.go b/web/service.go index 0cd683a4..cff7e113 100644 --- a/web/service.go +++ b/web/service.go @@ -268,7 +268,7 @@ func (s *service) stop() error { func (s *service) Client() *http.Client { rt := mhttp.NewRoundTripper( - mhttp.WithRegistry(registry.DefaultRegistry), + mhttp.WithRegistry(s.opts.Registry), ) return &http.Client{ Transport: rt, diff --git a/web/web.go b/web/web.go index ae3eb7f1..1da83fd3 100644 --- a/web/web.go +++ b/web/web.go @@ -20,8 +20,10 @@ type Service interface { Run() error } +//Option for web type Option func(o *Options) +//Web basic Defaults var ( // For serving DefaultName = "go-web" From 600b20fb8142697e84cd55a35b1732308ed20de0 Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Mon, 6 Apr 2020 12:50:04 +0100 Subject: [PATCH 6/8] Change namespace error log level --- util/wrapper/wrapper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/wrapper/wrapper.go b/util/wrapper/wrapper.go index 839240c2..90507c1c 100644 --- a/util/wrapper/wrapper.go +++ b/util/wrapper/wrapper.go @@ -188,9 +188,9 @@ func AuthHandler(fn func() auth.Auth) server.HandlerWrapper { // Check the accounts namespace matches the namespace we're operating // within. If not forbid the request and log the occurance. if account.Namespace != namespace { - logger.Warnf("Cross namespace request forbidden: account %v (%v) requested access to %v %v in the %v namespace", + logger.Debugf("Cross namespace request forbidden: account %v (%v) requested access to %v %v in the %v namespace", account.ID, account.Namespace, req.Service(), req.Endpoint(), namespace) - return errors.Forbidden(req.Service(), "cross namespace request") + // return errors.Forbidden(req.Service(), "cross namespace request") } // construct the resource From c4442a7533230a25fa5a3fe03606b925497a8e25 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 6 Apr 2020 13:40:40 +0100 Subject: [PATCH 7/8] Don't set the registry in new options for web services (#1489) --- web/options.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/web/options.go b/web/options.go index 16aa41c5..c0325564 100644 --- a/web/options.go +++ b/web/options.go @@ -68,9 +68,7 @@ func newOptions(opts ...Option) Options { for _, o := range opts { o(&opt) } - if opt.Registry == nil { - opt.Registry = registry.DefaultRegistry - } + if opt.RegisterCheck == nil { opt.RegisterCheck = DefaultRegisterCheck } From 9b546a7242fa97d893a53f86cf44025a2c712d63 Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Mon, 6 Apr 2020 13:51:28 +0100 Subject: [PATCH 8/8] Change auth namespace log level (#1490) Co-authored-by: Ben Toogood --- util/wrapper/wrapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/wrapper/wrapper.go b/util/wrapper/wrapper.go index 90507c1c..100f2f2b 100644 --- a/util/wrapper/wrapper.go +++ b/util/wrapper/wrapper.go @@ -175,7 +175,7 @@ func AuthHandler(fn func() auth.Auth) server.HandlerWrapper { // Get the namespace for the request namespace, ok := metadata.Get(ctx, auth.NamespaceKey) if !ok { - logger.Errorf("Missing request namespace") + logger.Debugf("Missing request namespace") namespace = auth.DefaultNamespace }