-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtls_client_option.go
More file actions
64 lines (57 loc) · 1.84 KB
/
Copy pathtls_client_option.go
File metadata and controls
64 lines (57 loc) · 1.84 KB
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
60
61
62
63
64
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
package tacquito
import (
"crypto/tls"
"net"
)
// SetClientTLSDialer creates a client that connects to the server using TLS.
// network and address specify the server to connect to.
// tlsConfig is the TLS configuration to use for the connection.
func SetClientTLSDialer(network, address string, tlsConfig *tls.Config) ClientOption {
return func(c *Client) error {
// Connect to the server using TLS
conn, err := tls.Dial(network, address, tlsConfig)
if err != nil {
return err
}
c.crypter = newCrypter(nil, conn, false, true)
return nil
}
}
// SetClientTLSDialerWithLocalAddr creates a client that connects to the server using TLS,
// allowing specification of the local address to connect from.
// network and raddr specify the server to connect to.
// laddr specifies the local address to connect from.
// tlsConfig is the TLS configuration to use for the connection.
// if laddr is empty, SetClientTLSDialer is used.
func SetClientTLSDialerWithLocalAddr(network, raddr, laddr string, tlsConfig *tls.Config) ClientOption {
return func(c *Client) error {
// Resolve the local address if provided
var localAddr *net.TCPAddr
var err error
if laddr != "" {
localAddr, err = net.ResolveTCPAddr(network, laddr)
if err != nil {
return err
}
} else {
// return standard client
return SetClientTLSDialer(network, raddr, tlsConfig)(c)
}
// Create a dialer with the local address
dialer := &net.Dialer{
LocalAddr: localAddr,
}
// Connect to the server using TLS with the dialer
conn, err := tls.DialWithDialer(dialer, network, raddr, tlsConfig)
if err != nil {
return err
}
c.crypter = newCrypter(nil, conn, false, true)
return nil
}
}