container_discovery/internal/discoverer/ipv4/service_conn.go
crusader bbb569b9da ing
2018-04-28 00:33:22 +09:00

61 lines
1.0 KiB
Go

package ipv4
import (
"crypto/tls"
"fmt"
"net"
"time"
)
type serviceConnector interface {
CryptoType() string
Dial(ip string, port int) (net.Conn, error)
}
type normalServiceConn struct {
cryptoType string
}
func (nsc *normalServiceConn) CryptoType() string {
return nsc.cryptoType
}
func (nsc *normalServiceConn) Dial(ip string, port int) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
conn, err := net.DialTimeout("tcp", addr, time.Duration(3)*time.Second)
if err != nil {
return nil, err
}
return conn, err
}
type tlsServiceConn struct {
cryptoType string
}
func (tsc *tlsServiceConn) CryptoType() string {
return tsc.cryptoType
}
func (tsc *tlsServiceConn) Dial(ip string, port int) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
dialer := &net.Dialer{
Timeout: 3 * time.Second,
}
conn, err := tls.DialWithDialer(
dialer,
"tcp",
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: ip,
},
)
if err != nil {
return nil, err
}
return conn, err
}