2018-12-11 22:01:50 +01:00
|
|
|
package ssh
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
import (
|
2019-03-22 19:41:12 +01:00
|
|
|
"context"
|
2018-12-11 22:01:50 +01:00
|
|
|
"fmt"
|
2018-08-27 22:21:45 +02:00
|
|
|
"net"
|
|
|
|
"path"
|
2018-09-05 01:41:54 +02:00
|
|
|
"sync/atomic"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/problame/go-netssh"
|
|
|
|
|
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
"github.com/zrepl/zrepl/daemon/nethelpers"
|
|
|
|
"github.com/zrepl/zrepl/transport"
|
2018-08-27 22:21:45 +02:00
|
|
|
)
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
func MultiStdinserverListenerFactoryFromConfig(g *config.Global, in *config.StdinserverServer) (transport.AuthenticatedListenerFactory, error) {
|
2018-08-27 22:21:45 +02:00
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
for _, ci := range in.ClientIdentities {
|
2018-12-11 22:01:50 +01:00
|
|
|
if err := transport.ValidateClientIdentity(ci); err != nil {
|
2018-09-05 01:41:54 +02:00
|
|
|
return nil, errors.Wrapf(err, "invalid client identity %q", ci)
|
|
|
|
}
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
clientIdentities := in.ClientIdentities
|
|
|
|
sockdir := g.Serve.StdinServer.SockDir
|
2018-08-27 22:21:45 +02:00
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
lf := func() (transport.AuthenticatedListener, error) {
|
2018-12-11 22:01:50 +01:00
|
|
|
return multiStdinserverListenerFromClientIdentities(sockdir, clientIdentities)
|
|
|
|
}
|
2018-08-27 22:21:45 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
return lf, nil
|
2018-09-05 01:41:54 +02:00
|
|
|
}
|
2018-08-27 22:21:45 +02:00
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
type multiStdinserverAcceptRes struct {
|
2018-12-11 22:01:50 +01:00
|
|
|
conn *transport.AuthConn
|
2019-03-22 19:41:12 +01:00
|
|
|
err error
|
2018-09-05 01:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type MultiStdinserverListener struct {
|
|
|
|
listeners []*stdinserverListener
|
2019-03-22 19:41:12 +01:00
|
|
|
accepts chan multiStdinserverAcceptRes
|
|
|
|
closed int32
|
2018-09-05 01:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// client identities must be validated
|
|
|
|
func multiStdinserverListenerFromClientIdentities(sockdir string, cis []string) (*MultiStdinserverListener, error) {
|
|
|
|
listeners := make([]*stdinserverListener, 0, len(cis))
|
|
|
|
var err error
|
|
|
|
for _, ci := range cis {
|
|
|
|
sockpath := path.Join(sockdir, ci)
|
2019-03-22 19:41:12 +01:00
|
|
|
l := &stdinserverListener{clientIdentity: ci}
|
2018-09-05 01:41:54 +02:00
|
|
|
if err = nethelpers.PreparePrivateSockpath(sockpath); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if l.l, err = netssh.Listen(sockpath); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
listeners = append(listeners, l)
|
|
|
|
}
|
2018-08-27 22:21:45 +02:00
|
|
|
if err != nil {
|
2018-09-05 01:41:54 +02:00
|
|
|
for _, l := range listeners {
|
|
|
|
l.Close() // FIXME error reporting?
|
|
|
|
}
|
2018-08-27 22:21:45 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-05 01:41:54 +02:00
|
|
|
return &MultiStdinserverListener{listeners: listeners}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
func (m *MultiStdinserverListener) Accept(ctx context.Context) (*transport.AuthConn, error) {
|
2018-09-05 01:41:54 +02:00
|
|
|
|
|
|
|
if m.accepts == nil {
|
|
|
|
m.accepts = make(chan multiStdinserverAcceptRes, len(m.listeners))
|
|
|
|
for i := range m.listeners {
|
|
|
|
go func(i int) {
|
|
|
|
for atomic.LoadInt32(&m.closed) == 0 {
|
|
|
|
conn, err := m.listeners[i].Accept(context.TODO())
|
|
|
|
m.accepts <- multiStdinserverAcceptRes{conn, err}
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
res := <-m.accepts
|
2018-09-05 01:41:54 +02:00
|
|
|
return res.conn, res.err
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
type multiListenerAddr struct {
|
|
|
|
clients []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (multiListenerAddr) Network() string { return "netssh" }
|
|
|
|
|
|
|
|
func (l multiListenerAddr) String() string {
|
|
|
|
return fmt.Sprintf("netssh:clients=%v", l.clients)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiStdinserverListener) Addr() net.Addr {
|
|
|
|
cis := make([]string, len(m.listeners))
|
|
|
|
for i := range cis {
|
|
|
|
cis[i] = m.listeners[i].clientIdentity
|
|
|
|
}
|
|
|
|
return multiListenerAddr{cis}
|
2018-09-05 01:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiStdinserverListener) Close() error {
|
|
|
|
atomic.StoreInt32(&m.closed, 1)
|
|
|
|
var oneErr error
|
|
|
|
for _, l := range m.listeners {
|
|
|
|
if err := l.Close(); err != nil && oneErr == nil {
|
|
|
|
oneErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oneErr
|
|
|
|
}
|
|
|
|
|
2020-02-23 23:24:12 +01:00
|
|
|
// a single stdinserverListener (part of multiStdinserverListener)
|
2018-09-05 01:41:54 +02:00
|
|
|
type stdinserverListener struct {
|
2019-03-22 19:41:12 +01:00
|
|
|
l *netssh.Listener
|
2018-09-05 01:41:54 +02:00
|
|
|
clientIdentity string
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
type listenerAddr struct {
|
|
|
|
clientIdentity string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (listenerAddr) Network() string { return "netssh" }
|
|
|
|
|
|
|
|
func (a listenerAddr) String() string {
|
|
|
|
return fmt.Sprintf("netssh:client=%q", a.clientIdentity)
|
|
|
|
}
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
func (l stdinserverListener) Addr() net.Addr {
|
2018-12-11 22:01:50 +01:00
|
|
|
return listenerAddr{l.clientIdentity}
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (l stdinserverListener) Accept(ctx context.Context) (*transport.AuthConn, error) {
|
2018-08-27 22:21:45 +02:00
|
|
|
c, err := l.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
return transport.NewAuthConn(c, l.clientIdentity), nil
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
func (l stdinserverListener) Close() (err error) {
|
2018-08-27 22:21:45 +02:00
|
|
|
return l.l.Close()
|
|
|
|
}
|