2017-09-11 13:48:07 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/pkg/errors"
|
2018-02-17 01:08:15 +01:00
|
|
|
"github.com/problame/go-netssh"
|
2018-08-08 13:09:51 +02:00
|
|
|
"net"
|
2017-09-11 13:48:07 +02:00
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StdinserverListenerFactory struct {
|
2017-09-11 15:45:10 +02:00
|
|
|
ClientIdentity string `mapstructure:"client_identity"`
|
2018-02-17 01:08:15 +01:00
|
|
|
sockpath string
|
2017-09-11 13:48:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
func parseStdinserverListenerFactory(c JobParsingContext, i map[string]interface{}) (f *StdinserverListenerFactory, err error) {
|
2017-09-11 13:48:07 +02:00
|
|
|
|
|
|
|
f = &StdinserverListenerFactory{}
|
|
|
|
|
|
|
|
if err = mapstructure.Decode(i, f); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "mapstructure error")
|
|
|
|
}
|
|
|
|
if !(len(f.ClientIdentity) > 0) {
|
|
|
|
err = errors.Errorf("must specify 'client_identity'")
|
|
|
|
return
|
|
|
|
}
|
2017-09-17 18:20:05 +02:00
|
|
|
|
2018-02-17 01:08:15 +01:00
|
|
|
f.sockpath = path.Join(c.Global.Serve.Stdinserver.SockDir, f.ClientIdentity)
|
2017-09-17 18:20:05 +02:00
|
|
|
|
2017-09-11 13:48:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-08 13:09:51 +02:00
|
|
|
func (f *StdinserverListenerFactory) Listen() (net.Listener, error) {
|
2017-09-11 13:48:07 +02:00
|
|
|
|
2018-08-08 13:09:51 +02:00
|
|
|
if err := PreparePrivateSockpath(f.sockpath); err != nil {
|
2018-02-17 14:12:44 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-17 01:08:15 +01:00
|
|
|
l, err := netssh.Listen(f.sockpath)
|
2017-09-11 13:48:07 +02:00
|
|
|
if err != nil {
|
2018-02-17 01:08:15 +01:00
|
|
|
return nil, err
|
2017-09-11 13:48:07 +02:00
|
|
|
}
|
2018-02-17 01:08:15 +01:00
|
|
|
return StdinserverListener{l}, nil
|
2017-09-11 13:48:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type StdinserverListener struct {
|
2018-02-17 01:08:15 +01:00
|
|
|
l *netssh.Listener
|
2017-09-11 13:48:07 +02:00
|
|
|
}
|
|
|
|
|
2018-08-08 13:09:51 +02:00
|
|
|
func (l StdinserverListener) Addr() net.Addr {
|
|
|
|
return netsshAddr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l StdinserverListener) Accept() (net.Conn, error) {
|
|
|
|
c, err := l.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return netsshConnToNetConnAdatper{c}, nil
|
2017-09-11 13:48:07 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 01:08:15 +01:00
|
|
|
func (l StdinserverListener) Close() (err error) {
|
|
|
|
return l.l.Close()
|
2017-09-11 13:48:07 +02:00
|
|
|
}
|