Skip to content

Commit

Permalink
use AllowFunc in client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
Snawoot committed Oct 5, 2023
1 parent 5aa4d35 commit 788ecfb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Client struct {
staleMode util.StaleMode
workerWG sync.WaitGroup
timeLimit time.Duration
allowFunc func(net.Addr, net.Addr) bool
}

func New(cfg *Config) (*Client, error) {
Expand All @@ -47,6 +48,7 @@ func New(cfg *Config) (*Client, error) {
cancelCtx: cancelCtx,
staleMode: cfg.StaleMode,
timeLimit: cfg.TimeLimit,
allowFunc: cfg.AllowFunc,
}

lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
Expand Down Expand Up @@ -89,6 +91,10 @@ func (client *Client) listen() {
continue
}

if !client.allowFunc(conn.LocalAddr(), conn.RemoteAddr()) {
continue
}

client.workerWG.Add(1)
go func(conn net.Conn) {
defer client.workerWG.Done()
Expand Down
6 changes: 6 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Server struct {
staleMode util.StaleMode
workerWG sync.WaitGroup
timeLimit time.Duration
allowFunc func(net.Addr, net.Addr) bool
}

func New(cfg *Config) (*Server, error) {
Expand All @@ -48,6 +49,7 @@ func New(cfg *Config) (*Server, error) {
cancelCtx: cancelCtx,
staleMode: cfg.StaleMode,
timeLimit: cfg.TimeLimit,
allowFunc: cfg.AllowFunc,
}

lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
Expand Down Expand Up @@ -101,6 +103,10 @@ func (srv *Server) listen() {
continue
}

if !srv.allowFunc(conn.LocalAddr(), conn.RemoteAddr()) {
continue
}

srv.workerWG.Add(1)
go func(conn net.Conn) {
defer srv.workerWG.Done()
Expand Down

0 comments on commit 788ecfb

Please sign in to comment.