2018-12-11 22:01:50 +01:00
|
|
|
package tcp
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
import (
|
2019-03-22 19:41:12 +01:00
|
|
|
"context"
|
2018-08-27 22:21:45 +02:00
|
|
|
"net"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
|
|
|
"github.com/zrepl/zrepl/config"
|
2018-12-11 22:01:50 +01:00
|
|
|
"github.com/zrepl/zrepl/transport"
|
2019-12-30 19:42:17 +01:00
|
|
|
"github.com/zrepl/zrepl/util/tcpsock"
|
2018-08-27 22:21:45 +02:00
|
|
|
)
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func TCPListenerFactoryFromConfig(c *config.Global, in *config.TCPServe) (transport.AuthenticatedListenerFactory, error) {
|
2018-09-05 01:41:54 +02:00
|
|
|
clientMap, err := ipMapFromConfig(in.Clients)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot parse client IP map")
|
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
lf := func() (transport.AuthenticatedListener, error) {
|
2019-12-30 19:42:17 +01:00
|
|
|
l, err := tcpsock.Listen(in.Listen, in.ListenFreeBind)
|
2018-12-11 22:01:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &TCPAuthListener{l, clientMap}, nil
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
return lf, nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
type TCPAuthListener struct {
|
|
|
|
*net.TCPListener
|
|
|
|
clientMap *ipMap
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
2018-09-05 01:41:54 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (f *TCPAuthListener) Accept(ctx context.Context) (*transport.AuthConn, error) {
|
|
|
|
nc, err := f.TCPListener.AcceptTCP()
|
2018-09-05 01:41:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-18 18:53:20 +01:00
|
|
|
clientAddr := &net.IPAddr{
|
|
|
|
IP: nc.RemoteAddr().(*net.TCPAddr).IP,
|
|
|
|
Zone: nc.RemoteAddr().(*net.TCPAddr).Zone,
|
|
|
|
}
|
|
|
|
clientIdent, err := f.clientMap.Get(clientAddr)
|
2018-09-05 01:41:54 +02:00
|
|
|
if err != nil {
|
2020-01-18 18:53:20 +01:00
|
|
|
transport.GetLogger(ctx).WithField("ipaddr", clientAddr).Error("client IP not in client map")
|
2018-09-05 01:41:54 +02:00
|
|
|
nc.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
return transport.NewAuthConn(nc, clientIdent), nil
|
2018-09-05 01:41:54 +02:00
|
|
|
}
|