probe/discovery/target/service/service-conn.go

76 lines
1.5 KiB
Go
Raw Permalink 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)
2018-09-01 13:09:33 +00:00
metaCryptoType() *omm.MetaCryptoType
2018-08-31 12:56:36 +00:00
}
type noneConnector struct {
2018-09-01 13:09:33 +00:00
_metaCryptoType *omm.MetaCryptoType
}
func (c *noneConnector) metaCryptoType() *omm.MetaCryptoType {
return c._metaCryptoType
2018-08-31 12:56:36 +00:00
}
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 {
2018-09-01 13:09:33 +00:00
_metaCryptoType *omm.MetaCryptoType
}
func (c *tlsConnector) metaCryptoType() *omm.MetaCryptoType {
return c._metaCryptoType
2018-08-31 12:56:36 +00:00
}
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{
2018-09-01 13:09:33 +00:00
_metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
2018-08-31 12:56:36 +00:00
},
&tlsConnector{
2018-09-01 13:09:33 +00:00
_metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS),
2018-08-31 12:56:36 +00:00
},
}
}