2018-12-11 22:01:50 +01:00
|
|
|
package tcp
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
"net"
|
2018-09-05 01:41:54 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"context"
|
2018-12-11 22:01:50 +01:00
|
|
|
"github.com/zrepl/zrepl/transport"
|
2018-08-27 22:21:45 +02:00
|
|
|
)
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
type ipMapEntry struct {
|
|
|
|
ip net.IP
|
|
|
|
ident string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipMap struct {
|
|
|
|
entries []ipMapEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipMapFromConfig(clients map[string]string) (*ipMap, error) {
|
|
|
|
entries := make([]ipMapEntry, 0, len(clients))
|
|
|
|
for clientIPString, clientIdent := range clients {
|
|
|
|
clientIP := net.ParseIP(clientIPString)
|
|
|
|
if clientIP == nil {
|
|
|
|
return nil, errors.Errorf("cannot parse client IP %q", clientIPString)
|
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
if err := transport.ValidateClientIdentity(clientIdent); err != nil {
|
2018-09-05 01:41:54 +02:00
|
|
|
return nil, errors.Wrapf(err,"invalid client identity for IP %q", clientIPString)
|
|
|
|
}
|
|
|
|
entries = append(entries, ipMapEntry{clientIP, clientIdent})
|
|
|
|
}
|
|
|
|
return &ipMap{entries: entries}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ipMap) Get(ip net.IP) (string, error) {
|
|
|
|
for _, e := range m.entries {
|
|
|
|
if e.ip.Equal(ip) {
|
|
|
|
return e.ident, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.Errorf("no identity mapping for client IP %s", ip)
|
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
|
|
|
addr, err := net.ResolveTCPAddr("tcp", in.Listen)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot parse listen address")
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
clientIP := nc.RemoteAddr().(*net.TCPAddr).IP
|
|
|
|
clientIdent, err := f.clientMap.Get(clientIP)
|
|
|
|
if err != nil {
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).WithField("ip", clientIP).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
|
|
|
}
|
|
|
|
|