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 middlewares for cozy filtering and logging #71

Merged
merged 1 commit into from
Sep 6, 2024
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
23 changes: 17 additions & 6 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,40 @@
return fmt.Errorf("bind to %v blocked by rules", req.RawDestAddr)
}

var last Handler
// Switch on the command
switch req.Command {
case statute.CommandConnect:
last = sf.handleConnect
if sf.userConnectHandle != nil {
return sf.userConnectHandle(ctx, write, req)
last = sf.userConnectHandle
}
if len(sf.userConnectMiddlewares) != 0 {
return sf.userConnectMiddlewares.Execute(ctx, write, req, last)
}
return sf.handleConnect(ctx, write, req)
case statute.CommandBind:
last = sf.handleBind

Check warning on line 95 in handle.go

View check run for this annotation

Codecov / codecov/patch

handle.go#L95

Added line #L95 was not covered by tests
if sf.userBindHandle != nil {
return sf.userBindHandle(ctx, write, req)
last = sf.userBindHandle

Check warning on line 97 in handle.go

View check run for this annotation

Codecov / codecov/patch

handle.go#L97

Added line #L97 was not covered by tests
}
if len(sf.userBindMiddlewares) != 0 {
return sf.userBindMiddlewares.Execute(ctx, write, req, last)

Check warning on line 100 in handle.go

View check run for this annotation

Codecov / codecov/patch

handle.go#L99-L100

Added lines #L99 - L100 were not covered by tests
}
return sf.handleBind(ctx, write, req)
case statute.CommandAssociate:
last = sf.handleAssociate
if sf.userAssociateHandle != nil {
return sf.userAssociateHandle(ctx, write, req)
last = sf.userAssociateHandle

Check warning on line 105 in handle.go

View check run for this annotation

Codecov / codecov/patch

handle.go#L105

Added line #L105 was not covered by tests
}
if len(sf.userAssociateMiddlewares) != 0 {
return sf.userAssociateMiddlewares.Execute(ctx, write, req, last)
}
return sf.handleAssociate(ctx, write, req)
default:
if err := SendReply(write, statute.RepCommandNotSupported, nil); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
return fmt.Errorf("unsupported command[%v]", req.Command)
}
return last(ctx, write, req)
}

// handleConnect is used to handle a connect command
Expand Down
43 changes: 43 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,46 @@
s.userAssociateHandle = h
}
}

// Handler is used to handle a user's commands
type Handler func(ctx context.Context, writer io.Writer, request *Request) error

// WithMiddleware is used to add interceptors in chain
type Middleware func(ctx context.Context, writer io.Writer, request *Request) error

// MiddlewareChain is used to add interceptors in chain
type MiddlewareChain []Middleware

// Execute is used to add interceptors in chain
func (m MiddlewareChain) Execute(ctx context.Context, writer io.Writer, request *Request, last Handler) error {
if len(m) == 0 {
return nil

Check warning on line 140 in option.go

View check run for this annotation

Codecov / codecov/patch

option.go#L140

Added line #L140 was not covered by tests
}
for i := 0; i < len(m); i++ {
if err := m[i](ctx, writer, request); err != nil {
return err
}
}
return last(ctx, writer, request)
}

// WithConnectMiddleware is used to add interceptors in chain
func WithConnectMiddleware(m Middleware) Option {
return func(s *Server) {
s.userConnectMiddlewares = append(s.userConnectMiddlewares, m)
}
}

// WithBindMiddleware is used to add interceptors in chain
func WithBindMiddleware(m Middleware) Option {
return func(s *Server) {
s.userBindMiddlewares = append(s.userBindMiddlewares, m)

Check warning on line 160 in option.go

View check run for this annotation

Codecov / codecov/patch

option.go#L158-L160

Added lines #L158 - L160 were not covered by tests
}
}

// WithAssociateMiddleware is used to add interceptors in chain
func WithAssociateMiddleware(m Middleware) Option {
return func(s *Server) {
s.userAssociateMiddlewares = append(s.userAssociateMiddlewares, m)
}
}
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type Server struct {
userConnectHandle func(ctx context.Context, writer io.Writer, request *Request) error
userBindHandle func(ctx context.Context, writer io.Writer, request *Request) error
userAssociateHandle func(ctx context.Context, writer io.Writer, request *Request) error
// user's middleware
userConnectMiddlewares MiddlewareChain
userBindMiddlewares MiddlewareChain
userAssociateMiddlewares MiddlewareChain
}

// NewServer creates a new Server
Expand Down
Loading
Loading