-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
59 lines (50 loc) · 1.22 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dane
import (
"crypto/x509"
"encoding/pem"
"net"
"strconv"
"strings"
"time"
)
//
// addressString returns address string from the given IP address and
// port.
//
func addressString(ipaddress net.IP, port int) string {
addr := ipaddress.String()
if !strings.Contains(addr, ":") {
return addr + ":" + strconv.Itoa(port)
}
return "[" + addr + "]" + ":" + strconv.Itoa(port)
}
//
// getTCPDialer returns a net.Dialer object, initialized with the given
// timeout (in seconds).
//
func getDialer(timeout int) *net.Dialer {
dialer := new(net.Dialer)
dialer.Timeout = time.Second * time.Duration(timeout)
return dialer
}
//
// getTCPconn establishes a TCP connection to the given address and port.
// Returns a TCP connection (net.Conn) on success. Populates error on
// failure.
//
func getTCPconn(address net.IP, port int, timeout int) (net.Conn, error) {
dialer := getDialer(timeout)
conn, err := dialer.Dial("tcp", addressString(address, port))
return conn, err
}
//
// CertToPEMBytes returns PEM encoded bytes corresponding to the given
// x.509 certificate.
//
func CertToPEMBytes(cert *x509.Certificate) []byte {
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
}
return pem.EncodeToMemory(block)
}