From 41af35829f7edcce5c47db26dc4956bed4c80123 Mon Sep 17 00:00:00 2001 From: SammyOina Date: Tue, 24 Oct 2023 11:31:44 +0300 Subject: [PATCH] update deps Signed-off-by: SammyOina --- coap/api/transport.go | 11 +- coap/client.go | 22 ++-- go.mod | 2 +- go.sum | 4 +- .../mainflux/mproxy/pkg/coap/coap.go | 107 +----------------- vendor/modules.txt | 4 +- 6 files changed, 28 insertions(+), 122 deletions(-) diff --git a/coap/api/transport.go b/coap/api/transport.go index 1246e412ee7..bcdeca31440 100644 --- a/coap/api/transport.go +++ b/coap/api/transport.go @@ -66,8 +66,9 @@ func MakeCoAPHandler(svc coap.Service, l mflog.Logger) mux.HandlerFunc { return handler } -func sendResp(ctx context.Context, w mux.ResponseWriter, resp *message.Message) { - m := w.Conn().AcquireMessage(ctx) +func sendResp(w mux.ResponseWriter, resp *message.Message) { + m := w.Conn().AcquireMessage(w.Conn().Context()) + defer w.Conn().ReleaseMessage(m) m.SetCode(resp.Code) m.SetBody(bytes.NewReader(resp.Payload)) m.SetToken(resp.Token) @@ -85,7 +86,7 @@ func handler(w mux.ResponseWriter, m *mux.Message) { Token: m.Token(), Options: make(message.Options, 0, 16), } - defer sendResp(m.Context(), w, &resp) + defer sendResp(w, &resp) msg, err := decodeMessage(m) if err != nil { logger.Warn(fmt.Sprintf("Error decoding message: %s", err)) @@ -100,7 +101,7 @@ func handler(w mux.ResponseWriter, m *mux.Message) { } switch m.Code() { case codes.GET: - err = handleGet(m.Context(), m, w.Conn(), msg, key) + err = handleGet(context.Background(), m, w, msg, key) case codes.POST: resp.Code = codes.Created err = nil @@ -122,7 +123,7 @@ func handler(w mux.ResponseWriter, m *mux.Message) { } } -func handleGet(ctx context.Context, m *mux.Message, c mux.Conn, msg *messaging.Message, key string) error { +func handleGet(ctx context.Context, m *mux.Message, c mux.ResponseWriter, msg *messaging.Message, key string) error { var obs uint32 obs, err := m.Observe() if err != nil { diff --git a/coap/client.go b/coap/client.go index 0702dfd02e2..063f479cac1 100644 --- a/coap/client.go +++ b/coap/client.go @@ -14,7 +14,8 @@ import ( "github.com/mainflux/mainflux/pkg/messaging" "github.com/plgd-dev/go-coap/v3/message" "github.com/plgd-dev/go-coap/v3/message/codes" - mux "github.com/plgd-dev/go-coap/v3/mux" + "github.com/plgd-dev/go-coap/v3/message/pool" + "github.com/plgd-dev/go-coap/v3/mux" ) // Client wraps CoAP client. @@ -36,14 +37,14 @@ type Client interface { var ErrOption = errors.New("unable to set option") type client struct { - client mux.Conn + client mux.ResponseWriter token message.Token observe uint32 logger logger.Logger } // NewClient instantiates a new Observer. -func NewClient(c mux.Conn, tkn message.Token, l logger.Logger) Client { +func NewClient(c mux.ResponseWriter, tkn message.Token, l logger.Logger) Client { return &client{ client: c, token: tkn, @@ -53,17 +54,17 @@ func NewClient(c mux.Conn, tkn message.Token, l logger.Logger) Client { } func (c *client) Done() <-chan struct{} { - return c.client.Done() + return c.client.Conn().Done() } func (c *client) Cancel() error { - pm := c.client.AcquireMessage(context.Background()) + pm := c.client.Conn().AcquireMessage(context.Background()) pm.SetCode(codes.Content) pm.SetToken(c.token) - if err := c.client.WriteMessage(pm); err != nil { + if err := c.client.Conn().WriteMessage(pm); err != nil { c.logger.Error(fmt.Sprintf("Error sending message: %s.", err)) } - return c.client.Close() + return c.client.Conn().Close() } func (c *client) Token() string { @@ -71,7 +72,7 @@ func (c *client) Token() string { } func (c *client) Handle(msg *messaging.Message) error { - pm := c.client.AcquireMessage(context.Background()) + pm := pool.NewMessage(context.Background()) pm.SetCode(codes.Content) pm.SetToken(c.token) pm.SetBody(bytes.NewReader(msg.Payload)) @@ -101,5 +102,8 @@ func (c *client) Handle(msg *messaging.Message) error { for _, option := range opts { pm.SetOptionBytes(option.ID, option.Value) } - return c.client.WriteMessage(pm) + if err := c.client.Conn().WriteMessage(pm); err != nil { + return err + } + return nil } diff --git a/go.mod b/go.mod index 682240c663c..bce4cd08121 100644 --- a/go.mod +++ b/go.mod @@ -230,4 +230,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/mainflux/mproxy => github.com/sammyoina/mproxy v0.0.0-20231006131650-51e30985e320 +replace github.com/mainflux/mproxy => github.com/sammyoina/mproxy v0.0.0-20231022202359-ab4bb5fcaef1 diff --git a/go.sum b/go.sum index 8746be686a3..3005a5afe81 100644 --- a/go.sum +++ b/go.sum @@ -645,8 +645,8 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= -github.com/sammyoina/mproxy v0.0.0-20231006131650-51e30985e320 h1:Ak8XO7tAEUdIyTjr/G82e1S+1EiEa1FPjozYrEsPdSU= -github.com/sammyoina/mproxy v0.0.0-20231006131650-51e30985e320/go.mod h1:m3eBZLtl3exBRMfRqz52m+r2jPKKBV+NWLMeibnrgDA= +github.com/sammyoina/mproxy v0.0.0-20231022202359-ab4bb5fcaef1 h1:yYevls3tXYwRejhPnnw9poJiZWQZBb9MaUcq2Gj8Btk= +github.com/sammyoina/mproxy v0.0.0-20231022202359-ab4bb5fcaef1/go.mod h1:79BIGoxY//nKN8dQi9Y+NaVU/Gdn3s9IwLiMpLJqgDg= github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo= github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= diff --git a/vendor/github.com/mainflux/mproxy/pkg/coap/coap.go b/vendor/github.com/mainflux/mproxy/pkg/coap/coap.go index b7ff6882dc2..5856be1f6af 100644 --- a/vendor/github.com/mainflux/mproxy/pkg/coap/coap.go +++ b/vendor/github.com/mainflux/mproxy/pkg/coap/coap.go @@ -1,12 +1,10 @@ package coap import ( - "bytes" "crypto/tls" "fmt" "github.com/mainflux/mproxy/pkg/logger" - "github.com/mainflux/mproxy/pkg/session" "github.com/pion/dtls/v2" coap "github.com/plgd-dev/go-coap/v3" "github.com/plgd-dev/go-coap/v3/message" @@ -21,7 +19,6 @@ type Proxy struct { logger logger.Logger targetConn *client.Conn address string - event session.Handler } func sendPoolMessage(cc mux.Conn, pm *pool.Message, token []byte) error { @@ -52,16 +49,6 @@ func sendPoolMessage(cc mux.Conn, pm *pool.Message, token []byte) error { return cc.WriteMessage(m) } -func sendErrorMessage(cc mux.Conn, token []byte, err error, code codes.Code) error { - m := cc.AcquireMessage(cc.Context()) - defer cc.ReleaseMessage(m) - m.SetCode(code) - m.SetBody(bytes.NewReader([]byte(err.Error()))) - m.SetToken(token) - m.SetContentFormat(message.TextPlain) - return cc.WriteMessage(m) -} - func (p *Proxy) postUpstream(cc mux.Conn, req *mux.Message, token []byte) error { format, err := req.ContentFormat() if err != nil { @@ -84,19 +71,6 @@ func (p *Proxy) getUpstream(cc mux.Conn, req *mux.Message, token []byte) error { if err != nil { return err } - - pm, err := p.targetConn.Get(cc.Context(), path, req.Options()...) - if err != nil { - return err - } - return sendPoolMessage(cc, pm, token) -} - -func (p *Proxy) observeUpstream(cc mux.Conn, req *mux.Message, token []byte) error { - path, err := req.Options().Path() - if err != nil { - return err - } if _, err = p.targetConn.Client.Observe(req.Context(), path, func(req *pool.Message) { if err := sendPoolMessage(cc, req, token); err != nil { p.logger.Error(err.Error()) @@ -108,94 +82,24 @@ func (p *Proxy) observeUpstream(cc mux.Conn, req *mux.Message, token []byte) err } func (p *Proxy) handler(w mux.ResponseWriter, r *mux.Message) { - tok, err := r.Options().GetBytes(message.URIQuery) - if err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.Unauthorized); err != nil { - p.logger.Error(err.Error()) - } - return - } - ctx := session.NewContext(r.Context(), &session.Session{Password: tok}) - if err := p.event.AuthConnect(ctx); err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.Unauthorized); err != nil { - p.logger.Error(err.Error()) - } - return - } path, err := r.Options().Path() if err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.BadOption); err != nil { - p.logger.Error(err.Error()) - } - return + p.logger.Error(err.Error()) } p.logger.Debug(fmt.Sprintf("Got message path=%v: %+v from %v", path, r, w.Conn().RemoteAddr())) switch r.Code() { case codes.GET: - if err := p.event.AuthSubscribe(ctx, &[]string{path}); err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.Unauthorized); err != nil { - p.logger.Error(err.Error()) - } - return - } - if err := p.event.Subscribe(ctx, &[]string{path}); err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.Unauthorized); err != nil { - p.logger.Error(err.Error()) - } - return - } - obs, err := r.Options().Observe() - switch { - // obs == 0, start observe - case obs == 0 && err == nil: - if err := p.observeUpstream(w.Conn(), r, r.Token()); err != nil { - p.logger.Debug(fmt.Sprintf("error performing get: %v\n", err)) - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.BadGateway); err != nil { - p.logger.Error(err.Error()) - } - return - } - default: - if err := p.getUpstream(w.Conn(), r, r.Token()); err != nil { - p.logger.Debug(fmt.Sprintf("error performing get: %v\n", err)) - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.BadGateway); err != nil { - p.logger.Error(err.Error()) - } - return - } + if err := p.getUpstream(w.Conn(), r, r.Token()); err != nil { + p.logger.Debug(fmt.Sprintf("error performing post: %v\n", err)) } - case codes.POST: - body, err := r.ReadBody() - if err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.BadRequest); err != nil { - p.logger.Error(err.Error()) - } - return - } - if err := p.event.AuthPublish(ctx, &path, &body); err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.Unauthorized); err != nil { - p.logger.Error(err.Error()) - } - return - } - if err := p.event.Publish(ctx, &path, &body); err != nil { - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.BadRequest); err != nil { - p.logger.Error(err.Error()) - } - return - } if err := p.postUpstream(w.Conn(), r, r.Token()); err != nil { p.logger.Debug(fmt.Sprintf("error performing post: %v\n", err)) - if err := sendErrorMessage(w.Conn(), r.Token(), err, codes.BadGateway); err != nil { - p.logger.Error(err.Error()) - } - return } } } -func NewProxy(address, target string, logger logger.Logger, handler session.Handler) (*Proxy, error) { +func NewProxy(address, target string, logger logger.Logger) (*Proxy, error) { targetConn, err := udp.Dial(address) if err != nil { return nil, err @@ -203,9 +107,6 @@ func NewProxy(address, target string, logger logger.Logger, handler session.Hand return &Proxy{ targetConn: targetConn, - logger: logger, - address: address, - event: handler, }, nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index 7f0140b3cc6..dcc37341da3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -571,7 +571,7 @@ github.com/mailru/easyjson/jwriter # github.com/mainflux/callhome v0.0.0-20230920140432-33c5663382ce ## explicit; go 1.21 github.com/mainflux/callhome/pkg/client -# github.com/mainflux/mproxy v0.3.1-0.20230822124450-4b4dfe600cc2 => github.com/sammyoina/mproxy v0.0.0-20231006131650-51e30985e320 +# github.com/mainflux/mproxy v0.3.1-0.20230822124450-4b4dfe600cc2 => github.com/sammyoina/mproxy v0.0.0-20231022202359-ab4bb5fcaef1 ## explicit; go 1.19 github.com/mainflux/mproxy/pkg/http github.com/mainflux/mproxy/pkg/coap @@ -1280,4 +1280,4 @@ gopkg.in/yaml.v2 # gopkg.in/yaml.v3 v3.0.1 ## explicit gopkg.in/yaml.v3 -# github.com/mainflux/mproxy => github.com/sammyoina/mproxy v0.0.0-20231006131650-51e30985e320 +# github.com/mainflux/mproxy => github.com/sammyoina/mproxy v0.0.0-20231022202359-ab4bb5fcaef1