2018-04-19 11:36:56 +00:00
|
|
|
package ipv4
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
2018-06-13 10:20:14 +00:00
|
|
|
|
|
|
|
ocmm "git.loafle.net/overflow/commons-go/model/meta"
|
2018-04-19 11:36:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type serviceConnector interface {
|
2018-06-13 10:20:14 +00:00
|
|
|
MetaCryptoType() *ocmm.MetaCryptoType
|
2018-04-19 11:36:56 +00:00
|
|
|
Dial(ip string, port int) (net.Conn, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type normalServiceConn struct {
|
2018-06-13 10:20:14 +00:00
|
|
|
metaCryptoType *ocmm.MetaCryptoType
|
2018-04-19 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 10:20:14 +00:00
|
|
|
func (nsc *normalServiceConn) MetaCryptoType() *ocmm.MetaCryptoType {
|
|
|
|
return nsc.metaCryptoType
|
2018-04-19 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-06-13 10:20:14 +00:00
|
|
|
metaCryptoType *ocmm.MetaCryptoType
|
2018-04-19 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 10:20:14 +00:00
|
|
|
func (tsc *tlsServiceConn) MetaCryptoType() *ocmm.MetaCryptoType {
|
|
|
|
return tsc.metaCryptoType
|
2018-04-19 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|