69 lines
1.1 KiB
Go
69 lines
1.1 KiB
Go
|
package ipv4
|
||
|
|
||
|
import (
|
||
|
"crypto/tls"
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type serviceConnector interface {
|
||
|
Type() string
|
||
|
Dial(ip string, port int) (net.Conn, error)
|
||
|
}
|
||
|
|
||
|
type normalServiceConn struct {
|
||
|
t string
|
||
|
}
|
||
|
|
||
|
func (nsc *normalServiceConn) Type() string {
|
||
|
return nsc.t
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
err = conn.SetDeadline(time.Now().Add(3 * time.Second))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return conn, err
|
||
|
}
|
||
|
|
||
|
type tlsServiceConn struct {
|
||
|
t string
|
||
|
}
|
||
|
|
||
|
func (tsc *tlsServiceConn) Type() string {
|
||
|
return tsc.t
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
err = conn.SetDeadline(time.Now().Add(3 * time.Second))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return conn, err
|
||
|
}
|