forked from storj/utp-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tls.go
55 lines (50 loc) · 2.09 KB
/
tls.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
package utp
import (
"crypto/tls"
"net"
)
// DialTLS connects to the given network address using net.Dial or utp.Dial as
// appropriate and then initiates a TLS handshake, returning the resulting TLS
// connection. DialTLS interprets a nil configuration as equivalent to the zero
// configuration; see the documentation of tls.Config for the details.
func DialTLS(network, addr string, config *tls.Config) (*tls.Conn, error) {
return DialTLSOptions(network, addr, config)
}
// DialTLSOptions connects to the given network address using net.Dial or
// utp.Dial as appropriate and then initiates a TLS handshake, returning the
// resulting TLS connection. DialTLS interprets a nil configuration as
// equivalent to the zero configuration; see the documentation of tls.Config
// for the details.
func DialTLSOptions(network, addr string, config *tls.Config, options ...ConnectOption) (*tls.Conn, error) {
utpAddr, err := ResolveUTPAddr(network, addr)
if err != nil {
return nil, err
}
utpConn, err := DialUTPOptions(network, nil, utpAddr, options...)
if err != nil {
return nil, err
}
return tls.Client(utpConn, config), nil
}
// ListenTLS creates a TLS listener accepting connections on the given network
// address using net.Listen or utp.Listen as appropriate. The configuration
// config must be non-nil and must include at least one certificate or else
// set GetCertificate.
func ListenTLS(network, laddr string, config *tls.Config) (net.Listener, error) {
return ListenTLSOptions(network, laddr, config)
}
// ListenTLSOptions creates a TLS listener accepting connections on the given
// network address using net.Listen or utp.Listen as appropriate. The
// configuration config must be non-nil and must include at least one
// certificate or else set GetCertificate.
func ListenTLSOptions(network, laddr string, config *tls.Config, options ...ConnectOption) (net.Listener, error) {
utpAddr, err := ResolveUTPAddr(network, laddr)
if err != nil {
return nil, err
}
listener, err := ListenUTPOptions(network, utpAddr, options...)
if err != nil {
return nil, err
}
return tls.NewListener(listener, config), nil
}