probe/discovery/service/service-conn.go

67 lines
1.3 KiB
Go
Raw Normal View History

2018-08-31 12:56:36 +00:00
package service
import (
"crypto/tls"
"net"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
)
type connector interface {
dial(targetPort *omd.Port) (net.Conn, error)
}
type noneConnector struct {
metaCryptoType *omm.MetaCryptoType
}
func (c *noneConnector) dial(targetPort *omd.Port) (net.Conn, error) {
addr := net.JoinHostPort(targetPort.Host.Address, targetPort.PortNumber.String())
conn, err := net.DialTimeout("tcp", addr, time.Duration(3)*time.Second)
if err != nil {
return nil, err
}
return conn, err
}
type tlsConnector struct {
metaCryptoType *omm.MetaCryptoType
}
func (c *tlsConnector) dial(targetPort *omd.Port) (net.Conn, error) {
addr := net.JoinHostPort(targetPort.Host.Address, targetPort.PortNumber.String())
dialer := &net.Dialer{
Timeout: 3 * time.Second,
}
conn, err := tls.DialWithDialer(
dialer,
"tcp",
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: targetPort.Host.Address,
},
)
if err != nil {
return nil, err
}
return conn, err
}
func newConnectors() []connector {
return []connector{
&noneConnector{
metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
},
&tlsConnector{
metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS),
},
}
}