container_discovery/internal/discoverer/ipv4/service_conn.go
crusader c65fd4ab7d ing
2018-06-13 19:20:14 +09:00

63 lines
1.2 KiB
Go

package ipv4
import (
"crypto/tls"
"fmt"
"net"
"time"
ocmm "git.loafle.net/overflow/commons-go/model/meta"
)
type serviceConnector interface {
MetaCryptoType() *ocmm.MetaCryptoType
Dial(ip string, port int) (net.Conn, error)
}
type normalServiceConn struct {
metaCryptoType *ocmm.MetaCryptoType
}
func (nsc *normalServiceConn) MetaCryptoType() *ocmm.MetaCryptoType {
return nsc.metaCryptoType
}
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 {
metaCryptoType *ocmm.MetaCryptoType
}
func (tsc *tlsServiceConn) MetaCryptoType() *ocmm.MetaCryptoType {
return tsc.metaCryptoType
}
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
}