probe/discovery/target/service/service-conn.go
crusader 266ffdd674 ing
2018-09-01 22:09:33 +09:00

76 lines
1.5 KiB
Go

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)
metaCryptoType() *omm.MetaCryptoType
}
type noneConnector struct {
_metaCryptoType *omm.MetaCryptoType
}
func (c *noneConnector) metaCryptoType() *omm.MetaCryptoType {
return c._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) metaCryptoType() *omm.MetaCryptoType {
return c._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),
},
}
}